Path: blob/main/contrib/kyua/utils/config/nodes.hpp
48081 views
// Copyright 2012 The Kyua Authors.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728/// \file utils/config/nodes.hpp29/// Representation of tree nodes.3031#if !defined(UTILS_CONFIG_NODES_HPP)32#define UTILS_CONFIG_NODES_HPP3334#include "utils/config/nodes_fwd.hpp"3536#include <set>37#include <string>3839#include <lutok/state.hpp>4041#include "utils/config/keys_fwd.hpp"42#include "utils/config/nodes_fwd.hpp"43#include "utils/noncopyable.hpp"44#include "utils/optional.hpp"4546namespace utils {47namespace config {484950namespace detail {515253/// Base representation of a node.54///55/// This abstract class provides the base type for every node in the tree. Due56/// to the dynamic nature of our trees (each leaf being able to hold arbitrary57/// data types), this base type is a necessity.58class base_node : noncopyable {59public:60virtual ~base_node(void) = 0;6162/// Copies the node.63///64/// \return A dynamically-allocated node.65virtual base_node* deep_copy(void) const = 0;6667/// Combines this node with another one.68///69/// \param key Key to this node.70/// \param other The node to combine with.71///72/// \return A new node representing the combination.73///74/// \throw bad_combination_error If the two nodes cannot be combined.75virtual base_node* combine(const tree_key& key, const base_node* other)76const = 0;77};787980} // namespace detail818283/// Abstract leaf node without any specified type.84///85/// This base abstract type is necessary to have a common pointer type to which86/// to cast any leaf. We later provide templated derivates of this class, and87/// those cannot act in this manner.88///89/// It is important to understand that a leaf can exist without actually holding90/// a value. Our trees are "strictly keyed": keys must have been pre-defined91/// before a value can be set on them. This is to ensure that the end user is92/// using valid key names and not making mistakes due to typos, for example. To93/// represent this condition, we define an "empty" key in the tree to denote94/// that the key is valid, yet it has not been set by the user. Only when an95/// explicit set is performed on the key, it gets a value.96class leaf_node : public detail::base_node {97public:98virtual ~leaf_node(void);99100virtual bool is_set(void) const = 0;101102base_node* combine(const detail::tree_key&, const base_node*) const;103104virtual void push_lua(lutok::state&) const = 0;105virtual void set_lua(lutok::state&, const int) = 0;106107virtual void set_string(const std::string&) = 0;108virtual std::string to_string(void) const = 0;109};110111112/// Base leaf node for a single arbitrary type.113///114/// This templated leaf node holds a single object of any type. The conversion115/// to/from string representations is undefined, as that depends on the116/// particular type being processed. You should reimplement this class for any117/// type that needs additional processing/validation during conversion.118template< typename ValueType >119class typed_leaf_node : public leaf_node {120public:121/// The type of the value held by this node.122typedef ValueType value_type;123124/// Constructs a new leaf node that contains no value.125typed_leaf_node(void);126127/// Checks whether the node has been set by the user.128bool is_set(void) const;129130/// Gets the value stored in the node.131const value_type& value(void) const;132133/// Gets the read-write value stored in the node.134value_type& value(void);135136/// Sets the value of the node.137void set(const value_type&);138139protected:140/// The value held by this node.141optional< value_type > _value;142143private:144virtual void validate(const value_type&) const;145};146147148/// Leaf node holding a native type.149///150/// This templated leaf node holds a native type. The conversion to/from string151/// representations of the value happens by means of iostreams.152template< typename ValueType >153class native_leaf_node : public typed_leaf_node< ValueType > {154public:155void set_string(const std::string&);156std::string to_string(void) const;157};158159160/// A leaf node that holds a boolean value.161class bool_node : public native_leaf_node< bool > {162public:163virtual base_node* deep_copy(void) const;164165void push_lua(lutok::state&) const;166void set_lua(lutok::state&, const int);167};168169170/// A leaf node that holds an integer value.171class int_node : public native_leaf_node< int > {172public:173virtual base_node* deep_copy(void) const;174175void push_lua(lutok::state&) const;176void set_lua(lutok::state&, const int);177};178179180/// A leaf node that holds a positive non-zero integer value.181class positive_int_node : public int_node {182virtual void validate(const value_type&) const;183};184185186/// A leaf node that holds a string value.187class string_node : public native_leaf_node< std::string > {188public:189virtual base_node* deep_copy(void) const;190191void push_lua(lutok::state&) const;192void set_lua(lutok::state&, const int);193};194195196/// Base leaf node for a set of native types.197///198/// This is a base abstract class because there is no generic way to parse a199/// single word in the textual representation of the set to the native value.200template< typename ValueType >201class base_set_node : public leaf_node {202public:203/// The type of the value held by this node.204typedef std::set< ValueType > value_type;205206base_set_node(void);207208/// Checks whether the node has been set by the user.209///210/// \return True if a value has been set in the node.211bool is_set(void) const;212213/// Gets the value stored in the node.214///215/// \pre The node must have a value.216///217/// \return The value in the node.218const value_type& value(void) const;219220/// Gets the read-write value stored in the node.221///222/// \pre The node must have a value.223///224/// \return The value in the node.225value_type& value(void);226227/// Sets the value of the node.228void set(const value_type&);229230/// Sets the value of the node from a raw string representation.231void set_string(const std::string&);232233/// Converts the contents of the node to a string.234std::string to_string(void) const;235236/// Pushes the node's value onto the Lua stack.237void push_lua(lutok::state&) const;238239/// Sets the value of the node from an entry in the Lua stack.240void set_lua(lutok::state&, const int);241242protected:243/// The value held by this node.244optional< value_type > _value;245246private:247/// Converts a single word to the native type.248///249/// \return The parsed value.250///251/// \throw value_error If the value is invalid.252virtual ValueType parse_one(const std::string&) const = 0;253254virtual void validate(const value_type&) const;255};256257258/// A leaf node that holds a set of strings.259class strings_set_node : public base_set_node< std::string > {260public:261virtual base_node* deep_copy(void) const;262263private:264std::string parse_one(const std::string&) const;265};266267268} // namespace config269} // namespace utils270271#endif // !defined(UTILS_CONFIG_NODES_HPP)272273274