From 4888233252d58f0df3c8773ec3268a0ed7bea1d2 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Tue, 28 Jun 2011 11:11:04 -0700 Subject: uhd: re-work to make the properties easier to use --- host/include/uhd/CMakeLists.txt | 2 +- host/include/uhd/property.hpp | 108 ------------------------------------- host/include/uhd/property_tree.hpp | 63 +++++++++++++++++++--- host/include/uhd/property_tree.ipp | 96 +++++++++++++++++++++++++++++++++ host/include/uhd/types/sensors.hpp | 3 -- 5 files changed, 153 insertions(+), 119 deletions(-) delete mode 100644 host/include/uhd/property.hpp create mode 100644 host/include/uhd/property_tree.ipp (limited to 'host/include') diff --git a/host/include/uhd/CMakeLists.txt b/host/include/uhd/CMakeLists.txt index 1ee3e69df..49562a7a0 100644 --- a/host/include/uhd/CMakeLists.txt +++ b/host/include/uhd/CMakeLists.txt @@ -26,7 +26,7 @@ INSTALL(FILES convert.hpp device.hpp exception.hpp - property.hpp + property_tree.ipp property_tree.hpp version.hpp wax.hpp diff --git a/host/include/uhd/property.hpp b/host/include/uhd/property.hpp deleted file mode 100644 index 10ce463e0..000000000 --- a/host/include/uhd/property.hpp +++ /dev/null @@ -1,108 +0,0 @@ -// -// Copyright 2011 Ettus Research LLC -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . -// - -#ifndef INCLUDED_UHD_PROPERTY_HPP -#define INCLUDED_UHD_PROPERTY_HPP - -#include -#include -#include -#include - -namespace uhd{ - -/*! - * A templated property interface for holding a value - * and registering callbacks when that value changes. - */ -template class UHD_API property{ -public: - typedef boost::function subscriber_type; - typedef boost::function publisher_type; - typedef boost::function master_type; - - //! Default constructor - property(void){ - /* NOP */ - } - - //! Value initializer constructor - property(const T &value){ - _value = value; - } - - /*! - * Register a master subscriber into the property. - * A master is a special subscriber that coerces the value. - * Only one master may be registered per property. - * Registering a master replaces the previous master. - */ - void subscribe_master(const master_type &master){ - _master = master; - } - - /*! - * Register a publisher into the property. - * A publisher is a special callback the provides the value. - * Publishers are useful for creating read-only properties. - * Only one publisher may be registered per property. - * Registering a publisher replaces the previous publisher. - */ - void publish(const publisher_type &publisher){ - _publisher = publisher; - } - - /*! - * Register a subscriber into the property. - * All subscribers are called when the value changes. - * Once a subscriber is registered, it cannot be unregistered. - */ - void subscribe(const subscriber_type &subscriber){ - _subscribers.push_back(subscriber); - } - - //! Update calls all subscribers w/ the current value - void update(void){ - this->set(this->get()); - } - - /*! - * Set the new value and call all subscribers. - * The master is called first to coerce the value. - */ - void set(const T &value){ - _value = _master.empty()? value : _master(value); - BOOST_FOREACH(subscriber_type &subscriber, _subscribers){ - subscriber(_value); //let errors propagate - } - } - - //! Get the current value of this property - T get(void) const{ - return _publisher.empty()? _value : _publisher(); - } - -private: - std::list _subscribers; - publisher_type _publisher; - master_type _master; - T _value; -}; - -} //namespace uhd - -#endif /* INCLUDED_UHD_PROPERTY_HPP */ diff --git a/host/include/uhd/property_tree.hpp b/host/include/uhd/property_tree.hpp index b23a6cc37..630e86bba 100644 --- a/host/include/uhd/property_tree.hpp +++ b/host/include/uhd/property_tree.hpp @@ -19,15 +19,66 @@ #define INCLUDED_UHD_PROPERTY_TREE_HPP #include -#include #include #include #include +#include #include #include namespace uhd{ +/*! + * A templated property interface for holding a value + * and registering callbacks when that value changes. + */ +template class UHD_API property : boost::noncopyable{ +public: + typedef boost::shared_ptr sptr; + typedef boost::function subscriber_type; + typedef boost::function publisher_type; + typedef boost::function master_type; + + //! Make a new property object + static sptr make(void); + + /*! + * Register a master subscriber into the property. + * A master is a special subscriber that coerces the value. + * Only one master may be registered per property. + * Registering a master replaces the previous master. + */ + virtual property &subscribe_master(const master_type &master) = 0; + + /*! + * Register a publisher into the property. + * A publisher is a special callback the provides the value. + * Publishers are useful for creating read-only properties. + * Only one publisher may be registered per property. + * Registering a publisher replaces the previous publisher. + */ + virtual property &publish(const publisher_type &publisher) = 0; + + /*! + * Register a subscriber into the property. + * All subscribers are called when the value changes. + * Once a subscriber is registered, it cannot be unregistered. + */ + virtual property &subscribe(const subscriber_type &subscriber) = 0; + + //! Update calls all subscribers w/ the current value + virtual property &update(void) = 0; + + /*! + * Set the new value and call all subscribers. + * The master is called first to coerce the value. + */ + virtual property &set(const T &value) = 0; + + //! Get the current value of this property + virtual T get(void) const = 0; +}; + /*! * The property tree provides a file system structure for accessing properties. */ @@ -49,14 +100,10 @@ public: virtual std::vector list(const path_type &path) = 0; //! Create a new property entry in the tree - template void create(const path_type &path, const property &prop = property()){ - return this->_create(path, prop); - } + template property &create(const path_type &path); //! Get access to a property in the tree - template property access(const path_type &path){ - return boost::any_cast >(this->_access(path)); - } + template property &access(const path_type &path); protected: //! Internal create property with wild-card type @@ -69,4 +116,6 @@ protected: } //namespace uhd +#include + #endif /* INCLUDED_UHD_PROPERTY_TREE_HPP */ diff --git a/host/include/uhd/property_tree.ipp b/host/include/uhd/property_tree.ipp new file mode 100644 index 000000000..6bd88929d --- /dev/null +++ b/host/include/uhd/property_tree.ipp @@ -0,0 +1,96 @@ +// +// Copyright 2011 Ettus Research LLC +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// + +#ifndef INCLUDED_UHD_PROPERTY_TREE_IPP +#define INCLUDED_UHD_PROPERTY_TREE_IPP + +#include +#include + +/*********************************************************************** + * Implement templated property impl + **********************************************************************/ +namespace uhd{ +namespace /*anon*/{ + +template class UHD_API property_impl : public property{ +public: + + property &subscribe_master(const typename property::master_type &master){ + _master = master; + return *this; + } + + property &publish(const typename property::publisher_type &publisher){ + _publisher = publisher; + return *this; + } + + property &subscribe(const typename property::subscriber_type &subscriber){ + _subscribers.push_back(subscriber); + return *this; + } + + property &update(void){ + this->set(this->get()); + return *this; + } + + property &set(const T &value){ + _value = _master.empty()? value : _master(value); + BOOST_FOREACH(typename property::subscriber_type &subscriber, _subscribers){ + subscriber(this->get()); //let errors propagate + } + return *this; + } + + T get(void) const{ + return _publisher.empty()? boost::any_cast(_value) : _publisher(); + } + +private: + std::vector::subscriber_type> _subscribers; + typename property::publisher_type _publisher; + typename property::master_type _master; + boost::any _value; //any type so we can assign structs w/ const members +}; + +} //namespace /*anon*/ + +template typename property::sptr property::make(void){ + return sptr(new property_impl()); +} + +} //namespace uhd + +/*********************************************************************** + * Implement templated methods for the property tree + **********************************************************************/ +namespace uhd{ + + template property &property_tree::create(const path_type &path){ + this->_create(path, property::make()); + return this->access(path); + } + + template property &property_tree::access(const path_type &path){ + return *boost::any_cast::sptr>(this->_access(path)); + } + +} //namespace uhd + +#endif /* INCLUDED_UHD_PROPERTY_TREE_IPP */ diff --git a/host/include/uhd/types/sensors.hpp b/host/include/uhd/types/sensors.hpp index a43546262..529e1e3e3 100644 --- a/host/include/uhd/types/sensors.hpp +++ b/host/include/uhd/types/sensors.hpp @@ -37,9 +37,6 @@ namespace uhd{ */ struct UHD_API sensor_value_t{ - //! Default constructor - sensor_value_t(void); - /*! * Create a sensor value from a boolean. * \param name the name of the sensor -- cgit v1.2.3