1 // Copyright 2012 The Kyua Authors. 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. 16 // 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. 28 29 /// \file utils/config/nodes.hpp 30 /// Representation of tree nodes. 31 32 #if !defined(UTILS_CONFIG_NODES_HPP) 33 #define UTILS_CONFIG_NODES_HPP 34 35 #include "utils/config/nodes_fwd.hpp" 36 37 #include <set> 38 #include <string> 39 40 #include <lutok/state.hpp> 41 42 #include "utils/config/keys_fwd.hpp" 43 #include "utils/config/nodes_fwd.hpp" 44 #include "utils/noncopyable.hpp" 45 #include "utils/optional.hpp" 46 47 namespace utils { 48 namespace config { 49 50 51 namespace detail { 52 53 54 /// Base representation of a node. 55 /// 56 /// This abstract class provides the base type for every node in the tree. Due 57 /// to the dynamic nature of our trees (each leaf being able to hold arbitrary 58 /// data types), this base type is a necessity. 59 class base_node : noncopyable { 60 public: 61 virtual ~base_node(void) = 0; 62 63 /// Copies the node. 64 /// 65 /// \return A dynamically-allocated node. 66 virtual base_node* deep_copy(void) const = 0; 67 68 /// Combines this node with another one. 69 /// 70 /// \param key Key to this node. 71 /// \param other The node to combine with. 72 /// 73 /// \return A new node representing the combination. 74 /// 75 /// \throw bad_combination_error If the two nodes cannot be combined. 76 virtual base_node* combine(const tree_key& key, const base_node* other) 77 const = 0; 78 }; 79 80 81 } // namespace detail 82 83 84 /// Abstract leaf node without any specified type. 85 /// 86 /// This base abstract type is necessary to have a common pointer type to which 87 /// to cast any leaf. We later provide templated derivates of this class, and 88 /// those cannot act in this manner. 89 /// 90 /// It is important to understand that a leaf can exist without actually holding 91 /// a value. Our trees are "strictly keyed": keys must have been pre-defined 92 /// before a value can be set on them. This is to ensure that the end user is 93 /// using valid key names and not making mistakes due to typos, for example. To 94 /// represent this condition, we define an "empty" key in the tree to denote 95 /// that the key is valid, yet it has not been set by the user. Only when an 96 /// explicit set is performed on the key, it gets a value. 97 class leaf_node : public detail::base_node { 98 public: 99 virtual ~leaf_node(void); 100 101 /// Checks whether the node has been set by the user. 102 /// 103 /// Nodes of the tree are predefined by the caller to specify the valid 104 /// types of the leaves. Such predefinition results in the creation of 105 /// nodes within the tree, but these nodes have not yet been set. 106 /// Traversing these nodes is invalid and should result in an "unknown key" 107 /// error. 108 /// 109 /// \return True if a value has been set in the node. 110 virtual bool is_set(void) const = 0; 111 112 base_node* combine(const detail::tree_key&, const base_node*) const; 113 114 /// Pushes the node's value onto the Lua stack. 115 /// 116 /// \param state The Lua state onto which to push the value. 117 virtual void push_lua(lutok::state& state) const = 0; 118 119 /// Sets the value of the node from an entry in the Lua stack. 120 /// 121 /// \param state The Lua state from which to get the value. 122 /// \param value_index The stack index in which the value resides. 123 /// 124 /// \throw value_error If the value in state(value_index) cannot be 125 /// processed by this node. 126 virtual void set_lua(lutok::state& state, const int value_index) = 0; 127 128 /// Sets the value of the node from a raw string representation. 129 /// 130 /// \param raw_value The value to set the node to. 131 /// 132 /// \throw value_error If the value is invalid. 133 virtual void set_string(const std::string& raw_value) = 0; 134 135 /// Converts the contents of the node to a string. 136 /// 137 /// \pre The node must have a value. 138 /// 139 /// \return A string representation of the value held by the node. 140 virtual std::string to_string(void) const = 0; 141 }; 142 143 144 /// Base leaf node for a single arbitrary type. 145 /// 146 /// This templated leaf node holds a single object of any type. The conversion 147 /// to/from string representations is undefined, as that depends on the 148 /// particular type being processed. You should reimplement this class for any 149 /// type that needs additional processing/validation during conversion. 150 template< typename ValueType > 151 class typed_leaf_node : public leaf_node { 152 public: 153 /// The type of the value held by this node. 154 typedef ValueType value_type; 155 156 typed_leaf_node(void); 157 158 bool is_set(void) const; 159 160 /// Gets the value stored in the node. 161 /// 162 /// \todo Figure out why Doxygen is unable to pick up the documentation for 163 /// this function from the nodes.ipp file. 164 /// 165 /// \pre The node must have a value. 166 /// 167 /// \return The value in the node. 168 const value_type& value(void) const; 169 170 /// Gets the read-write value stored in the node. 171 /// 172 /// \todo Figure out why Doxygen is unable to pick up the documentation for 173 /// this function from the nodes.ipp file. 174 /// 175 /// \pre The node must have a value. 176 /// 177 /// \return The value in the node. 178 value_type& value(void); 179 180 /// Sets the value of the node. 181 /// 182 /// \todo Figure out why Doxygen is unable to pick up the documentation for 183 /// this function from the nodes.ipp file. 184 /// 185 /// \param value_ The new value to set the node to. 186 void set(const value_type&); 187 188 protected: 189 /// The value held by this node. 190 optional< value_type > _value; 191 192 private: 193 virtual void validate(const value_type&) const; 194 }; 195 196 197 /// Leaf node holding a native type. 198 /// 199 /// This templated leaf node holds a native type. The conversion to/from string 200 /// representations of the value happens by means of iostreams. 201 template< typename ValueType > 202 class native_leaf_node : public typed_leaf_node< ValueType > { 203 public: 204 void set_string(const std::string&); 205 std::string to_string(void) const; 206 }; 207 208 209 /// A leaf node that holds a boolean value. 210 class bool_node : public native_leaf_node< bool > { 211 public: 212 virtual base_node* deep_copy(void) const; 213 214 void push_lua(lutok::state&) const; 215 void set_lua(lutok::state&, const int); 216 }; 217 218 219 /// A leaf node that holds an integer value. 220 class int_node : public native_leaf_node< int > { 221 public: 222 virtual base_node* deep_copy(void) const; 223 224 void push_lua(lutok::state&) const; 225 void set_lua(lutok::state&, const int); 226 }; 227 228 229 /// A leaf node that holds a positive non-zero integer value. 230 class positive_int_node : public int_node { 231 virtual void validate(const value_type&) const; 232 }; 233 234 235 /// A leaf node that holds a string value. 236 class string_node : public native_leaf_node< std::string > { 237 public: 238 virtual base_node* deep_copy(void) const; 239 240 void push_lua(lutok::state&) const; 241 void set_lua(lutok::state&, const int); 242 }; 243 244 245 /// Base leaf node for a set of native types. 246 /// 247 /// This is a base abstract class because there is no generic way to parse a 248 /// single word in the textual representation of the set to the native value. 249 template< typename ValueType > 250 class base_set_node : public leaf_node { 251 public: 252 /// The type of the value held by this node. 253 typedef std::set< ValueType > value_type; 254 255 base_set_node(void); 256 257 bool is_set(void) const; 258 259 /// Gets the value stored in the node. 260 /// 261 /// \todo Figure out why Doxygen is unable to pick up the documentation for 262 /// this function from the nodes.ipp file. 263 /// 264 /// \pre The node must have a value. 265 /// 266 /// \return The value in the node. 267 const value_type& value(void) const; 268 269 /// Gets the read-write value stored in the node. 270 /// 271 /// \todo Figure out why Doxygen is unable to pick up the documentation for 272 /// this function from the nodes.ipp file. 273 /// 274 /// \pre The node must have a value. 275 /// 276 /// \return The value in the node. 277 value_type& value(void); 278 279 /// Sets the value of the node. 280 /// 281 /// \todo Figure out why Doxygen is unable to pick up the documentation for 282 /// this function from the nodes.ipp file. 283 /// 284 /// \param value_ The new value to set the node to. 285 void set(const value_type&); 286 287 void set_string(const std::string&); 288 std::string to_string(void) const; 289 290 void push_lua(lutok::state&) const; 291 void set_lua(lutok::state&, const int); 292 293 protected: 294 /// The value held by this node. 295 optional< value_type > _value; 296 297 private: 298 /// Converts a single word to the native type. 299 /// 300 /// \param raw_value The value to parse. 301 /// 302 /// \return The parsed value. 303 /// 304 /// \throw value_error If the value is invalid. 305 virtual ValueType parse_one(const std::string& raw_value) const = 0; 306 307 virtual void validate(const value_type&) const; 308 }; 309 310 311 /// A leaf node that holds a set of strings. 312 class strings_set_node : public base_set_node< std::string > { 313 public: 314 virtual base_node* deep_copy(void) const; 315 316 private: 317 std::string parse_one(const std::string&) const; 318 }; 319 320 321 } // namespace config 322 } // namespace utils 323 324 #endif // !defined(UTILS_CONFIG_NODES_HPP) 325