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
36#include <drain/RegExp.h>
37#include <drain/TypeName.h>
38
39
40namespace drain {
41
43
54class StringMatcher : protected drain::RegExp {
55
56public:
57
63 static
64 const std::string & regExpSpecialChars();
65
67 const std::string & value;
68
69 inline
70 StringMatcher(const std::string & s = "") : value(regExpString), isRegExp(false){
71 set(s);
72 }
73
74 inline
75 StringMatcher(const char *s) : value(regExpString), isRegExp(false){
76 set(s);
77 }
78
79 inline
80 StringMatcher(const StringMatcher & matcher) : RegExp(), value(regExpString), isRegExp(false){
81 set(matcher.value);
82 }
83
84 inline
85 bool empty() const {
86 return RegExp::empty();
87 };
88
90
93 void set(const std::string & s);
94
95 inline
96 StringMatcher & operator=(const std::string &s){
97 set(s);
98 return *this;
99 }
100
101 inline
102 StringMatcher & operator=(const StringMatcher &s){
103 set(s.value);
104 return *this;
105 }
106
107
108
109 inline
110 bool operator==(const std::string &s) const {
111 return test(s);
112 }
113
114 inline
115 bool operator==(const char *s) const {
116 return test(s);
117 }
118
119 inline
120 bool operator==(const StringMatcher &m) const {
121 return (this->value == m.value) && (this->isRegExp == m.isRegExp);
122 }
123
125
128 bool test(const std::string & s) const;
129
130 const std::string & getType() const;
131
133
140protected:
141
143
144
145};
146
147
148inline
149std::ostream & operator<<(std::ostream & ostr, const StringMatcher & m){
150 ostr << m.value; // string or regExp string.
151 return ostr;
152}
153
154DRAIN_TYPENAME(StringMatcher);
155
156} // drain::
157
158#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:54
void set(const std::string &s)
Assign a string – which may be a literal or a regular expression.
Definition StringMatcher.cpp:49
static const std::string & regExpSpecialChars()
Definition StringMatcher.cpp:42
bool isRegExp
Checks if the key conforms to ODIM convention: DBZH, VRAD, etc. (capital letters, underscores)
Definition StringMatcher.h:142
const std::string & value
A visible reference to the current string / regExp.
Definition StringMatcher.h:67
bool test(const std::string &s) const
Test with direct string matching or regExp, if defined.
Definition StringMatcher.cpp:73
Definition Data.h:1473