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