1 // Copyright 2012 The Kyua Authors.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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 #include "utils/config/tree.ipp"
31 #include "utils/config/exceptions.hpp"
32 #include "utils/config/keys.hpp"
33 #include "utils/config/nodes.ipp"
34 #include "utils/format/macros.hpp"
36 namespace config = utils::config;
41 /// \param strict Whether keys must be validated at "set" time.
42 config::tree::tree(const bool strict) :
43 _strict(strict), _root(new detail::static_inner_node())
48 /// Constructor with a non-empty root.
50 /// \param strict Whether keys must be validated at "set" time.
51 /// \param root The root to the tree to be owned by this instance.
52 config::tree::tree(const bool strict, detail::static_inner_node* root) :
53 _strict(strict), _root(root)
59 config::tree::~tree(void)
64 /// Generates a deep copy of the input tree.
66 /// \return A new tree that is an exact copy of this tree.
68 config::tree::deep_copy(void) const
70 detail::static_inner_node* new_root =
71 dynamic_cast< detail::static_inner_node* >(_root->deep_copy());
72 return config::tree(_strict, new_root);
76 /// Combines two trees.
78 /// By combination we understand a new tree that contains the full key space of
79 /// the two input trees and, for the keys that match, respects the value of the
80 /// right-hand side (aka "other") tree.
82 /// Any nodes marked as dynamic "win" over non-dynamic nodes and the resulting
83 /// tree will have the dynamic property set on those.
85 /// \param overrides The tree to use as value overrides.
87 /// \return The combined tree.
89 /// \throw bad_combination_error If the two trees cannot be combined; for
90 /// example, if a single key represents an inner node in one tree but a leaf
91 /// node in the other one.
93 config::tree::combine(const tree& overrides) const
95 const detail::static_inner_node* other_root =
96 dynamic_cast< const detail::static_inner_node * >(
97 overrides._root.get());
99 detail::static_inner_node* new_root =
100 dynamic_cast< detail::static_inner_node* >(
101 _root->combine(detail::tree_key(), other_root));
102 return config::tree(_strict, new_root);
106 /// Registers a node as being dynamic.
108 /// This operation creates the given key as an inner node. Further set
109 /// operations that trespass this node will automatically create any missing
112 /// This method does not raise errors on invalid/unknown keys or other
113 /// tree-related issues. The reasons is that define() is a method that does not
114 /// depend on user input: it is intended to pre-populate the tree with a
115 /// specific structure, and that happens once at coding time.
117 /// \param dotted_key The key to be registered in dotted representation.
119 config::tree::define_dynamic(const std::string& dotted_key)
122 const detail::tree_key key = detail::parse_key(dotted_key);
123 _root->define(key, 0, detail::new_node< detail::dynamic_inner_node >);
124 } catch (const error& e) {
125 UNREACHABLE_MSG("define() failing due to key errors is a programming "
126 "mistake: " + std::string(e.what()));
131 /// Checks if a given node is set.
133 /// \param dotted_key The key to be checked.
135 /// \return True if the key is set to a specific value (not just defined).
136 /// False if the key is not set or if the key does not exist.
138 /// \throw invalid_key_error If the provided key has an invalid format.
140 config::tree::is_set(const std::string& dotted_key) const
142 const detail::tree_key key = detail::parse_key(dotted_key);
144 const detail::base_node* raw_node = _root->lookup_ro(key, 0);
146 const leaf_node& child = dynamic_cast< const leaf_node& >(
148 return child.is_set();
149 } catch (const std::bad_cast& unused_error) {
152 } catch (const unknown_key_error& unused_error) {
158 /// Pushes a leaf node's value onto the Lua stack.
160 /// \param dotted_key The key to be pushed.
161 /// \param state The Lua state into which to push the key's value.
163 /// \throw invalid_key_error If the provided key has an invalid format.
164 /// \throw unknown_key_error If the provided key is unknown.
166 config::tree::push_lua(const std::string& dotted_key, lutok::state& state) const
168 const detail::tree_key key = detail::parse_key(dotted_key);
169 const detail::base_node* raw_node = _root->lookup_ro(key, 0);
171 const leaf_node& child = dynamic_cast< const leaf_node& >(*raw_node);
172 child.push_lua(state);
173 } catch (const std::bad_cast& unused_error) {
174 throw unknown_key_error(key);
179 /// Sets a leaf node's value from a value in the Lua stack.
181 /// \param dotted_key The key to be set.
182 /// \param state The Lua state from which to retrieve the value.
183 /// \param value_index The position in the Lua stack holding the value.
185 /// \throw invalid_key_error If the provided key has an invalid format.
186 /// \throw invalid_key_value If the value mismatches the node type.
187 /// \throw unknown_key_error If the provided key is unknown.
189 config::tree::set_lua(const std::string& dotted_key, lutok::state& state,
190 const int value_index)
192 const detail::tree_key key = detail::parse_key(dotted_key);
194 detail::base_node* raw_node = _root->lookup_rw(
195 key, 0, detail::new_node< string_node >);
196 leaf_node& child = dynamic_cast< leaf_node& >(*raw_node);
197 child.set_lua(state, value_index);
198 } catch (const unknown_key_error& e) {
201 } catch (const value_error& e) {
202 throw invalid_key_value(key, e.what());
203 } catch (const std::bad_cast& unused_error) {
204 throw invalid_key_value(key, "Type mismatch");
209 /// Gets the value of a node as a plain string.
211 /// \param dotted_key The key to be looked up.
213 /// \return The value of the located node as a string.
215 /// \throw invalid_key_error If the provided key has an invalid format.
216 /// \throw unknown_key_error If the provided key is unknown.
218 config::tree::lookup_string(const std::string& dotted_key) const
220 const detail::tree_key key = detail::parse_key(dotted_key);
221 const detail::base_node* raw_node = _root->lookup_ro(key, 0);
223 const leaf_node& child = dynamic_cast< const leaf_node& >(*raw_node);
224 return child.to_string();
225 } catch (const std::bad_cast& unused_error) {
226 throw unknown_key_error(key);
231 /// Sets the value of a leaf addressed by its key from a string value.
233 /// This respects the native types of all the nodes that have been predefined.
234 /// For new nodes under a dynamic subtree, this has no mechanism of determining
235 /// what type they need to have, so they are created as plain string nodes.
237 /// \param dotted_key The key to be registered in dotted representation.
238 /// \param raw_value The string representation of the value to set the node to.
240 /// \throw invalid_key_error If the provided key has an invalid format.
241 /// \throw invalid_key_value If the value mismatches the node type.
242 /// \throw unknown_key_error If the provided key is unknown.
244 config::tree::set_string(const std::string& dotted_key,
245 const std::string& raw_value)
247 const detail::tree_key key = detail::parse_key(dotted_key);
249 detail::base_node* raw_node = _root->lookup_rw(
250 key, 0, detail::new_node< string_node >);
251 leaf_node& child = dynamic_cast< leaf_node& >(*raw_node);
252 child.set_string(raw_value);
253 } catch (const unknown_key_error& e) {
256 } catch (const value_error& e) {
257 throw invalid_key_value(key, e.what());
258 } catch (const std::bad_cast& unused_error) {
259 throw invalid_key_value(key, "Type mismatch");
264 /// Converts the tree to a collection of key/value string pairs.
266 /// \param dotted_key Subtree from which to start the export.
267 /// \param strip_key If true, remove the dotted_key prefix from the resulting
270 /// \return A map of keys to values in their textual representation.
272 /// \throw invalid_key_error If the provided key has an invalid format.
273 /// \throw unknown_key_error If the provided key is unknown.
274 /// \throw value_error If the provided key points to a leaf.
275 config::properties_map
276 config::tree::all_properties(const std::string& dotted_key,
277 const bool strip_key) const
279 PRE(!strip_key || !dotted_key.empty());
281 properties_map properties;
283 detail::tree_key key;
284 const detail::base_node* raw_node;
285 if (dotted_key.empty()) {
286 raw_node = _root.get();
288 key = detail::parse_key(dotted_key);
289 raw_node = _root->lookup_ro(key, 0);
292 const detail::inner_node& child =
293 dynamic_cast< const detail::inner_node& >(*raw_node);
294 child.all_properties(properties, key);
295 } catch (const std::bad_cast& unused_error) {
296 INV(!dotted_key.empty());
297 throw value_error(F("Cannot export properties from a leaf node; "
298 "'%s' given") % dotted_key);
302 properties_map stripped;
303 for (properties_map::const_iterator iter = properties.begin();
304 iter != properties.end(); ++iter) {
305 stripped[(*iter).first.substr(dotted_key.length() + 1)] =
308 properties = stripped;
315 /// Equality comparator.
317 /// \param other The other object to compare this one to.
319 /// \return True if this object and other are equal; false otherwise.
321 config::tree::operator==(const tree& other) const
323 // TODO(jmmv): Would be nicer to perform the comparison directly on the
324 // nodes, instead of exporting the values to strings first.
325 return _root == other._root || all_properties() == other.all_properties();
329 /// Inequality comparator.
331 /// \param other The other object to compare this one to.
333 /// \return True if this object and other are different; false otherwise.
335 config::tree::operator!=(const tree& other) const
337 return !(*this == other);