1// Copyright 2012 Google Inc. 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/nodes.ipp" 38#include "utils/format/macros.hpp" 39#include "utils/sanity.hpp" 40 41namespace utils { 42 43 44/// Registers a key as valid and having a specific type. 45/// 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. 50/// 51/// \tparam LeafType The node type of the leaf we are defining. 52/// \param dotted_key The key to be registered in dotted representation. 53template< class LeafType > 54void 55config::tree::define(const std::string& dotted_key) 56{ 57 try { 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()); 63 } 64} 65 66 67/// Gets a read-only reference to the value of a leaf addressed by its key. 68/// 69/// \tparam LeafType The node type of the leaf we are querying. 70/// \param dotted_key The key to be registered in dotted representation. 71/// 72/// \return A reference to the value in the located leaf, if successful. 73/// 74/// \throw invalid_key_error If the provided key has an invalid format. 75/// \throw unknown_key_error If the provided key is unknown. 76template< class LeafType > 77const typename LeafType::value_type& 78config::tree::lookup(const std::string& dotted_key) const 79{ 80 const detail::tree_key key = detail::parse_key(dotted_key); 81 const detail::base_node* raw_node = _root->lookup_ro(key, 0); 82 try { 83 const LeafType& child = dynamic_cast< const LeafType& >(*raw_node); 84 if (child.is_set()) 85 return child.value(); 86 else 87 throw unknown_key_error(key); 88 } catch (const std::bad_cast& unused_error) { 89 throw unknown_key_error(key); 90 } 91} 92 93 94/// Gets a read-write reference to the value of a leaf addressed by its key. 95/// 96/// \tparam LeafType The node type of the leaf we are querying. 97/// \param dotted_key The key to be registered in dotted representation. 98/// 99/// \return A reference to the value in the located leaf, if successful. 100/// 101/// \throw invalid_key_error If the provided key has an invalid format. 102/// \throw unknown_key_error If the provided key is unknown. 103template< class LeafType > 104typename LeafType::value_type& 105config::tree::lookup_rw(const std::string& dotted_key) 106{ 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 >); 110 try { 111 LeafType& child = dynamic_cast< LeafType& >(*raw_node); 112 if (child.is_set()) 113 return child.value(); 114 else 115 throw unknown_key_error(key); 116 } catch (const std::bad_cast& unused_error) { 117 throw unknown_key_error(key); 118 } 119} 120 121 122/// Sets the value of a leaf addressed by its key. 123/// 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. 127/// 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. 131template< class LeafType > 132void 133config::tree::set(const std::string& dotted_key, 134 const typename LeafType::value_type& value) 135{ 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 >); 139 try { 140 LeafType& child = dynamic_cast< LeafType& >(*raw_node); 141 child.set(value); 142 } catch (const std::bad_cast& unused_error) { 143 throw value_error(F("Invalid value for key '%s'") % 144 detail::flatten_key(key)); 145 } 146} 147 148 149} // namespace utils 150 151#endif // !defined(UTILS_CONFIG_TREE_IPP) 152