Path: blob/main/contrib/kyua/utils/config/parser.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/parser.hpp29/// Utilities to read a configuration file into memory.3031#if !defined(UTILS_CONFIG_PARSER_HPP)32#define UTILS_CONFIG_PARSER_HPP3334#include "utils/config/parser_fwd.hpp"3536#include <memory>3738#include "utils/config/tree_fwd.hpp"39#include "utils/fs/path_fwd.hpp"40#include "utils/noncopyable.hpp"4142namespace utils {43namespace config {444546/// A configuration parser.47///48/// This parser is a class rather than a function because we need to support49/// callbacks to perform the initialization of the config file schema. The50/// configuration files always start with a call to syntax(), which define the51/// particular version of the schema being used. Depending on such version, the52/// layout of the internal tree representation needs to be different.53///54/// A parser implementation must provide a setup() method to set up the55/// configuration schema based on the particular combination of syntax format56/// and version specified on the file.57///58/// Parser objects are not supposed to be reused, and specific trees are not59/// supposed to be passed to multiple parsers (even if sequentially). Doing so60/// will cause all kinds of inconsistencies in the managed tree itself or in the61/// Lua state.62class parser : noncopyable {63public:64struct impl;6566private:67/// Pointer to the internal implementation.68std::unique_ptr< impl > _pimpl;6970/// Hook to initialize the tree keys before reading the file.71///72/// This hook gets called when the configuration file defines its specific73/// format by calling the syntax() function. We have to delay the tree74/// initialization until this point because, before we know what version of75/// a configuration file we are parsing, we cannot know what keys are valid.76///77/// \param [in,out] config_tree The tree in which to define the key78/// structure.79/// \param syntax_version The version of the file format as specified in the80/// configuration file.81virtual void setup(tree& config_tree, const int syntax_version) = 0;8283public:84explicit parser(tree&);85virtual ~parser(void);8687void parse(const fs::path&);88};899091} // namespace config92} // namespace utils9394#endif // !defined(UTILS_CONFIG_PARSER_HPP)959697