From ba2a6b4b39f66f0edaef710561019d7cd43cb417 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Sat, 25 Jun 2011 20:23:19 -0700 Subject: uhd: forgot to commit properties file --- host/include/uhd/property_tree.hpp | 73 ++++++++++++++++++++++++++ host/include/uhd/types/serial.hpp | 5 ++ host/lib/property_tree.cpp | 104 +++++++++++++++++++++++++++++++++++++ 3 files changed, 182 insertions(+) create mode 100644 host/include/uhd/property_tree.hpp create mode 100644 host/lib/property_tree.cpp diff --git a/host/include/uhd/property_tree.hpp b/host/include/uhd/property_tree.hpp new file mode 100644 index 000000000..9f8f30b57 --- /dev/null +++ b/host/include/uhd/property_tree.hpp @@ -0,0 +1,73 @@ +// +// 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_HPP +#define INCLUDED_UHD_PROPERTY_TREE_HPP + +#include +#include +#include +#include +#include +#include +#include + +namespace uhd{ + +/*! + * The property tree provides a file system structure for accessing properties. + */ +class UHD_API property_tree : boost::noncopyable{ +public: + typedef boost::shared_ptr sptr; + typedef boost::filesystem::path path_type; + typedef std::list iterator_type; + + //! Create a new + empty property tree + static sptr make(void); + + //! Remove a property or directory (recursive) + virtual void remove(const path_type &path) = 0; + + //! True if the path exists in the tree + virtual bool exists(const path_type &path) = 0; + + //! Get an iterator to all things in the given path + virtual iterator_type iterate(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); + } + + //! Get access to a property in the tree + template property access(const path_type &path){ + return boost::any_cast >(this->_access(path)); + } + +protected: + //! Internal create property with wild-card type + virtual void _create(const path_type &path, const boost::any &prop) = 0; + + //! Internal access property with wild-card type + virtual boost::any &_access(const path_type &path) = 0; + +}; + +} //namespace uhd + +#endif /* INCLUDED_UHD_PROPERTY_TREE_HPP */ diff --git a/host/include/uhd/types/serial.hpp b/host/include/uhd/types/serial.hpp index 8281fd3ec..cf7e494f9 100644 --- a/host/include/uhd/types/serial.hpp +++ b/host/include/uhd/types/serial.hpp @@ -19,6 +19,7 @@ #define INCLUDED_UHD_TYPES_SERIAL_HPP #include +#include #include #include @@ -43,6 +44,8 @@ namespace uhd{ */ class UHD_API i2c_iface{ public: + typedef boost::shared_ptr sptr; + /*! * Write bytes over the i2c. * \param addr the address @@ -123,6 +126,8 @@ namespace uhd{ */ class UHD_API spi_iface{ public: + typedef boost::shared_ptr sptr; + /*! * Perform a spi transaction. * \param which_slave the slave device number diff --git a/host/lib/property_tree.cpp b/host/lib/property_tree.cpp new file mode 100644 index 000000000..7cda7a524 --- /dev/null +++ b/host/lib/property_tree.cpp @@ -0,0 +1,104 @@ +// +// 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 . +// + +#include +#include +#include +#include +#include + +class property_tree_impl : public uhd::property_tree{ +public: + + void remove(const path_type &path){ + boost::mutex::scoped_lock lock(_mutex); + + node_type *parent = NULL; + node_type *node = &_root; + BOOST_FOREACH(const std::string &leaf, path){ + if (not node->has_key(leaf)) throw_path_not_found(path); + parent = node; + node = &(*node)[leaf]; + } + if (parent == NULL) throw uhd::runtime_error("Cannot uproot"); + parent->pop(path.leaf()); + } + + bool exists(const path_type &path){ + boost::mutex::scoped_lock lock(_mutex); + + node_type *node = &_root; + BOOST_FOREACH(const std::string &leaf, path){ + if (not node->has_key(leaf)) return false; + node = &(*node)[leaf]; + } + return true; + } + + iterator_type iterate(const path_type &path){ + boost::mutex::scoped_lock lock(_mutex); + + node_type *node = &_root; + BOOST_FOREACH(const std::string &leaf, path){ + if (not node->has_key(leaf)) throw_path_not_found(path); + node = &(*node)[leaf]; + } + + iterator_type iter; + BOOST_FOREACH(const std::string &name, node->keys()){ + iter.push_back(name); + } + return iter; + } + + void _create(const path_type &path, const boost::any &prop){ + boost::mutex::scoped_lock lock(_mutex); + + node_type *node = &_root; + BOOST_FOREACH(const std::string &leaf, path){ + if (not node->has_key(leaf)) (*node)[leaf] = node_type(); + node = &(*node)[leaf]; + } + node->prop = prop; + } + + boost::any &_access(const path_type &path){ + boost::mutex::scoped_lock lock(_mutex); + + node_type *node = &_root; + BOOST_FOREACH(const std::string &leaf, path){ + if (not node->has_key(leaf)) throw_path_not_found(path); + node = &(*node)[leaf]; + } + return node->prop; + } + +private: + void throw_path_not_found(const path_type &path){ + throw uhd::lookup_error("Path not found in tree: " + path.string()); + } + + struct node_type : uhd::dict{ + boost::any prop; + } _root; + + boost::mutex _mutex; +}; + +uhd::property_tree::sptr uhd::property_tree::make(void){ + return sptr(new property_tree_impl()); +} -- cgit v1.2.3