neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
i_ui_element_parser.hpp
Go to the documentation of this file.
1// i_ui_element_parser.hpp
2/*
3neoGFX Resource Compiler
4Copyright(C) 2019 Leigh Johnston
5
6This program is free software: you can redistribute it and / or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#pragma once
21
22#include <neogfx/neogfx.hpp>
23#include <boost/format.hpp>
30
31namespace neogfx::nrc
32{
33 class i_ui_element;
34
36 {
37 // exceptions
38 public:
39 struct duplicate_element_id : std::runtime_error
40 {
41 duplicate_element_id(std::string const& aId) : std::runtime_error{ "Duplicate element ID '" + aId + "'." } {}
42 };
43 struct element_not_found : std::logic_error
44 {
45 element_not_found(std::string const& aId) : std::logic_error{ "Element ID '" + aId + "' not found." } {}
46 };
47 struct element_type_not_found : std::runtime_error
48 {
49 element_type_not_found(std::string const& aType) : std::runtime_error{ "Element type '" + aType + "' not found." } {}
50 element_type_not_found(std::string const& aType, std::string const& aParentElement) : std::runtime_error{ "Element type '" + aType + "' not found for element '" + aParentElement + "'." } {}
51 };
52 struct element_data_not_found : std::runtime_error
53 {
54 element_data_not_found(std::string const& aData) : std::runtime_error{ "Element data '" + aData + "' not found." } {}
55 element_data_not_found(std::string const& aData, std::string const& aParentElement) : std::runtime_error{ "Element data '" + aData + "' not found for element '" + aParentElement + "'." } {}
56 };
57 // types
58 public:
61 // operations
62 public:
63 virtual const neolib::i_string& element_namespace() const = 0;
64 virtual const neolib::i_string& current_fragment() const = 0;
65 virtual const neolib::i_string& current_element() const = 0;
66 virtual void index(const neolib::i_string& aId, const i_ui_element& aElement) const = 0;
67 virtual const i_ui_element* find(const neolib::i_string& aId) const = 0;
68 virtual const i_ui_element& at(const neolib::i_string& aId) const = 0;
69 virtual void generate_anonymous_id(neolib::i_string& aNewAnonymousId) const = 0;
70 virtual void indent(int32_t aLevel, neolib::i_string& aResult) const = 0;
71 virtual void emit(const neolib::i_string& aText) const = 0;
72 // implementation
73 private:
74 virtual void do_source_location(neolib::i_string& aLocation) const = 0;
75 virtual bool do_data_exists(const neolib::i_string& aKey) const = 0;
76 virtual bool do_array_data_exists(const neolib::i_string& aKey) const = 0;
77 virtual const data_t& do_get_data(const neolib::i_string& aKey) const = 0;
78 virtual data_t& do_get_data(const neolib::i_string& aKey) = 0;
79 virtual const array_data_t& do_get_array_data(const neolib::i_string& aKey) const = 0;
80 virtual array_data_t& do_get_array_data(const neolib::i_string& aKey) = 0;
81 // helpers
82 public:
84 {
85 neolib::string result;
87 return result;
88 }
89 std::string indent(int32_t aLevel) const
90 {
91 neolib::string result;
92 indent(aLevel, result);
93 return result.to_std_string();
94 }
95 public:
96 std::string source_location() const
97 {
98 neolib::string location;
99 do_source_location(location);
100 return location.to_std_string();
101 }
102 bool data_exists(std::string const& aKey) const
103 {
104 return do_data_exists(neolib::string{ aKey });
105 }
106 bool array_data_exists(std::string const& aKey) const
107 {
108 return do_array_data_exists(neolib::string{ aKey });
109 }
110 const data_t& get_data(std::string const& aKey) const
111 {
112 return do_get_data(neolib::string{ aKey });
113 }
114 data_t& get_data(std::string const& aKey)
115 {
116 return do_get_data(neolib::string{ aKey });
117 }
118 const array_data_t& get_array_data(std::string const& aKey) const
119 {
120 return do_get_array_data(neolib::string{ aKey });
121 }
122 array_data_t& get_array_data(std::string const& aKey)
123 {
124 return do_get_array_data(neolib::string{ aKey });
125 }
126 template <typename T>
127 const abstract_t<T>& get(std::string const& aKey) const
128 {
129 return get_data(aKey).get<abstract_t<T>>();
130 }
131 template <typename T>
132 abstract_t<T>& get(std::string const& aKey)
133 {
134 return get_data(aKey).get<abstract_t<T>>();
135 }
136 template <typename T>
137 neolib::optional<T> get_optional(std::string const& aKey) const
138 {
139 if (data_exists(aKey))
140 {
141 if constexpr (std::is_integral_v<T> && !std::is_same_v<T, bool>)
142 return static_cast<T>(get_data(aKey).get<int64_t>());
143 else
144 return get_data(aKey).get<abstract_t<T>>();
145 }
146 else
147 return neolib::optional<T>{};
148 }
149 template <typename T>
150 neolib::optional<T> get_optional_enum(std::string const& aKey) const
151 {
152 if (data_exists(aKey))
153 return neolib::string_to_enum<T>(get_data(aKey).get<neolib::i_string>());
154 else
155 return neolib::optional<T>{};
156 }
157 template <typename T, typename U>
158 const abstract_t<T>& get(std::string const& aKey, const U& aDefault) const
159 {
160 if (data_exists(aKey))
161 return get_data(aKey).get<abstract_t<T>>();
162 else
163 return aDefault;
164 }
165 template <typename T, typename U>
166 abstract_t<T>& get(std::string const& aKey, U& aDefault)
167 {
168 if (data_exists(aKey))
169 return get_data(aKey).get<abstract_t<T>>();
170 else
171 return aDefault;
172 }
173 public:
174 void emit(std::string const& aArgument) const
175 {
176 emit(neolib::string{ aArgument });
177 }
178 template <typename T>
179 void emit(std::string const& aFormat, const T& aArgument) const
180 {
181 emit(neolib::string{ (boost::format(aFormat) % aArgument).str() });
182 }
183 template <typename T1, typename T2>
184 void emit(std::string const& aFormat, const T1& aArgument1, const T2& aArgument2) const
185 {
186 emit(neolib::string{ (boost::format(aFormat) % aArgument1 % aArgument2).str() });
187 }
188 template <typename T1, typename T2, typename T3>
189 void emit(std::string const& aFormat, const T1& aArgument1, const T2& aArgument2, const T3& aArgument3) const
190 {
191 emit(neolib::string{ (boost::format(aFormat) % aArgument1 % aArgument2 % aArgument3).str() });
192 }
193 template <typename T1, typename T2, typename T3, typename T4>
194 void emit(std::string const& aFormat, const T1& aArgument1, const T2& aArgument2, const T3& aArgument3, const T4& aArgument4) const
195 {
196 emit(neolib::string{ (boost::format(aFormat) % aArgument1 % aArgument2 % aArgument3 % aArgument4).str() });
197 }
198 template <typename T1, typename T2, typename T3, typename T4, typename T5>
199 void emit(std::string const& aFormat, const T1& aArgument1, const T2& aArgument2, const T3& aArgument3, const T4& aArgument4, const T5& aArgument5) const
200 {
201 emit(neolib::string{ (boost::format(aFormat) % aArgument1 % aArgument2 % aArgument3 % aArgument4 % aArgument5).str() });
202 }
203 private:
204 };
205}
neolib::string generate_anonymous_id() const
const data_t & get_data(std::string const &aKey) const
void emit(std::string const &aFormat, const T1 &aArgument1, const T2 &aArgument2, const T3 &aArgument3) const
const abstract_t< T > & get(std::string const &aKey, const U &aDefault) const
void emit(std::string const &aFormat, const T &aArgument) const
virtual void index(const neolib::i_string &aId, const i_ui_element &aElement) const =0
virtual const neolib::i_string & element_namespace() const =0
virtual void emit(const neolib::i_string &aText) const =0
virtual const neolib::i_string & current_element() const =0
neolib::i_vector< neolib::i_simple_variant > array_data_t
std::string indent(int32_t aLevel) const
neolib::optional< T > get_optional_enum(std::string const &aKey) const
virtual void indent(int32_t aLevel, neolib::i_string &aResult) const =0
abstract_t< T > & get(std::string const &aKey)
data_t & get_data(std::string const &aKey)
bool data_exists(std::string const &aKey) const
const array_data_t & get_array_data(std::string const &aKey) const
const abstract_t< T > & get(std::string const &aKey) const
abstract_t< T > & get(std::string const &aKey, U &aDefault)
virtual const i_ui_element * find(const neolib::i_string &aId) const =0
bool array_data_exists(std::string const &aKey) const
virtual void generate_anonymous_id(neolib::i_string &aNewAnonymousId) const =0
void emit(std::string const &aFormat, const T1 &aArgument1, const T2 &aArgument2) const
void emit(std::string const &aFormat, const T1 &aArgument1, const T2 &aArgument2, const T3 &aArgument3, const T4 &aArgument4) const
virtual const i_ui_element & at(const neolib::i_string &aId) const =0
virtual const neolib::i_string & current_fragment() const =0
neolib::optional< T > get_optional(std::string const &aKey) const
void emit(std::string const &aArgument) const
void emit(std::string const &aFormat, const T1 &aArgument1, const T2 &aArgument2, const T3 &aArgument3, const T4 &aArgument4, const T5 &aArgument5) const
array_data_t & get_array_data(std::string const &aKey)
std::string to_std_string() const
Definition string.hpp:85
ref_ptr< ConcreteType > make_ref(Args &&... args)
Definition plf_hive.h:79
element_data_not_found(std::string const &aData, std::string const &aParentElement)
element_type_not_found(std::string const &aType, std::string const &aParentElement)