cpp-hocon 0.3.0
Loading...
Searching...
No Matches
config.hpp
1#pragma once
2
3#include "types.hpp"
4#include "config_mergeable.hpp"
5#include "config_origin.hpp"
6#include "config_object.hpp"
7#include "config_resolve_options.hpp"
8#include "config_value.hpp"
9#include "config_list.hpp"
10#include "config_exception.hpp"
11#include "path.hpp"
12#include <vector>
13#include <string>
14#include <set>
15#include "export.h"
16#include <leatherman/locale/locale.hpp>
17
18namespace hocon {
19
20 enum class time_unit { NANOSECONDS, MICROSECONDS, MILLISECONDS, SECONDS, MINUTES, HOURS, DAYS };
21
172 class LIBCPP_HOCON_EXPORT config : public config_mergeable, public std::enable_shared_from_this<config> {
173 friend class config_object;
174 friend class config_value;
175 friend class config_parseable;
176 friend class parseable;
177
178 public:
211 static shared_config parse_file_any_syntax(std::string file_basename, config_parse_options options);
212
221 static shared_config parse_file_any_syntax(std::string file_basename);
222
231 static shared_config parse_string(std::string s, config_parse_options options);
232
239 static shared_config parse_string(std::string s);
240
248 virtual shared_object root() const;
249
256 virtual shared_origin origin() const;
257
258 std::shared_ptr<const config_mergeable> with_fallback(std::shared_ptr<const config_mergeable> other) const override;
259
260 shared_value to_fallback_value() const override;
261
313 virtual shared_config resolve() const;
314
324 virtual shared_config resolve(config_resolve_options options) const;
325
338 virtual bool is_resolved() const;
339
360 virtual shared_config resolve_with(shared_config source) const;
361
373 virtual shared_config resolve_with(shared_config source, config_resolve_options options) const;
374
448 virtual void check_valid(shared_config reference, std::vector<std::string> restrict_to_paths) const;
449
471 virtual bool has_path(std::string const& path) const;
472
510 virtual bool has_path_or_null(std::string const& path) const;
511
518 virtual bool is_empty() const;
519
542 virtual std::set<std::pair<std::string, std::shared_ptr<const config_value>>> entry_set() const;
543
565 virtual bool get_is_null(std::string const& path) const;
566
567 virtual bool get_bool(std::string const& path) const;
568 virtual int get_int(std::string const& path) const;
569 virtual int64_t get_long(std::string const& path) const;
570 virtual double get_double(std::string const& path) const;
571 virtual std::string get_string(std::string const& path) const;
572 virtual std::shared_ptr<const config_object> get_object(std::string const& path) const;
573 virtual shared_config get_config(std::string const& path) const;
574 virtual unwrapped_value get_any_ref(std::string const& path) const;
575 virtual std::shared_ptr<const config_value> get_value(std::string const& path) const;
576
577 template<typename T>
578 std::vector<T> get_homogeneous_unwrapped_list(std::string const& path) const {
579 auto list = boost::get<std::vector<unwrapped_value>>(get_list(path)->unwrapped());
580 std::vector<T> T_list;
581 for (auto item : list) {
582 try {
583 T_list.push_back(boost::get<T>(item));
584 } catch (boost::bad_get &ex) {
585 throw config_exception(leatherman::locale::format("The list did not contain only the desired type."));
586 }
587 }
588 return T_list;
589 }
590
591 virtual shared_list get_list(std::string const& path) const;
592 virtual std::vector<bool> get_bool_list(std::string const& path) const;
593 virtual std::vector<int> get_int_list(std::string const& path) const;
594 virtual std::vector<int64_t> get_long_list(std::string const& path) const;
595 virtual std::vector<double> get_double_list(std::string const& path) const;
596 virtual std::vector<std::string> get_string_list(std::string const& path) const;
597 virtual std::vector<shared_object> get_object_list(std::string const& path) const;
598 virtual std::vector<shared_config> get_config_list(std::string const& path) const;
599
600 // TODO: memory parsing
601
610 virtual int64_t get_duration(std::string const& path, time_unit unit) const;
611
623 virtual shared_config with_only_path(std::string const& path) const;
624
635 virtual shared_config without_path(std::string const& path) const;
636
648 virtual shared_config at_path(std::string const& path) const;
649
660 virtual shared_config at_key(std::string const& key) const;
661
677 virtual shared_config with_value(std::string const& path, std::shared_ptr<const config_value> value) const;
678
679 bool operator==(config const& other) const;
680
681 config(shared_object object);
682
683 static shared_object env_variables_as_config_object();
684
685 protected:
686 shared_value find(std::string const& path_expression, config_value::type expected) const;
687 shared_value find(path path_expression, config_value::type expected, path original_path) const;
688 shared_value find(path path_expression, config_value::type expected) const;
689 shared_config at_key(shared_origin origin, std::string const& key) const;
690
691 static shared_includer default_includer();
692
693 // TODO: memory and duration parsing
694
695 private:
706 static duration parse_duration(std::string input, shared_origin origin_for_exception, std::string path_for_exception);
707
708 static duration convert(int64_t number, time_unit units);
709 static duration convert(double number, time_unit units);
710 static time_unit get_units(std::string const& unit_string);
711 duration get_duration(std::string const& path) const;
712
713 shared_value has_path_peek(std::string const& path_expression) const;
714 shared_value peek_path(path desired_path) const;
715
716 static void find_paths(std::set<std::pair<std::string, std::shared_ptr<const config_value>>>& entries,
717 path parent, shared_object obj);
718 static shared_value throw_if_null(shared_value v, config_value::type expected, path original_path);
719 static shared_value find_key(shared_object self, std::string const& key,
720 config_value::type expected, path original_path);
721 static shared_value find_key_or_null(shared_object self, std::string const& key,
722 config_value::type expected, path original_path);
723 static shared_value find_or_null(shared_object self, path desired_path,
724 config_value::type expected, path original_path);
725 shared_value find_or_null(std::string const& path_expression, config_value::type expected) const;
726 shared_value find_or_null(path path_expression, config_value::type expected, path original_path) const;
727
728 shared_object _object;
729 };
730
731 template<>
732 std::vector<int64_t> config::get_homogeneous_unwrapped_list(std::string const& path) const;
733
734} // namespace hocon
A set of options related to parsing.
An opaque handle to something that can be parsed, obtained from config_include_context.
A set of options related to resolving substitutions.
An immutable value, following the JSON type schema.
type
The type of a configuration value (following the JSON type schema).
An immutable map from config paths to config values.
Definition: config.hpp:172
virtual shared_config with_only_path(std::string const &path) const
Clone the config with only the given path (and its children) retained; all sibling paths are removed.
virtual shared_config with_value(std::string const &path, std::shared_ptr< const config_value > value) const
Returns a config based on this one, but with the given path set to the given value.
virtual bool is_resolved() const
Checks whether the config is completely resolved.
virtual shared_config at_key(std::string const &key) const
Places the config inside a config at the given key.
virtual bool get_is_null(std::string const &path) const
Checks whether a value is set to null at the given path, but throws an exception if the value is enti...
static shared_config parse_file_any_syntax(std::string file_basename, config_parse_options options)
Parses a file with a flexible extension.
std::shared_ptr< const config_mergeable > with_fallback(std::shared_ptr< const config_mergeable > other) const override
Returns a new value computed by merging this value with another, with keys in this value "winning" ov...
virtual shared_origin origin() const
Gets the origin of the Config, which may be a file, or a file with a line number, or just a descripti...
virtual bool is_empty() const
Returns true if the Config's root object contains no key-value pairs.
virtual shared_config without_path(std::string const &path) const
Clone the config with the given path removed.
shared_value to_fallback_value() const override
Converts a config to its root object and a config_value to itself.
virtual shared_config resolve_with(shared_config source, config_resolve_options options) const
Like config#resolve_with(config) but allows you to specify non-default options.
virtual int64_t get_duration(std::string const &path, time_unit unit) const
Gets a value as an integer number of the specified units.
virtual bool has_path_or_null(std::string const &path) const
Checks whether a value is present at the given path, even if the value is null.
virtual shared_object root() const
Gets the Config as a tree of ConfigObject.
virtual shared_config at_path(std::string const &path) const
Places the config inside another config at the given path.
virtual shared_config resolve(config_resolve_options options) const
Like config#resolve() but allows you to specify non-default options.
virtual bool has_path(std::string const &path) const
Checks whether a value is present and non-null at the given path.
static shared_config parse_string(std::string s, config_parse_options options)
Parses a string (which should be valid HOCON or JSON by default, or the syntax specified in the optio...
virtual void check_valid(shared_config reference, std::vector< std::string > restrict_to_paths) const
Validates this config against a reference config, throwing an exception if it is invalid.
static shared_config parse_string(std::string s)
Parses a string (which should be valid HOCON or JSON).
static shared_config parse_file_any_syntax(std::string file_basename)
Like parseFileAnySyntax(File,ConfigParseOptions) but always uses default parse options.
virtual shared_config resolve_with(shared_config source) const
Like config#resolve() except that substitution values are looked up in the given source,...
virtual std::set< std::pair< std::string, std::shared_ptr< const config_value > > > entry_set() const
Returns the set of path-value pairs, excluding any null values, found by recursing the root object.
virtual shared_config resolve() const
Returns a replacement config with all substitutions (the ${foo.bar} syntax, see the spec) resolved.
Factory for creating config_document instances.
Definition: config.hpp:18
std::pair< int64_t, int > duration
A duration represented as a 64-bit integer of seconds plus a 32-bit number of nanoseconds representin...
Definition: types.hpp:21
bool operator==(config_document const &lhs, config_document const &rhs)
Config documents compare via rendered strings.
All exceptions thrown by the library are subclasses of config_exception.