xref: /freebsd/contrib/kyua/utils/config/tree.ipp (revision 02e9120893770924227138ba49df1edb3896112a)
1// Copyright 2012 The Kyua Authors.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
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.
16//
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.
28
29#include "utils/config/tree.hpp"
30
31#if !defined(UTILS_CONFIG_TREE_IPP)
32#define UTILS_CONFIG_TREE_IPP
33
34#include <typeinfo>
35
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"
41
42namespace utils {
43
44
45/// Registers a key as valid and having a specific type.
46///
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.
51///
52/// \tparam LeafType The node type of the leaf we are defining.
53/// \param dotted_key The key to be registered in dotted representation.
54template< class LeafType >
55void
56config::tree::define(const std::string& dotted_key)
57{
58    try {
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());
64    }
65}
66
67
68/// Gets a read-only reference to the value of a leaf addressed by its key.
69///
70/// \tparam LeafType The node type of the leaf we are querying.
71/// \param dotted_key The key to be registered in dotted representation.
72///
73/// \return A reference to the value in the located leaf, if successful.
74///
75/// \throw invalid_key_error If the provided key has an invalid format.
76/// \throw unknown_key_error If the provided key is unknown.
77template< class LeafType >
78const typename LeafType::value_type&
79config::tree::lookup(const std::string& dotted_key) const
80{
81    const detail::tree_key key = detail::parse_key(dotted_key);
82    const detail::base_node* raw_node = _root->lookup_ro(key, 0);
83    try {
84        const LeafType& child = dynamic_cast< const LeafType& >(*raw_node);
85        if (child.is_set())
86            return child.value();
87        else
88            throw unknown_key_error(key);
89    } catch (const std::bad_cast& unused_error) {
90        throw unknown_key_error(key);
91    }
92}
93
94
95/// Gets a read-write reference to the value of a leaf addressed by its key.
96///
97/// \tparam LeafType The node type of the leaf we are querying.
98/// \param dotted_key The key to be registered in dotted representation.
99///
100/// \return A reference to the value in the located leaf, if successful.
101///
102/// \throw invalid_key_error If the provided key has an invalid format.
103/// \throw unknown_key_error If the provided key is unknown.
104template< class LeafType >
105typename LeafType::value_type&
106config::tree::lookup_rw(const std::string& dotted_key)
107{
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 >);
111    try {
112        LeafType& child = dynamic_cast< LeafType& >(*raw_node);
113        if (child.is_set())
114            return child.value();
115        else
116            throw unknown_key_error(key);
117    } catch (const std::bad_cast& unused_error) {
118        throw unknown_key_error(key);
119    }
120}
121
122
123/// Sets the value of a leaf addressed by its key.
124///
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.
128///
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.
132template< class LeafType >
133void
134config::tree::set(const std::string& dotted_key,
135                  const typename LeafType::value_type& value)
136{
137    const detail::tree_key key = detail::parse_key(dotted_key);
138    try {
139        leaf_node* raw_node = _root->lookup_rw(key, 0,
140                                               detail::new_node< LeafType >);
141        LeafType& child = dynamic_cast< LeafType& >(*raw_node);
142        child.set(value);
143    } catch (const unknown_key_error& e) {
144        if (_strict)
145            throw 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");
150    }
151}
152
153
154}  // namespace utils
155
156#endif  // !defined(UTILS_CONFIG_TREE_IPP)
157