Loading...
Searching...
No Matches
SelectorXML.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 * TreeXML.h
33 *
34 * Created on: Jun 24, 2012
35 * Author: mpeura
36 */
37
38
39
40#ifndef DRAIN_SELECTOR_XML
41#define DRAIN_SELECTOR_XML
42
43//#include <ostream>
44#include <drain/StringBuilder.h>
45#include <drain/StringTools.h>
46#include "ClassXML.h"
47
48namespace drain {
49
50
51enum PseudoClassCSS {
52 none=0,
53 active,
54 disabled,
55 focus,
56 hover,
57 link,
58 scope,
59 target,
60 valid,
61 visited,
62};
63
64
65// Alternative 1: redefine the assignment method of the super class
66template <>
67void StringConverter<PseudoClassCSS>::convertToString(const PseudoClassCSS & value, std::string &s);
68/*
69{
70 s.assign(drain::EnumDict<PseudoClassCSS>::dict.getKey(value));
71}
72*/
73
74
75// Alternative 2: redefine the assignment method.
76/*
77template <>
78template <>
79void StringWrapper<PseudoClassCSS>::set(const PseudoClassCSS & value){
80 assign(drain::EnumDict<PseudoClassCSS>::dict.getKey(value));
81}
82*/
83
84
88template <typename E>
89class SelectXML { // : public std::string {
90
91public:
92
93 template <class ...TT>
94 inline
95 SelectXML(TT... args) {
96 set(args...);
97 }
98
105 template <class T, class ...TT>
106 void set(const T & arg, TT... args){
107 set(arg);
108 set(args...);
109 }
110
111 template <class T>
112 void set(const T & arg){
113 //Logger mout(__FILE__, __FUNCTION__);
114 cls.set(arg);
115 // Logger(__FILE__, __FUNCTION__).experimental<LOG_INFO>("arg '", arg, "' of type:", typeid(T).name(), ", -> CSS class: ", cls);
116 //elem = drain::EnumDict<E>::dict.getValue(arg, false);
117 };
118
120
126 inline
127 void set(const E & arg){
128 elem = arg;
129 };
130
132
136 inline
137 void set(const std::string & arg){
138 cls.assign(arg);
139 //elem = drain::EnumDict<E>::dict.getValue(arg, false);
140 };
141
143 inline
144 void set(const char *arg){
145 cls.assign(arg);
146 // elem = drain::EnumDict<E>::dict.getValue(arg, false);
147 };
148
149
151 inline
152 void set(const ClassXML & arg){
153 cls.assign(arg);
154 };
155
157
160 inline
161 void set(const PseudoClassCSS & arg){
162 pseudoClass.set(arg);
163 };
164
166
171 inline
173 pseudoClass.assign(p);
174 };
175
177
183 template <class T>
184 inline
185 void setElement(const T & arg){
186 elem = drain::EnumDict<E>::dict.getValue(arg, false);
187 };
188
189
190 template <class T>
191 inline
192 void setClass(const T & arg){
193 // FIX StringConv
194 cls = arg;
195 };
196
198
203 template <class T>
204 inline
205 void setPseudoClass(const T & psCls){
206 pseudoClass.set(psCls);
207 };
208
209
210
211
212
213 inline
214 void clear(){
215 elem = 0;
216 cls.clear();
217 pseudoClass.clear();
218 };
219
220 void toStream(std::ostream & ostr) const {
221
222 // TODO id?
223
224 // Undefined ~ 0 (contract)
225 if (static_cast<int>(elem) != 0){
226 ostr << drain::EnumDict<E>::dict.getKey(elem);
227 }
228
229 if (!cls.empty()){
230 ostr << '.' << cls;
231 }
232
233 if (!pseudoClass.empty()){
234 ostr << ':' << pseudoClass;
235 }
236
237 }
238
239 const std::string & str() const {
240 //sstr.str("");
241 std::stringstream sstr;
242 toStream(sstr);
243 currentStr = sstr.str();
244 return currentStr;
245 //return sstr.str();
246 }
247
248 inline
249 operator const std::string &() const {
250 return str();
251 }
252
253
254protected:
255
256 E elem = static_cast<E>(0);
257 ClassXML cls = "";
258 StringWrapper<PseudoClassCSS> pseudoClass;
259
260 inline
261 void set(){};
262
263 mutable
264 std::string currentStr;
265 //std::stringstream sstr;
266
267};
268
269}
270
271
272
273DRAIN_ENUM_DICT(drain::PseudoClassCSS);
274
275
276DRAIN_ENUM_OSTREAM(drain::PseudoClassCSS);
277
278
279namespace drain {
280
281
282
283template <typename X>
284std::ostream & operator<<(std::ostream & ostr, const drain::SelectXML<X> &x){
285 //return ostr << x.str();
286 x.toStream(ostr);
287 return ostr;
288}
289
290
292/*
293class SelectorXML : public std::string {
294
295public:
296
297 static
298 const char CLASS = '.';
299
300 static
301 const char ID = '#';
302
303
304 inline
305 SelectorXML(const std::string &s) : std::string(s){
306 }
307
308 inline
309 SelectorXML(const char *s) : std::string(s){
310 }
311
312 template <class ...T>
313 inline
314 SelectorXML(T... args) : std::string(StringBuilder<>(args...)){
315 }
316
317};
318*/
319
320// OLD CSS class selector.
339/*
340class SelectorXMLid : public SelectorXML {
341public:
342
343 template <class T>
344 inline
345 SelectorXMLid(const T & arg) : SelectorXML(ID, arg){
346 }
347
348};
349*/
350
351
352/*
353inline
354std::ostream & operator<<(std::ostream &ostr, const TreeXML & t){
355 // DOC def? TODO: preamble/prologToStream()
356 TreeXML::node_data_t::docTypeToStream(ostr); // must be member, to support virtual?
357 TreeXML::node_data_t::toStream(ostr, t, "");
358 return ostr;
359}
360*/
361
362
363} // drain::
364
365#endif /* TREEXML_H_ */
366
A wrapper marking string an CSS effect.
Definition ClassXML.h:57
Definition SelectorXML.h:89
void set(const T &arg, TT... args)
Definition SelectorXML.h:106
void setPseudoClass(const T &psCls)
Set one of the element pseudo classes: focus, hover.
Definition SelectorXML.h:205
void set(const char *arg)
Set CSS class.
Definition SelectorXML.h:144
void set(const E &arg)
Set element.
Definition SelectorXML.h:127
void set(const StringWrapper< drain::PseudoClassCSS > &p)
Set one of the element pseudo classes: focus, hover.
Definition SelectorXML.h:172
void set(const PseudoClassCSS &arg)
Set one of the element pseudo classes: focus, hover.
Definition SelectorXML.h:161
void set(const std::string &arg)
Set CSS class.
Definition SelectorXML.h:137
void setElement(const T &arg)
Set element explicitly.
Definition SelectorXML.h:185
void set(const ClassXML &arg)
Set CSS class.
Definition SelectorXML.h:152
Definition StringTools.h:72
Definition DataSelector.cpp:1277
A container for a static dictionary of enumeration values.
Definition EnumUtils.h:52
static E getValue(const E &value, bool lenient=true)
Convenience for object.set(...) like commands.
Definition EnumUtils.h:97