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