2016-07-05 01:15:58 +00:00
|
|
|
#ifndef INCLUDED_UTIL_CONFIG_H
|
|
|
|
#define INCLUDED_UTIL_CONFIG_H
|
|
|
|
|
2016-08-21 22:22:45 +00:00
|
|
|
#include "Util/GenericValue.h"
|
2016-07-05 01:15:58 +00:00
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
#include <iterator>
|
2016-08-21 22:22:45 +00:00
|
|
|
#include <exception>
|
2016-07-05 01:15:58 +00:00
|
|
|
|
|
|
|
namespace Util
|
|
|
|
{
|
|
|
|
namespace Config
|
|
|
|
{
|
|
|
|
class Node
|
|
|
|
{
|
|
|
|
private:
|
2016-08-21 22:22:45 +00:00
|
|
|
typedef std::shared_ptr<Node> ptr_t;
|
|
|
|
typedef std::shared_ptr<const Node> const_ptr_t;
|
|
|
|
const std::string m_key; // this cannot be changed (maps depend on it)
|
|
|
|
std::shared_ptr<GenericValue> m_value; // null pointer marks this node as an invalid, empty node
|
|
|
|
ptr_t m_next_sibling;
|
|
|
|
ptr_t m_first_child;
|
|
|
|
ptr_t m_last_child;
|
|
|
|
std::map<std::string, ptr_t> m_children;
|
|
|
|
mutable std::map<std::string, Node> m_missing_nodes; // missing nodes from failed queries (must also be empty)
|
|
|
|
bool m_missing = false;
|
2016-07-05 01:15:58 +00:00
|
|
|
|
2016-08-18 04:14:36 +00:00
|
|
|
void Destroy()
|
|
|
|
{
|
2016-08-21 22:22:45 +00:00
|
|
|
m_value.reset();
|
2016-08-18 04:14:36 +00:00
|
|
|
m_next_sibling.reset();
|
|
|
|
m_first_child.reset();
|
|
|
|
m_last_child.reset();
|
|
|
|
m_children.clear();
|
|
|
|
}
|
|
|
|
|
2016-08-21 22:22:45 +00:00
|
|
|
void CheckEmptyOrMissing() const;
|
|
|
|
const Node &MissingNode(const std::string &key) const;
|
2023-03-04 19:58:52 +00:00
|
|
|
Node &AddEmpty(const std::string &path);
|
2016-08-21 22:22:45 +00:00
|
|
|
void AddChild(Node &parent, ptr_t &node);
|
2016-08-18 04:14:36 +00:00
|
|
|
void DeepCopy(const Node &that);
|
2016-08-19 00:01:45 +00:00
|
|
|
void Swap(Node &rhs);
|
2016-08-21 22:22:45 +00:00
|
|
|
Node(); // prohibit accidental/unintentional creation of empty nodes
|
2016-07-05 01:15:58 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
class const_iterator;
|
|
|
|
|
2021-02-18 10:29:15 +00:00
|
|
|
class iterator
|
2016-07-05 01:15:58 +00:00
|
|
|
{
|
|
|
|
private:
|
2016-08-21 22:22:45 +00:00
|
|
|
ptr_t m_node;
|
2021-02-18 10:29:15 +00:00
|
|
|
friend class const_iterator;
|
2016-07-05 01:15:58 +00:00
|
|
|
public:
|
2016-08-21 22:22:45 +00:00
|
|
|
inline iterator(ptr_t node = ptr_t())
|
2016-07-05 01:15:58 +00:00
|
|
|
: m_node(node)
|
|
|
|
{}
|
|
|
|
inline iterator(const iterator &it)
|
|
|
|
: m_node(it.m_node)
|
|
|
|
{}
|
|
|
|
inline iterator operator++()
|
|
|
|
{
|
|
|
|
// Prefix increment
|
|
|
|
m_node = m_node->m_next_sibling;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
inline iterator operator++(int)
|
|
|
|
{
|
|
|
|
// Postfix increment
|
|
|
|
iterator current(*this);
|
|
|
|
m_node = m_node->m_next_sibling;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
inline Node &operator*() const
|
|
|
|
{
|
|
|
|
return *m_node;
|
|
|
|
}
|
2016-08-21 22:22:45 +00:00
|
|
|
inline ptr_t operator->() const
|
2016-07-05 01:15:58 +00:00
|
|
|
{
|
|
|
|
return m_node;
|
|
|
|
}
|
|
|
|
inline bool operator==(const iterator &rhs) const
|
|
|
|
{
|
|
|
|
return m_node == rhs.m_node;
|
|
|
|
}
|
|
|
|
inline bool operator!=(const iterator &rhs) const
|
|
|
|
{
|
|
|
|
return m_node != rhs.m_node;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-02-18 10:29:15 +00:00
|
|
|
class const_iterator
|
2016-07-05 01:15:58 +00:00
|
|
|
{
|
|
|
|
private:
|
2016-08-21 22:22:45 +00:00
|
|
|
const_ptr_t m_node;
|
2016-07-05 01:15:58 +00:00
|
|
|
public:
|
2017-03-27 18:15:15 +00:00
|
|
|
inline const_iterator()
|
|
|
|
: m_node(const_ptr_t())
|
|
|
|
{}
|
|
|
|
inline const_iterator(const_ptr_t node)
|
2016-07-05 01:15:58 +00:00
|
|
|
: m_node(node)
|
|
|
|
{}
|
2017-03-27 18:15:15 +00:00
|
|
|
inline const_iterator(ptr_t node)
|
2016-07-05 01:15:58 +00:00
|
|
|
: m_node(std::const_pointer_cast<const Node>(node))
|
|
|
|
{}
|
|
|
|
inline const_iterator(const const_iterator &it)
|
|
|
|
: m_node(it.m_node)
|
|
|
|
{}
|
|
|
|
inline const_iterator(const Node::iterator &it)
|
|
|
|
: m_node(it.m_node)
|
|
|
|
{}
|
|
|
|
inline const_iterator operator++()
|
|
|
|
{
|
|
|
|
// Prefix increment
|
|
|
|
m_node = m_node->m_next_sibling;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
inline const_iterator operator++(int)
|
|
|
|
{
|
|
|
|
// Postfix increment
|
2017-03-27 18:15:15 +00:00
|
|
|
//iterator current(*this); //unreferenced local variable
|
2016-07-05 01:15:58 +00:00
|
|
|
m_node = m_node->m_next_sibling;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
inline const Node &operator*() const
|
|
|
|
{
|
|
|
|
return *m_node;
|
|
|
|
}
|
2016-08-21 22:22:45 +00:00
|
|
|
inline const_ptr_t operator->() const
|
2016-07-05 01:15:58 +00:00
|
|
|
{
|
|
|
|
return m_node;
|
|
|
|
}
|
|
|
|
inline bool operator==(const const_iterator &rhs) const
|
|
|
|
{
|
|
|
|
return m_node == rhs.m_node;
|
|
|
|
}
|
|
|
|
inline bool operator!=(const const_iterator &rhs) const
|
|
|
|
{
|
|
|
|
return m_node != rhs.m_node;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inline iterator begin()
|
|
|
|
{
|
|
|
|
return iterator(m_first_child);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline iterator end()
|
|
|
|
{
|
|
|
|
return iterator();
|
|
|
|
}
|
2021-02-18 10:29:15 +00:00
|
|
|
|
2016-07-05 01:15:58 +00:00
|
|
|
inline const_iterator begin() const
|
|
|
|
{
|
|
|
|
return iterator(m_first_child);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const_iterator end() const
|
|
|
|
{
|
|
|
|
return iterator();
|
|
|
|
}
|
|
|
|
|
|
|
|
const inline std::string &Key() const
|
|
|
|
{
|
|
|
|
return m_key;
|
|
|
|
}
|
|
|
|
|
2016-08-21 22:22:45 +00:00
|
|
|
inline std::shared_ptr<GenericValue> GetValue() const
|
2016-07-05 01:15:58 +00:00
|
|
|
{
|
|
|
|
return m_value;
|
|
|
|
}
|
|
|
|
|
2023-03-04 19:58:52 +00:00
|
|
|
inline void Clear()
|
|
|
|
{
|
|
|
|
m_value = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-08-21 22:22:45 +00:00
|
|
|
inline void SetValue(const std::shared_ptr<GenericValue> &value)
|
2016-07-05 01:15:58 +00:00
|
|
|
{
|
2016-08-21 22:22:45 +00:00
|
|
|
m_value = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
const T &Value() const
|
|
|
|
{
|
|
|
|
CheckEmptyOrMissing();
|
|
|
|
return m_value->Value<T>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T ValueAs() const
|
|
|
|
{
|
|
|
|
CheckEmptyOrMissing();
|
|
|
|
return m_value->ValueAs<T>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T ValueAsDefault(const T &default_value) const
|
|
|
|
{
|
|
|
|
if (Empty())
|
|
|
|
return default_value;
|
|
|
|
return m_value->ValueAs<T>();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set value of this node
|
|
|
|
template <typename T>
|
|
|
|
inline void SetValue(const T &value)
|
|
|
|
{
|
|
|
|
if (!m_missing)
|
|
|
|
{
|
|
|
|
if (!Empty() && m_value->Is<T>())
|
|
|
|
m_value->Set(value);
|
|
|
|
else
|
|
|
|
m_value = std::make_shared<ValueInstance<T>>(value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw std::range_error(Util::Format() << "Node \"" << m_key << "\" does not exist");
|
|
|
|
}
|
|
|
|
|
|
|
|
// char* is a troublesome case because we want to convert it to std::string
|
|
|
|
inline void SetValue(const char *value)
|
|
|
|
{
|
|
|
|
SetValue<std::string>(value);
|
2016-07-05 01:15:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-11 07:03:10 +00:00
|
|
|
// Add a child node. Multiple nodes of the same key may be added but only
|
2021-02-18 10:29:15 +00:00
|
|
|
// when specified as leaves. For example, adding "foo/bar" twice will
|
2017-04-11 07:03:10 +00:00
|
|
|
// result in one "foo" with two "bar" children.
|
2016-08-21 22:22:45 +00:00
|
|
|
template <typename T>
|
|
|
|
Node &Add(const std::string &path, const T &value)
|
|
|
|
{
|
2023-03-04 19:58:52 +00:00
|
|
|
Node &new_leaf_node = AddEmpty(path);
|
|
|
|
new_leaf_node.SetValue(value);
|
|
|
|
return new_leaf_node;
|
2016-08-21 22:22:45 +00:00
|
|
|
}
|
2017-04-11 07:03:10 +00:00
|
|
|
|
2016-08-21 22:22:45 +00:00
|
|
|
Node &Add(const std::string &path)
|
|
|
|
{
|
|
|
|
return Add(path, std::string());
|
|
|
|
}
|
2016-07-10 04:25:41 +00:00
|
|
|
|
2016-08-21 22:22:45 +00:00
|
|
|
// Set the value of the matching child node if it exists, else add new
|
|
|
|
template <typename T>
|
|
|
|
void Set(const std::string &key, const T &value)
|
2016-07-05 01:15:58 +00:00
|
|
|
{
|
2016-08-21 22:22:45 +00:00
|
|
|
Node *node = TryGet(key);
|
|
|
|
if (node)
|
|
|
|
node->SetValue(value);
|
|
|
|
else
|
|
|
|
Add(key, value);
|
2016-07-05 01:15:58 +00:00
|
|
|
}
|
|
|
|
|
2023-03-04 19:58:52 +00:00
|
|
|
void SetEmpty(const std::string &key)
|
|
|
|
{
|
|
|
|
Node *node = TryGet(key);
|
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
node->Clear();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AddEmpty(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-21 22:22:45 +00:00
|
|
|
// True if value is empty (does not exist)
|
2016-07-05 01:15:58 +00:00
|
|
|
inline bool Empty() const
|
|
|
|
{
|
2016-08-21 22:22:45 +00:00
|
|
|
return !m_value;
|
2016-07-05 01:15:58 +00:00
|
|
|
}
|
2021-02-18 10:29:15 +00:00
|
|
|
|
2016-08-21 22:22:45 +00:00
|
|
|
// True if value exists (is not empty)
|
2016-07-10 04:25:41 +00:00
|
|
|
inline bool Exists() const
|
|
|
|
{
|
|
|
|
return !Empty();
|
|
|
|
}
|
2016-07-05 01:15:58 +00:00
|
|
|
|
2016-08-21 22:22:45 +00:00
|
|
|
// True if no keys under this node
|
2016-07-05 01:15:58 +00:00
|
|
|
inline bool IsLeaf() const
|
|
|
|
{
|
|
|
|
return m_children.empty();
|
|
|
|
}
|
|
|
|
|
2016-08-21 22:22:45 +00:00
|
|
|
// True if this node has keys
|
2016-07-05 01:15:58 +00:00
|
|
|
inline bool HasChildren() const
|
|
|
|
{
|
|
|
|
return !IsLeaf();
|
|
|
|
}
|
|
|
|
|
2016-08-21 22:22:45 +00:00
|
|
|
// Always succeeds -- failed lookups permanently create an empty node.
|
|
|
|
// Use with caution. Intended for hard-coded lookups.
|
2016-07-05 01:15:58 +00:00
|
|
|
const Node &operator[](const std::string &path) const;
|
2021-02-18 10:29:15 +00:00
|
|
|
|
2016-08-21 22:22:45 +00:00
|
|
|
// These throw if the node is missing
|
|
|
|
Node &Get(const std::string &path);
|
2016-07-05 01:15:58 +00:00
|
|
|
const Node &Get(const std::string &path) const;
|
2021-02-18 10:29:15 +00:00
|
|
|
|
2016-08-21 22:22:45 +00:00
|
|
|
// This returns nullptr if node is missing
|
|
|
|
Node *TryGet(const std::string &path);
|
|
|
|
const Node *TryGet(const std::string &path) const;
|
|
|
|
|
|
|
|
void Serialize(std::ostream *os, size_t indent_level = 0) const;
|
2016-07-09 14:45:48 +00:00
|
|
|
std::string ToString(size_t indent_level = 0) const;
|
2016-08-18 04:14:36 +00:00
|
|
|
Node &operator=(const Node &rhs);
|
2021-12-19 17:48:38 +00:00
|
|
|
Node& operator=(Node&& rhs) noexcept;
|
2016-07-05 01:15:58 +00:00
|
|
|
Node(const std::string &key);
|
|
|
|
Node(const std::string &key, const std::string &value);
|
|
|
|
Node(const Node &that);
|
2021-12-19 17:48:38 +00:00
|
|
|
Node(Node&& that) noexcept;
|
2016-07-05 01:15:58 +00:00
|
|
|
~Node();
|
|
|
|
};
|
2023-03-04 19:58:52 +00:00
|
|
|
|
|
|
|
void PrintConfigTree(const Node &config, int indent_level = 0, int tab_stops = 2);
|
2016-07-05 01:15:58 +00:00
|
|
|
} // Config
|
|
|
|
} // Util
|
|
|
|
|
|
|
|
#endif // INCLUDED_UTIL_CONFIG_H
|