diff options
-rw-r--r-- | include/gras/block.hpp | 9 | ||||
-rw-r--r-- | include/gras/detail/block.hpp | 8 | ||||
-rw-r--r-- | include/gras/detail/property.hpp | 25 | ||||
-rw-r--r-- | lib/block_props.cpp | 83 | ||||
-rw-r--r-- | lib/gras_impl/block_actor.hpp | 7 | ||||
-rw-r--r-- | lib/gras_impl/messages.hpp | 9 | ||||
-rw-r--r-- | lib/register_messages.cpp | 1 |
7 files changed, 120 insertions, 22 deletions
diff --git a/include/gras/block.hpp b/include/gras/block.hpp index d33457b..d3e9848 100644 --- a/include/gras/block.hpp +++ b/include/gras/block.hpp @@ -106,7 +106,7 @@ struct GRAS_API OutputPortConfig size_t maximum_items; }; -struct GRAS_API Block : Element, PropertyInterface +struct GRAS_API Block : Element { //! Contruct an empty/null block @@ -458,6 +458,13 @@ struct GRAS_API Block : Element, PropertyInterface const SBufferConfig &config ); + /******************************************************************* + * private implementation guts for template support + ******************************************************************/ + void _register_property(const std::string &, PropertyRegistrySptr); + void _set_property(const std::string &, const PMCC &); + PMCC _get_property(const std::string &); + }; } //namespace gras diff --git a/include/gras/detail/block.hpp b/include/gras/detail/block.hpp index 483b2bf..a8bc2b1 100644 --- a/include/gras/detail/block.hpp +++ b/include/gras/detail/block.hpp @@ -13,21 +13,21 @@ GRAS_FORCE_INLINE void Block::register_property( void(ClassType::*set)(const ValueType &) ) { - boost::shared_ptr<PropertyRegistry> pr; + PropertyRegistrySptr pr; pr.reset(new PropertyRegistryImpl<ClassType, ValueType>(this, get, set)); - this->property_interface_register(key, pr); + this->_register_property(key, pr); } template <typename ValueType> GRAS_FORCE_INLINE void Block::set_property(const std::string &key, const ValueType &value) { - return this->property_interface_set(key, PMC_M(value)); + return this->_set_property(key, PMC_M(value)); } template <typename ValueType> GRAS_FORCE_INLINE ValueType Block::get_property(const std::string &key) { - return this->property_interface_get(key).as<ValueType>(); + return this->_get_property(key).as<ValueType>(); } } //namespace gras diff --git a/include/gras/detail/property.hpp b/include/gras/detail/property.hpp index 467fb70..fd13025 100644 --- a/include/gras/detail/property.hpp +++ b/include/gras/detail/property.hpp @@ -3,19 +3,23 @@ #ifndef INCLUDED_GRAS_DETAIL_PROPERTY_HPP #define INCLUDED_GRAS_DETAIL_PROPERTY_HPP +#include <gras/gras.hpp> #include <PMC/PMC.hpp> +#include <boost/shared_ptr.hpp> namespace gras { -struct PropertyRegistry +struct GRAS_API PropertyRegistry { - PropertyRegistry(void){} - ~PropertyRegistry(void){} + PropertyRegistry(void); + virtual ~PropertyRegistry(void); virtual void set(const PMCC &) = 0; virtual PMCC get(void) = 0; }; +typedef boost::shared_ptr<PropertyRegistry> PropertyRegistrySptr; + template <typename ClassType, typename ValueType> struct PropertyRegistryImpl : PropertyRegistry { @@ -28,7 +32,7 @@ struct PropertyRegistryImpl : PropertyRegistry _getter(getter), _setter(setter) {} - ~PropertyRegistryImpl(void){} + virtual ~PropertyRegistryImpl(void){} void set(const PMCC &value) { @@ -45,19 +49,6 @@ struct PropertyRegistryImpl : PropertyRegistry void(ClassType::*_setter)(const ValueType &); }; -struct PropertyInterface -{ - virtual void property_interface_register( - const std::string &key, - boost::shared_ptr<PropertyRegistry> pr - ) = 0; - - virtual void property_interface_set(const std::string &key, const PMCC &value) = 0; - - virtual PMCC property_interface_get(const std::string &key) = 0; - -}; - } //namespace gras #endif /*INCLUDED_GRAS_DETAIL_PROPERTY_HPP*/ diff --git a/lib/block_props.cpp b/lib/block_props.cpp index 641ecbb..bba9441 100644 --- a/lib/block_props.cpp +++ b/lib/block_props.cpp @@ -2,3 +2,86 @@ #include "element_impl.hpp" #include <gras/block.hpp> + +using namespace gras; + +PropertyRegistry::PropertyRegistry(void){} +PropertyRegistry::~PropertyRegistry(void){} + +void BlockActor::handle_prop_access( + const PropAccessMessage &message, + const Theron::Address from +) +{ + ASSERT(this->prio_count.Load() != 0); + this->prio_count.Decrement(); + + PropAccessMessage reply; + reply.set = not message.set; + reply.key = message.key; + + PropertyRegistrySptr pr = prop_registry[message.key]; + if (not pr) reply.error = "no property registered for key: " + message.key; + else try + { + if (message.set) pr->set(message.value); + else reply.value = pr->get(); + } + catch (const std::exception &e) + { + reply.error = e.what(); + } + catch (...) + { + reply.error = "unknown error"; + } + + this->Send(reply, from); //ACK +} + +void Block::_register_property(const std::string &key, PropertyRegistrySptr pr) +{ + (*this)->block->prop_registry[key] = pr; +} + +struct PropAccessReceiver : Theron::Receiver +{ + PropAccessReceiver(void) + { + this->RegisterHandler(this, &PropAccessReceiver::handle_prop_access); + } + + void handle_prop_access(const PropAccessMessage &msg, const Theron::Address) + { + this->message = msg; + } + + PropAccessMessage message; +}; + +PMCC BlockActor::prop_access_dispatcher(const std::string &key, const PMCC &value, const bool set) +{ + PropAccessReceiver receiver; + PropAccessMessage message; + message.set = set; + message.key = key; + message.value = value; + this->Push(message, receiver.GetAddress()); + this->prio_count.Increment(); + receiver.Wait(); + if (not receiver.message.error.empty()) + { + throw std::runtime_error(receiver.message.error); + } + return receiver.message.value; +} + +void Block::_set_property(const std::string &key, const PMCC &value) +{ + (*this)->block->prop_access_dispatcher(key, value, true); +} + +PMCC Block::_get_property(const std::string &key) +{ + return (*this)->block->prop_access_dispatcher(key, PMCC(), false); +} diff --git a/lib/gras_impl/block_actor.hpp b/lib/gras_impl/block_actor.hpp index 935c87d..d7429f1 100644 --- a/lib/gras_impl/block_actor.hpp +++ b/lib/gras_impl/block_actor.hpp @@ -19,6 +19,7 @@ #include <Theron/Detail/Threading/Atomic.h> #include <vector> #include <set> +#include <map> namespace gras { @@ -61,6 +62,7 @@ struct BlockActor : Apology::Worker this->RegisterHandler(this, &BlockActor::handle_output_alloc); this->RegisterHandler(this, &BlockActor::handle_output_update); + this->RegisterHandler(this, &BlockActor::handle_prop_access); this->RegisterHandler(this, &BlockActor::handle_self_kick); this->RegisterHandler(this, &BlockActor::handle_get_stats); } @@ -90,6 +92,7 @@ struct BlockActor : Apology::Worker void handle_output_alloc(const OutputAllocMessage &, const Theron::Address); void handle_output_update(const OutputUpdateMessage &, const Theron::Address); + void handle_prop_access(const PropAccessMessage &, const Theron::Address); void handle_self_kick(const SelfKickMessage &, const Theron::Address); void handle_get_stats(const GetStatsMessage &, const Theron::Address); @@ -180,6 +183,10 @@ struct BlockActor : Apology::Worker std::vector<std::vector<OutputHintMessage> > output_allocation_hints; + //property stuff + PMCC prop_access_dispatcher(const std::string &key, const PMCC &value, const bool set); + std::map<std::string, PropertyRegistrySptr> prop_registry; + BlockStats stats; }; diff --git a/lib/gras_impl/messages.hpp b/lib/gras_impl/messages.hpp index 6cc685b..15a7c2e 100644 --- a/lib/gras_impl/messages.hpp +++ b/lib/gras_impl/messages.hpp @@ -131,6 +131,14 @@ struct OutputUpdateMessage //-- do not ack //---------------------------------------------------------------------- +struct PropAccessMessage +{ + bool set; + std::string key; + PMCC value; + std::string error; +}; + struct SelfKickMessage { //empty @@ -172,6 +180,7 @@ THERON_DECLARE_REGISTERED_MESSAGE(gras::OutputHintMessage); THERON_DECLARE_REGISTERED_MESSAGE(gras::OutputAllocMessage); THERON_DECLARE_REGISTERED_MESSAGE(gras::OutputUpdateMessage); +THERON_DECLARE_REGISTERED_MESSAGE(gras::PropAccessMessage); THERON_DECLARE_REGISTERED_MESSAGE(gras::SelfKickMessage); THERON_DECLARE_REGISTERED_MESSAGE(gras::GetStatsMessage); diff --git a/lib/register_messages.cpp b/lib/register_messages.cpp index e502e5b..058f308 100644 --- a/lib/register_messages.cpp +++ b/lib/register_messages.cpp @@ -26,5 +26,6 @@ THERON_DEFINE_REGISTERED_MESSAGE(gras::OutputHintMessage); THERON_DEFINE_REGISTERED_MESSAGE(gras::OutputAllocMessage); THERON_DEFINE_REGISTERED_MESSAGE(gras::OutputUpdateMessage); +THERON_DEFINE_REGISTERED_MESSAGE(gras::PropAccessMessage); THERON_DEFINE_REGISTERED_MESSAGE(gras::SelfKickMessage); THERON_DEFINE_REGISTERED_MESSAGE(gras::GetStatsMessage); |