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/StringWrapper.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
64DRAIN_ENUM_CONV(PseudoClassCSS); // for StringWrapper?
65
66
67
74template <typename E>
75class SelectXML { // : public std::string {
76
77public:
78
79 /*
80 inline
81 SelectXML(const ClassXML & arg) {
82 cls.set(arg);
83 }
84 */
85
86
87 template <class ...TT>
88 inline
89 SelectXML(TT... args) {
90 set(args...);
91 }
92
93
94 template <class T>
95 inline
96 SelectXML<E> & operator=(const T & arg){
97 set(arg);
98 return *this;
99 }
100
101 inline
102 SelectXML<E> & operator=(const char *arg){
103 set(arg);
104 return *this;
105 }
106
113 template <class T, class ...TT>
114 void set(const T & arg, TT... args){
115 set(arg);
116 set(args...);
117 }
118
120
126 inline
127 void set(const E & arg){
128 elem = arg;
129 };
130
131
132 template <class T>
133 void set(const T & arg){
134 //Logger mout(__FILE__, __FUNCTION__);
135 cls.set(arg);
136 // cls = arg;
137 // Logger(__FILE__, __FUNCTION__).experimental<LOG_INFO>("arg '", arg, "' of type:", typeid(T).name(), ", -> CSS class: ", cls);
138 //elem = drain::Enum<E>::dict.getValue(arg, false);
139 }
140
142
146 inline
147 void set(const std::string & arg){
148 cls.set(arg);
149 // cls.assign(arg);
150 // elem = drain::Enum<E>::dict.getValue(arg, false);
151 };
152
154 inline
155 void set(const char *arg){
156 cls.set(arg);
157 // cls.assign(arg);
158 };
159
160
162 inline
163 void set(const ClassXML & arg){
164 cls.set(arg);
165 // str = arg.baseStr;
166 //cls.assign(arg);
167 };
168
170
173 inline
174 void set(const PseudoClassCSS & arg){
175 pseudoClass.set(arg);
176 };
177
179
184 inline
186 pseudoClass.assign(p);
187 };
188
190
198 template <class T>
199 inline
200 void setElement(const T & arg){
201 //elem = drain::Enum<E>::dict.getValue(arg, false);
202 elem = arg;
203 }
204
205
206
207 template <class T>
208 inline
209 void setClass(const T & arg){
210 // FIX StringConv
211 cls = arg;
212 }
213
215
220 template <class T>
221 inline
222 void setPseudoClass(const T & psCls){
223 pseudoClass.set(psCls);
224 }
225
226
227
228
229
230 inline
231 void clear(){
232 elem = 0;
233 cls.clear();
234 pseudoClass.clear();
235 };
236
237 void toStream(std::ostream & ostr) const {
238
239 // TODO id?
240
241 // Undefined ~ 0 (contract)
242 if (static_cast<int>(elem) != 0){
243 ostr << drain::Enum<E>::dict.getKey(elem);
244 }
245
246 if (!cls.empty()){
247 ostr << cls.strPrefixed();
248 }
249
250 if (!pseudoClass.empty()){
251 ostr << ':' << pseudoClass;
252 }
253
254 }
255
256 const std::string & str() const {
257 //sstr.str("");
258 std::stringstream sstr;
259 toStream(sstr);
260 currentStr = sstr.str();
261 return currentStr;
262 //return sstr.str();
263 }
264
265 inline
266 operator const std::string &() const {
267 return str();
268 }
269
270
271protected:
272
273 E elem = static_cast<E>(0);
274 ClassXML cls = "";
275 StringWrapper<PseudoClassCSS> pseudoClass;
276
277 inline
278 void set(){};
279
280 mutable
281 std::string currentStr;
282 //std::stringstream sstr;
283
284};
285
286} // drain::
287
288
289
290DRAIN_ENUM_DICT(drain::PseudoClassCSS);
291DRAIN_ENUM_OSTREAM(drain::PseudoClassCSS)
292
293
294namespace drain {
295
296template <typename X>
297std::ostream & operator<<(std::ostream & ostr, const drain::SelectXML<X> &x){
298 x.toStream(ostr);
299 return ostr;
300}
301
302
303} // drain::
304
305#endif
A wrapper marking string an CSS effect.
Definition ClassXML.h:57
Definition SelectorXML.h:75
void set(const T &arg, TT... args)
Definition SelectorXML.h:114
void setPseudoClass(const T &psCls)
Set one of the element pseudo classes: focus, hover.
Definition SelectorXML.h:222
void set(const char *arg)
Set CSS class.
Definition SelectorXML.h:155
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:185
void set(const PseudoClassCSS &arg)
Set one of the element pseudo classes: focus, hover.
Definition SelectorXML.h:174
void set(const std::string &arg)
Set CSS class.
Definition SelectorXML.h:147
void setElement(const T &arg)
Set element explicitly.
Definition SelectorXML.h:200
void set(const ClassXML &arg)
Set CSS class.
Definition SelectorXML.h:163
String-like object easily supporting conversion from other types, like Enum values.
Definition StringWrapper.h:51
Definition Data.h:1473
std::ostream & operator<<(std::ostream &ostr, const PlainData< DT > &d)
Definition Data.h:631