cpp-hocon 0.3.0
Loading...
Searching...
No Matches
parseable.hpp
1#pragma once
2
3#include <hocon/config_parseable.hpp>
4#include <boost/nowide/fstream.hpp>
5#include <internal/simple_config_origin.hpp>
6#include <hocon/config_object.hpp>
7#include <hocon/config_include_context.hpp>
8
9namespace hocon {
10
11 class config_document;
12
13 class parseable : public config_parseable, public std::enable_shared_from_this<parseable> {
14 public:
15 static std::shared_ptr<parseable> new_file(std::string input_file_path, config_parse_options options);
16 static std::shared_ptr<parseable> new_string(std::string s, config_parse_options options);
17 static std::shared_ptr<parseable> new_not_found(std::string what_not_found, std::string message,
19
20 static config_syntax syntax_from_extension(std::string name);
21
22 void post_construct(config_parse_options const& base_options);
23
24 std::shared_ptr<config_document> parse_config_document();
25 shared_object parse(config_parse_options const& options) const override;
26 shared_object parse() const;
27
28 shared_value parse_value() const;
29
30 config_parse_options const& options() const override;
31 std::shared_ptr<const config_origin> origin() const override;
32
33 virtual std::unique_ptr<std::istream> reader(config_parse_options const& options) const;
34 virtual std::unique_ptr<std::istream> reader() const = 0;
35 virtual shared_origin create_origin() const = 0;
36
37 virtual config_syntax guess_syntax() const;
38 virtual config_syntax content_type() const;
39 virtual std::shared_ptr<config_parseable> relative_to(std::string file_name) const;
40
41 std::string to_string() const;
42 std::string get_cur_dir() const;
43 void set_cur_dir(std::string dir) const;
44 void separate_filepath(const std::string& path, std::string* file_dir,
45 std::string* file_name) const;
46
47 // Disable copy constructors, as include_context assumes it can hold a reference to parseable.
48 parseable() = default;
49 parseable(parseable const&) = delete;
50 parseable& operator=(parseable const&) = delete;
51
52 private:
53 std::shared_ptr<config_document> parse_document(config_parse_options const& base_options) const;
54 std::shared_ptr<config_document> parse_document(shared_origin origin,
55 config_parse_options const& final_options) const;
56 std::shared_ptr<config_document> raw_parse_document(std::unique_ptr<std::istream> stream, shared_origin origin,
57 config_parse_options const& options) const;
58 std::shared_ptr<config_document> raw_parse_document(shared_origin origin,
59 config_parse_options const& options) const;
60
61 shared_value parse_value(config_parse_options const& base_options) const;
62 shared_value parse_value(shared_origin origin, config_parse_options const& options) const;
63 shared_value raw_parse_value(std::unique_ptr<std::istream> stream,
64 shared_origin origin,
65 config_parse_options const& options) const;
66 shared_value raw_parse_value(shared_origin origin, config_parse_options const& options) const;
67
68 config_parse_options fixup_options(config_parse_options const& base_options) const;
69
70 std::vector<parseable> _parse_stack;
71
72 shared_origin _initial_origin;
73 config_parse_options _initial_options;
74 shared_include_context _include_context;
75
76 static const int MAX_INCLUDE_DEPTH;
77 };
78
79 class parseable_file : public parseable {
80 public:
81 parseable_file(std::string input_file_path, config_parse_options options);
82 std::unique_ptr<std::istream> reader() const override;
83 shared_origin create_origin() const override;
84 config_syntax guess_syntax() const override;
85
86 private:
87 std::string _input;
88 };
89
90 class parseable_string : public parseable {
91 public:
93 std::unique_ptr<std::istream> reader() const override;
94 shared_origin create_origin() const override;
95
96 private:
97 std::string _input;
98 };
99
100 // NOTE: this is not a faithful port of the `ParseableResources` class from the
101 // upstream, because at least for now we're not going to try to do anything
102 // crazy like look for files on the ruby load path. However, there is a decent
103 // chunk of logic elsewhere in the codebase that is written with the assumption
104 // that this class will provide the 'last resort' attempt to find a config file
105 // before giving up, so we're basically port just enough to have it provide
106 // that last resort behavior
108 public:
109 parseable_resources(std::string resource, config_parse_options options);
110
111 std::unique_ptr<std::istream> reader() const override;
112 shared_origin create_origin() const override;
113
114 private:
115 std::string _resource;
116 };
117
118 // this is a parseable that doesn't exist and just throws when you try to
119 // parse it
121 public:
122 parseable_not_found(std::string what, std::string message, config_parse_options options);
123
124 std::unique_ptr<std::istream> reader() const override;
125 shared_origin create_origin() const override;
126
127 private:
128 std::string _what;
129 std::string _message;
130 };
131
132} // namespace hocon
A set of options related to parsing.
An opaque handle to something that can be parsed, obtained from config_include_context.
shared_object parse(config_parse_options const &options) const override
Parse whatever it is.
config_parse_options const & options() const override
Get the initial options, which can be modified then passed to parse().
std::shared_ptr< const config_origin > origin() const override
Returns a config_origin describing the origin of the paresable item.
Factory for creating config_document instances.
Definition: config.hpp:18