mirror of
https://github.com/saymrwulf/uhd.git
synced 2026-05-16 21:10:10 +00:00
This is a very mechanical task that could almost have been done with sed. Boost versions of mutexes and locks were removed, and replaced with std:: versions. The replacement tables are as follows: == Mutexes == - boost::mutex -> std::mutex - boost::recursive_mutex -> std::recursive_mutex Mutexes behave identically between Boost and std:: and have the same API. == Locks == C++11 has only two types of lock that we use/need in UHD: - std::lock_guard: Identical to boost::lock_guard - std::unique_lock: Identical to boost::unique_lock Boost also has boost::mutex::scoped_lock, which is a typedef for boost::unique_lock<>. However, we often have used scoped_lock where we meant to use lock_guard<>. The name is a bit misleading, "scoped lock" sounding a bit like an RAII mechanism. Therefore, some previous boost::mutex::scoped_lock are now std::lock_guard<>. std::unique_lock is required when doing more than RAII locking (i.e., unlocking, relocking, usage with condition variables, etc.). == Condition Variables == Condition variables were out of the scope of this lock/mutex change, but in UHD, we inconsistently use boost::condition vs. boost::condition_variable. The former is a templated version of the latter, and thus works fine with std::mutex'es. Therefore, some boost::condition_variable where changed to boost::condition. All locks and mutexes use `#include <mutex>`. The corresponding Boost includes were removed. In some cases, this exposed issues with implicit Boost includes elsewhere. The missing explicit includes were added.
226 lines
6.8 KiB
C++
226 lines
6.8 KiB
C++
//
|
|
// Copyright 2011,2014-2016 Ettus Research
|
|
// Copyright 2018 Ettus Research, a National Instruments Company
|
|
// Copyright 2019 Ettus Research, a National Instruments Brand
|
|
//
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
//
|
|
|
|
#include <uhd/property_tree.hpp>
|
|
#include <uhd/types/dict.hpp>
|
|
#include <memory>
|
|
#include <mutex>
|
|
|
|
using namespace uhd;
|
|
|
|
/***********************************************************************
|
|
* Helper function to iterate through paths
|
|
**********************************************************************/
|
|
#include <boost/tokenizer.hpp>
|
|
#define path_tokenizer(path) \
|
|
boost::tokenizer<boost::char_separator<char>>(path, boost::char_separator<char>("/"))
|
|
|
|
/***********************************************************************
|
|
* Property path implementation wrapper
|
|
**********************************************************************/
|
|
fs_path::fs_path(void) : std::string() {}
|
|
fs_path::fs_path(const char* p) : std::string(p) {}
|
|
fs_path::fs_path(const std::string& p) : std::string(p) {}
|
|
|
|
std::string fs_path::leaf(void) const
|
|
{
|
|
const size_t pos = this->rfind("/");
|
|
if (pos == std::string::npos)
|
|
return *this;
|
|
return this->substr(pos + 1);
|
|
}
|
|
|
|
fs_path fs_path::branch_path(void) const
|
|
{
|
|
const size_t pos = this->rfind("/");
|
|
if (pos == std::string::npos)
|
|
return *this;
|
|
return fs_path(this->substr(0, pos));
|
|
}
|
|
|
|
fs_path uhd::operator/(const fs_path& lhs, const fs_path& rhs)
|
|
{
|
|
// strip trailing slash on left-hand-side
|
|
if (not lhs.empty() and *lhs.rbegin() == '/') {
|
|
return fs_path(lhs.substr(0, lhs.size() - 1)) / rhs;
|
|
}
|
|
|
|
// strip leading slash on right-hand-side
|
|
if (not rhs.empty() and *rhs.begin() == '/') {
|
|
return lhs / fs_path(rhs.substr(1));
|
|
}
|
|
|
|
return fs_path(lhs + "/" + rhs);
|
|
}
|
|
|
|
fs_path uhd::operator/(const fs_path& lhs, size_t rhs)
|
|
{
|
|
fs_path rhs_str = std::to_string(rhs);
|
|
return lhs / rhs_str;
|
|
}
|
|
|
|
/***********************************************************************
|
|
* Property tree implementation
|
|
**********************************************************************/
|
|
class property_tree_impl : public uhd::property_tree
|
|
{
|
|
public:
|
|
property_tree_impl(const fs_path& root = fs_path()) : _root(root)
|
|
{
|
|
_guts = std::make_shared<tree_guts_type>();
|
|
}
|
|
|
|
sptr subtree(const fs_path& path_) const override
|
|
{
|
|
const fs_path path = _root / path_;
|
|
std::lock_guard<std::mutex> lock(_guts->mutex);
|
|
|
|
property_tree_impl* subtree = new property_tree_impl(path);
|
|
subtree->_guts = this->_guts; // copy the guts sptr
|
|
return sptr(subtree);
|
|
}
|
|
|
|
void remove(const fs_path& path_) override
|
|
{
|
|
const fs_path path = _root / path_;
|
|
std::lock_guard<std::mutex> lock(_guts->mutex);
|
|
|
|
node_type* parent = NULL;
|
|
node_type* node = &_guts->root;
|
|
for (const std::string& name : path_tokenizer(path)) {
|
|
if (not node->has_key(name))
|
|
throw_path_not_found(path);
|
|
parent = node;
|
|
node = &(*node)[name];
|
|
}
|
|
if (parent == NULL)
|
|
throw uhd::runtime_error("Cannot uproot");
|
|
parent->pop(fs_path(path.leaf()));
|
|
}
|
|
|
|
bool exists(const fs_path& path_) const override
|
|
{
|
|
const fs_path path = _root / path_;
|
|
std::lock_guard<std::mutex> lock(_guts->mutex);
|
|
|
|
node_type* node = &_guts->root;
|
|
for (const std::string& name : path_tokenizer(path)) {
|
|
if (not node->has_key(name))
|
|
return false;
|
|
node = &(*node)[name];
|
|
}
|
|
return true;
|
|
}
|
|
|
|
std::vector<std::string> list(const fs_path& path_) const override
|
|
{
|
|
const fs_path path = _root / path_;
|
|
std::lock_guard<std::mutex> lock(_guts->mutex);
|
|
|
|
node_type* node = &_guts->root;
|
|
for (const std::string& name : path_tokenizer(path)) {
|
|
if (not node->has_key(name))
|
|
throw_path_not_found(path);
|
|
node = &(*node)[name];
|
|
}
|
|
|
|
return node->keys();
|
|
}
|
|
|
|
std::shared_ptr<void> _pop(const fs_path& path_) override
|
|
{
|
|
const fs_path path = _root / path_;
|
|
std::lock_guard<std::mutex> lock(_guts->mutex);
|
|
|
|
node_type* parent = NULL;
|
|
node_type* node = &_guts->root;
|
|
for (const std::string& name : path_tokenizer(path)) {
|
|
if (not node->has_key(name))
|
|
throw_path_not_found(path);
|
|
parent = node;
|
|
node = &(*node)[name];
|
|
}
|
|
|
|
if (node->prop.get() == NULL)
|
|
throw uhd::runtime_error("Cannot access! Property uninitialized at: " + path);
|
|
if (parent == NULL)
|
|
throw uhd::runtime_error("Cannot pop");
|
|
auto prop = node->prop;
|
|
parent->pop(fs_path(path.leaf()));
|
|
return prop;
|
|
}
|
|
|
|
void _create(const fs_path& path_, const std::shared_ptr<void>& prop) override
|
|
{
|
|
const fs_path path = _root / path_;
|
|
std::lock_guard<std::mutex> lock(_guts->mutex);
|
|
|
|
node_type* node = &_guts->root;
|
|
for (const std::string& name : path_tokenizer(path)) {
|
|
if (not node->has_key(name))
|
|
(*node)[name] = node_type();
|
|
node = &(*node)[name];
|
|
}
|
|
if (node->prop.get() != NULL)
|
|
throw uhd::runtime_error(
|
|
"Cannot create! Property already exists at: " + path);
|
|
node->prop = prop;
|
|
}
|
|
|
|
std::shared_ptr<void>& _access(const fs_path& path_) const override
|
|
{
|
|
const fs_path path = _root / path_;
|
|
std::lock_guard<std::mutex> lock(_guts->mutex);
|
|
|
|
node_type* node = &_guts->root;
|
|
for (const std::string& name : path_tokenizer(path)) {
|
|
if (not node->has_key(name))
|
|
throw_path_not_found(path);
|
|
node = &(*node)[name];
|
|
}
|
|
if (node->prop.get() == NULL)
|
|
throw uhd::runtime_error("Cannot access! Property uninitialized at: " + path);
|
|
return node->prop;
|
|
}
|
|
|
|
private:
|
|
void throw_path_not_found(const fs_path& path) const
|
|
{
|
|
throw uhd::lookup_error("Path not found in tree: " + path);
|
|
}
|
|
|
|
// basic structural node element
|
|
struct node_type : uhd::dict<std::string, node_type>
|
|
{
|
|
std::shared_ptr<void> prop;
|
|
};
|
|
|
|
// tree guts which may be referenced in a subtree
|
|
struct tree_guts_type
|
|
{
|
|
node_type root;
|
|
std::mutex mutex;
|
|
};
|
|
|
|
// members, the tree and root prefix
|
|
std::shared_ptr<tree_guts_type> _guts;
|
|
const fs_path _root;
|
|
};
|
|
|
|
property_tree::~property_tree(void)
|
|
{
|
|
/* NOP */
|
|
}
|
|
|
|
/***********************************************************************
|
|
* Property tree factory
|
|
**********************************************************************/
|
|
uhd::property_tree::sptr uhd::property_tree::make(void)
|
|
{
|
|
return sptr(new property_tree_impl());
|
|
}
|