summaryrefslogtreecommitdiff
path: root/utils/config/nodes.hpp
blob: 6b766ff5d8f7bace42ad51cdd0fd13552815ad6d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Copyright 2012 The Kyua Authors.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
//   notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in the
//   documentation and/or other materials provided with the distribution.
// * Neither the name of Google Inc. nor the names of its contributors
//   may be used to endorse or promote products derived from this software
//   without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

/// \file utils/config/nodes.hpp
/// Representation of tree nodes.

#if !defined(UTILS_CONFIG_NODES_HPP)
#define UTILS_CONFIG_NODES_HPP

#include "utils/config/nodes_fwd.hpp"

#include <set>
#include <string>

#include <lutok/state.hpp>

#include "utils/config/keys_fwd.hpp"
#include "utils/config/nodes_fwd.hpp"
#include "utils/noncopyable.hpp"
#include "utils/optional.hpp"

namespace utils {
namespace config {


namespace detail {


/// Base representation of a node.
///
/// This abstract class provides the base type for every node in the tree.  Due
/// to the dynamic nature of our trees (each leaf being able to hold arbitrary
/// data types), this base type is a necessity.
class base_node : noncopyable {
public:
    virtual ~base_node(void) = 0;

    /// Copies the node.
    ///
    /// \return A dynamically-allocated node.
    virtual base_node* deep_copy(void) const = 0;

    /// Combines this node with another one.
    ///
    /// \param key Key to this node.
    /// \param other The node to combine with.
    ///
    /// \return A new node representing the combination.
    ///
    /// \throw bad_combination_error If the two nodes cannot be combined.
    virtual base_node* combine(const tree_key& key, const base_node* other)
        const = 0;
};


}  // namespace detail


/// Abstract leaf node without any specified type.
///
/// This base abstract type is necessary to have a common pointer type to which
/// to cast any leaf.  We later provide templated derivates of this class, and
/// those cannot act in this manner.
///
/// It is important to understand that a leaf can exist without actually holding
/// a value.  Our trees are "strictly keyed": keys must have been pre-defined
/// before a value can be set on them.  This is to ensure that the end user is
/// using valid key names and not making mistakes due to typos, for example.  To
/// represent this condition, we define an "empty" key in the tree to denote
/// that the key is valid, yet it has not been set by the user.  Only when an
/// explicit set is performed on the key, it gets a value.
class leaf_node : public detail::base_node {
public:
    virtual ~leaf_node(void);

    virtual bool is_set(void) const = 0;

    base_node* combine(const detail::tree_key&, const base_node*) const;

    virtual void push_lua(lutok::state&) const = 0;
    virtual void set_lua(lutok::state&, const int) = 0;

    virtual void set_string(const std::string&) = 0;
    virtual std::string to_string(void) const = 0;
};


/// Base leaf node for a single arbitrary type.
///
/// This templated leaf node holds a single object of any type.  The conversion
/// to/from string representations is undefined, as that depends on the
/// particular type being processed.  You should reimplement this class for any
/// type that needs additional processing/validation during conversion.
template< typename ValueType >
class typed_leaf_node : public leaf_node {
public:
    /// The type of the value held by this node.
    typedef ValueType value_type;

    /// Constructs a new leaf node that contains no value.
    typed_leaf_node(void);

    /// Checks whether the node has been set by the user.
    bool is_set(void) const;

    /// Gets the value stored in the node.
    const value_type& value(void) const;

    /// Gets the read-write value stored in the node.
    value_type& value(void);

    /// Sets the value of the node.
    void set(const value_type&);

protected:
    /// The value held by this node.
    optional< value_type > _value;

private:
    virtual void validate(const value_type&) const;
};


/// Leaf node holding a native type.
///
/// This templated leaf node holds a native type.  The conversion to/from string
/// representations of the value happens by means of iostreams.
template< typename ValueType >
class native_leaf_node : public typed_leaf_node< ValueType > {
public:
    void set_string(const std::string&);
    std::string to_string(void) const;
};


/// A leaf node that holds a boolean value.
class bool_node : public native_leaf_node< bool > {
public:
    virtual base_node* deep_copy(void) const;

    void push_lua(lutok::state&) const;
    void set_lua(lutok::state&, const int);
};


/// A leaf node that holds an integer value.
class int_node : public native_leaf_node< int > {
public:
    virtual base_node* deep_copy(void) const;

    void push_lua(lutok::state&) const;
    void set_lua(lutok::state&, const int);
};


/// A leaf node that holds a positive non-zero integer value.
class positive_int_node : public int_node {
    virtual void validate(const value_type&) const;
};


/// A leaf node that holds a string value.
class string_node : public native_leaf_node< std::string > {
public:
    virtual base_node* deep_copy(void) const;

    void push_lua(lutok::state&) const;
    void set_lua(lutok::state&, const int);
};


/// Base leaf node for a set of native types.
///
/// This is a base abstract class because there is no generic way to parse a
/// single word in the textual representation of the set to the native value.
template< typename ValueType >
class base_set_node : public leaf_node {
public:
    /// The type of the value held by this node.
    typedef std::set< ValueType > value_type;

    base_set_node(void);

    /// Checks whether the node has been set by the user.
    ///
    /// \return True if a value has been set in the node.
    bool is_set(void) const;

    /// Gets the value stored in the node.
    ///
    /// \pre The node must have a value.
    ///
    /// \return The value in the node.
    const value_type& value(void) const;

    /// Gets the read-write value stored in the node.
    ///
    /// \pre The node must have a value.
    ///
    /// \return The value in the node.
    value_type& value(void);

    /// Sets the value of the node.
    void set(const value_type&);

    /// Sets the value of the node from a raw string representation.
    void set_string(const std::string&);

    /// Converts the contents of the node to a string.
    std::string to_string(void) const;

    /// Pushes the node's value onto the Lua stack.
    void push_lua(lutok::state&) const;

    /// Sets the value of the node from an entry in the Lua stack.
    void set_lua(lutok::state&, const int);

protected:
    /// The value held by this node.
    optional< value_type > _value;

private:
    /// Converts a single word to the native type.
    ///
    /// \return The parsed value.
    ///
    /// \throw value_error If the value is invalid.
    virtual ValueType parse_one(const std::string&) const = 0;

    virtual void validate(const value_type&) const;
};


/// A leaf node that holds a set of strings.
class strings_set_node : public base_set_node< std::string > {
public:
    virtual base_node* deep_copy(void) const;

private:
    std::string parse_one(const std::string&) const;
};


}  // namespace config
}  // namespace utils

#endif  // !defined(UTILS_CONFIG_NODES_HPP)