cpp-hocon  0.3.0
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 
18 namespace 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 immutable map from config paths to config values.
Definition: config.hpp:172
All exceptions thrown by the library are subclasses of config_exception.
type
The type of a configuration value (following the JSON type schema).
Factory for creating config_document instances.
Definition: config.hpp:18
An opaque handle to something that can be parsed, obtained from config_include_context.
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
A set of options related to resolving substitutions.
bool operator==(config_document const &lhs, config_document const &rhs)
Config documents compare via rendered strings.
An immutable value, following the JSON type schema.