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
182 void insertKeys(const std::string & args); // , const std::string & separators = ","
183
184 inline
185 void addKey(){};
186
187 //void adaptKey(const std::string & s);
188 void adaptKey(const matcher_t & s);
189
191 // mutable // called by updateBean()?
192 // drain::RegExp regExp;
193
194 // mutable
195 // list_t quantities;
196
197
198};
199
200template <class T>
201void StringMatcherList<T>::insertKeys(const std::string & args){ //, const std::string & separators){
202 // drain::Logger mout(__FILE__, __FUNCTION__);
203 this->clear();
204 std::vector<std::string> argv;
205 //const char s =
206 drain::StringTools::split(args, argv, ",:"); //separators);
207 for (const std::string & arg: argv){
208 // mout.attention("adapting: ", arg, " split by '", separators, "'");
209 adaptKey(arg);
210 }
211
212}
213
214
218template <class T>
219//void StringMatcherList<T>::adaptKey(const std::string & s){
220void StringMatcherList<T>::adaptKey(const matcher_t & matcher){
221
222 if (matcher.empty()){
223 return;
224 }
225 else {
226
227 //matcher_t matcher(s);
228 for (const matcher_t & m: *this){
229 if (m == matcher){ // two matchers are equal if they accept and reject equally.
230 // exists already, dont append...
231 return;
232 }
233 }
234
235 this->push_back(matcher);
236 // Save copying?
237 /*
238 push_back(KeyMatcher());
239 back().set(s);
240 */
241 }
242
243}
244
245
246
247template <class T>
248bool StringMatcherList<T>::test(const std::string & key, bool defaultResult) const {
249
250 if (this->empty()){
251 return defaultResult; // Alternative: could need empty k - but on the other hand, why add it in a list, as it accepts anything.
252 }
253 else {
254 for (const auto & k: *this){
255 if (k == key){ //
256 return true;
257 }
258 }
259 }
260
261 return false;
262
263}
264
265template <class T>
266const typename StringMatcherList<T>::matcher_t & StringMatcherList<T>::retrieve(const std::string & key) const {
267
268 static
269 const matcher_t empty;
270
271 if (this->empty()){
272 return empty; // Alternative: could need empty k - but on the other hand, why add it in a list, as it accepts anything.
273 }
274 else {
275 for (const auto & matcher: *this){
276 if (matcher == key){ //
277 return matcher;
278 }
279 }
280 }
281
282 return empty;
283
284}
285
286
287template <class T>
288inline
289std::ostream & operator<<(std::ostream & ostr, const StringMatcherList<T> & selector){
290 selector.toStream(ostr);
291 return ostr;
292}
293
294typedef StringMatcherList<StringMatcher> KeySelector;
295
296DRAIN_TYPENAME(KeySelector);
297
298} // drain::
299
300#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: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:220
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:201
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:248
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
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:516
Definition Data.h:1473
std::ostream & operator<<(std::ostream &ostr, const PlainData< DT > &d)
Definition Data.h:631