Skip to content

Commit

Permalink
[context] remove emitter from store
Browse files Browse the repository at this point in the history
  - context gets a fresh start
  • Loading branch information
ricejasonf committed Feb 28, 2016
1 parent 8de83a9 commit e1dec1f
Show file tree
Hide file tree
Showing 25 changed files with 142 additions and 752 deletions.
101 changes: 19 additions & 82 deletions src/Context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,97 +7,34 @@
#ifndef NBDL_CONTEXT_HPP
#define NBDL_CONTEXT_HPP

#include<memory>

namespace nbdl {

namespace details {

template<typename Traits>
class Context : public std::enable_shared_from_this<Context<Traits>>
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<Context>;
using WeakPtr = std::weak_ptr<Context>;

template<typename PathType>
using Listener = details::Listener<ListenerHandler_, WeakPtr, PathType>;

private:

ProviderMap providers;
Consumers consumers;
StoreMap stores;

public:

template<typename PathType, typename... Args>
Listener<PathType> makeListener(PathType path, Args... args)
{
auto wrapper = Listener<PathType>(path, this->shared_from_this(), args...);
stores[hana::type_c<PathType>].addListener(path, wrapper.handler());
return wrapper;
}
template<typename PathType>
void addListener(const PathType& path, const ListenerHandler_& listener)
{
stores[hana::type_c<PathType>].addListener(path, listener);
}
template<typename PathType>
void removeListener(const PathType& path, const ListenerHandler_& listener)
{
stores[hana::type_c<PathType>].removeListener(path, listener);
}

template<typename Path,typename... MatchFns>
auto read(Path path, MatchFns... fns)
{
return stores[hana::type_c<Path>].get(
//called if store needs to request value
[&](const Path& path) {
//request from provider
auto self = this->shared_from_this();
providers[hana::type_c<Path>].read(path, [path, self](auto&& value) {
self->stores[hana::type_c<Path>]
.suggestAssign(path, std::forward<decltype(value)>(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<typename Message>
auto pushDownstream(Message&&)
{
// access providers, consumers and stores
}

}//details
// should be callable only by consumers
template<typename Message>
auto pushUpstream(Message&&)
{
// access providers, consumers and stores
}

template<typename Traits>
struct Context
{
using Type = details::Context<Traits>;
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<typename... Args>
static SharedPtr create(Args... args)
{
return std::make_shared<Type>(args...);
}
};

}//nbdl
Expand Down
128 changes: 0 additions & 128 deletions src/Listener.hpp

This file was deleted.

53 changes: 9 additions & 44 deletions src/Store.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename T>
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<typename T>
void forceAssign(const PathType& path, T&& value)
{
emitChange(path, impl.assign(path, std::forward<T>(value)));
container.assign(path, std::forward<T>(value));
}

template<typename T>
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<Unresolved>())
{
value = std::forward<T>(val);
emitChange(path, value);
}
}

template<typename RequestFn, typename... Fns>
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<typename... MatchFns>
auto get(const PathType& path, MatchFns... fns)
{
emitter.removeListener(path, listener);
return container.get(path).match(fns...);
}
};

Expand Down
65 changes: 0 additions & 65 deletions src/StoreCollection.hpp

This file was deleted.

0 comments on commit e1dec1f

Please sign in to comment.