diff options
author | Josh Blum <josh@joshknows.com> | 2010-02-01 12:35:34 -0800 |
---|---|---|
committer | Josh Blum <josh@joshknows.com> | 2010-02-01 12:35:34 -0800 |
commit | cc8caeb1230fbaed4a6bc64848a584d51b69362a (patch) | |
tree | 086264364fe1d9d1f790a077cd7d9f3219cc369a | |
parent | 5e455ca92280e3c22f5484cb81a2aef0cdfb5de4 (diff) | |
download | uhd-cc8caeb1230fbaed4a6bc64848a584d51b69362a.tar.gz uhd-cc8caeb1230fbaed4a6bc64848a584d51b69362a.tar.bz2 uhd-cc8caeb1230fbaed4a6bc64848a584d51b69362a.zip |
Work on the properties framwork with wax::obj.
Now the obj handles all 3 things in 1, properties, polymorphic container, proxy.
-rw-r--r-- | include/usrp_uhd/device.hpp | 2 | ||||
-rw-r--r-- | include/usrp_uhd/gain_handler.hpp | 20 | ||||
-rw-r--r-- | include/usrp_uhd/props.hpp | 4 | ||||
-rw-r--r-- | include/usrp_uhd/usrp/dboard/base.hpp | 16 | ||||
-rw-r--r-- | include/usrp_uhd/usrp/mboard/base.hpp | 4 | ||||
-rw-r--r-- | include/usrp_uhd/usrp/mboard/test.hpp | 4 | ||||
-rw-r--r-- | include/usrp_uhd/usrp/mboard/usrp2.hpp | 4 | ||||
-rw-r--r-- | include/usrp_uhd/usrp/usrp.hpp | 4 | ||||
-rw-r--r-- | include/usrp_uhd/utils.hpp | 4 | ||||
-rw-r--r-- | include/usrp_uhd/wax.hpp | 170 | ||||
-rw-r--r-- | lib/gain_handler.cpp | 20 | ||||
-rw-r--r-- | lib/usrp/dboard/base.cpp | 9 | ||||
-rw-r--r-- | lib/usrp/dboard/basic.cpp | 8 | ||||
-rw-r--r-- | lib/usrp/dboard/dboards.hpp | 8 | ||||
-rw-r--r-- | lib/usrp/dboard/manager.cpp | 4 | ||||
-rw-r--r-- | lib/usrp/mboard/test.cpp | 16 | ||||
-rw-r--r-- | lib/usrp/usrp.cpp | 10 | ||||
-rw-r--r-- | lib/wax.cpp | 97 | ||||
-rw-r--r-- | test/device_test.cpp | 2 | ||||
-rw-r--r-- | test/gain_handler_test.cpp | 12 | ||||
-rw-r--r-- | test/wax_test.cpp | 16 |
21 files changed, 245 insertions, 189 deletions
diff --git a/include/usrp_uhd/device.hpp b/include/usrp_uhd/device.hpp index b2627f887..e0356feb0 100644 --- a/include/usrp_uhd/device.hpp +++ b/include/usrp_uhd/device.hpp @@ -82,7 +82,7 @@ public: virtual void recv_raw(const recv_args_t &) = 0; //connect dsps and subdevs - void connect(const wax::type &src, const wax::type &sink); + void connect(const wax::obj &src, const wax::obj &sink); }; } //namespace usrp_uhd diff --git a/include/usrp_uhd/gain_handler.hpp b/include/usrp_uhd/gain_handler.hpp index e036ab701..22e987803 100644 --- a/include/usrp_uhd/gain_handler.hpp +++ b/include/usrp_uhd/gain_handler.hpp @@ -51,7 +51,7 @@ public: * Ensures that the gain name is valid. * \return true for handled, false to pass on */ - bool intercept_get(const wax::type &key, wax::type &val); + bool intercept_get(const wax::obj &key, wax::obj &val); /*! * Intercept sets for overall gain. @@ -59,16 +59,16 @@ public: * Ensures that the new gain is within range. * \return true for handled, false to pass on */ - bool intercept_set(const wax::type &key, const wax::type &val); + bool intercept_set(const wax::obj &key, const wax::obj &val); private: wax::obj::ptr _wax_obj_ptr; - wax::type _gain_prop; - wax::type _gain_min_prop; - wax::type _gain_max_prop; - wax::type _gain_step_prop; - wax::type _gain_names_prop; + wax::obj _gain_prop; + wax::obj _gain_min_prop; + wax::obj _gain_max_prop; + wax::obj _gain_step_prop; + wax::obj _gain_names_prop; /*! * Verify that the key is valid: @@ -76,14 +76,14 @@ private: * If the name if not valid, throw a std::invalid_argument. * The name can only be valid if its in the list of gain names. */ - void _check_key(const wax::type &key); + void _check_key(const wax::obj &key); /* * Private interface to test if two wax types are equal: * The constructor will bind an instance of this for a specific type. * This bound equals functions allows the intercept methods to be non-templated. */ - template <class T> static bool is_equal(const wax::type &a, const wax::type &b){ + template <class T> static bool is_equal(const wax::obj &a, const wax::obj &b){ try{ return wax::cast<T>(a) == wax::cast<T>(b); } @@ -91,7 +91,7 @@ private: return false; } } - boost::function<bool(const wax::type &, const wax::type &)> _is_equal; + boost::function<bool(const wax::obj &, const wax::obj &)> _is_equal; }; diff --git a/include/usrp_uhd/props.hpp b/include/usrp_uhd/props.hpp index 6370bdd28..de3280969 100644 --- a/include/usrp_uhd/props.hpp +++ b/include/usrp_uhd/props.hpp @@ -42,14 +42,14 @@ namespace usrp_uhd{ //typedef for handling named properties typedef std::vector<std::string> prop_names_t; - typedef boost::tuple<wax::type, std::string> named_prop_t; + typedef boost::tuple<wax::obj, std::string> named_prop_t; /*! * Utility function to separate a named property into its components. * \param key a reference to the prop object * \param name a reference to the name object */ - inline named_prop_t extract_named_prop(const wax::type &key, const std::string &name = ""){ + inline named_prop_t extract_named_prop(const wax::obj &key, const std::string &name = ""){ if (key.type() == typeid(named_prop_t)){ return wax::cast<named_prop_t>(key); } diff --git a/include/usrp_uhd/usrp/dboard/base.hpp b/include/usrp_uhd/usrp/dboard/base.hpp index 161d47a9e..50896b9a8 100644 --- a/include/usrp_uhd/usrp/dboard/base.hpp +++ b/include/usrp_uhd/usrp/dboard/base.hpp @@ -43,10 +43,10 @@ public: virtual ~base(void); //interface - virtual void rx_get(const wax::type &key, wax::type &val) = 0; - virtual void rx_set(const wax::type &key, const wax::type &val) = 0; - virtual void tx_get(const wax::type &key, wax::type &val) = 0; - virtual void tx_set(const wax::type &key, const wax::type &val) = 0; + virtual void rx_get(const wax::obj &key, wax::obj &val) = 0; + virtual void rx_set(const wax::obj &key, const wax::obj &val) = 0; + virtual void tx_get(const wax::obj &key, wax::obj &val) = 0; + virtual void tx_set(const wax::obj &key, const wax::obj &val) = 0; protected: std::string get_subdev_name(void); @@ -84,8 +84,8 @@ public: virtual ~rx_base(void); //override here so the derived classes cannot - void tx_get(const wax::type &key, wax::type &val); - void tx_set(const wax::type &key, const wax::type &val); + void tx_get(const wax::obj &key, wax::obj &val); + void tx_set(const wax::obj &key, const wax::obj &val); }; /*! @@ -102,8 +102,8 @@ public: virtual ~tx_base(void); //override here so the derived classes cannot - void rx_get(const wax::type &key, wax::type &val); - void rx_set(const wax::type &key, const wax::type &val); + void rx_get(const wax::obj &key, wax::obj &val); + void rx_set(const wax::obj &key, const wax::obj &val); }; }}} //namespace diff --git a/include/usrp_uhd/usrp/mboard/base.hpp b/include/usrp_uhd/usrp/mboard/base.hpp index 59236dce9..65810f329 100644 --- a/include/usrp_uhd/usrp/mboard/base.hpp +++ b/include/usrp_uhd/usrp/mboard/base.hpp @@ -36,8 +36,8 @@ public: //TODO other api calls private: - virtual void get(const wax::type &, wax::type &) = 0; - virtual void set(const wax::type &, const wax::type &) = 0; + virtual void get(const wax::obj &, wax::obj &) = 0; + virtual void set(const wax::obj &, const wax::obj &) = 0; }; }}} //namespace diff --git a/include/usrp_uhd/usrp/mboard/test.hpp b/include/usrp_uhd/usrp/mboard/test.hpp index 75e1f838f..fc1ea6e70 100644 --- a/include/usrp_uhd/usrp/mboard/test.hpp +++ b/include/usrp_uhd/usrp/mboard/test.hpp @@ -35,8 +35,8 @@ public: ~test(void); private: - void get(const wax::type &, wax::type &); - void set(const wax::type &, const wax::type &); + void get(const wax::obj &, wax::obj &); + void set(const wax::obj &, const wax::obj &); std::map<std::string, dboard::manager::sptr> _dboard_managers; }; diff --git a/include/usrp_uhd/usrp/mboard/usrp2.hpp b/include/usrp_uhd/usrp/mboard/usrp2.hpp index ea0083bc4..a8e950d93 100644 --- a/include/usrp_uhd/usrp/mboard/usrp2.hpp +++ b/include/usrp_uhd/usrp/mboard/usrp2.hpp @@ -42,8 +42,8 @@ public: ~usrp2(void); private: - void get(const wax::type &, wax::type &); - void set(const wax::type &, const wax::type &); + void get(const wax::obj &, wax::obj &); + void set(const wax::obj &, const wax::obj &); std::map<std::string, dboard::manager::sptr> _dboard_managers; }; diff --git a/include/usrp_uhd/usrp/usrp.hpp b/include/usrp_uhd/usrp/usrp.hpp index a8cd1dc7c..a8a052b52 100644 --- a/include/usrp_uhd/usrp/usrp.hpp +++ b/include/usrp_uhd/usrp/usrp.hpp @@ -39,8 +39,8 @@ public: void recv_raw(const recv_args_t &); private: - void get(const wax::type &, wax::type &); - void set(const wax::type &, const wax::type &); + void get(const wax::obj &, wax::obj &); + void set(const wax::obj &, const wax::obj &); std::map<std::string, mboard::base::sptr> _mboards; boost::function<void(const device::send_args_t &)> _send_raw_cb; diff --git a/include/usrp_uhd/utils.hpp b/include/usrp_uhd/utils.hpp index 6a2848b96..a8f1c132d 100644 --- a/include/usrp_uhd/utils.hpp +++ b/include/usrp_uhd/utils.hpp @@ -48,11 +48,11 @@ template<typename T> T signum(T n){ inline void tune( freq_t target_freq, freq_t lo_offset, - wax::proxy subdev_freq_proxy, + wax::obj subdev_freq_proxy, bool subdev_quadrature, bool subdev_spectrum_inverted, bool subdev_is_tx, - wax::proxy dsp_freq_proxy, + wax::obj dsp_freq_proxy, freq_t dsp_sample_rate ){ // Ask the d'board to tune as closely as it can to target_freq+lo_offset diff --git a/include/usrp_uhd/wax.hpp b/include/usrp_uhd/wax.hpp index c8a671a61..f0e7a3acc 100644 --- a/include/usrp_uhd/wax.hpp +++ b/include/usrp_uhd/wax.hpp @@ -19,21 +19,18 @@ #define INCLUDED_WAX_HPP #include <boost/any.hpp> -#include <boost/function.hpp> #include <boost/shared_ptr.hpp> -#include <boost/weak_ptr.hpp> -#include <boost/pointer_cast.hpp> #include <iostream> /*! * WAX - it's a metaphor! * - * The WAX framework allows object to have generic/anytype properties. - * These properties can be addressed through generic/anytype identifiers. + * The WAX framework allows object to have generic/anyobj properties. + * These properties can be addressed through generic/anyobj identifiers. * A property of a WAX object may even be another WAX object. * * When a property is a WAX object, the returned value must be an obj pointer. - * A WAX object provides two types of pointers: obj::ptr and obj::sptr. + * A WAX object provides two objs of pointers: obj::ptr and obj::sptr. * The choice of pointer vs smart pointer depends on the owner of the memory. * * Proprties may be referenced though the [] overloaded operator. @@ -42,101 +39,132 @@ * my_obj[prop1][prop2][prop3] = value * value = my_obj[prop1][prop2][prop3] * - * Any value returned from an access operation is of wax::type. - * To use this value, it must be cast with wax::cast<new_type>(value). + * Any value returned from an access operation is of wax::obj. + * To use this value, it must be cast with wax::cast<new_obj>(value). */ namespace wax{ - //general typedefs - typedef boost::any type; typedef boost::bad_any_cast bad_cast; - //dummy class declarations - class obj; class proxy; - /*! * WAX object base class: - * A wax object subclass should override the set and get methods. - * The magic of operator chaining is handled by the [] operator. + * + * A wax obj has two major purposes: + * 1) to act as a polymorphic container, just like boost any + * 2) to provide a nested set/get properties interface + * + * Internally, the polymorphic container is handled by a boost any. + * For properties, a subclass should override the set and get methods. + * For property nesting, wax obj subclasses return special links + * to other wax obj subclasses, and the api handles the magic. */ class obj{ public: //obj pointer typedefs typedef boost::shared_ptr<obj> sptr; - typedef obj* ptr; + typedef obj* ptr; - //cast derived pointer to obj base class pointer - template <class T> static sptr cast(boost::shared_ptr<T> r){ - return boost::static_pointer_cast<obj>(r); - } - template <class T> static ptr cast(T *r){ - return dynamic_cast<ptr>(r); - } - - //structors + /*! + * Default constructor: + * The contents will be empty. + */ obj(void); - virtual ~obj(void); - //public interface - proxy operator[](const type &key); - - private: - //private interface - virtual void get(const type &key, type &val) = 0; - virtual void set(const type &key, const type &val) = 0; - }; + /*! + * Copy constructor: + * The contents will be cloned. + * \param o another wax::obj + */ + obj(const obj &o); + + /*! + * Templated any type constructor: + * The contents can be anything. + * Uses the boost::any to handle the magic. + * \param o an object of any type + */ + template<class T> obj(const T &o){ + _contents = o; + } - /*! - * WAX proxy class: - * Allows the obj [] operator to return a proxy result. - * This result can be assigned to via the = operator. - * Or this result can be called again with the [] operator. - */ - class proxy{ - public: - //destructors - ~proxy(void); + /*! + * Destructor. + */ + virtual ~obj(void); - //overloaded - type operator()(void); - proxy operator[](const type &key); - proxy operator=(const type &key); + /*! + * The chaining operator: + * This operator allows access objs with properties. + * A call to the [] operator will return a new proxy obj. + * The proxy object is an obj with special proxy contents. + * Assignment and casting can be used on this special object + * to access the property referenced by the obj key. + * \param key a key to identify a property within this obj + * \return a special wax obj that proxies the obj and key + */ + obj operator[](const obj &key); + + /*! + * The assignment operator: + * This operator allows for assignment of new contents. + * In the special case where this obj contains a proxy, + * the value will be set to the proxy's property reference. + * \param val the new value to assign to the wax obj + * \return a reference to this obj (*this) + */ + obj & operator=(const obj &val); + + /*! + * Get a link in the chain: + * When a wax obj returns another wax obj as part of a get call, + * the return value should be set to the result of this method. + * Doing so will ensure chain-ability of the returned object. + * \return an obj containing a valid link to a wax obj + */ + obj get_link(void) const; + + /*! + * Get the type of the contents of this obj. + * \return a reference to the type_info + */ + const std::type_info & type(void) const; private: - //typedefs for callables from the object that built this proxy - typedef boost::function<void(const type &)> setter_t; - typedef boost::function<void(type &)> getter_t; + //private interface (override in subclasses) + virtual void get(const obj &, obj &); + virtual void set(const obj &, const obj &); + + /*! + * Resolve the contents of this obj. + * In the case where this obj is a proxy, + * the referenced property will be resolved. + * Otherwise, just get the private contents. + * \return a boost any type with contents + */ + boost::any resolve(void) const; + template<class T> friend T cast(const obj &); + + //private contents of this obj + boost::any _contents; - //private contructor - proxy(getter_t, setter_t); - //access to private contructor - friend proxy obj::operator[](const type &key); - - getter_t d_getter; - setter_t d_setter; }; /*! - * Cast a wax::type into the desired type - * Usage wax::cast<new_type>(my_value). + * Cast a wax::obj into the desired obj. + * Usage wax::cast<new_obj>(my_value). * - * \param val the any type to cast - * \return data of the desired type + * \param val the obj to cast + * \return an object of the desired type * \throw wax::bad_cast when the cast fails */ - template<typename T> T cast(const type & val){ - //special case to handle the proxy - if (val.type() == typeid(proxy)){ - return cast<T>(boost::any_cast<proxy>(val)()); - } - //do the type cast - return boost::any_cast<T>(val); + template<class T> T cast(const obj &val){ + return boost::any_cast<T>(val.resolve()); } } //namespace wax -//ability to use types with stream operators -std::ostream& operator<<(std::ostream &os, const wax::type &x); +//ability to use wax::obj with stream operators +std::ostream& operator<<(std::ostream &, const wax::obj &); #endif /* INCLUDED_WAX_HPP */ diff --git a/lib/gain_handler.cpp b/lib/gain_handler.cpp index 0c54a0b8b..3d0c38aa1 100644 --- a/lib/gain_handler.cpp +++ b/lib/gain_handler.cpp @@ -33,7 +33,7 @@ using namespace usrp_uhd; /*! * Helper function to simplify getting a named gain (also min, max, step). */ -static gain_t get_named_gain(wax::obj::ptr wax_obj_ptr, wax::type prop, std::string name){ +static gain_t get_named_gain(wax::obj::ptr wax_obj_ptr, wax::obj prop, std::string name){ return wax::cast<gain_t>((*wax_obj_ptr)[named_prop_t(prop, name)]); } @@ -44,9 +44,9 @@ gain_handler::~gain_handler(void){ /* NOP */ } -void gain_handler::_check_key(const wax::type &key_){ - wax::type key; std::string name; - tie(key, name) = extract_named_prop(key_); +void gain_handler::_check_key(const wax::obj &key_){ + wax::obj key; std::string name; + boost::tie(key, name) = extract_named_prop(key_); try{ //only handle non wildcard names @@ -72,13 +72,13 @@ void gain_handler::_check_key(const wax::type &key_){ catch(const std::assert_error &){} } -bool gain_handler::intercept_get(const wax::type &key, wax::type &val){ +bool gain_handler::intercept_get(const wax::obj &key, wax::obj &val){ _check_key(key); //verify the key // use a vector of tuples to map properties to a reducer function - // we cant use a map because the wax::type cant be sorted + // we cant use a map because the wax::obj cant be sorted typedef boost::function<gain_t(gain_t, gain_t)> reducer_t; - typedef boost::tuple<wax::type, reducer_t> tuple_t; + typedef boost::tuple<wax::obj, reducer_t> tuple_t; reducer_t reducer_sum = boost::bind(std::sum<gain_t>, _1, _2); reducer_t reducer_max = boost::bind(std::max<gain_t>, _1, _2); std::vector<tuple_t> prop_to_reducer = boost::assign::tuple_list_of @@ -108,11 +108,11 @@ bool gain_handler::intercept_get(const wax::type &key, wax::type &val){ return false; } -bool gain_handler::intercept_set(const wax::type &key_, const wax::type &val){ +bool gain_handler::intercept_set(const wax::obj &key_, const wax::obj &val){ _check_key(key_); //verify the key - wax::type key; std::string name; - tie(key, name) = extract_named_prop(key_); + wax::obj key; std::string name; + boost::tie(key, name) = extract_named_prop(key_); /*! * Verify that a named gain component is in range. diff --git a/lib/usrp/dboard/base.cpp b/lib/usrp/dboard/base.cpp index 1dd722ed1..18489f3b2 100644 --- a/lib/usrp/dboard/base.cpp +++ b/lib/usrp/dboard/base.cpp @@ -16,6 +16,7 @@ // #include <usrp_uhd/usrp/dboard/base.hpp> +#include <stdexcept> using namespace usrp_uhd::usrp::dboard; @@ -60,11 +61,11 @@ rx_base::~rx_base(void){ /* NOP */ } -void rx_base::tx_get(const wax::type &, wax::type &){ +void rx_base::tx_get(const wax::obj &, wax::obj &){ throw std::runtime_error("cannot call tx_get on a rx dboard"); } -void rx_base::tx_set(const wax::type &, const wax::type &){ +void rx_base::tx_set(const wax::obj &, const wax::obj &){ throw std::runtime_error("cannot call tx_set on a rx dboard"); } @@ -79,10 +80,10 @@ tx_base::~tx_base(void){ /* NOP */ } -void tx_base::rx_get(const wax::type &, wax::type &){ +void tx_base::rx_get(const wax::obj &, wax::obj &){ throw std::runtime_error("cannot call rx_get on a tx dboard"); } -void tx_base::rx_set(const wax::type &, const wax::type &){ +void tx_base::rx_set(const wax::obj &, const wax::obj &){ throw std::runtime_error("cannot call rx_set on a tx dboard"); } diff --git a/lib/usrp/dboard/basic.cpp b/lib/usrp/dboard/basic.cpp index d92d02eec..35512aa5f 100644 --- a/lib/usrp/dboard/basic.cpp +++ b/lib/usrp/dboard/basic.cpp @@ -28,11 +28,11 @@ basic_rx::~basic_rx(void){ /* NOP */ } -void basic_rx::rx_get(const wax::type &, wax::type &){ +void basic_rx::rx_get(const wax::obj &, wax::obj &){ /* TODO */ } -void basic_rx::rx_set(const wax::type &, const wax::type &){ +void basic_rx::rx_set(const wax::obj &, const wax::obj &){ /* TODO */ } @@ -47,10 +47,10 @@ basic_tx::~basic_tx(void){ /* NOP */ } -void basic_tx::tx_get(const wax::type &, wax::type &){ +void basic_tx::tx_get(const wax::obj &, wax::obj &){ /* TODO */ } -void basic_tx::tx_set(const wax::type &, const wax::type &){ +void basic_tx::tx_set(const wax::obj &, const wax::obj &){ /* TODO */ } diff --git a/lib/usrp/dboard/dboards.hpp b/lib/usrp/dboard/dboards.hpp index 35433bb3a..218849eb6 100644 --- a/lib/usrp/dboard/dboards.hpp +++ b/lib/usrp/dboard/dboards.hpp @@ -33,8 +33,8 @@ public: basic_rx(ctor_args_t const& args); ~basic_rx(void); - void rx_get(const wax::type &key, wax::type &val); - void rx_set(const wax::type &key, const wax::type &val); + void rx_get(const wax::obj &key, wax::obj &val); + void rx_set(const wax::obj &key, const wax::obj &val); }; class basic_tx : public tx_base{ @@ -45,8 +45,8 @@ public: basic_tx(ctor_args_t const& args); ~basic_tx(void); - void tx_get(const wax::type &key, wax::type &val); - void tx_set(const wax::type &key, const wax::type &val); + void tx_get(const wax::obj &key, wax::obj &val); + void tx_set(const wax::obj &key, const wax::obj &val); }; diff --git a/lib/usrp/dboard/manager.cpp b/lib/usrp/dboard/manager.cpp index fcdde84db..12cfdd156 100644 --- a/lib/usrp/dboard/manager.cpp +++ b/lib/usrp/dboard/manager.cpp @@ -93,7 +93,7 @@ private: type_t _type; //forward the get calls to the rx or tx - void get(const wax::type &key, wax::type &val){ + void get(const wax::obj &key, wax::obj &val){ switch(_type){ case RX_TYPE: return _subdev->rx_get(key, val); case TX_TYPE: return _subdev->tx_get(key, val); @@ -101,7 +101,7 @@ private: } //forward the set calls to the rx or tx - void set(const wax::type &key, const wax::type &val){ + void set(const wax::obj &key, const wax::obj &val){ switch(_type){ case RX_TYPE: return _subdev->rx_set(key, val); case TX_TYPE: return _subdev->tx_set(key, val); diff --git a/lib/usrp/mboard/test.cpp b/lib/usrp/mboard/test.cpp index 071a10c2b..57482448c 100644 --- a/lib/usrp/mboard/test.cpp +++ b/lib/usrp/mboard/test.cpp @@ -59,9 +59,9 @@ public: } ~shell_dboard(void){} private: - void get(const wax::type &key_, wax::type &val){ - wax::type key; std::string name; - tie(key, name) = extract_named_prop(key_); + void get(const wax::obj &key_, wax::obj &val){ + wax::obj key; std::string name; + boost::tie(key, name) = extract_named_prop(key_); //handle the get request conditioned on the key switch(wax::cast<dboard_prop_t>(key)){ @@ -97,7 +97,7 @@ private: } } - void set(const wax::type &, const wax::type &){ + void set(const wax::obj &, const wax::obj &){ throw std::runtime_error("Cannot set on usrp test dboard"); } @@ -122,9 +122,9 @@ test::~test(void){ /* NOP */ } -void test::get(const wax::type &key_, wax::type &val){ - wax::type key; std::string name; - tie(key, name) = extract_named_prop(key_); +void test::get(const wax::obj &key_, wax::obj &val){ + wax::obj key; std::string name; + boost::tie(key, name) = extract_named_prop(key_); //handle the get request conditioned on the key switch(wax::cast<mboard_prop_t>(key)){ @@ -179,6 +179,6 @@ void test::get(const wax::type &key_, wax::type &val){ } } -void test::set(const wax::type &, const wax::type &){ +void test::set(const wax::obj &, const wax::obj &){ throw std::runtime_error("Cannot set on usrp test mboard"); } diff --git a/lib/usrp/usrp.cpp b/lib/usrp/usrp.cpp index 56bfd37fc..e3016be5f 100644 --- a/lib/usrp/usrp.cpp +++ b/lib/usrp/usrp.cpp @@ -54,9 +54,9 @@ usrp::~usrp(void){ /* NOP */ } -void usrp::get(const wax::type &key_, wax::type &val){ - wax::type key; std::string name; - tie(key, name) = extract_named_prop(key_); +void usrp::get(const wax::obj &key_, wax::obj &val){ + wax::obj key; std::string name; + boost::tie(key, name) = extract_named_prop(key_); //handle the get request conditioned on the key switch(wax::cast<device_prop_t>(key)){ @@ -70,7 +70,7 @@ void usrp::get(const wax::type &key_, wax::type &val){ ); //turn the mboard sptr object into a wax::obj::sptr //this allows the properties access through the wax::proxy - val = wax::obj::cast(_mboards[name]); + val = _mboards[name]->get_link(); return; case DEVICE_PROP_MBOARD_NAMES: @@ -79,7 +79,7 @@ void usrp::get(const wax::type &key_, wax::type &val){ } } -void usrp::set(const wax::type &, const wax::type &){ +void usrp::set(const wax::obj &, const wax::obj &){ throw std::runtime_error("Cannot set in usrp device"); } diff --git a/lib/wax.cpp b/lib/wax.cpp index 025aa1766..2a6fdc3bb 100644 --- a/lib/wax.cpp +++ b/lib/wax.cpp @@ -19,6 +19,15 @@ #include <stdexcept> #include <boost/bind.hpp> #include <boost/format.hpp> +#include <boost/function.hpp> + +/*********************************************************************** + * Proxy Contents + **********************************************************************/ +struct proxy_args_t{ + boost::function<void(wax::obj &)> get; + boost::function<void(const wax::obj &)> set; +}; /*********************************************************************** * WAX Object @@ -27,58 +36,74 @@ wax::obj::obj(void){ /* NOP */ } +wax::obj::obj(const obj &o){ + _contents = o._contents; +} + wax::obj::~obj(void){ /* NOP */ } -wax::proxy wax::obj::operator[](const type &key){ - return proxy( - boost::bind(&obj::get, this, key, _1), - boost::bind(&obj::set, this, key, _1) - ); +wax::obj wax::obj::operator[](const obj &key){ + if (_contents.type() == typeid(proxy_args_t)){ + obj val = resolve(); + //check if its a regular pointer and call + if (val.type() == typeid(obj::ptr)){ + return (*cast<obj::ptr>(val))[key]; + } + //check if its a smart pointer and call + if (val.type() == typeid(obj::sptr)){ + return (*cast<obj::sptr>(val))[key]; + } + //unknown obj + throw std::runtime_error("cannot use [] on non wax::obj pointer"); + } + else{ + proxy_args_t proxy_args; + proxy_args.get = boost::bind(&obj::get, this, key, _1); + proxy_args.set = boost::bind(&obj::set, this, key, _1); + return wax::obj(proxy_args); + } } -/*********************************************************************** - * WAX Proxy - **********************************************************************/ -wax::proxy::proxy(wax::proxy::getter_t getter, wax::proxy::setter_t setter) -: d_getter(getter), d_setter(setter){ - /* NOP */ +wax::obj & wax::obj::operator=(const obj &val){ + if (_contents.type() == typeid(proxy_args_t)){ + boost::any_cast<proxy_args_t>(_contents).set(val); + } + else{ + _contents = val._contents; + } + return *this; } -wax::proxy::~proxy(void){ - /* NOP */ +wax::obj wax::obj::get_link(void) const{ + return ptr(this); +} + +const std::type_info & wax::obj::type(void) const{ + return _contents.type(); } -wax::proxy wax::proxy::operator[](const type &key){ - type val((*this)()); - //check if its a regular pointer and call - if (val.type() == typeid(obj::ptr)){ - return (*cast<obj::ptr>(val))[key]; +boost::any wax::obj::resolve(void) const{ + if (_contents.type() == typeid(proxy_args_t)){ + obj val; + boost::any_cast<proxy_args_t>(_contents).get(val); + return val.resolve(); } - //check if its a smart pointer and call - if (val.type() == typeid(obj::sptr)){ - return (*cast<obj::sptr>(val))[key]; + else{ + return _contents; } - //unknown type - throw std::runtime_error("cannot use [] on non wax::obj pointer"); } -wax::proxy wax::proxy::operator=(const type &val){ - d_setter(val); - return *this; +std::ostream& operator<<(std::ostream &os, const wax::obj &x){ + os << boost::format("WAX obj (%s)") % x.type().name(); + return os; } -wax::type wax::proxy::operator()(void){ - type val; - d_getter(val); - return val; +void wax::obj::get(const obj &, obj &){ + throw std::runtime_error("Cannot call get on wax obj base class"); } -/*********************************************************************** - * WAX Type - **********************************************************************/ -std::ostream& operator<<(std::ostream &os, const wax::type &x){ - os << boost::format("WAX type (%s)") % x.type().name(); - return os; +void wax::obj::set(const obj &, const obj &){ + throw std::runtime_error("Cannot call set on wax obj base class"); } diff --git a/test/device_test.cpp b/test/device_test.cpp index 863be4351..328ea9c0c 100644 --- a/test/device_test.cpp +++ b/test/device_test.cpp @@ -31,7 +31,7 @@ BOOST_AUTO_TEST_CASE(test_device){ std::cout << wax::cast<std::string>((*dev)[DEVICE_PROP_NAME]) << std::endl; std::cout << "Access the mboard" << std::endl; - wax::proxy mb0 = (*dev)[DEVICE_PROP_MBOARD]; + wax::obj mb0 = (*dev)[DEVICE_PROP_MBOARD]; std::cout << wax::cast<std::string>(mb0[MBOARD_PROP_NAME]) << std::endl; BOOST_CHECK_EQUAL( device_addr.virtual_args.num_dboards, diff --git a/test/gain_handler_test.cpp b/test/gain_handler_test.cpp index 26141ab21..074a67ec8 100644 --- a/test/gain_handler_test.cpp +++ b/test/gain_handler_test.cpp @@ -49,11 +49,11 @@ public: ~gainful_obj(void){} private: - void get(const wax::type &key_, wax::type &val){ + void get(const wax::obj &key_, wax::obj &val){ if (_gain_handler->intercept_get(key_, val)) return; - wax::type key; std::string name; - tie(key, name) = extract_named_prop(key_); + wax::obj key; std::string name; + boost::tie(key, name) = extract_named_prop(key_); //handle the get request conditioned on the key switch(wax::cast<prop_t>(key)){ @@ -79,11 +79,11 @@ private: } } - void set(const wax::type &key_, const wax::type &val){ + void set(const wax::obj &key_, const wax::obj &val){ if (_gain_handler->intercept_set(key_, val)) return; - wax::type key; std::string name; - tie(key, name) = extract_named_prop(key_); + wax::obj key; std::string name; + boost::tie(key, name) = extract_named_prop(key_); //handle the get request conditioned on the key switch(wax::cast<prop_t>(key)){ diff --git a/test/wax_test.cpp b/test/wax_test.cpp index 4c3ec0635..b329788f2 100644 --- a/test/wax_test.cpp +++ b/test/wax_test.cpp @@ -22,7 +22,7 @@ enum opt_a_t{OPTION_A_0, OPTION_A_1}; enum opt_b_t{OPTION_B_0, OPTION_B_1}; BOOST_AUTO_TEST_CASE(test_enums){ - wax::type opta = OPTION_A_0; + wax::obj opta = OPTION_A_0; BOOST_CHECK_THROW(wax::cast<opt_b_t>(opta), wax::bad_cast); BOOST_CHECK_EQUAL(wax::cast<opt_a_t>(opta), OPTION_A_0); } @@ -46,14 +46,14 @@ public: ~wax_demo(void){ /* NOP */ } - void get(const wax::type &key, wax::type &value){ + void get(const wax::obj &key, wax::obj &value){ if (d_subs.size() == 0){ value = d_nums[wax::cast<size_t>(key)]; }else{ - value = obj::cast(&d_subs[wax::cast<size_t>(key)]); + value = d_subs[wax::cast<size_t>(key)].get_link(); } } - void set(const wax::type &key, const wax::type &value){ + void set(const wax::obj &key, const wax::obj &value){ if (d_subs.size() == 0){ d_nums[wax::cast<size_t>(key)] = wax::cast<float>(value); }else{ @@ -71,6 +71,8 @@ BOOST_AUTO_TEST_CASE(test_chaining){ wd[size_t(0)][size_t(0)]; std::cout << "chain 3" << std::endl; wd[size_t(0)][size_t(0)][size_t(0)]; + std::cout << "cast proxy with link" << std::endl; + wax::cast<wax::obj::ptr>(wd[size_t(0)][size_t(0)]); } BOOST_AUTO_TEST_CASE(test_set_get){ @@ -89,16 +91,16 @@ BOOST_AUTO_TEST_CASE(test_set_get){ BOOST_AUTO_TEST_CASE(test_proxy){ std::cout << "store proxy" << std::endl; - wax::proxy p = wd[size_t(0)][size_t(0)]; + wax::obj p = wd[size_t(0)][size_t(0)]; p[size_t(0)] = float(5); std::cout << "assign proxy" << std::endl; - wax::type a = p[size_t(0)]; + wax::obj a = p[size_t(0)]; BOOST_CHECK_EQUAL(wax::cast<float>(a), float(5)); } BOOST_AUTO_TEST_CASE(test_print){ std::cout << "print type" << std::endl; - wax::type test_type = float(3.33); + wax::obj test_type = float(3.33); std::cout << test_type << std::endl; } |