Loading...
Searching...
No Matches
StringMatcher.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_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
44namespace drain {
45
47
58class StringMatcher : protected drain::RegExp {
59
60public:
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 char *s) : value(regExpString), isRegExp(false){
75 set(s);
76 }
77
78 inline
79 StringMatcher(const StringMatcher & matcher) : value(regExpString), isRegExp(false){
80 set(matcher.value);
81 }
82
83 inline
84 bool empty() const {
85 return RegExp::empty();
86 };
87
89
92 void set(const std::string & s);
93
94 inline
95 StringMatcher & operator=(const std::string &s){
96 set(s);
97 return *this;
98 }
99
100 inline
101 StringMatcher & operator=(const StringMatcher &s){
102 set(s.value);
103 return *this;
104 }
105
106
107
108 inline
109 bool operator==(const std::string &s) const {
110 return test(s);
111 }
112
113 inline
114 bool operator==(const char *s) const {
115 return test(s);
116 }
117
118 inline
119 bool operator==(const StringMatcher &m) const {
120 return (this->value == m.value) && (this->isRegExp == m.isRegExp);
121 }
122
124
127 bool test(const std::string & s) const;
128
129 const std::string & getType() const;
130
132
139protected:
140
142
143
144};
145
146
147inline
148std::ostream & operator<<(std::ostream & ostr, const StringMatcher & m){
149 ostr << m.value; // string or regExp string.
150 return ostr;
151}
152
153DRAIN_TYPENAME(StringMatcher);
154
155} // drain::
156
157#endif
Definition RegExp.h:58
bool empty() const
Returns true, if expression is empty.
Definition RegExp.h:99
General-purpose key matcher: tests string equality, or regExp, if defined as such.
Definition StringMatcher.h:58
void set(const std::string &s)
Assign a string – which may be a 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:141
const std::string & value
A visible reference to the current string / 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
DRAIN_TYPENAME(void)
Add a specialization for each type of those you want to support.