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 <drain/Converter.h>
47#include "ClassXML.h"
48
49namespace drain {
50
51
52enum PseudoClassCSS {
53 none=0,
54 active,
55 disabled,
56 focus,
57 hover,
58 link,
59 scope,
60 target,
61 valid,
62 visited,
63};
64
65DRAIN_ENUM_CONV(PseudoClassCSS); // for StringWrapper?
66
67
68
72template <typename E>
73class SelectXML { // : public std::string {
74
75public:
76
77 template <class ...TT>
78 inline
79 SelectXML(TT... args) {
80 set(args...);
81 }
82
89 template <class T, class ...TT>
90 void set(const T & arg, TT... args){
91 set(arg);
92 set(args...);
93 }
94
95 template <class T>
96 void set(const T & arg){
97 //Logger mout(__FILE__, __FUNCTION__);
98 cls.set(arg);
99 // Logger(__FILE__, __FUNCTION__).experimental<LOG_INFO>("arg '", arg, "' of type:", typeid(T).name(), ", -> CSS class: ", cls);
100 //elem = drain::Enum<E>::dict.getValue(arg, false);
101 };
102
104
110 inline
111 void set(const E & arg){
112 elem = arg;
113 };
114
116
120 inline
121 void set(const std::string & arg){
122 cls.assign(arg);
123 //elem = drain::Enum<E>::dict.getValue(arg, false);
124 };
125
127 inline
128 void set(const char *arg){
129 cls.assign(arg);
130 // elem = drain::Enum<E>::dict.getValue(arg, false);
131 };
132
133
135 inline
136 void set(const ClassXML & arg){
137 cls.assign(arg);
138 };
139
141
144 inline
145 void set(const PseudoClassCSS & arg){
146 pseudoClass.set(arg);
147 };
148
150
155 inline
157 pseudoClass.assign(p);
158 };
159
161
167 template <class T>
168 inline
169 void setElement(const T & arg){
170 elem = drain::Enum<E>::dict.getValue(arg, false);
171 };
172
173
174 template <class T>
175 inline
176 void setClass(const T & arg){
177 // FIX StringConv
178 cls = arg;
179 };
180
182
187 template <class T>
188 inline
189 void setPseudoClass(const T & psCls){
190 pseudoClass.set(psCls);
191 };
192
193
194
195
196
197 inline
198 void clear(){
199 elem = 0;
200 cls.clear();
201 pseudoClass.clear();
202 };
203
204 void toStream(std::ostream & ostr) const {
205
206 // TODO id?
207
208 // Undefined ~ 0 (contract)
209 if (static_cast<int>(elem) != 0){
210 ostr << drain::Enum<E>::dict.getKey(elem);
211 }
212
213 if (!cls.empty()){
214 ostr << '.' << cls;
215 }
216
217 if (!pseudoClass.empty()){
218 ostr << ':' << pseudoClass;
219 }
220
221 }
222
223 const std::string & str() const {
224 //sstr.str("");
225 std::stringstream sstr;
226 toStream(sstr);
227 currentStr = sstr.str();
228 return currentStr;
229 //return sstr.str();
230 }
231
232 inline
233 operator const std::string &() const {
234 return str();
235 }
236
237
238protected:
239
240 E elem = static_cast<E>(0);
241 ClassXML cls = "";
242 StringWrapper<PseudoClassCSS> pseudoClass;
243
244 inline
245 void set(){};
246
247 mutable
248 std::string currentStr;
249 //std::stringstream sstr;
250
251};
252
253} // drain::
254
255
256
257DRAIN_ENUM_DICT(drain::PseudoClassCSS);
258DRAIN_ENUM_OSTREAM(drain::PseudoClassCSS);
259
260
261namespace drain {
262
263template <typename X>
264std::ostream & operator<<(std::ostream & ostr, const drain::SelectXML<X> &x){
265 x.toStream(ostr);
266 return ostr;
267}
268
269
270} // drain::
271
272#endif
A wrapper marking string an CSS effect.
Definition ClassXML.h:57
Definition SelectorXML.h:73
void set(const T &arg, TT... args)
Definition SelectorXML.h:90
void setPseudoClass(const T &psCls)
Set one of the element pseudo classes: focus, hover.
Definition SelectorXML.h:189
void set(const char *arg)
Set CSS class.
Definition SelectorXML.h:128
void set(const E &arg)
Set element.
Definition SelectorXML.h:111
void set(const StringWrapper< drain::PseudoClassCSS > &p)
Set one of the element pseudo classes: focus, hover.
Definition SelectorXML.h:156
void set(const PseudoClassCSS &arg)
Set one of the element pseudo classes: focus, hover.
Definition SelectorXML.h:145
void set(const std::string &arg)
Set CSS class.
Definition SelectorXML.h:121
void setElement(const T &arg)
Set element explicitly.
Definition SelectorXML.h:169
void set(const ClassXML &arg)
Set CSS class.
Definition SelectorXML.h:136
String-like object easily supporting conversion from other types, like Enum values.
Definition StringWrapper.h:50
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