xref: /freebsd/contrib/kyua/utils/config/tree.hpp (revision b0d29bc47dba79f6f38e67eabadfb4b32ffd9390)
1*b0d29bc4SBrooks Davis // Copyright 2012 The Kyua Authors.
2*b0d29bc4SBrooks Davis // All rights reserved.
3*b0d29bc4SBrooks Davis //
4*b0d29bc4SBrooks Davis // Redistribution and use in source and binary forms, with or without
5*b0d29bc4SBrooks Davis // modification, are permitted provided that the following conditions are
6*b0d29bc4SBrooks Davis // met:
7*b0d29bc4SBrooks Davis //
8*b0d29bc4SBrooks Davis // * Redistributions of source code must retain the above copyright
9*b0d29bc4SBrooks Davis //   notice, this list of conditions and the following disclaimer.
10*b0d29bc4SBrooks Davis // * Redistributions in binary form must reproduce the above copyright
11*b0d29bc4SBrooks Davis //   notice, this list of conditions and the following disclaimer in the
12*b0d29bc4SBrooks Davis //   documentation and/or other materials provided with the distribution.
13*b0d29bc4SBrooks Davis // * Neither the name of Google Inc. nor the names of its contributors
14*b0d29bc4SBrooks Davis //   may be used to endorse or promote products derived from this software
15*b0d29bc4SBrooks Davis //   without specific prior written permission.
16*b0d29bc4SBrooks Davis //
17*b0d29bc4SBrooks Davis // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*b0d29bc4SBrooks Davis // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*b0d29bc4SBrooks Davis // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*b0d29bc4SBrooks Davis // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*b0d29bc4SBrooks Davis // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*b0d29bc4SBrooks Davis // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*b0d29bc4SBrooks Davis // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*b0d29bc4SBrooks Davis // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*b0d29bc4SBrooks Davis // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*b0d29bc4SBrooks Davis // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*b0d29bc4SBrooks Davis // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*b0d29bc4SBrooks Davis 
29*b0d29bc4SBrooks Davis /// \file utils/config/tree.hpp
30*b0d29bc4SBrooks Davis /// Data type to represent a tree of arbitrary values with string keys.
31*b0d29bc4SBrooks Davis 
32*b0d29bc4SBrooks Davis #if !defined(UTILS_CONFIG_TREE_HPP)
33*b0d29bc4SBrooks Davis #define UTILS_CONFIG_TREE_HPP
34*b0d29bc4SBrooks Davis 
35*b0d29bc4SBrooks Davis #include "utils/config/tree_fwd.hpp"
36*b0d29bc4SBrooks Davis 
37*b0d29bc4SBrooks Davis #include <memory>
38*b0d29bc4SBrooks Davis #include <string>
39*b0d29bc4SBrooks Davis 
40*b0d29bc4SBrooks Davis #include <lutok/state.hpp>
41*b0d29bc4SBrooks Davis 
42*b0d29bc4SBrooks Davis #include "utils/config/keys_fwd.hpp"
43*b0d29bc4SBrooks Davis #include "utils/config/nodes_fwd.hpp"
44*b0d29bc4SBrooks Davis 
45*b0d29bc4SBrooks Davis namespace utils {
46*b0d29bc4SBrooks Davis namespace config {
47*b0d29bc4SBrooks Davis 
48*b0d29bc4SBrooks Davis 
49*b0d29bc4SBrooks Davis /// Representation of a tree.
50*b0d29bc4SBrooks Davis ///
51*b0d29bc4SBrooks Davis /// The string keys of the tree are in dotted notation and actually represent
52*b0d29bc4SBrooks Davis /// path traversals through the nodes.
53*b0d29bc4SBrooks Davis ///
54*b0d29bc4SBrooks Davis /// Our trees are "strictly-keyed": keys must be defined as "existent" before
55*b0d29bc4SBrooks Davis /// their values can be set.  Defining a key is a separate action from setting
56*b0d29bc4SBrooks Davis /// its value.  The rationale is that we want to be able to control what keys
57*b0d29bc4SBrooks Davis /// get defined: because trees are used to hold configuration, we want to catch
58*b0d29bc4SBrooks Davis /// typos as early as possible.  Also, users cannot set keys unless the types
59*b0d29bc4SBrooks Davis /// are known in advance because our leaf nodes are strictly typed.
60*b0d29bc4SBrooks Davis ///
61*b0d29bc4SBrooks Davis /// However, there is an exception to the strict keys: the inner nodes of the
62*b0d29bc4SBrooks Davis /// tree can be static or dynamic.  Static inner nodes have a known subset of
63*b0d29bc4SBrooks Davis /// children and attempting to set keys not previously defined will result in an
64*b0d29bc4SBrooks Davis /// error.  Dynamic inner nodes do not have a predefined set of keys and can be
65*b0d29bc4SBrooks Davis /// used to accept arbitrary user input.
66*b0d29bc4SBrooks Davis ///
67*b0d29bc4SBrooks Davis /// For simplicity reasons, we force the root of the tree to be a static inner
68*b0d29bc4SBrooks Davis /// node.  In other words, the root can never contain a value by itself and this
69*b0d29bc4SBrooks Davis /// is not a problem because the root is not addressable by the key space.
70*b0d29bc4SBrooks Davis /// Additionally, the root is strict so all of its direct children must be
71*b0d29bc4SBrooks Davis /// explicitly defined.
72*b0d29bc4SBrooks Davis ///
73*b0d29bc4SBrooks Davis /// This is, effectively, a simple wrapper around the node representing the
74*b0d29bc4SBrooks Davis /// root.  Having a separate class aids in clearly representing the concept of a
75*b0d29bc4SBrooks Davis /// tree and all of its public methods.  Also, the tree accepts dotted notations
76*b0d29bc4SBrooks Davis /// for the keys while the internal structures do not.
77*b0d29bc4SBrooks Davis ///
78*b0d29bc4SBrooks Davis /// Note that trees are shallow-copied unless a deep copy is requested with
79*b0d29bc4SBrooks Davis /// deep_copy().
80*b0d29bc4SBrooks Davis class tree {
81*b0d29bc4SBrooks Davis     /// Whether keys must be validated at "set" time.
82*b0d29bc4SBrooks Davis     bool _strict;
83*b0d29bc4SBrooks Davis 
84*b0d29bc4SBrooks Davis     /// The root of the tree.
85*b0d29bc4SBrooks Davis     std::shared_ptr< detail::static_inner_node > _root;
86*b0d29bc4SBrooks Davis 
87*b0d29bc4SBrooks Davis     tree(const bool, detail::static_inner_node*);
88*b0d29bc4SBrooks Davis 
89*b0d29bc4SBrooks Davis public:
90*b0d29bc4SBrooks Davis     tree(const bool = true);
91*b0d29bc4SBrooks Davis     ~tree(void);
92*b0d29bc4SBrooks Davis 
93*b0d29bc4SBrooks Davis     tree deep_copy(void) const;
94*b0d29bc4SBrooks Davis     tree combine(const tree&) const;
95*b0d29bc4SBrooks Davis 
96*b0d29bc4SBrooks Davis     template< class LeafType >
97*b0d29bc4SBrooks Davis     void define(const std::string&);
98*b0d29bc4SBrooks Davis 
99*b0d29bc4SBrooks Davis     void define_dynamic(const std::string&);
100*b0d29bc4SBrooks Davis 
101*b0d29bc4SBrooks Davis     bool is_set(const std::string&) const;
102*b0d29bc4SBrooks Davis 
103*b0d29bc4SBrooks Davis     template< class LeafType >
104*b0d29bc4SBrooks Davis     const typename LeafType::value_type& lookup(const std::string&) const;
105*b0d29bc4SBrooks Davis     template< class LeafType >
106*b0d29bc4SBrooks Davis     typename LeafType::value_type& lookup_rw(const std::string&);
107*b0d29bc4SBrooks Davis 
108*b0d29bc4SBrooks Davis     template< class LeafType >
109*b0d29bc4SBrooks Davis     void set(const std::string&, const typename LeafType::value_type&);
110*b0d29bc4SBrooks Davis 
111*b0d29bc4SBrooks Davis     void push_lua(const std::string&, lutok::state&) const;
112*b0d29bc4SBrooks Davis     void set_lua(const std::string&, lutok::state&, const int);
113*b0d29bc4SBrooks Davis 
114*b0d29bc4SBrooks Davis     std::string lookup_string(const std::string&) const;
115*b0d29bc4SBrooks Davis     void set_string(const std::string&, const std::string&);
116*b0d29bc4SBrooks Davis 
117*b0d29bc4SBrooks Davis     properties_map all_properties(const std::string& = "",
118*b0d29bc4SBrooks Davis                                   const bool = false) const;
119*b0d29bc4SBrooks Davis 
120*b0d29bc4SBrooks Davis     bool operator==(const tree&) const;
121*b0d29bc4SBrooks Davis     bool operator!=(const tree&) const;
122*b0d29bc4SBrooks Davis };
123*b0d29bc4SBrooks Davis 
124*b0d29bc4SBrooks Davis 
125*b0d29bc4SBrooks Davis }  // namespace config
126*b0d29bc4SBrooks Davis }  // namespace utils
127*b0d29bc4SBrooks Davis 
128*b0d29bc4SBrooks Davis #endif  // !defined(UTILS_CONFIG_TREE_HPP)
129