diff options
author | Josh Blum <josh@joshknows.com> | 2011-06-28 11:11:04 -0700 |
---|---|---|
committer | Josh Blum <josh@joshknows.com> | 2011-06-28 11:11:04 -0700 |
commit | 4888233252d58f0df3c8773ec3268a0ed7bea1d2 (patch) | |
tree | 33daab577ff64117e647bb5d6e97397c89ff6b11 /host/include | |
parent | 3a6187515677b277489df4ec59f9cf21f1ed62bc (diff) | |
download | uhd-4888233252d58f0df3c8773ec3268a0ed7bea1d2.tar.gz uhd-4888233252d58f0df3c8773ec3268a0ed7bea1d2.tar.bz2 uhd-4888233252d58f0df3c8773ec3268a0ed7bea1d2.zip |
uhd: re-work to make the properties easier to use
Diffstat (limited to 'host/include')
-rw-r--r-- | host/include/uhd/CMakeLists.txt | 2 | ||||
-rw-r--r-- | host/include/uhd/property.hpp | 108 | ||||
-rw-r--r-- | host/include/uhd/property_tree.hpp | 63 | ||||
-rw-r--r-- | host/include/uhd/property_tree.ipp | 96 | ||||
-rw-r--r-- | host/include/uhd/types/sensors.hpp | 3 |
5 files changed, 153 insertions, 119 deletions
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 <http://www.gnu.org/licenses/>. -// - -#ifndef INCLUDED_UHD_PROPERTY_HPP -#define INCLUDED_UHD_PROPERTY_HPP - -#include <uhd/config.hpp> -#include <boost/foreach.hpp> -#include <boost/function.hpp> -#include <list> - -namespace uhd{ - -/*! - * A templated property interface for holding a value - * and registering callbacks when that value changes. - */ -template <typename T> class UHD_API property{ -public: - typedef boost::function<void(const T &)> subscriber_type; - typedef boost::function<T(void)> publisher_type; - typedef boost::function<T(const T &)> 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<subscriber_type> _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,16 +19,67 @@ #define INCLUDED_UHD_PROPERTY_TREE_HPP #include <uhd/config.hpp> -#include <uhd/property.hpp> #include <boost/any.hpp> #include <boost/utility.hpp> #include <boost/shared_ptr.hpp> +#include <boost/function.hpp> #include <boost/filesystem/path.hpp> #include <vector> namespace uhd{ /*! + * A templated property interface for holding a value + * and registering callbacks when that value changes. + */ +template <typename T> class UHD_API property : boost::noncopyable{ +public: + typedef boost::shared_ptr<property> sptr; + typedef boost::function<void(const T &)> subscriber_type; + typedef boost::function<T(void)> publisher_type; + typedef boost::function<T(const T &)> 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<T> &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<T> &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<T> &subscribe(const subscriber_type &subscriber) = 0; + + //! Update calls all subscribers w/ the current value + virtual property<T> &update(void) = 0; + + /*! + * Set the new value and call all subscribers. + * The master is called first to coerce the value. + */ + virtual property<T> &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. */ class UHD_API property_tree : boost::noncopyable{ @@ -49,14 +100,10 @@ public: virtual std::vector<std::string> list(const path_type &path) = 0; //! Create a new property entry in the tree - template <typename T> void create(const path_type &path, const property<T> &prop = property<T>()){ - return this->_create(path, prop); - } + template <typename T> property<T> &create(const path_type &path); //! Get access to a property in the tree - template <typename T> property<T> access(const path_type &path){ - return boost::any_cast<property<T> >(this->_access(path)); - } + template <typename T> property<T> &access(const path_type &path); protected: //! Internal create property with wild-card type @@ -69,4 +116,6 @@ protected: } //namespace uhd +#include <uhd/property_tree.ipp> + #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 <http://www.gnu.org/licenses/>. +// + +#ifndef INCLUDED_UHD_PROPERTY_TREE_IPP +#define INCLUDED_UHD_PROPERTY_TREE_IPP + +#include <boost/foreach.hpp> +#include <vector> + +/*********************************************************************** + * Implement templated property impl + **********************************************************************/ +namespace uhd{ +namespace /*anon*/{ + +template <typename T> class UHD_API property_impl : public property<T>{ +public: + + property<T> &subscribe_master(const typename property<T>::master_type &master){ + _master = master; + return *this; + } + + property<T> &publish(const typename property<T>::publisher_type &publisher){ + _publisher = publisher; + return *this; + } + + property<T> &subscribe(const typename property<T>::subscriber_type &subscriber){ + _subscribers.push_back(subscriber); + return *this; + } + + property<T> &update(void){ + this->set(this->get()); + return *this; + } + + property<T> &set(const T &value){ + _value = _master.empty()? value : _master(value); + BOOST_FOREACH(typename property<T>::subscriber_type &subscriber, _subscribers){ + subscriber(this->get()); //let errors propagate + } + return *this; + } + + T get(void) const{ + return _publisher.empty()? boost::any_cast<T>(_value) : _publisher(); + } + +private: + std::vector<typename property<T>::subscriber_type> _subscribers; + typename property<T>::publisher_type _publisher; + typename property<T>::master_type _master; + boost::any _value; //any type so we can assign structs w/ const members +}; + +} //namespace /*anon*/ + +template <typename T> typename property<T>::sptr property<T>::make(void){ + return sptr(new property_impl<T>()); +} + +} //namespace uhd + +/*********************************************************************** + * Implement templated methods for the property tree + **********************************************************************/ +namespace uhd{ + + template <typename T> property<T> &property_tree::create(const path_type &path){ + this->_create(path, property<T>::make()); + return this->access<T>(path); + } + + template <typename T> property<T> &property_tree::access(const path_type &path){ + return *boost::any_cast<typename property<T>::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 |