Path: blob/main/contrib/kyua/utils/config/tree.hpp
48081 views
// Copyright 2012 The Kyua Authors.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728/// \file utils/config/tree.hpp29/// Data type to represent a tree of arbitrary values with string keys.3031#if !defined(UTILS_CONFIG_TREE_HPP)32#define UTILS_CONFIG_TREE_HPP3334#include "utils/config/tree_fwd.hpp"3536#include <memory>37#include <string>3839#include <lutok/state.hpp>4041#include "utils/config/keys_fwd.hpp"42#include "utils/config/nodes_fwd.hpp"4344namespace utils {45namespace config {464748/// Representation of a tree.49///50/// The string keys of the tree are in dotted notation and actually represent51/// path traversals through the nodes.52///53/// Our trees are "strictly-keyed": keys must be defined as "existent" before54/// their values can be set. Defining a key is a separate action from setting55/// its value. The rationale is that we want to be able to control what keys56/// get defined: because trees are used to hold configuration, we want to catch57/// typos as early as possible. Also, users cannot set keys unless the types58/// are known in advance because our leaf nodes are strictly typed.59///60/// However, there is an exception to the strict keys: the inner nodes of the61/// tree can be static or dynamic. Static inner nodes have a known subset of62/// children and attempting to set keys not previously defined will result in an63/// error. Dynamic inner nodes do not have a predefined set of keys and can be64/// used to accept arbitrary user input.65///66/// For simplicity reasons, we force the root of the tree to be a static inner67/// node. In other words, the root can never contain a value by itself and this68/// is not a problem because the root is not addressable by the key space.69/// Additionally, the root is strict so all of its direct children must be70/// explicitly defined.71///72/// This is, effectively, a simple wrapper around the node representing the73/// root. Having a separate class aids in clearly representing the concept of a74/// tree and all of its public methods. Also, the tree accepts dotted notations75/// for the keys while the internal structures do not.76///77/// Note that trees are shallow-copied unless a deep copy is requested with78/// deep_copy().79class tree {80/// Whether keys must be validated at "set" time.81bool _strict;8283/// The root of the tree.84std::shared_ptr< detail::static_inner_node > _root;8586tree(const bool, detail::static_inner_node*);8788public:89tree(const bool = true);90~tree(void);9192tree deep_copy(void) const;93tree combine(const tree&) const;9495template< class LeafType >96void define(const std::string&);9798void define_dynamic(const std::string&);99100bool is_set(const std::string&) const;101102template< class LeafType >103const typename LeafType::value_type& lookup(const std::string&) const;104template< class LeafType >105typename LeafType::value_type& lookup_rw(const std::string&);106107template< class LeafType >108void set(const std::string&, const typename LeafType::value_type&);109110void push_lua(const std::string&, lutok::state&) const;111void set_lua(const std::string&, lutok::state&, const int);112113std::string lookup_string(const std::string&) const;114void set_string(const std::string&, const std::string&);115116properties_map all_properties(const std::string& = "",117const bool = false) const;118119bool operator==(const tree&) const;120bool operator!=(const tree&) const;121};122123124} // namespace config125} // namespace utils126127#endif // !defined(UTILS_CONFIG_TREE_HPP)128129130