cpp-hocon  0.3.0
config_value.hpp
1 
2 #pragma once
3 
4 #include "config_origin.hpp"
5 #include "config_render_options.hpp"
6 #include "config_mergeable.hpp"
7 #include "path.hpp"
8 #include <string>
9 #include <vector>
10 #include "export.h"
11 #include <leatherman/locale/locale.hpp>
12 
13 namespace hocon {
14 
15  class unmergeable;
16  class resolve_source;
17  class resolve_context;
18 
19  template<typename T>
21 
22  enum class resolve_status { RESOLVED, UNRESOLVED };
23 
39  class LIBCPP_HOCON_EXPORT config_value : public config_mergeable, public std::enable_shared_from_this<config_value> {
40  friend class token;
41  friend class value;
42  friend class default_transformer;
43  friend class config;
44  friend class config_object;
45  friend class simple_config_object;
46  friend class simple_config_list;
47  friend class config_concatenation;
48  friend class resolve_context;
49  friend class config_delayed_merge;
50  friend class config_delayed_merge_object;
51  public:
56  enum class type {
57  OBJECT, LIST, NUMBER, BOOLEAN, CONFIG_NULL, STRING, UNSPECIFIED
58  };
59  static char const* type_name(type t) {
60  switch (t) {
61  case type::OBJECT: return "object";
62  case type::LIST: return "list";
63  case type::NUMBER: return "number";
64  case type::BOOLEAN: return "boolean";
65  case type::CONFIG_NULL: return "null";
66  case type::STRING: return "string";
67  case type::UNSPECIFIED: return "unspecified";
68  default: throw std::logic_error(leatherman::locale::format("Got impossible value for type enum"));
69  }
70  }
71 
78  virtual shared_origin const& origin() const;
79 
85  virtual type value_type() const = 0;
86 
92  char const* value_type_name() const {
93  return type_name(value_type());
94  }
95 
96  virtual unwrapped_value unwrapped() const = 0;
97 
115  virtual std::string render() const;
116 
136  virtual std::string render(config_render_options options) const;
137 
146  shared_config at_key(std::string const& key) const;
147 
157  shared_config at_path(std::string const& path_expression) const;
158 
167  virtual shared_value with_origin(shared_origin origin) const;
168 
181  virtual shared_value relativized(std::string prefix) const { return shared_from_this(); }
182 
183  virtual resolve_status get_resolve_status() const;
184 
185  friend resolve_status resolve_status_from_values(std::vector<shared_value> const& v);
186 
187  std::shared_ptr<const config_mergeable> with_fallback(std::shared_ptr<const config_mergeable> other) const override;
188 
189  virtual bool operator==(config_value const& other) const = 0;
190 
191  virtual std::string transform_to_string() const;
192 
193  protected:
194  config_value(shared_origin origin);
195 
196  virtual void render(std::string& result, int indent, bool at_root, std::string const& at_key, config_render_options options) const;
197  virtual void render(std::string& result, int indent, bool at_root, config_render_options options) const;
198  static void indent(std::string& result, int indent, config_render_options const& options);
199 
200  shared_config at_key(shared_origin origin, std::string const& key) const;
201  shared_config at_path(shared_origin origin, path raw_path) const;
202 
203  virtual shared_value new_copy(shared_origin origin) const = 0;
204 
206  resolve_substitutions(resolve_context const& context, resolve_source const& source) const;
207 
208  static std::vector<shared_value> replace_child_in_list(std::vector<shared_value> const& values,
209  shared_value const& child, shared_value replacement);
210  static bool has_descendant_in_list(std::vector<shared_value> const& values, shared_value const& descendant);
211 
212  class modifier {
213  public:
214  virtual shared_value modify_child_may_throw(std::string const& key_or_null, shared_value v) = 0;
215  };
216 
218  public:
219  no_exceptions_modifier(std::string prefix);
220 
221  shared_value modify_child_may_throw(std::string const &key_or_null, shared_value v) override;
222  shared_value modify_child(std::string const& key, shared_value v) const;
223  private:
224  std::string _prefix;
225  };
226  void require_not_ignoring_fallbacks() const;
227 
228  /* this is virtualized rather than a field because only some subclasses
229  * really need to store the boolean, and they may be able to pack it
230  * with another boolean to save space.
231  */
232  virtual bool ignores_fallbacks() const;
233  virtual shared_value with_fallbacks_ignored() const;
234 
235  shared_value merged_with_the_unmergeable(std::vector<shared_value> stack,
236  std::shared_ptr<const unmergeable> fallback) const;
237  shared_value merged_with_the_unmergeable(std::shared_ptr<const unmergeable> fallback) const;
238 
239  shared_value merged_with_object(std::vector<shared_value> stack, shared_object fallback) const;
240  virtual shared_value merged_with_object(shared_object fallback) const;
241 
242  shared_value merged_with_non_object(std::vector<shared_value> stack, shared_value fallback) const;
243  shared_value merged_with_non_object(shared_value fallback) const;
244 
245  virtual shared_value construct_delayed_merge(shared_origin origin, std::vector<shared_value> stack) const;
246 
247  shared_value to_fallback_value() const override;
248 
249  template<typename T>
250  static bool equals(config_value const& other, std::function<bool(T const&)> checker)
251  {
252  // Use pointer casts to avoid exceptions.
253  auto other_t = dynamic_cast<T const*>(&other);
254  if (!other_t) {
255  return false;
256  }
257 
258  // Pass to checker as a reference to clarify the object will exist.
259  return checker(*other_t);
260  }
261 
262  private:
263  shared_value delay_merge(std::vector<shared_value> stack, shared_value fallback) const;
264 
265  shared_origin _origin;
266  };
267 } // namespace hocon
hocon::config_value::at_key
shared_config at_key(std::string const &key) const
Places the value inside a config at the given key.
hocon::config_value::at_path
shared_config at_path(std::string const &path_expression) const
Places the value inside a config at the given path.
hocon::config_value::origin
virtual shared_origin const & origin() const
The origin of the value (file, line number, etc.), for debugging and error messages.
hocon::token
Definition: token.hpp:18
hocon::config_delayed_merge_object
Definition: config_delayed_merge_object.hpp:10
hocon::config_value::modifier
Definition: config_value.hpp:212
hocon::config
An immutable map from config paths to config values.
Definition: config.hpp:172
hocon::config_value::type
type
The type of a configuration value (following the JSON type schema).
Definition: config_value.hpp:56
hocon::config_value::with_origin
virtual shared_value with_origin(shared_origin origin) const
Returns a.
hocon
Factory for creating config_document instances.
Definition: config.hpp:18
hocon::default_transformer
Definition: default_transformer.hpp:7
hocon::simple_config_list
Definition: simple_config_list.hpp:15
hocon::config_mergeable
Definition: config_mergeable.hpp:8
hocon::resolve_context
Definition: resolve_context.hpp:16
hocon::simple_config_object
Definition: simple_config_object.hpp:11
hocon::config_value::value_type
virtual type value_type() const =0
The type of the value; matches the JSON type schema.
hocon::config_value::render
virtual std::string render() const
Renders the config value as a HOCON string.
hocon::resolve_result
Definition: config_value.hpp:20
hocon::config_render_options
Definition: config_render_options.hpp:20
hocon::config_value::no_exceptions_modifier
Definition: config_value.hpp:217
hocon::operator==
bool operator==(config_document const &lhs, config_document const &rhs)
Config documents compare via rendered strings.
hocon::value
Definition: tokens.hpp:8
hocon::path
Definition: path.hpp:13
hocon::config_value::with_fallback
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...
hocon::config_value
An immutable value, following the JSON type schema.
Definition: config_value.hpp:39
hocon::config_concatenation
A config_concatenation represents a list of values to be concatenated (see the spec).
Definition: config_concatenation.hpp:24
hocon::resolve_source
Definition: resolve_source.hpp:15
hocon::config_value::to_fallback_value
shared_value to_fallback_value() const override
Converts a config to its root object and a config_value to itself.
hocon::config_value::value_type_name
char const * value_type_name() const
The printable name of the value type.
Definition: config_value.hpp:92
hocon::config_value::render
virtual std::string render(config_render_options options) const
Renders the config value to a string, using the provided options.
hocon::config_value::relativized
virtual shared_value relativized(std::string prefix) const
This is used when including one file in another; the included file is relativized to the path it's in...
Definition: config_value.hpp:181
hocon::config_delayed_merge
Definition: config_delayed_merge.hpp:11
hocon::config_object
Definition: config_object.hpp:11