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
71template <typename E>
72class SelectXML { // : public std::string {
73
74public:
75
76 template <class ...TT>
77 inline
78 SelectXML(TT... args) {
79 set(args...);
80 }
81
88 template <class T, class ...TT>
89 void set(const T & arg, TT... args){
90 set(arg);
91 set(args...);
92 }
93
94 template <class T>
95 void set(const T & arg){
96 //Logger mout(__FILE__, __FUNCTION__);
97 cls.set(arg);
98 // Logger(__FILE__, __FUNCTION__).experimental<LOG_INFO>("arg '", arg, "' of type:", typeid(T).name(), ", -> CSS class: ", cls);
99 //elem = drain::Enum<E>::dict.getValue(arg, false);
100 };
101
103
109 inline
110 void set(const E & arg){
111 elem = arg;
112 };
113
115
119 inline
120 void set(const std::string & arg){
121 cls.assign(arg);
122 //elem = drain::Enum<E>::dict.getValue(arg, false);
123 };
124
126 inline
127 void set(const char *arg){
128 cls.assign(arg);
129 // elem = drain::Enum<E>::dict.getValue(arg, false);
130 };
131
132
134 inline
135 void set(const ClassXML & arg){
136 cls.assign(arg);
137 };
138
140
143 inline
144 void set(const PseudoClassCSS & arg){
145 pseudoClass.set(arg);
146 };
147
149
154 inline
156 pseudoClass.assign(p);
157 };
158
160
166 template <class T>
167 inline
168 void setElement(const T & arg){
169 elem = drain::Enum<E>::dict.getValue(arg, false);
170 };
171
172
173 template <class T>
174 inline
175 void setClass(const T & arg){
176 // FIX StringConv
177 cls = arg;
178 };
179
181
186 template <class T>
187 inline
188 void setPseudoClass(const T & psCls){
189 pseudoClass.set(psCls);
190 };
191
192
193
194
195
196 inline
197 void clear(){
198 elem = 0;
199 cls.clear();
200 pseudoClass.clear();
201 };
202
203 void toStream(std::ostream & ostr) const {
204
205 // TODO id?
206
207 // Undefined ~ 0 (contract)
208 if (static_cast<int>(elem) != 0){
209 ostr << drain::Enum<E>::dict.getKey(elem);
210 }
211
212 if (!cls.empty()){
213 ostr << '.' << cls;
214 }
215
216 if (!pseudoClass.empty()){
217 ostr << ':' << pseudoClass;
218 }
219
220 }
221
222 const std::string & str() const {
223 //sstr.str("");
224 std::stringstream sstr;
225 toStream(sstr);
226 currentStr = sstr.str();
227 return currentStr;
228 //return sstr.str();
229 }
230
231 inline
232 operator const std::string &() const {
233 return str();
234 }
235
236
237protected:
238
239 E elem = static_cast<E>(0);
240 ClassXML cls = "";
241 StringWrapper<PseudoClassCSS> pseudoClass;
242
243 inline
244 void set(){};
245
246 mutable
247 std::string currentStr;
248 //std::stringstream sstr;
249
250};
251
252} // drain::
253
254
255
256DRAIN_ENUM_DICT(drain::PseudoClassCSS);
257DRAIN_ENUM_OSTREAM(drain::PseudoClassCSS);
258
259
260namespace drain {
261
262template <typename X>
263std::ostream & operator<<(std::ostream & ostr, const drain::SelectXML<X> &x){
264 x.toStream(ostr);
265 return ostr;
266}
267
268
269} // drain::
270
271#endif
A wrapper marking string an CSS effect.
Definition ClassXML.h:57
Definition SelectorXML.h:72
void set(const T &arg, TT... args)
Definition SelectorXML.h:89
void setPseudoClass(const T &psCls)
Set one of the element pseudo classes: focus, hover.
Definition SelectorXML.h:188
void set(const char *arg)
Set CSS class.
Definition SelectorXML.h:127
void set(const E &arg)
Set element.
Definition SelectorXML.h:110
void set(const StringWrapper< drain::PseudoClassCSS > &p)
Set one of the element pseudo classes: focus, hover.
Definition SelectorXML.h:155
void set(const PseudoClassCSS &arg)
Set one of the element pseudo classes: focus, hover.
Definition SelectorXML.h:144
void set(const std::string &arg)
Set CSS class.
Definition SelectorXML.h:120
void setElement(const T &arg)
Set element explicitly.
Definition SelectorXML.h:168
void set(const ClassXML &arg)
Set CSS class.
Definition SelectorXML.h:135
String-like object easily supporting conversion from other types, like Enum values.
Definition StringWrapper.h:51
Definition DataSelector.cpp:1277
A container for a static dictionary of enumeration values.
Definition Enum.h:51
static E getValue(const E &value, bool lenient=true)
Convenience for object.set(...) like commands.
Definition Enum.h:96