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.hpp"
31 #if !defined(UTILS_CONFIG_TREE_IPP)
32 #define UTILS_CONFIG_TREE_IPP
36 #include "utils/config/exceptions.hpp"
37 #include "utils/config/keys.hpp"
38 #include "utils/config/nodes.ipp"
39 #include "utils/format/macros.hpp"
40 #include "utils/sanity.hpp"
45 /// Registers a key as valid and having a specific type.
47 /// This method does not raise errors on invalid/unknown keys or other
48 /// tree-related issues. The reasons is that define() is a method that does not
49 /// depend on user input: it is intended to pre-populate the tree with a
50 /// specific structure, and that happens once at coding time.
52 /// \tparam LeafType The node type of the leaf we are defining.
53 /// \param dotted_key The key to be registered in dotted representation.
54 template< class LeafType >
56 config::tree::define(const std::string& dotted_key)
59 const detail::tree_key key = detail::parse_key(dotted_key);
60 _root->define(key, 0, detail::new_node< LeafType >);
61 } catch (const error& e) {
62 UNREACHABLE_MSG(F("define() failing due to key errors is a programming "
63 "mistake: %s") % e.what());
68 /// Gets a read-only reference to the value of a leaf addressed by its key.
70 /// \tparam LeafType The node type of the leaf we are querying.
71 /// \param dotted_key The key to be registered in dotted representation.
73 /// \return A reference to the value in the located leaf, if successful.
75 /// \throw invalid_key_error If the provided key has an invalid format.
76 /// \throw unknown_key_error If the provided key is unknown.
77 template< class LeafType >
78 const typename LeafType::value_type&
79 config::tree::lookup(const std::string& dotted_key) const
81 const detail::tree_key key = detail::parse_key(dotted_key);
82 const detail::base_node* raw_node = _root->lookup_ro(key, 0);
84 const LeafType& child = dynamic_cast< const LeafType& >(*raw_node);
88 throw unknown_key_error(key);
89 } catch (const std::bad_cast& unused_error) {
90 throw unknown_key_error(key);
95 /// Gets a read-write reference to the value of a leaf addressed by its key.
97 /// \tparam LeafType The node type of the leaf we are querying.
98 /// \param dotted_key The key to be registered in dotted representation.
100 /// \return A reference to the value in the located leaf, if successful.
102 /// \throw invalid_key_error If the provided key has an invalid format.
103 /// \throw unknown_key_error If the provided key is unknown.
104 template< class LeafType >
105 typename LeafType::value_type&
106 config::tree::lookup_rw(const std::string& dotted_key)
108 const detail::tree_key key = detail::parse_key(dotted_key);
109 detail::base_node* raw_node = _root->lookup_rw(
110 key, 0, detail::new_node< LeafType >);
112 LeafType& child = dynamic_cast< LeafType& >(*raw_node);
114 return child.value();
116 throw unknown_key_error(key);
117 } catch (const std::bad_cast& unused_error) {
118 throw unknown_key_error(key);
123 /// Sets the value of a leaf addressed by its key.
125 /// \tparam LeafType The node type of the leaf we are setting.
126 /// \param dotted_key The key to be registered in dotted representation.
127 /// \param value The value to set into the node.
129 /// \throw invalid_key_error If the provided key has an invalid format.
130 /// \throw invalid_key_value If the value mismatches the node type.
131 /// \throw unknown_key_error If the provided key is unknown.
132 template< class LeafType >
134 config::tree::set(const std::string& dotted_key,
135 const typename LeafType::value_type& value)
137 const detail::tree_key key = detail::parse_key(dotted_key);
139 leaf_node* raw_node = _root->lookup_rw(key, 0,
140 detail::new_node< LeafType >);
141 LeafType& child = dynamic_cast< LeafType& >(*raw_node);
143 } catch (const unknown_key_error& e) {
146 } catch (const value_error& e) {
147 throw invalid_key_value(key, e.what());
148 } catch (const std::bad_cast& unused_error) {
149 throw invalid_key_value(key, "Type mismatch");
156 #endif // !defined(UTILS_CONFIG_TREE_IPP)