Loading...
Searching...
No Matches
ClassXML.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30*/
31/*
32 * ClassXML.h
33 *
34 * Author: mpeura
35 */
36
37
38
39#ifndef DRAIN_CLASS_XML
40#define DRAIN_CLASS_XML
41
42#include <drain/Enum.h>
43#include <ostream>
44
45//#include <drain/Sprinter.h>
46//#include <drain/FlexibleVariable.h>
47
48#include "ReferenceMap.h"
49#include "TreeUnordered.h"
50
51namespace drain {
52
53
55// Note: Currently separate from ClassListXML
56//class ClassXML : public StringWrapper<std::string> {
57class ClassXML : public std::string { // : public MultiEnumWrapper {
58
59 // consider : public std::string?
60
61public:
62
63 /*
64 inline
65 ClassXML(const std::string & s="") : MultiEnumWrapper(s){};
66
67 template <typename T>
68 inline
69 ClassXML(const T & arg) : MultiEnumWrapper(arg){};
70 //ClassXML(const T & x) : StringWrapper<std::string>(drain::Enum<T>::dict.getKey(x)){};
71
72 // inline
73 // ClassXML(const ClassXML & e) : MultiEnumWrapper((const std::string &)e){};
74
75 inline
76 ClassXML(const char *arg) : MultiEnumWrapper(std::string(arg)){};
77 */
78 // Redefine for enums, for example
79
80 inline
81 ClassXML(const std::string & arg=""){ // : std::string(arg)
82 set(arg);
83 };
84
85 inline
86 ClassXML(const char *arg){
87 set(arg);
88 };
89
90 template <typename T>
91 inline
92 ClassXML(const T & arg){
93 set(arg);
94 }
95
96 inline
97 ClassXML(const ClassXML & e) : std::string(){
98 set(e.str());
99 };
100
101 //ClassXML(const T & x) : StringWrapper<std::string>(drain::Enum<T>::dict.getKey(x)){};
102
103 // inline
104 // ClassXML(const ClassXML & e) : MultiEnumWrapper((const std::string &)e){};
105
106
107
108
109 virtual inline
110 ~ClassXML(){};
111
112 /* from string
113 inline
114 bool empty() const {
115 return baseStr.empty();
116 }
117
118 inline
119 void clear(){
120 baseStr.clear();
121 }
122 */
123
124 inline
125 void set(const char *arg){
126 assign(arg);
127 }
128
129 void set(const std::string & arg){
130 // baseStr =
131 assign(arg);
132 }
133
134
135 void set(const ClassXML & cls){
136 // baseStr =
137 assign(cls);
138 }
139
140 // Must be specialised for any class other than string and ClassXML.
141 template <typename T>
142 void set(const T & arg);
143
144
145 inline
146 const std::string & str() const {
147 return *this; // baseStr; //*this;
148 }
149
150 inline
151 const std::string & strPrefixed() const {
152 prefixed = ".";
153 prefixed += str();
154 return prefixed;
155 }
156
157protected:
158
159 //std::string baseStr;
160
161 // Class name prefixed with ".".
162 mutable
163 std::string prefixed;
164
165};
166
167
168inline
169std::ostream & operator<<(std::ostream &ostr, const ClassXML & cls){
170 // static const SprinterLayout layout = {" "}; // , "\n", "=", ""};
171 //ostr << '.' << cls.baseStr;
172 ostr << cls.str();
173 return ostr;
174}
175
176/*
177template <class E>
178inline void ClassXML::set(const E & arg)
179{ assign(drain::Enum<E>::dict.getKey(arg));}
180
181template <class T>
182inline void ClassXML::set(const T & arg){
183 Logger(__FILE__, __FUNCTION__).unimplemented<LOG_ERR>("Unknown argument type: ", drain::TypeName<T>::str(), " value=", arg);
184}
185*/
186
188#define DRAIN_ENUM_CLASSXML(enumtype) template <> inline void ClassXML::set(const enumtype & arg){ assign(drain::Enum<enumtype>::dict.getKey(arg));}
189
190
192
195// Note: Currently separate from ClassXML
196class ClassListXML : public std::set<std::string> {
197
198public:
199
200 template <typename ... TT>
201 inline
202 ClassListXML(const TT &... args){
203 add(args...);
204 }
205
207 template <typename ... TT>
208 inline
209 void add(const std::string & arg, const TT &... args) {
210 if (!arg.empty()){
211 insert(arg);
212 }
213 add(args...);
214 }
215
217 template <typename ... TT>
218 inline
219 void add(const char *arg, const TT &... args) {
220 add(std::string(arg), args...);
221 }
222
224 template <typename ... TT>
225 inline
226 void add(const ClassXML & arg, const TT &... args) {
227 if (!arg.empty()){
228 insert(arg);
229 }
230 add(args...);
231 }
232
234
240 template <typename T, typename ...TT>
241 inline
242 void add(const T & arg, const TT &... args) {
243 insert(drain::Enum<T>::dict.getKey(arg));
244 add(args...);
245 }
246
247
248 /* Compiler cannot derive (inner?) template
249 template <typename T, typename ... TT>
250 inline // drain::EnumFlagger<
251 void add(const typename drain::EnumFlagger<SingleFlagger<T> >::ivalue_t & enumArg, const TT &... args) {
252 // std::cerr << "ENUMS:" << enumArg.str() << '\n';
253 insert(enumArg.str());
254 add(args...);
255 };
256
257 template <typename EE>
258 inline // drain::EnumFlagger<
259 void add2(const typename SingleFlagger<EE>::ivalue_t & enumArg) {
260 // std::cerr << "ENUMS:" << enumArg.str() << '\n';
261 insert(enumArg.str());
262 // add(args...);
263 };
264 */
265
266 // Below all:
267 // TODO: iterator it = retrieve(arg);
268
269 /*
270 inline
271 bool has(const std::string & arg) const {
272 return (find(arg) != end());
273 };
274
275 inline
276 bool has(const char *arg) const {
277 return (find(arg) != end());
278 };
279 */
280
281 template <typename T, typename ...TT>
282 inline
283 bool has(const T & arg) const {
284 return (retrieve(arg) != end());
285 //return (find(drain::Enum<E>::dict.getKey(arg)) != end());
286 }
287
288 template <typename T, typename ...TT>
289 inline
290 bool hasAny(const T & arg, const TT &... args) const {
291 return has(arg) || hasAny(args...);
292 //return (find(drain::Enum<E>::dict.getKey(arg)) != end());
293 }
294
295
296
297 /*
298 inline
299 void remove(const std::string & arg) {
300 iterator it = find(arg);
301 if (it != end()){
302 erase(it);
303 }
304 };
305
306 inline
307 void remove(const char *arg) {
308 iterator it = find(arg);
309 if (it != end()){
310 erase(it);
311 }
312 };
313
314 inline
315 void remove(const ClassXML & arg) {
316 iterator it = find(arg);
317 if (it != end()){
318 erase(it);
319 }
320 };
321 */
322
323 template <typename E>
324 inline
325 void remove(const E & arg){
326 iterator it = retrieve(arg); // find(drain::Enum<E>::dict.getKey(arg));
327 if (it != end()){
328 erase(it);
329 }
330 }
331
333 static
334 const SprinterLayout layout; // = {" "}; // , "\n", "=", ""};
335
336protected:
337
338 inline
339 void add(){};
340
341 inline
342 iterator retrieve(const std::string & arg) const {
343 return find(arg);
344 }
345
346 inline
347 iterator retrieve(const char *arg) const {
348 return find(arg);
349 }
350
351 inline
352 iterator retrieve(const ClassXML & arg) const {
353 return find(arg);
354 }
355
356 template <typename E>
357 inline
358 iterator retrieve(const E & arg) const {
359 return find(drain::Enum<E>::dict.getKey(arg));
360 }
361
362 inline
363 bool hasAny() const {
364 return false;
365 }
366};
367
368
369inline
370std::ostream & operator<<(std::ostream &ostr, const ClassListXML & cls){
371 // static const SprinterLayout layout = {" "}; // , "\n", "=", ""};
372 Sprinter::sequenceToStream(ostr, (const std::set<std::string> &)cls, ClassListXML::layout);
373 return ostr;
374}
375
376
377
378
379
380} // drain::
381
382#endif /* TREEXML_H_ */
383
Container for style classes. Essentially a set of strings, with space-separated output support.
Definition ClassXML.h:196
void add(const T &arg, const TT &... args)
Add one or several classes.
Definition ClassXML.h:242
void add(const std::string &arg, const TT &... args)
Add one or several classes.
Definition ClassXML.h:209
void add(const char *arg, const TT &... args)
Add one or several classes.
Definition ClassXML.h:219
static const SprinterLayout layout
Uses spaces as separators.
Definition ClassXML.h:334
void add(const ClassXML &arg, const TT &... args)
Add one or several classes.
Definition ClassXML.h:226
A wrapper marking string an CSS effect.
Definition ClassXML.h:57
static std::ostream & sequenceToStream(std::ostream &ostr, const T &x, const SprinterLayout &layout)
Convenience: if sequence type (array, list, set, map) not given, assume array.
Definition Sprinter.h:324
Definition Data.h:1473
std::ostream & operator<<(std::ostream &ostr, const PlainData< DT > &d)
Definition Data.h:631
A container for a static dictionary of enumeration values.
Definition Enum.h:51
Definition Sprinter.h:136