diff --git a/src/Context.hpp b/src/Context.hpp index ec364fff..60593ebc 100644 --- a/src/Context.hpp +++ b/src/Context.hpp @@ -7,97 +7,34 @@ #ifndef NBDL_CONTEXT_HPP #define NBDL_CONTEXT_HPP -#include - namespace nbdl { -namespace details { - -template -class Context : public std::enable_shared_from_this> +template< + typename ProviderMap, + typename Consumers, + typename StoreMap> +class Context { - using ProviderMap = typename Traits::ProviderMap; - using StoreMap = typename Traits::StoreMap; - using Consumers = typename Traits::Consumers; - using ListenerHandler_ = nbdl::ListenerHandlerDummy<>; - - public: - - using SharedPtr = std::shared_ptr; - using WeakPtr = std::weak_ptr; - - template - using Listener = details::Listener; - - private: - ProviderMap providers; + Consumers consumers; StoreMap stores; - public: - - template - Listener makeListener(PathType path, Args... args) - { - auto wrapper = Listener(path, this->shared_from_this(), args...); - stores[hana::type_c].addListener(path, wrapper.handler()); - return wrapper; - } - template - void addListener(const PathType& path, const ListenerHandler_& listener) - { - stores[hana::type_c].addListener(path, listener); - } - template - void removeListener(const PathType& path, const ListenerHandler_& listener) - { - stores[hana::type_c].removeListener(path, listener); - } - - template - auto read(Path path, MatchFns... fns) - { - return stores[hana::type_c].get( - //called if store needs to request value - [&](const Path& path) { - //request from provider - auto self = this->shared_from_this(); - providers[hana::type_c].read(path, [path, self](auto&& value) { - self->stores[hana::type_c] - .suggestAssign(path, std::forward(value)); - }); - }, - path, fns...); - } -}; - -template< - typename ProviderMap_, - typename Consumers_, - typename StoreMap_> -struct ContextTraits -{ - using ProviderMap = ProviderMap_; - using Consumers = Consumers_; - using StoreMap = StoreMap_; -}; - + // should be callable only by providers + template + auto pushDownstream(Message&&) + { + // access providers, consumers and stores + } -}//details + // should be callable only by consumers + template + auto pushUpstream(Message&&) + { + // access providers, consumers and stores + } -template -struct Context -{ - using Type = details::Context; - using SharedPtr = typename Type::SharedPtr; + public: - //todo i could probably wrap the shared object and put the logic in here - //and eliminate the shared_from_this junk - template - static SharedPtr create(Args... args) - { - return std::make_shared(args...); - } }; }//nbdl diff --git a/src/Listener.hpp b/src/Listener.hpp deleted file mode 100644 index 3870dcd4..00000000 --- a/src/Listener.hpp +++ /dev/null @@ -1,128 +0,0 @@ -// -// Copyright Jason Rice 2015 -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -#ifndef NBDL_LISTENER_HPP -#define NBDL_LISTENER_HPP - -#include -#include - -namespace nbdl { - -using ListenerHandle = std::weak_ptr; - -template -class ListenerHandlerBase -{ - ListenerHandle weak; - - public: - - ListenerHandlerBase(ListenerHandle w) : - weak(w) - {} - - bool operator== (const Impl& h) - { - //to find in a vector or other lookup - return !weak.owner_before(h.weak) && !h.weak.owner_before(weak); - } - - template - void notify_(const VariantType& value) - { - if (!weak.expired()) - { - static_cast(this)->notify(value); - } - } -}; - -/* - this ListenerHandler just calls a - dummy callback on any kind of - state change with no args -*/ -template> -class ListenerHandlerDummy : public ListenerHandlerBase> -{ - using ElementType = typename ListenerHandle::element_type; - - friend class ListenerHandlerBase>; - - Fn fn; - - template - void notify(const VariantType&) - { - fn(); - } - - public: - - ListenerHandlerDummy(ListenerHandle w, Fn f) : - ListenerHandlerBase>(w), - fn(f) - {} -}; - -namespace details { - -template -class Listener -{ - PathType path; - ContextWeakPtr context; //needs to delete self with context - std::shared_ptr handle_master; - Handler handler_; - - public: - - template - Listener(PathType p, ContextWeakPtr c, Args... args) : - path(p), - context(c), - handle_master(std::make_shared(0)), - handler_(Handler(handle_master, args...)) - {} - //todo copy constructor needs to be deleted or something - - ~Listener() - { - auto ctx = context.lock(); - if (ctx) - { - ctx->removeListener(path, handler_); - } - } - - bool operator== (const Handler& h) - { - //handler_ is stored with the store's emitter - return handler_ == h; - } - - Handler handler() - { - return handler_; - } - - //todo delete self from context - //todo == operator needed for key comparison - - template - void notify(Args... args) - { - handler_.notify(args...); - } - -}; - -}//details - -}//nbdl - -#endif diff --git a/src/Store.hpp b/src/Store.hpp index 51cb954e..93e3dcdf 100644 --- a/src/Store.hpp +++ b/src/Store.hpp @@ -8,77 +8,42 @@ #define NBDL_STORE_HPP #include "store/HashMap.hpp" -#include "store_emitter/HashMap.hpp" namespace nbdl { template< - typename Impl, - typename EmitterImpl, - typename ListenerHandler, + typename Container, typename PathType > class Store { using Entity = typename PathType::Entity; - Impl impl; - EmitterImpl emitter; - - template - void emitChange(const PathType& path, const T& value) - { - emitter.emit(path, [&](ListenerHandler& listener) { - listener.notify_(value); - }); - } + Container container; public: - using Variant_ = typename Impl::Variant_; + using Variant_ = typename Container::Variant_; template void forceAssign(const PathType& path, T&& value) { - emitChange(path, impl.assign(path, std::forward(value))); + container.assign(path, std::forward(value)); } template void suggestAssign(const PathType& path, T&& val) { - Variant_& value = impl.get(path); - bool unresolved = value.match( - [](Unresolved) { - return true; - }, - [](auto) { - return false; - }); - if (unresolved) + Variant_& value = container.get(path); + if (value.template is()) { value = std::forward(val); - emitChange(path, value); } } - template - auto get(RequestFn request, const PathType& path, Fns... fns) - { - if (!impl.hasEntry(path)) - { - impl.assign(path, Unresolved{}); - request(path); - } - return impl.get(path).match(fns...); - } - - //emitter interface - void addListener(const PathType& path, const ListenerHandler& listener) - { - emitter.addListener(path, listener); - } - void removeListener(const PathType& path, const ListenerHandler& listener) + template + auto get(const PathType& path, MatchFns... fns) { - emitter.removeListener(path, listener); + return container.get(path).match(fns...); } }; diff --git a/src/StoreCollection.hpp b/src/StoreCollection.hpp deleted file mode 100644 index c52fdb70..00000000 --- a/src/StoreCollection.hpp +++ /dev/null @@ -1,65 +0,0 @@ -// -// Copyright Jason Rice 2015 -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -#ifndef NBDL_STORE_COLLECTION_HPP -#define NBDL_STORE_COLLECTION_HPP - -#include -#include "Store.hpp" - -namespace nbdl { - -template -class StoreCollection -{ - using StoreMap = decltype(hana::to(StoreMap_{})); - StoreMap stores; - - template - auto& getStore(T) - { - return stores[hana::type_c]; - } - - public: - - StoreCollection() : - stores(hana::to(StoreMap_{})) - {} - - template - void forceAssign(const PathType& path, T&& value) - { - getStore(path).forceAssign(path, std::forward(value)); - } - - template - void suggestAssign(const PathType& path, T&& value) - { - getStore(path).suggestAssign(path, std::forward(value)); - } - template - auto get(RequestFn request, const PathType path, Fns... fns) - { - return getStore(path).get(request, path, fns...); - } - - //emitter interface - template - void addListener(const PathType& path, const ListenerHandler_& listener) - { - getStore(path).addListener(path, listener); - } - template - void removeListener(const PathType& path, const ListenerHandler_& listener) - { - getStore(path).removeListener(path, listener); - } -}; - -}//nbdl - -#endif diff --git a/src/Variant.hpp b/src/Variant.hpp index d01fecd4..24f3b967 100644 --- a/src/Variant.hpp +++ b/src/Variant.hpp @@ -40,9 +40,9 @@ class Variant Storage value_; template - constexpr int typeIdFromType(T type) const + constexpr int typeIdFromType() const { - return *hana::find(typeIds(), type); + return *hana::find(typeIds(), hana::type_c); } template @@ -129,7 +129,7 @@ class Variant type_id = 0; //in case shit goes horribly wrong destroy(type_id, &value_); new (&value_) Type(val); - type_id = typeIdFromType(hana::type_c); + type_id = typeIdFromType(); } public: @@ -173,6 +173,7 @@ class Variant bool isValidTypeId(int type_id_x) const { + // perhaps this should just assume that type_ids are sequential indices if (type_id_x == 0) { return true; @@ -181,7 +182,7 @@ class Variant { return matchByType(type_id_x, [&](auto type) { - return (type_id_x == typeIdFromType(type)); + return (type_id_x == typeIdFromType()); }); } } @@ -201,6 +202,12 @@ class Variant ); } + template + bool is() + { + return (type_id == typeIdFromType()); + } + template auto match(Fns... fns) const { diff --git a/src/def/builder/AccessPointMeta.hpp b/src/def/builder/AccessPointMeta.hpp index b282faa2..8588ea45 100644 --- a/src/def/builder/AccessPointMeta.hpp +++ b/src/def/builder/AccessPointMeta.hpp @@ -18,13 +18,14 @@ MPDEF_METASTRUCT( , name , actions , storeContainer - , storeEmitter , entityNames ); #if 0 - // I'm going to leave this here to show what the MPDEF_METASTRUCT macro does. + // I'm going to leave this here to show what the MPDEF_METASTRUCT macro does, + // and make it easier to try out different representations. + struct AccessPointMeta : ::mpdef::Metastruct { @@ -32,10 +33,9 @@ struct AccessPointMeta static constexpr auto name = at_c< 0 >; static constexpr auto actions = at_c< 1 >; static constexpr auto storeContainer = at_c< 2 >; - static constexpr auto storeEmitter = at_c< 3 >; - static constexpr auto entityNames = at_c< 4 >; + static constexpr auto entityNames = at_c< 3 >; - static constexpr auto spec = mpdef::make_list(name, actions, storeContainer, storeEmitter, entityNames); + static constexpr auto spec = mpdef::make_list(name, actions, storeContainer, entityNames); }; constexpr auto makeAccessPointMeta = mpdef::makeMetastruct; constexpr auto makeAccessPointMetaWithMap = mpdef::makeMetastructWithMap; diff --git a/src/def/builder/Context.hpp b/src/def/builder/Context.hpp index 3370542a..5881448b 100644 --- a/src/def/builder/Context.hpp +++ b/src/def/builder/Context.hpp @@ -30,13 +30,11 @@ struct Context constexpr auto entityMetaMap = builder::mapEntityMeta(defs[tag::Entities]); constexpr auto providersMeta = builder::enumerateProviders(Def{}); return hana::template_( - hana::template_( - builder::providerMap(entityMetaMap, providersMeta), - hana::type_c, //Consumers - builder::storeMap(entityMetaMap, - hana::flatten(hana::unpack(providersMeta, - mpdef::make_list ^hana::on^ ProviderMeta::accessPoints))) - ) + builder::providerMap(entityMetaMap, providersMeta), + hana::type_c, //TODO Consumers + builder::storeMap(entityMetaMap, + hana::flatten(hana::unpack(providersMeta, + mpdef::make_list ^hana::on^ ProviderMeta::accessPoints))) ); } }; @@ -48,7 +46,7 @@ struct ContextFactory template auto operator()(Args... args) const { - return Context_::create(args...); + return Context_(args...); } }; diff --git a/src/def/builder/EnumerateAccessPoints.hpp b/src/def/builder/EnumerateAccessPoints.hpp index c3378e63..e601c76c 100644 --- a/src/def/builder/EnumerateAccessPoints.hpp +++ b/src/def/builder/EnumerateAccessPoints.hpp @@ -8,7 +8,6 @@ #define NBDL_DEF_BUILDER_ENUMERATE_ACCESS_POINTS_HPP #include -#include #include #include #include @@ -28,7 +27,7 @@ namespace hana = boost::hana; namespace enum_access_points_detail { - constexpr auto settings = mpdef::withSettings(tag::StoreContainer, tag::StoreEmitter); + constexpr auto settings = mpdef::withSettings(tag::StoreContainer); constexpr auto pred = hana::demux(hana::eval_if) ( mpdef::is_tree_node, @@ -108,10 +107,6 @@ namespace enum_access_points_detail { settings[tag::StoreContainer].value_or( hana::template_ ), - settings[tag::StoreEmitter].value_or( - hana::partial(hana::template_, - hana::type_c>) - ), entity_names ); } diff --git a/src/def/builder/Store.hpp b/src/def/builder/Store.hpp index 17e96d48..4ca77fa8 100644 --- a/src/def/builder/Store.hpp +++ b/src/def/builder/Store.hpp @@ -8,7 +8,6 @@ #define NBDL_DEF_BUILDER_STORE_HPP #include -#include #include namespace nbdl_def { @@ -19,13 +18,8 @@ struct Store template constexpr auto operator()(PathType path_type, AccessPoint access_point) const { - auto store_emitter = AccessPointMeta::storeEmitter(access_point)(path_type); - //this adds a requirement for StoreEmitter to export a ListenerHandler - auto listener_handler = hana::type_c; return hana::template_( AccessPointMeta::storeContainer(access_point)(path_type), - store_emitter, - listener_handler, path_type ); } diff --git a/src/def/directives.hpp b/src/def/directives.hpp index f4ce97ac..6ba2fda0 100644 --- a/src/def/directives.hpp +++ b/src/def/directives.hpp @@ -27,7 +27,6 @@ MPDEF_DIRECTIVE(UpdateRaw) MPDEF_DIRECTIVE(Delete) MPDEF_DIRECTIVE_LEAF(StoreContainer) -MPDEF_DIRECTIVE_LEAF(StoreEmitter) MPDEF_DIRECTIVE_LEAF(PrivatePayload) MPDEF_DIRECTIVE_LEAF(UseLocalVersion) MPDEF_DIRECTIVE_LEAF(FireAndForget) diff --git a/src/nbdl b/src/nbdl deleted file mode 100644 index 0b690aa7..00000000 --- a/src/nbdl +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef NBDL_MAIN_HEADER_FILE -#define NBDL_MAIN_HEADER_FILE - -#include - -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -#include - - -#endif diff --git a/src/store_emitter/HashMap.hpp b/src/store_emitter/HashMap.hpp deleted file mode 100644 index 9cdd37c2..00000000 --- a/src/store_emitter/HashMap.hpp +++ /dev/null @@ -1,73 +0,0 @@ -// -// Copyright Jason Rice 2015 -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -#ifndef NBDL_STORE_EMITTER_HASH_MAP_HPP -#define NBDL_STORE_EMITTER_HASH_MAP_HPP - -#include -#include -#include - -namespace nbdl { -namespace store_emitter { - -template -class HashMap -{ - public: - - using Variant_ = Variant; - using ListenerHandler = ListenerHandlerType; - - private: - - using HashFn = typename PathType::HashFn; - using PredFn = typename PathType::PredFn; - using Vector = std::vector; - using Container = std::unordered_map; - Container map; - - public: - - template - void emit(const PathType& path, Fn fn) - { - auto map_node = map.find(path); - if (map_node != map.end()) - { - Vector vector = map_node->second; - for (ListenerHandler listener : vector) - { - fn(listener); - } - } - } - - void addListener(const PathType& path, ListenerHandler listener) - { - map[path].push_back(listener); - } - - void removeListener(const PathType& path, const ListenerHandler& listener) - { - auto map_node = map.find(path); - if (map_node != map.end()) - { - Vector vector = map_node->second; - vector.erase(std::find(vector.begin(), vector.end(), listener)); - if (!vector.size()) - { - map.erase(path); - return; - } - }; - } -}; - -}//store_emitter -}//nbdl - -#endif diff --git a/test/nbdl/builder/CMakeLists.txt b/test/nbdl/builder/CMakeLists.txt index 80bc176b..aee7df0f 100644 --- a/test/nbdl/builder/CMakeLists.txt +++ b/test/nbdl/builder/CMakeLists.txt @@ -52,3 +52,7 @@ nbdl_add_test("test.nbdl.builder.entity_messages" nbdl_add_test("test.nbdl.builder.message_variants" message_variants.cpp ) + +nbdl_add_test("test.nbdl.builder.context" + context.cpp +) diff --git a/test/nbdl/builder/access_point_meta.cpp b/test/nbdl/builder/access_point_meta.cpp index 657ee757..54bb0c5f 100644 --- a/test/nbdl/builder/access_point_meta.cpp +++ b/test/nbdl/builder/access_point_meta.cpp @@ -18,20 +18,17 @@ int main() constexpr auto name = hana::int_c<0>; constexpr auto actions = hana::int_c<1>; constexpr auto storeContainer = hana::int_c<2>; - constexpr auto storeEmitter = hana::int_c<3>; constexpr auto entityNames = hana::int_c<4>; constexpr auto x = builder::makeAccessPointMeta( name , actions , storeContainer - , storeEmitter , entityNames ); BOOST_HANA_CONSTANT_ASSERT(AccessPointMeta::name(x) == name); BOOST_HANA_CONSTANT_ASSERT(AccessPointMeta::actions(x) == actions); BOOST_HANA_CONSTANT_ASSERT(AccessPointMeta::storeContainer(x) == storeContainer); - BOOST_HANA_CONSTANT_ASSERT(AccessPointMeta::storeEmitter(x) == storeEmitter); BOOST_HANA_CONSTANT_ASSERT(AccessPointMeta::entityNames(x) == entityNames); BOOST_HANA_CONSTANT_ASSERT(hana::equal(x, @@ -39,7 +36,6 @@ int main() AccessPointMeta::name = name, AccessPointMeta::actions = actions, AccessPointMeta::storeContainer = storeContainer, - AccessPointMeta::storeEmitter = storeEmitter, AccessPointMeta::entityNames = entityNames ) )); @@ -48,8 +44,7 @@ int main() AccessPointMeta::actions = actions, AccessPointMeta::name = name, AccessPointMeta::storeContainer = storeContainer, - AccessPointMeta::entityNames = entityNames, - AccessPointMeta::storeEmitter = storeEmitter + AccessPointMeta::entityNames = entityNames ) )); } diff --git a/test/nbdl/builder/context.cpp b/test/nbdl/builder/context.cpp new file mode 100644 index 00000000..6c3c52e3 --- /dev/null +++ b/test/nbdl/builder/context.cpp @@ -0,0 +1,84 @@ +// +// Copyright Jason Rice 2016 +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// + +#include +#include +#include +#include + +#include + +namespace hana = boost::hana; + +struct DummyProvider { }; + +namespace entity { + struct Client + { + int id; + }; + struct MyEntity + { + int id; + int client_id; + }; +}//entity + +namespace nbdl { + NBDL_ENTITY( + entity::Client, + id); + NBDL_ENTITY( + entity::MyEntity, + id, + client_id ); +}//nbdl +using OnlySupportedPath = + typename decltype( + nbdl::path_type + )::type; + + +namespace my_context { + using namespace nbdl_def; + + auto create = buildContextFactory( + Context( + Entities( + Entity( + Type(hana::type_c) + ), + Entity( + Type(hana::type_c) + ) + ), + // TODO: Define a Consumer here. + Provider( + Type(hana::type_c), + AccessPoint( + Name(hana::type_c), + EntityName(hana::type_c), + Actions(Read()), + AccessPoint( + Name(hana::type_c), + EntityName(hana::type_c), + Actions(Read()) + ) + ) + ) + ) + ); +} // my_context + +int main() +{ + // Okay, We're not actually testing anything here, but + // at least we'll know this compiles, and a better test + // can be added once the representation of Context is + // closer to being sorted out. + auto ctx = my_context::create(); +} diff --git a/test/nbdl/builder/entity_messages.cpp b/test/nbdl/builder/entity_messages.cpp index b33070f5..d75dba78 100644 --- a/test/nbdl/builder/entity_messages.cpp +++ b/test/nbdl/builder/entity_messages.cpp @@ -58,7 +58,6 @@ int main() AccessPointMeta::name = names::Foo, AccessPointMeta::actions = mpdef::make_list(nbdl_def::tag::Create), AccessPointMeta::storeContainer = hana::type_c, - AccessPointMeta::storeEmitter = hana::type_c, AccessPointMeta::entityNames = mpdef::make_list(names::Entity1) ); using PathType = typename decltype(nbdl::path_type)::type; @@ -99,7 +98,6 @@ int main() nbdl_def::tag::Delete ), AccessPointMeta::storeContainer = hana::type_c, - AccessPointMeta::storeEmitter = hana::type_c, AccessPointMeta::entityNames = mpdef::make_list(names::Entity1) ); using PathType = typename decltype(nbdl::path_type)::type; diff --git a/test/nbdl/builder/enumerate_access_points.cpp b/test/nbdl/builder/enumerate_access_points.cpp index c5e70437..ac6e86cb 100644 --- a/test/nbdl/builder/enumerate_access_points.cpp +++ b/test/nbdl/builder/enumerate_access_points.cpp @@ -6,7 +6,6 @@ // #include -#include #include #include #include @@ -63,7 +62,6 @@ int main() Context( PrimaryKey(Type(hana::type_c)), StoreContainer(hana::type_c)>), - StoreEmitter(hana::type_c)>), Providers( Provider( Name(names::Provider1), @@ -116,8 +114,6 @@ int main() ), AccessPointMeta::storeContainer = hana::type_c)>, - AccessPointMeta::storeEmitter = - hana::type_c)>, AccessPointMeta::entityNames = mpdef::make_list(names::Root2) ), // Root3 @@ -128,8 +124,6 @@ int main() ), AccessPointMeta::storeContainer = hana::type_c)>, - AccessPointMeta::storeEmitter = - hana::type_c)>, AccessPointMeta::entityNames = mpdef::make_list(names::Root3) ), // Root1/Nested1 @@ -143,8 +137,6 @@ int main() ), AccessPointMeta::storeContainer = hana::type_c)>, - AccessPointMeta::storeEmitter = - hana::type_c)>, AccessPointMeta::entityNames = mpdef::make_list(names::Root1, names::Nested1) ), // Root1/Nested2/Nested3 @@ -155,8 +147,6 @@ int main() ), AccessPointMeta::storeContainer = hana::type_c)>, - AccessPointMeta::storeEmitter = - hana::type_c)>, AccessPointMeta::entityNames = mpdef::make_list( names::Root1, names::Nested2, @@ -174,8 +164,6 @@ int main() ), AccessPointMeta::storeContainer = hana::type_c)>, - AccessPointMeta::storeEmitter = - hana::type_c)>, AccessPointMeta::entityNames = mpdef::make_list( names::Root2, names::Nested1 @@ -189,8 +177,6 @@ int main() ), AccessPointMeta::storeContainer = hana::type_c)>, - AccessPointMeta::storeEmitter = - hana::type_c)>, AccessPointMeta::entityNames = mpdef::make_list( names::Root2, names::Nested2, @@ -208,8 +194,6 @@ int main() ), AccessPointMeta::storeContainer = hana::type_c)>, - AccessPointMeta::storeEmitter = - hana::type_c)>, AccessPointMeta::entityNames = mpdef::make_list( names::Root3, names::Nested1 @@ -223,8 +207,6 @@ int main() ), AccessPointMeta::storeContainer = hana::type_c)>, - AccessPointMeta::storeEmitter = - hana::type_c)>, AccessPointMeta::entityNames = mpdef::make_list( names::Root3, names::Nested2, @@ -249,11 +231,6 @@ int main() == hana::type_c)> ); - BOOST_HANA_CONSTANT_ASSERT( - AccessPointMeta::storeEmitter(hana::at(result, hana::int_c<0>)) - == - hana::type_c)> - ); BOOST_HANA_CONSTANT_ASSERT( AccessPointMeta::entityNames(hana::at(result, hana::int_c<0>)) == diff --git a/test/nbdl/builder/message_variants.cpp b/test/nbdl/builder/message_variants.cpp index 3ae412f4..533554f0 100644 --- a/test/nbdl/builder/message_variants.cpp +++ b/test/nbdl/builder/message_variants.cpp @@ -63,7 +63,6 @@ int main() AccessPointMeta::name = names::Foo, AccessPointMeta::actions = hana::make_tuple(nbdl_def::tag::Create), AccessPointMeta::storeContainer = hana::type_c, - AccessPointMeta::storeEmitter = hana::type_c, AccessPointMeta::entityNames = hana::make_tuple(names::Entity1) ); using PathType = typename decltype(nbdl::path_type)::type; diff --git a/test/nbdl/builder/path.cpp b/test/nbdl/builder/path.cpp index 847f5f95..02f1c4f7 100644 --- a/test/nbdl/builder/path.cpp +++ b/test/nbdl/builder/path.cpp @@ -73,7 +73,6 @@ int main() AccessPointMeta::name = names::Foo, AccessPointMeta::actions = hana::make_tuple(nbdl_def::tag::Create), AccessPointMeta::storeContainer = hana::type_c, - AccessPointMeta::storeEmitter = hana::type_c, AccessPointMeta::entityNames = hana::make_tuple(names::Entity1) ); @@ -88,7 +87,6 @@ int main() AccessPointMeta::name = names::Foo, AccessPointMeta::actions = hana::make_tuple(nbdl_def::tag::Create), AccessPointMeta::storeContainer = hana::type_c, - AccessPointMeta::storeEmitter = hana::type_c, AccessPointMeta::entityNames = hana::make_tuple(names::Entity1, names::Entity2, names::Entity3) ); diff --git a/test/nbdl/builder/provider_map.cpp b/test/nbdl/builder/provider_map.cpp index 3662fe87..522867f5 100644 --- a/test/nbdl/builder/provider_map.cpp +++ b/test/nbdl/builder/provider_map.cpp @@ -74,14 +74,12 @@ constexpr auto access_points_1 = hana::make_tuple( builder::AccessPointMeta::name = names::Foo, builder::AccessPointMeta::actions = hana::make_tuple(nbdl_def::tag::Create), builder::AccessPointMeta::storeContainer = hana::type_c, - builder::AccessPointMeta::storeEmitter = hana::type_c, builder::AccessPointMeta::entityNames = hana::make_tuple(names::Entity_1_1) ), builder::makeAccessPointMetaWithMap( builder::AccessPointMeta::name = names::Foo, builder::AccessPointMeta::actions = hana::make_tuple(nbdl_def::tag::Create), builder::AccessPointMeta::storeContainer = hana::type_c, - builder::AccessPointMeta::storeEmitter = hana::type_c, builder::AccessPointMeta::entityNames = hana::make_tuple(names::Entity_1_2) ) ); @@ -91,7 +89,6 @@ constexpr auto access_points_2 = hana::make_tuple( builder::AccessPointMeta::name = names::Foo, builder::AccessPointMeta::actions = hana::make_tuple(nbdl_def::tag::Create), builder::AccessPointMeta::storeContainer = hana::type_c, - builder::AccessPointMeta::storeEmitter = hana::type_c, builder::AccessPointMeta::entityNames = hana::make_tuple(names::Entity_2_1) ) ); diff --git a/test/nbdl/builder/store.cpp b/test/nbdl/builder/store.cpp index 66bf0815..e9718ef6 100644 --- a/test/nbdl/builder/store.cpp +++ b/test/nbdl/builder/store.cpp @@ -11,7 +11,6 @@ #include #include #include -#include #include #include @@ -42,18 +41,12 @@ int main() { { using builder::AccessPointMeta; - constexpr auto listener_handler = hana::type_c>; constexpr auto store_impl = hana::template_; - constexpr auto emitter_impl = hana::partial( - hana::template_, - listener_handler - ); constexpr auto path_type = nbdl::path_type; constexpr auto access_point = builder::makeAccessPointMetaWithMap( AccessPointMeta::name = names::Foo, AccessPointMeta::actions = hana::make_tuple(nbdl_def::tag::Create), AccessPointMeta::storeContainer = store_impl, - AccessPointMeta::storeEmitter = emitter_impl, AccessPointMeta::entityNames = hana::make_tuple(names::E1) ); @@ -62,8 +55,6 @@ int main() == hana::template_( store_impl(path_type), - emitter_impl(path_type), - listener_handler, path_type ) ); diff --git a/test/nbdl/builder/store_map.cpp b/test/nbdl/builder/store_map.cpp index 8ab30796..91c7d1f8 100644 --- a/test/nbdl/builder/store_map.cpp +++ b/test/nbdl/builder/store_map.cpp @@ -11,7 +11,6 @@ #include #include #include -#include #include #include @@ -86,32 +85,24 @@ int main() constexpr auto path_type_2 = nbdl::path_type; constexpr auto path_type_3 = nbdl::path_type; - constexpr auto listener_handler = hana::type_c>; constexpr auto store_impl = hana::template_; - constexpr auto emitter_impl = hana::partial( - hana::template_, - listener_handler - ); constexpr auto access_points = hana::make_tuple( builder::makeAccessPointMetaWithMap( AccessPointMeta::name = names::Foo1, AccessPointMeta::actions = hana::make_tuple(nbdl_def::tag::Create), AccessPointMeta::storeContainer = store_impl, - AccessPointMeta::storeEmitter = emitter_impl, AccessPointMeta::entityNames = hana::make_tuple(names::Entity1) ), builder::makeAccessPointMetaWithMap( AccessPointMeta::name = names::Foo2, AccessPointMeta::actions = hana::make_tuple(nbdl_def::tag::Create), AccessPointMeta::storeContainer = store_impl, - AccessPointMeta::storeEmitter = emitter_impl, AccessPointMeta::entityNames = hana::make_tuple(names::Entity1, names::Entity2) ), builder::makeAccessPointMetaWithMap( AccessPointMeta::name = names::Foo3, AccessPointMeta::actions = hana::make_tuple(nbdl_def::tag::Create), AccessPointMeta::storeContainer = store_impl, - AccessPointMeta::storeEmitter = emitter_impl, AccessPointMeta::entityNames = hana::make_tuple(names::Entity3) ) ); @@ -124,24 +115,18 @@ int main() hana::make_pair(path_type_1, typename decltype(hana::template_( store_impl(path_type_1), - emitter_impl(path_type_1), - listener_handler, path_type_1 ))::type{} ), hana::make_pair(path_type_2, typename decltype(hana::template_( store_impl(path_type_2), - emitter_impl(path_type_2), - listener_handler, path_type_2 ))::type{} ), hana::make_pair(path_type_3, typename decltype(hana::template_( store_impl(path_type_3), - emitter_impl(path_type_3), - listener_handler, path_type_3 ))::type{} ) diff --git a/test/nbdl/core/CMakeLists.txt b/test/nbdl/core/CMakeLists.txt index 725e5953..47319f98 100644 --- a/test/nbdl/core/CMakeLists.txt +++ b/test/nbdl/core/CMakeLists.txt @@ -10,7 +10,4 @@ nbdl_catch_test_suite(build_targets "test.nbdl.core" variant.cpp store.cpp provider_map.cpp - context_read.cpp ) -#requires boost -#msg_id.cpp diff --git a/test/nbdl/core/context_read.cpp b/test/nbdl/core/context_read.cpp deleted file mode 100644 index c24e2051..00000000 --- a/test/nbdl/core/context_read.cpp +++ /dev/null @@ -1,222 +0,0 @@ -// -// Copyright Jason Rice 2015 -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -#include - -#include -#include -#include - -namespace hana = boost::hana; - -namespace entity { - struct Client - { - int id; - }; - struct MyEntity - { - int id; - int client_id; - }; -}//entity - -namespace nbdl { - NBDL_ENTITY( - entity::Client, - id); - NBDL_ENTITY( - entity::MyEntity, - id, - client_id ); -}//nbdl - - -namespace my_context { - using namespace nbdl_def; - - template - auto create = nbdl_def::buildContextFactory( - nbdl_def::Context( - Entities( - Entity( - Type(hana::type_c) - ), - Entity( - Type(hana::type_c) - ) - ), - Provider( - Type(hana::type_c), - AccessPoint( - Name(hana::type_c), - EntityName(hana::type_c), - Actions(Read()), - AccessPoint( - Name(hana::type_c), - EntityName(hana::type_c), - Actions(Read()) - ) - ) - ) - ) - ); -}//my_context - -namespace test_stuff { - -using OnlySupportedPath = typename decltype( - nbdl::path_type)::type; -static_assert(std::is_same::value, - "Only supported type entity is not MyEntity."); - -struct TestProvider -{ - template - void read(OnlySupportedPath path, Fn fn) - { - if (path.getKey() == 1 && path.getKey() == 5) - { - entity::MyEntity my_entity = { 5, 1 }; - fn(my_entity); - } - else - { - fn(nbdl::NotFound{}); - } - } -}; - -class TestProviderAsync -{ - static std::function m_fn; - - public: - - static void flush__() - { - m_fn(); - } - - template - void read(OnlySupportedPath path, Fn fn) - { - m_fn = [path, fn]() { - if (path.getKey() == 1 && path.getKey() == 5) - { - entity::MyEntity my_entity = { 5, 1 }; - fn(my_entity); - } - else - { - fn(nbdl::NotFound{}); - } - }; - } -}; -std::function TestProviderAsync::m_fn = [](){}; - -}//test_stuff - -TEST_CASE("Read an object from a context.", "[context]") -{ - bool result; - auto ctx = my_context::create(); - - result = ctx->read(test_stuff::OnlySupportedPath(1, 5), - [](nbdl::Unresolved) { - return false; - }, - [](nbdl::NotFound) { - return false; - }, - [](entity::MyEntity m) { - return (m.id == 5 && m.client_id == 1); - }); - - CHECK(result); -} - -TEST_CASE("Context should propagate NotFound from server callback.", "[context]") -{ - bool result = false; - auto ctx = my_context::create(); - - result = ctx->read(test_stuff::OnlySupportedPath(1, 6), - [](nbdl::Unresolved) { - return false; - }, - [](nbdl::NotFound) { - return true; - }, - [](entity::MyEntity const&) { - return false; - }); - - CHECK(result); -} - -TEST_CASE("Context should emit change to listener", "[context]") -{ - bool function_was_called = false; - int result = 0; - auto ctx = my_context::create(); - test_stuff::OnlySupportedPath path(1, 5); - - auto listener = ctx->makeListener(path, [&]() { - function_was_called = true; - result = ctx->read(path, - [](nbdl::Unresolved) { - return 1; - }, - [](entity::MyEntity const&) { - return 2; - }, - [](nbdl::NotFound) { - return 3; - }); - }); - - ctx->read(path, nbdl::noop); - CHECK_FALSE(result); //does not emit for unresolved - test_stuff::TestProviderAsync::flush__(); - CHECK(function_was_called); - CHECK(result == 2); //entity::MyEntity -} - -TEST_CASE("Context should not emit to a listener that has been destroyed.", "[context]") -{ - bool function_was_called = false; - int result = 0; - test_stuff::OnlySupportedPath path(1, 5); - - { - auto ctx = my_context::create(); - /* - in this test if the listener fails - to unregister it would result in UB. - */ - auto listener = ctx->makeListener(path, [&]() { - function_was_called = true; - result = ctx->read(path, - [](nbdl::Unresolved) { - return 1; - }, - [](entity::MyEntity const&) { - return 2; - }, - [](nbdl::NotFound) { - return 3; - }); - }); - ctx->read(path, nbdl::noop); - } //listener is destroyed and should unregister from the context - - CHECK(result == 0); //does not emit for unresolved - test_stuff::TestProviderAsync::flush__(); - CHECK_FALSE(function_was_called); - CHECK(result == 0); -} diff --git a/test/nbdl/core/store.cpp b/test/nbdl/core/store.cpp index fd78cca0..f152b9d0 100644 --- a/test/nbdl/core/store.cpp +++ b/test/nbdl/core/store.cpp @@ -4,7 +4,6 @@ // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // -#include #include #include #include @@ -15,8 +14,6 @@ template using Store = nbdl::Store< nbdl::store::HashMap, - nbdl::store_emitter::HashMap, Path_>, - nbdl::ListenerHandlerDummy<>, Path_ >; struct Client @@ -28,7 +25,6 @@ struct MyEntity int id; int client_id; }; -//using ClientPath = typename decltype(nbdl::path_type)::type; using MyEntityPath = typename decltype(nbdl::path_type)::type; namespace nbdl { NBDL_ENTITY( @@ -44,9 +40,9 @@ TEST_CASE("Access an uninitialized value from a store.", "[store]") { Store store; MyEntityPath path = MyEntityPath(1, 5); - bool did_make_request = false; - bool result = store.get([&](MyEntityPath ) { did_make_request = true; }, path, + bool result = store.get( + path, [](nbdl::Unresolved) { return true; }, @@ -55,20 +51,15 @@ TEST_CASE("Access an uninitialized value from a store.", "[store]") }); CHECK(result == true); - CHECK(did_make_request == true); } TEST_CASE("Force assign and access a value from a store.", "[store]") { Store store; MyEntityPath path = MyEntityPath(1, 5); MyEntity my_entity = { 5, 1 }; - bool did_make_request = false; store.forceAssign(path, my_entity); bool result = store.get( - [&](MyEntityPath ) { - did_make_request = true; - }, path, [](MyEntity entity) { CHECK(entity.id == 5); @@ -79,7 +70,6 @@ TEST_CASE("Force assign and access a value from a store.", "[store]") }); CHECK(result == true); - CHECK(did_make_request == false); } TEST_CASE("Suggest a value to a store.", "[store]") @@ -87,13 +77,9 @@ TEST_CASE("Suggest a value to a store.", "[store]") Store store; MyEntityPath path = MyEntityPath(1, 5); MyEntity my_entity = { 5, 1 }; - bool did_make_request = false; store.suggestAssign(path, my_entity); bool result = store.get( - [&](MyEntityPath ) { - did_make_request = true; - }, path, [](MyEntity entity) { CHECK(entity.id == 5); @@ -104,7 +90,6 @@ TEST_CASE("Suggest a value to a store.", "[store]") }); CHECK(result); - CHECK_FALSE(did_make_request); } @@ -114,16 +99,12 @@ TEST_CASE("Suggest a value to a store where the value already exists.", "[store] MyEntityPath path = MyEntityPath(1, 5); MyEntity my_entity_original = { 6, 1 }; MyEntity my_entity = { 5, 1 }; - bool did_make_request = false; store.forceAssign(path, my_entity_original); store.suggestAssign(path, my_entity); bool result = store.get( - [&](MyEntityPath) { - did_make_request = true; - }, path, [](MyEntity entity) { CHECK(entity.id == 6); @@ -134,5 +115,4 @@ TEST_CASE("Suggest a value to a store where the value already exists.", "[store] }); CHECK(result); - CHECK_FALSE(did_make_request); }