Improve the process for GNU tools
[minix3.git] / external / bsd / kyua-cli / dist / utils / config / nodes.hpp
bloba94f99dc563edbcd143938f4970a40d91ef4df11
1 // Copyright 2012 Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 /// \file utils/config/nodes.hpp
30 /// Representation of tree nodes.
32 #if !defined(UTILS_CONFIG_NODES_HPP)
33 #define UTILS_CONFIG_NODES_HPP
35 #include <map>
36 #include <set>
37 #include <string>
39 #include <lutok/state.hpp>
41 #include "utils/config/keys.hpp"
42 #include "utils/noncopyable.hpp"
43 #include "utils/optional.hpp"
45 namespace utils {
46 namespace config {
49 /// Flat representation of all properties as strings.
50 typedef std::map< std::string, std::string > properties_map;
53 namespace detail {
56 /// Base representation of a node.
57 ///
58 /// This abstract class provides the base type for every node in the tree. Due
59 /// to the dynamic nature of our trees (each leaf being able to hold arbitrary
60 /// data types), this base type is a necessity.
61 class base_node : noncopyable {
62 public:
63 virtual ~base_node(void) = 0;
65 /// Copies the node.
66 ///
67 /// \return A dynamically-allocated node.
68 virtual base_node* deep_copy(void) const = 0;
72 class static_inner_node;
75 } // namespace detail
78 /// Abstract leaf node without any specified type.
79 ///
80 /// This base abstract type is necessary to have a common pointer type to which
81 /// to cast any leaf. We later provide templated derivates of this class, and
82 /// those cannot act in this manner.
83 ///
84 /// It is important to understand that a leaf can exist without actually holding
85 /// a value. Our trees are "strictly keyed": keys must have been pre-defined
86 /// before a value can be set on them. This is to ensure that the end user is
87 /// using valid key names and not making mistakes due to typos, for example. To
88 /// represent this condition, we define an "empty" key in the tree to denote
89 /// that the key is valid, yet it has not been set by the user. Only when an
90 /// explicit set is performed on the key, it gets a value.
91 class leaf_node : public detail::base_node {
92 public:
93 virtual ~leaf_node(void);
95 /// Checks whether the node has been set by the user.
96 ///
97 /// Nodes of the tree are predefined by the caller to specify the valid
98 /// types of the leaves. Such predefinition results in the creation of
99 /// nodes within the tree, but these nodes have not yet been set.
100 /// Traversing these nodes is invalid and should result in an "unknown key"
101 /// error.
103 /// \return True if a value has been set in the node.
104 virtual bool is_set(void) const = 0;
106 /// Pushes the node's value onto the Lua stack.
108 /// \param state The Lua state onto which to push the value.
109 virtual void push_lua(lutok::state& state) const = 0;
111 /// Sets the value of the node from an entry in the Lua stack.
113 /// \param state The Lua state from which to get the value.
114 /// \param value_index The stack index in which the value resides.
116 /// \throw value_error If the value in state(value_index) cannot be
117 /// processed by this node.
118 virtual void set_lua(lutok::state& state, const int value_index) = 0;
120 /// Sets the value of the node from a raw string representation.
122 /// \param raw_value The value to set the node to.
124 /// \throw value_error If the value is invalid.
125 virtual void set_string(const std::string& raw_value) = 0;
127 /// Converts the contents of the node to a string.
129 /// \pre The node must have a value.
131 /// \return A string representation of the value held by the node.
132 virtual std::string to_string(void) const = 0;
136 /// Base leaf node for a single arbitrary type.
138 /// This templated leaf node holds a single object of any type. The conversion
139 /// to/from string representations is undefined, as that depends on the
140 /// particular type being processed. You should reimplement this class for any
141 /// type that needs additional processing/validation during conversion.
142 template< typename ValueType >
143 class typed_leaf_node : public leaf_node {
144 public:
145 /// The type of the value held by this node.
146 typedef ValueType value_type;
148 typed_leaf_node(void);
150 bool is_set(void) const;
152 /// Gets the value stored in the node.
154 /// \todo Figure out why Doxygen is unable to pick up the documentation for
155 /// this function from the nodes.ipp file.
157 /// \pre The node must have a value.
159 /// \return The value in the node.
160 const value_type& value(void) const;
162 /// Gets the read-write value stored in the node.
164 /// \todo Figure out why Doxygen is unable to pick up the documentation for
165 /// this function from the nodes.ipp file.
167 /// \pre The node must have a value.
169 /// \return The value in the node.
170 value_type& value(void);
172 /// Sets the value of the node.
174 /// \todo Figure out why Doxygen is unable to pick up the documentation for
175 /// this function from the nodes.ipp file.
177 /// \param value_ The new value to set the node to.
178 void set(const value_type&);
180 protected:
181 /// The value held by this node.
182 optional< value_type > _value;
184 private:
185 virtual void validate(const value_type&) const;
189 /// Leaf node holding a native type.
191 /// This templated leaf node holds a native type. The conversion to/from string
192 /// representations of the value happens by means of iostreams.
193 template< typename ValueType >
194 class native_leaf_node : public typed_leaf_node< ValueType > {
195 public:
196 void set_string(const std::string&);
197 std::string to_string(void) const;
201 /// A leaf node that holds a boolean value.
202 class bool_node : public native_leaf_node< bool > {
203 public:
204 virtual base_node* deep_copy(void) const;
206 void push_lua(lutok::state&) const;
207 void set_lua(lutok::state&, const int);
211 /// A leaf node that holds an integer value.
212 class int_node : public native_leaf_node< int > {
213 public:
214 virtual base_node* deep_copy(void) const;
216 void push_lua(lutok::state&) const;
217 void set_lua(lutok::state&, const int);
221 /// A leaf node that holds a string value.
222 class string_node : public native_leaf_node< std::string > {
223 public:
224 virtual base_node* deep_copy(void) const;
226 void push_lua(lutok::state&) const;
227 void set_lua(lutok::state&, const int);
231 /// Base leaf node for a set of native types.
233 /// This is a base abstract class because there is no generic way to parse a
234 /// single word in the textual representation of the set to the native value.
235 template< typename ValueType >
236 class base_set_node : public leaf_node {
237 public:
238 /// The type of the value held by this node.
239 typedef std::set< ValueType > value_type;
241 base_set_node(void);
243 bool is_set(void) const;
245 /// Gets the value stored in the node.
247 /// \todo Figure out why Doxygen is unable to pick up the documentation for
248 /// this function from the nodes.ipp file.
250 /// \pre The node must have a value.
252 /// \return The value in the node.
253 const value_type& value(void) const;
255 /// Gets the read-write value stored in the node.
257 /// \todo Figure out why Doxygen is unable to pick up the documentation for
258 /// this function from the nodes.ipp file.
260 /// \pre The node must have a value.
262 /// \return The value in the node.
263 value_type& value(void);
265 /// Sets the value of the node.
267 /// \todo Figure out why Doxygen is unable to pick up the documentation for
268 /// this function from the nodes.ipp file.
270 /// \param value_ The new value to set the node to.
271 void set(const value_type&);
273 void set_string(const std::string&);
274 std::string to_string(void) const;
276 void push_lua(lutok::state&) const;
277 void set_lua(lutok::state&, const int);
279 protected:
280 /// The value held by this node.
281 optional< value_type > _value;
283 private:
284 /// Converts a single word to the native type.
286 /// \param raw_value The value to parse.
288 /// \return The parsed value.
290 /// \throw value_error If the value is invalid.
291 virtual ValueType parse_one(const std::string& raw_value) const = 0;
293 virtual void validate(const value_type&) const;
297 /// A leaf node that holds a set of strings.
298 class strings_set_node : public base_set_node< std::string > {
299 public:
300 virtual base_node* deep_copy(void) const;
302 private:
303 std::string parse_one(const std::string&) const;
307 } // namespace config
308 } // namespace utils
310 #endif // !defined(UTILS_CONFIG_NODES_HPP)