StringMatcher.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_KEY_MATCHER
33 #define DRAIN_KEY_MATCHER
34 
35 #include <set>
36 #include <list>
37 #include <map>
38 #include <stdexcept>
39 
40 #include <drain/Type.h>
41 #include <drain/RegExp.h>
42 
43 
44 namespace drain {
45 
47 
58 class StringMatcher : protected drain::RegExp {
59 
60 public:
61 
62  static
63  const std::string regExpSpecialChars;
64 
66  const std::string & value;
67 
68  inline
69  StringMatcher(const std::string & s = "") : value(regExpString), isRegExp(false){
70  set(s);
71  }
72 
73  inline
74  StringMatcher(const StringMatcher & matcher) : value(regExpString), isRegExp(false){
75  set(matcher.value);
76  }
77 
79 
82  void set(const std::string & s);
83 
84  inline
85  bool operator==(const std::string &s) const {
86  return test(s);
87  }
88 
89  inline
90  bool operator==(const char *s) const {
91  return test(s);
92  }
93 
94  inline
95  bool operator==(const StringMatcher &m) const {
96  return (this->value == m.value) && (this->isRegExp == m.isRegExp);
97  }
98 
100 
103  bool test(const std::string & s) const;
104 
105  const std::string & getType() const;
106 
108 
115 protected:
116 
117  bool isRegExp;
118 
119 
120 };
121 
122 
123 inline
124 std::ostream & operator<<(std::ostream & ostr, const StringMatcher & m){
125  ostr << m.value; // string or regExp string.
126  return ostr;
127 }
128 
129 DRAIN_TYPENAME(StringMatcher);
130 
131 } // drain::
132 
133 #endif
Definition: RegExp.h:58
General-purpose key matcher: tests string equality, or regExp, if defined as such.
Definition: StringMatcher.h:58
void set(const std::string &s)
Assign string which may be literal or a regular expression.
Definition: StringMatcher.cpp:46
bool isRegExp
Checks if the key conforms to ODIM convention: DBZH, VRAD, etc. (capital letters, underscores)
Definition: StringMatcher.h:117
const std::string & value
Visible reference to the RegExp.
Definition: StringMatcher.h:66
bool test(const std::string &s) const
Test with direct string matching or regExp, if defined.
Definition: StringMatcher.cpp:70
Definition: DataSelector.cpp:1277