xref: /freebsd/contrib/kyua/utils/config/tree.ipp (revision b0d29bc47dba79f6f38e67eabadfb4b32ffd9390)
1*b0d29bc4SBrooks Davis// Copyright 2012 The Kyua Authors.
2*b0d29bc4SBrooks Davis// All rights reserved.
3*b0d29bc4SBrooks Davis//
4*b0d29bc4SBrooks Davis// Redistribution and use in source and binary forms, with or without
5*b0d29bc4SBrooks Davis// modification, are permitted provided that the following conditions are
6*b0d29bc4SBrooks Davis// met:
7*b0d29bc4SBrooks Davis//
8*b0d29bc4SBrooks Davis// * Redistributions of source code must retain the above copyright
9*b0d29bc4SBrooks Davis//   notice, this list of conditions and the following disclaimer.
10*b0d29bc4SBrooks Davis// * Redistributions in binary form must reproduce the above copyright
11*b0d29bc4SBrooks Davis//   notice, this list of conditions and the following disclaimer in the
12*b0d29bc4SBrooks Davis//   documentation and/or other materials provided with the distribution.
13*b0d29bc4SBrooks Davis// * Neither the name of Google Inc. nor the names of its contributors
14*b0d29bc4SBrooks Davis//   may be used to endorse or promote products derived from this software
15*b0d29bc4SBrooks Davis//   without specific prior written permission.
16*b0d29bc4SBrooks Davis//
17*b0d29bc4SBrooks Davis// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*b0d29bc4SBrooks Davis// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*b0d29bc4SBrooks Davis// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*b0d29bc4SBrooks Davis// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*b0d29bc4SBrooks Davis// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*b0d29bc4SBrooks Davis// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*b0d29bc4SBrooks Davis// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*b0d29bc4SBrooks Davis// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*b0d29bc4SBrooks Davis// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*b0d29bc4SBrooks Davis// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*b0d29bc4SBrooks Davis// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*b0d29bc4SBrooks Davis
29*b0d29bc4SBrooks Davis#include "utils/config/tree.hpp"
30*b0d29bc4SBrooks Davis
31*b0d29bc4SBrooks Davis#if !defined(UTILS_CONFIG_TREE_IPP)
32*b0d29bc4SBrooks Davis#define UTILS_CONFIG_TREE_IPP
33*b0d29bc4SBrooks Davis
34*b0d29bc4SBrooks Davis#include <typeinfo>
35*b0d29bc4SBrooks Davis
36*b0d29bc4SBrooks Davis#include "utils/config/exceptions.hpp"
37*b0d29bc4SBrooks Davis#include "utils/config/keys.hpp"
38*b0d29bc4SBrooks Davis#include "utils/config/nodes.ipp"
39*b0d29bc4SBrooks Davis#include "utils/format/macros.hpp"
40*b0d29bc4SBrooks Davis#include "utils/sanity.hpp"
41*b0d29bc4SBrooks Davis
42*b0d29bc4SBrooks Davisnamespace utils {
43*b0d29bc4SBrooks Davis
44*b0d29bc4SBrooks Davis
45*b0d29bc4SBrooks Davis/// Registers a key as valid and having a specific type.
46*b0d29bc4SBrooks Davis///
47*b0d29bc4SBrooks Davis/// This method does not raise errors on invalid/unknown keys or other
48*b0d29bc4SBrooks Davis/// tree-related issues.  The reasons is that define() is a method that does not
49*b0d29bc4SBrooks Davis/// depend on user input: it is intended to pre-populate the tree with a
50*b0d29bc4SBrooks Davis/// specific structure, and that happens once at coding time.
51*b0d29bc4SBrooks Davis///
52*b0d29bc4SBrooks Davis/// \tparam LeafType The node type of the leaf we are defining.
53*b0d29bc4SBrooks Davis/// \param dotted_key The key to be registered in dotted representation.
54*b0d29bc4SBrooks Davistemplate< class LeafType >
55*b0d29bc4SBrooks Davisvoid
56*b0d29bc4SBrooks Davisconfig::tree::define(const std::string& dotted_key)
57*b0d29bc4SBrooks Davis{
58*b0d29bc4SBrooks Davis    try {
59*b0d29bc4SBrooks Davis        const detail::tree_key key = detail::parse_key(dotted_key);
60*b0d29bc4SBrooks Davis        _root->define(key, 0, detail::new_node< LeafType >);
61*b0d29bc4SBrooks Davis    } catch (const error& e) {
62*b0d29bc4SBrooks Davis        UNREACHABLE_MSG(F("define() failing due to key errors is a programming "
63*b0d29bc4SBrooks Davis                          "mistake: %s") % e.what());
64*b0d29bc4SBrooks Davis    }
65*b0d29bc4SBrooks Davis}
66*b0d29bc4SBrooks Davis
67*b0d29bc4SBrooks Davis
68*b0d29bc4SBrooks Davis/// Gets a read-only reference to the value of a leaf addressed by its key.
69*b0d29bc4SBrooks Davis///
70*b0d29bc4SBrooks Davis/// \tparam LeafType The node type of the leaf we are querying.
71*b0d29bc4SBrooks Davis/// \param dotted_key The key to be registered in dotted representation.
72*b0d29bc4SBrooks Davis///
73*b0d29bc4SBrooks Davis/// \return A reference to the value in the located leaf, if successful.
74*b0d29bc4SBrooks Davis///
75*b0d29bc4SBrooks Davis/// \throw invalid_key_error If the provided key has an invalid format.
76*b0d29bc4SBrooks Davis/// \throw unknown_key_error If the provided key is unknown.
77*b0d29bc4SBrooks Davistemplate< class LeafType >
78*b0d29bc4SBrooks Davisconst typename LeafType::value_type&
79*b0d29bc4SBrooks Davisconfig::tree::lookup(const std::string& dotted_key) const
80*b0d29bc4SBrooks Davis{
81*b0d29bc4SBrooks Davis    const detail::tree_key key = detail::parse_key(dotted_key);
82*b0d29bc4SBrooks Davis    const detail::base_node* raw_node = _root->lookup_ro(key, 0);
83*b0d29bc4SBrooks Davis    try {
84*b0d29bc4SBrooks Davis        const LeafType& child = dynamic_cast< const LeafType& >(*raw_node);
85*b0d29bc4SBrooks Davis        if (child.is_set())
86*b0d29bc4SBrooks Davis            return child.value();
87*b0d29bc4SBrooks Davis        else
88*b0d29bc4SBrooks Davis            throw unknown_key_error(key);
89*b0d29bc4SBrooks Davis    } catch (const std::bad_cast& unused_error) {
90*b0d29bc4SBrooks Davis        throw unknown_key_error(key);
91*b0d29bc4SBrooks Davis    }
92*b0d29bc4SBrooks Davis}
93*b0d29bc4SBrooks Davis
94*b0d29bc4SBrooks Davis
95*b0d29bc4SBrooks Davis/// Gets a read-write reference to the value of a leaf addressed by its key.
96*b0d29bc4SBrooks Davis///
97*b0d29bc4SBrooks Davis/// \tparam LeafType The node type of the leaf we are querying.
98*b0d29bc4SBrooks Davis/// \param dotted_key The key to be registered in dotted representation.
99*b0d29bc4SBrooks Davis///
100*b0d29bc4SBrooks Davis/// \return A reference to the value in the located leaf, if successful.
101*b0d29bc4SBrooks Davis///
102*b0d29bc4SBrooks Davis/// \throw invalid_key_error If the provided key has an invalid format.
103*b0d29bc4SBrooks Davis/// \throw unknown_key_error If the provided key is unknown.
104*b0d29bc4SBrooks Davistemplate< class LeafType >
105*b0d29bc4SBrooks Davistypename LeafType::value_type&
106*b0d29bc4SBrooks Davisconfig::tree::lookup_rw(const std::string& dotted_key)
107*b0d29bc4SBrooks Davis{
108*b0d29bc4SBrooks Davis    const detail::tree_key key = detail::parse_key(dotted_key);
109*b0d29bc4SBrooks Davis    detail::base_node* raw_node = _root->lookup_rw(
110*b0d29bc4SBrooks Davis        key, 0, detail::new_node< LeafType >);
111*b0d29bc4SBrooks Davis    try {
112*b0d29bc4SBrooks Davis        LeafType& child = dynamic_cast< LeafType& >(*raw_node);
113*b0d29bc4SBrooks Davis        if (child.is_set())
114*b0d29bc4SBrooks Davis            return child.value();
115*b0d29bc4SBrooks Davis        else
116*b0d29bc4SBrooks Davis            throw unknown_key_error(key);
117*b0d29bc4SBrooks Davis    } catch (const std::bad_cast& unused_error) {
118*b0d29bc4SBrooks Davis        throw unknown_key_error(key);
119*b0d29bc4SBrooks Davis    }
120*b0d29bc4SBrooks Davis}
121*b0d29bc4SBrooks Davis
122*b0d29bc4SBrooks Davis
123*b0d29bc4SBrooks Davis/// Sets the value of a leaf addressed by its key.
124*b0d29bc4SBrooks Davis///
125*b0d29bc4SBrooks Davis/// \tparam LeafType The node type of the leaf we are setting.
126*b0d29bc4SBrooks Davis/// \param dotted_key The key to be registered in dotted representation.
127*b0d29bc4SBrooks Davis/// \param value The value to set into the node.
128*b0d29bc4SBrooks Davis///
129*b0d29bc4SBrooks Davis/// \throw invalid_key_error If the provided key has an invalid format.
130*b0d29bc4SBrooks Davis/// \throw invalid_key_value If the value mismatches the node type.
131*b0d29bc4SBrooks Davis/// \throw unknown_key_error If the provided key is unknown.
132*b0d29bc4SBrooks Davistemplate< class LeafType >
133*b0d29bc4SBrooks Davisvoid
134*b0d29bc4SBrooks Davisconfig::tree::set(const std::string& dotted_key,
135*b0d29bc4SBrooks Davis                  const typename LeafType::value_type& value)
136*b0d29bc4SBrooks Davis{
137*b0d29bc4SBrooks Davis    const detail::tree_key key = detail::parse_key(dotted_key);
138*b0d29bc4SBrooks Davis    try {
139*b0d29bc4SBrooks Davis        leaf_node* raw_node = _root->lookup_rw(key, 0,
140*b0d29bc4SBrooks Davis                                               detail::new_node< LeafType >);
141*b0d29bc4SBrooks Davis        LeafType& child = dynamic_cast< LeafType& >(*raw_node);
142*b0d29bc4SBrooks Davis        child.set(value);
143*b0d29bc4SBrooks Davis    } catch (const unknown_key_error& e) {
144*b0d29bc4SBrooks Davis        if (_strict)
145*b0d29bc4SBrooks Davis            throw e;
146*b0d29bc4SBrooks Davis    } catch (const value_error& e) {
147*b0d29bc4SBrooks Davis        throw invalid_key_value(key, e.what());
148*b0d29bc4SBrooks Davis    } catch (const std::bad_cast& unused_error) {
149*b0d29bc4SBrooks Davis        throw invalid_key_value(key, "Type mismatch");
150*b0d29bc4SBrooks Davis    }
151*b0d29bc4SBrooks Davis}
152*b0d29bc4SBrooks Davis
153*b0d29bc4SBrooks Davis
154*b0d29bc4SBrooks Davis}  // namespace utils
155*b0d29bc4SBrooks Davis
156*b0d29bc4SBrooks Davis#endif  // !defined(UTILS_CONFIG_TREE_IPP)
157