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
60 /*
61 inline
62 StringMatcherList(){
63 };
64 */
65
67
68 template <typename ... TT>
69 inline
70 StringMatcherList(const TT &... args){
71 // reset not needed
72 addKey(args...);
73 };
74
75
77 inline
79 };
80
81 inline
82 StringMatcherList(std::initializer_list<matcher_t> l){
83 setKeys(l);
84 };
85
87 /*
88 template <class T2>
89 StringMatcherList(const std::initializer_list<T2> & l){
90 setKeys(l);
91 };
92 */
93
94
95
97 virtual inline
99
100 inline
102 setKeys(l);
103 return *this;
104 }
105
106 StringMatcherList operator=(const std::initializer_list<matcher_t> &l){
107 setKeys(l);
108 return *this;
109 }
110
111
112 /*
113 StringMatcherList operator=(StringMatcherList<matcher_t> l){
114 setKeys(l);
115 return *this;
116 }
117 */
118
119 /*
120 template <class T2>
121 StringMatcherList operator=(const std::initializer_list<T2> &l){
122 // setKeys(l);
123 return *this;
124 }
125 */
126
127 void setKeys(const StringMatcherList<matcher_t> & l){
128 this->clear();
129 for (const matcher_t & key: l) {
130 this->push_back(key);
131 }
132 }
133
134 template <class T2>
135 void setKeys(const std::initializer_list<T2> & l){
136 this->clear();
137 for (const T2 & entry: l) {
138 adaptKey(entry);
139 }
140 };
141
142 template <class T2>
143 void setKeys(const std::list<T2> & l){
144 this->clear();
145 for (const T2 & key: l) {
146 adaptKey(key);
147 }
148 }
149
151
156 template <typename ...TT>
157 void setKeys(const std::string & arg, const TT & ... args){
158 //clear();
159 insertKeys(arg); // split and adapt
160 addKey(args...);
161 }
162
163
165 template <typename ...TT>
166 //void addKey(const std::string & arg, const TT & ... args){
167 void addKey(const matcher_t & arg, const TT & ... args){
168 adaptKey(arg);
169 addKey(args...);
170 }
171
172
174
179 bool test(const std::string & key, bool defaultResult = true) const;
180
181 const matcher_t & retrieve(const std::string & key) const;
182
183
184 inline
185 bool isSet() const {
186 return !this->empty();
187 //return (regExp.isSet() || !quantities.empty()) ;
188 }
189
190 inline
191 const list_t & getList() const {
192 return *this;
193 }
194
195 inline
196 void toStream(std::ostream & ostr) const {
198 }
199
200protected:
201
203
207 void insertKeys(const std::string & args); // , const std::string & separators = ","
208
209 inline
210 void addKey(){};
211
212 //void adaptKey(const std::string & s);
213 void adaptKey(const matcher_t & s);
214
216 // mutable // called by updateBean()?
217 // drain::RegExp regExp;
218
219 // mutable
220 // list_t quantities;
221
222
223};
224
225template <class T>
226void StringMatcherList<T>::insertKeys(const std::string & args){ //, const std::string & separators){
227 // drain::Logger mout(__FILE__, __FUNCTION__);
228 this->clear();
229 std::vector<std::string> argv;
230 //const char s =
231 drain::StringTools::split(args, argv, ",:"); //separators);
232 for (const std::string & arg: argv){
233 // mout.attention("adapting: ", arg, " split by '", separators, "'");
234 adaptKey(arg);
235 }
236
237}
238
239
243template <class T>
244//void StringMatcherList<T>::adaptKey(const std::string & s){
245void StringMatcherList<T>::adaptKey(const matcher_t & matcher){
246
247 if (matcher.empty()){
248 return;
249 }
250 else {
251
252 //matcher_t matcher(s);
253 for (const matcher_t & m: *this){
254 if (m == matcher){ // two matchers are equal if they accept and reject equally.
255 // exists already, dont append...
256 return;
257 }
258 }
259
260 this->push_back(matcher);
261 // Save copying?
262 /*
263 push_back(KeyMatcher());
264 back().set(s);
265 */
266 }
267
268}
269
270
271
272template <class T>
273bool StringMatcherList<T>::test(const std::string & key, bool defaultResult) const {
274
275 if (this->empty()){
276 return defaultResult; // Alternative: could need empty k - but on the other hand, why add it in a list, as it accepts anything.
277 }
278 else {
279 for (const auto & k: *this){
280 if (k == key){ //
281 return true;
282 }
283 }
284 }
285
286 return false;
287
288}
289
290template <class T>
291const typename StringMatcherList<T>::matcher_t & StringMatcherList<T>::retrieve(const std::string & key) const {
292
293 static
294 const matcher_t empty;
295
296 if (this->empty()){
297 return empty; // Alternative: could need empty k - but on the other hand, why add it in a list, as it accepts anything.
298 }
299 else {
300 for (const auto & matcher: *this){
301 if (matcher == key){ //
302 return matcher;
303 }
304 }
305 }
306
307 return empty;
308
309}
310
311
312template <class T>
313inline
314std::ostream & operator<<(std::ostream & ostr, const StringMatcherList<T> & selector){
315 selector.toStream(ostr);
316 return ostr;
317}
318
319typedef StringMatcherList<StringMatcher> KeySelector;
320
321DRAIN_TYPENAME(KeySelector);
322
323} // drain::
324
325#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: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 adaptKey(const matcher_t &s)
Definition StringMatcherList.h:245
StringMatcherList(const TT &... args)
Basic constructor.
Definition StringMatcherList.h:70
void insertKeys(const std::string &args)
Define the list of accepted quantities as a string.
Definition StringMatcherList.h:226
virtual ~StringMatcherList()
Initialiser constructor.
Definition StringMatcherList.h:98
bool test(const std::string &key, bool defaultResult=true) const
Check if key is accepted.
Definition StringMatcherList.h:273
void addKey(const matcher_t &arg, const TT &... args)
Append keys to existing list.matcher_t.
Definition StringMatcherList.h:167
void setKeys(const std::string &arg, const TT &... args)
Define the list of accepted quantities.
Definition StringMatcherList.h:157
StringMatcherList(const StringMatcherList< T > &l)
Copy constructor. Copies the list of quantities.
Definition StringMatcherList.h:78
General-purpose key matcher: tests string equality, or regExp, if defined as such.
Definition StringMatcher.h:58
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.