1 // Copyright 2012 Google Inc.
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.hpp"
31 #if !defined(UTILS_CONFIG_TREE_IPP)
32 #define UTILS_CONFIG_TREE_IPP
36 #include "utils/config/exceptions.hpp"
37 #include "utils/config/nodes.ipp"
38 #include "utils/format/macros.hpp"
39 #include "utils/sanity.hpp"
44 /// Registers a key as valid and having a specific type.
46 /// This method does not raise errors on invalid/unknown keys or other
47 /// tree-related issues. The reasons is that define() is a method that does not
48 /// depend on user input: it is intended to pre-populate the tree with a
49 /// specific structure, and that happens once at coding time.
51 /// \tparam LeafType The node type of the leaf we are defining.
52 /// \param dotted_key The key to be registered in dotted representation.
53 template< class LeafType >
55 config::tree::define(const std::string& dotted_key)
58 const detail::tree_key key = detail::parse_key(dotted_key);
59 _root->define(key, 0, detail::new_node< LeafType >);
60 } catch (const error& e) {
61 UNREACHABLE_MSG(F("define() failing due to key errors is a programming "
62 "mistake: %s") % e.what());
67 /// Gets a read-only reference to the value of a leaf addressed by its key.
69 /// \tparam LeafType The node type of the leaf we are querying.
70 /// \param dotted_key The key to be registered in dotted representation.
72 /// \return A reference to the value in the located leaf, if successful.
74 /// \throw invalid_key_error If the provided key has an invalid format.
75 /// \throw unknown_key_error If the provided key is unknown.
76 template< class LeafType >
77 const typename LeafType::value_type&
78 config::tree::lookup(const std::string& dotted_key) const
80 const detail::tree_key key = detail::parse_key(dotted_key);
81 const detail::base_node* raw_node = _root->lookup_ro(key, 0);
83 const LeafType& child = dynamic_cast< const LeafType& >(*raw_node);
87 throw unknown_key_error(key);
88 } catch (const std::bad_cast& unused_error) {
89 throw unknown_key_error(key);
94 /// Gets a read-write reference to the value of a leaf addressed by its key.
96 /// \tparam LeafType The node type of the leaf we are querying.
97 /// \param dotted_key The key to be registered in dotted representation.
99 /// \return A reference to the value in the located leaf, if successful.
101 /// \throw invalid_key_error If the provided key has an invalid format.
102 /// \throw unknown_key_error If the provided key is unknown.
103 template< class LeafType >
104 typename LeafType::value_type&
105 config::tree::lookup_rw(const std::string& dotted_key)
107 const detail::tree_key key = detail::parse_key(dotted_key);
108 detail::base_node* raw_node = _root->lookup_rw(
109 key, 0, detail::new_node< LeafType >);
111 LeafType& child = dynamic_cast< LeafType& >(*raw_node);
113 return child.value();
115 throw unknown_key_error(key);
116 } catch (const std::bad_cast& unused_error) {
117 throw unknown_key_error(key);
122 /// Sets the value of a leaf addressed by its key.
124 /// \tparam LeafType The node type of the leaf we are setting.
125 /// \param dotted_key The key to be registered in dotted representation.
126 /// \param value The value to set into the node.
128 /// \throw invalid_key_error If the provided key has an invalid format.
129 /// \throw unknown_key_error If the provided key is unknown.
130 /// \throw value_error If the value mismatches the node type.
131 template< class LeafType >
133 config::tree::set(const std::string& dotted_key,
134 const typename LeafType::value_type& value)
136 const detail::tree_key key = detail::parse_key(dotted_key);
137 leaf_node* raw_node = _root->lookup_rw(key, 0,
138 detail::new_node< LeafType >);
140 LeafType& child = dynamic_cast< LeafType& >(*raw_node);
142 } catch (const std::bad_cast& unused_error) {
143 throw value_error(F("Invalid value for key '%s'") %
144 detail::flatten_key(key));
151 #endif // !defined(UTILS_CONFIG_TREE_IPP)