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