Loading...
Searching...
No Matches
StringMatcherList.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#ifndef DRAIN_STRING_MATCHER_LIST
33#define DRAIN_STRING_MATCHER_LIST
34
35#include <set>
36#include <list>
37#include <map>
38#include <stdexcept>
39
40// #include <drain/Log.h>
41#include <drain/Sprinter.h>
42#include <drain/String.h>
43#include "StringMatcher.h"
44
45
46namespace drain {
47
48
49
51template <class T=std::string>
52class StringMatcherList : public std::list<T> { // string temporary ?
53
54public:
55
56 typedef T matcher_t;
57
58 typedef std::list<T> list_t;
59
61 template <typename ... TT>
62 inline
63 StringMatcherList(const TT &... args){
64 // reset not needed
65 addKey(args...);
66 };
67
68
70 inline
72 };
73
74 inline
75 StringMatcherList(std::initializer_list<matcher_t> l){
76 setKeys(l);
77 };
78
79
81 virtual inline
83
84
86 operator bool() const {
87 return !this->empty();
88 }
89
90
92 inline
94 setKeys(l);
95 return *this;
96 }
97
99 StringMatcherList operator=(const std::initializer_list<matcher_t> &l){
100 setKeys(l);
101 return *this;
102 }
103
106 this->clear();
107 for (const matcher_t & key: l) {
108 this->push_back(key);
109 }
110 }
111
113 template <class T2>
114 void setKeys(const std::initializer_list<T2> & l){
115 this->clear();
116 for (const T2 & entry: l) {
117 adaptKey(entry);
118 }
119 };
120
122 template <class T2>
123 void setKeys(const std::list<T2> & l){
124 this->clear();
125 for (const T2 & key: l) {
126 adaptKey(key);
127 }
128 }
129
131
136 template <typename ...TT>
137 void setKeys(const std::string & arg, const TT & ... args){
138 //clear();
139 insertKeys(arg); // split and adapt
140 addKey(args...);
141 }
142
143
145 template <typename ...TT>
146 //void addKey(const std::string & arg, const TT & ... args){
147 void addKey(const matcher_t & arg, const TT & ... args){
148 adaptKey(arg);
149 addKey(args...);
150 }
151
152
154
159 bool test(const std::string & key, bool defaultResult = true) const;
160
161 const matcher_t & retrieve(const std::string & key) const;
162
163
164 inline
165 bool isSet() const {
166 return !this->empty();
167 //return (regExp.isSet() || !quantities.empty()) ;
168 }
169
170 inline
171 const list_t & getList() const {
172 return *this;
173 }
174
175 inline
176 void toStream(std::ostream & ostr) const {
178 }
179
180protected:
181
183
187 void insertKeys(const std::string & args); // , const std::string & separators = ","
188
189 inline
190 void addKey(){};
191
192 //void adaptKey(const std::string & s);
193 void adaptKey(const matcher_t & s);
194
196 // mutable // called by updateBean()?
197 // drain::RegExp regExp;
198
199 // mutable
200 // list_t quantities;
201
202
203};
204
205template <class T>
206void StringMatcherList<T>::insertKeys(const std::string & args){ //, const std::string & separators){
207 // drain::Logger mout(__FILE__, __FUNCTION__);
208 this->clear();
209 std::vector<std::string> argv;
210 //const char s =
211 drain::StringTools::split(args, argv, ",:"); //separators);
212 for (const std::string & arg: argv){
213 // mout.attention("adapting: ", arg, " split by '", separators, "'");
214 adaptKey(arg);
215 }
216
217}
218
219
223template <class T>
224//void StringMatcherList<T>::adaptKey(const std::string & s){
225void StringMatcherList<T>::adaptKey(const matcher_t & matcher){
226
227 if (matcher.empty()){
228 return;
229 }
230 else {
231
232 //matcher_t matcher(s);
233 for (const matcher_t & m: *this){
234 if (m == matcher){ // two matchers are equal if they accept and reject equally.
235 // exists already, dont append...
236 return;
237 }
238 }
239
240 this->push_back(matcher);
241 // Save copying?
242 /*
243 push_back(KeyMatcher());
244 back().set(s);
245 */
246 }
247
248}
249
250
251
252template <class T>
253bool StringMatcherList<T>::test(const std::string & key, bool defaultResult) const {
254
255 if (this->empty()){
256 return defaultResult; // Alternative: could need empty k - but on the other hand, why add it in a list, as it accepts anything.
257 }
258 else {
259 for (const auto & k: *this){
260 if (k == key){ //
261 return true;
262 }
263 }
264 }
265
266 return false;
267
268}
269
270template <class T>
271const typename StringMatcherList<T>::matcher_t & StringMatcherList<T>::retrieve(const std::string & key) const {
272
273 static
274 const matcher_t empty;
275
276 if (this->empty()){
277 return empty; // Alternative: could need empty k - but on the other hand, why add it in a list, as it accepts anything.
278 }
279 else {
280 for (const auto & matcher: *this){
281 if (matcher == key){ //
282 return matcher;
283 }
284 }
285 }
286
287 return empty;
288
289}
290
291
292template <class T>
293inline
294std::ostream & operator<<(std::ostream & ostr, const StringMatcherList<T> & selector){
295 selector.toStream(ostr);
296 return ostr;
297}
298
299typedef StringMatcherList<StringMatcher> KeySelector;
300
301DRAIN_TYPENAME(KeySelector);
302
303} // drain::
304
305#endif // QUANTITY_MATCHER
Definition QuantitySelector.h:37
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:321
static const SprinterLayout cmdLineLayout
Simulates how arguments are given to command line options.
Definition Sprinter.h:248
Utility for selecting a quantity label Applied by DataSelector.
Definition StringMatcherList.h:52
void setKeys(const std::initializer_list< T2 > &l)
Replace current list with a new one.
Definition StringMatcherList.h:114
void adaptKey(const matcher_t &s)
Definition StringMatcherList.h:225
StringMatcherList(const TT &... args)
Basic constructor.
Definition StringMatcherList.h:63
StringMatcherList operator=(const std::initializer_list< matcher_t > &l)
Replace current list with a new one.
Definition StringMatcherList.h:99
void insertKeys(const std::string &args)
Define the list of accepted quantities as a string.
Definition StringMatcherList.h:206
void setKeys(const std::list< T2 > &l)
Replace current list with a new one.
Definition StringMatcherList.h:123
virtual ~StringMatcherList()
Destructor.
Definition StringMatcherList.h:82
bool test(const std::string &key, bool defaultResult=true) const
Check if key is accepted.
Definition StringMatcherList.h:253
void addKey(const matcher_t &arg, const TT &... args)
Append keys to existing list.matcher_t.
Definition StringMatcherList.h:147
void setKeys(const std::string &arg, const TT &... args)
Define the list of accepted quantities.
Definition StringMatcherList.h:137
StringMatcherList operator=(const StringMatcherList< matcher_t > &l)
Replace current list with a new one.
Definition StringMatcherList.h:93
void setKeys(const StringMatcherList< matcher_t > &l)
Replace current list with a new one.
Definition StringMatcherList.h:105
StringMatcherList(const StringMatcherList< T > &l)
Copy constructor. Copies the list of quantities.
Definition StringMatcherList.h:71
static void split(const std::string &s, T &sequence, const C &separators, const std::string &trimChars=" \t\n")
Splits and trims a given std::string to a std Sequence.
Definition String.h:380
Definition DataSelector.cpp:1277
DRAIN_TYPENAME(void)
Add a specialization for each type of those you want to support.