StringMatcherList.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25  */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood 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 
46 namespace drain {
47 
48 
49 
51 template <class T=std::string>
52 class StringMatcherList : public std::list<T> { // string temporary ?
53 
54 public:
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 
69  inline
70  StringMatcherList(const StringMatcherList<T> &slct) : list_t(slct) {
71  };
72 
73 
75  virtual inline
77 
78 
80 
85  template <typename ...TT>
86  void setKey(const std::string & arg, const TT & ... args){
87  //clear();
88  setKeys(arg); // split and adapt
89  addKey(args...);
90  }
91 
93 
97  void setKeys(const std::string & args); // , const std::string & separators = ","
98 
100 
105  //template <typename T, typename ...TT>
106  template <typename ...TT>
107  void addKey(const std::string & arg, const TT & ... args){
108  adaptKey(arg);
109  addKey(args...);
110  }
111 
112 
114 
119  bool test(const std::string & key, bool defaultResult = true) const;
120 
121  inline
122  bool isSet() const {
123  return !this->empty();
124  //return (regExp.isSet() || !quantities.empty()) ;
125  }
126 
127  inline
128  const list_t & getList() const {
129  return *this;
130  }
131 
132  inline
133  void toStream(std::ostream & ostr) const {
135  }
136 
137 protected:
138 
139  // inline
140  // void setQuantity(){};
141 
142  inline
143  void addKey(){};
144 
145  void adaptKey(const std::string & s);
146 
148  // mutable // called by updateBean()?
149  // drain::RegExp regExp;
150 
151  // mutable
152  // list_t quantities;
153 
154 
155 };
156 
157 template <class T>
158 void StringMatcherList<T>::setKeys(const std::string & args){ //, const std::string & separators){
159  drain::Logger mout(__FILE__, __FUNCTION__);
160  this->clear();
161  std::vector<std::string> argv;
162  //const char s =
163  drain::StringTools::split(args, argv, ",:"); //separators);
164  for (const std::string & arg: argv){
165  // mout.attention("adapting: ", arg, " split by '", separators, "'");
166  adaptKey(arg);
167  }
168 
169 }
170 
171 
175 template <class T>
176 void StringMatcherList<T>::adaptKey(const std::string & s){
177 
178  if (s.empty()){
179  return;
180  }
181  else {
182 
183  matcher_t matcher(s);
184  for (const matcher_t & m: *this){
185  if (m == matcher){ // two matchers are equal if they accept and reject equally.
186  // exists already, dont append...
187  return;
188  }
189  }
190 
191  this->push_back(matcher);
192  // Save copying?
193  /*
194  push_back(KeyMatcher());
195  back().set(s);
196  */
197  }
198 
199 }
200 
201 
202 
203 template <class T>
204 bool StringMatcherList<T>::test(const std::string & key, bool defaultResult) const {
205 
206  drain::Logger mout(__FILE__, __FUNCTION__);
207 
208  if (this->empty()){
209  return defaultResult; // Alternative: could need empty k - but on the other hand, why add it in a list, as it accepts anything.
210  }
211  else {
212  for (const auto & k: *this){
213  // mout.experimental("testing [", s, "] vs [", q, "]");
214  //if (k.test(key)){ //
215  if (k == key){ //
216  return true;
217  }
218  }
219  }
220 
221  return false;
222 
223 }
224 
225 template <class T>
226 inline
227 std::ostream & operator<<(std::ostream & ostr, const StringMatcherList<T> & selector){
228  selector.toStream(ostr);
229  return ostr;
230 }
231 
232 typedef StringMatcherList<StringMatcher> KeySelector;
233 
234 DRAIN_TYPENAME(KeySelector);
235 
236 } // drain::
237 
238 #endif // QUANTITY_MATCHER
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition: Log.h:308
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
StringMatcherList(const TT &... args)
Basic constructor.
Definition: StringMatcherList.h:63
void adaptKey(const std::string &s)
Definition: StringMatcherList.h:176
void addKey(const std::string &arg, const TT &... args)
Define a syntax for quantity key. Will be checked if listed quantity keys do not match.
Definition: StringMatcherList.h:107
virtual ~StringMatcherList()
Destructor.
Definition: StringMatcherList.h:76
bool test(const std::string &key, bool defaultResult=true) const
Check if key is accepted.
Definition: StringMatcherList.h:204
StringMatcherList(const StringMatcherList< T > &slct)
Copy constructor. Copies the list of quantities.
Definition: StringMatcherList.h:70
void setKey(const std::string &arg, const TT &... args)
Define the list of accepted quantities.
Definition: StringMatcherList.h:86
void setKeys(const std::string &args)
Define the list of accepted quantities as a string.
Definition: StringMatcherList.h:158
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:309
Definition: DataSelector.cpp:1277