Loading...
Searching...
No Matches
ODIMPathMatcher.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#ifndef ODIM_PATH_MATCHER
32#define ODIM_PATH_MATCHER
33
34
35//#include <drain/util/Dictionary.h>
36#include <drain/util/Flags.h>
37#include <drain/util/Range.h>
38
39#include "ODIMPath.h"
40
41
42namespace rack {
43
45/*
46 * Index range [index,indexMax] is applied in testing path elements of a same group.
47 * \see ODIMPathMatcher
48 */
50
51public:
52
53 inline
54 ODIMPathElemMatcher(ODIMPathElem::group_t groups = ROOT, index_t index = 0, index_t indexMax = INDEX_MAX) :
55 // ODIMPathElem(group, index),
56 // index(index),
57 // indexMax(indexMax),
58 flags(ODIMPathElem::getDictionary(), this->group, '|'){
59 this->group = groups;
60 this->index = index;
61 this->indexMax = indexMax;
62 };
63
64
65 inline
67 // ODIMPathElem(e),
68 // index(e.index),
69 // indexMax(e.indexMax),
70 flags(ODIMPathElem::getDictionary(), this->group, '|') {
71 this->group = e.group; // needed?
72 this->index = e.index;
73 this->indexMax = e.indexMax;
74 }
75
76 inline
78 // ODIMPathElem(e),
79 // index(e.index),
80 // indexMax(0xffff),
81 flags(ODIMPathElem::getDictionary(), this->group, '|') {
82 this->group = e.group; // needed?
83 this->index = e.index;
84 this->indexMax = INDEX_MAX; // ??
85 }
86
87 inline
88 ODIMPathElemMatcher(const std::string &s) :
89 // index(0),
90 // indexMax(0xffff),
91 flags(ODIMPathElem::getDictionary(), this->group, '|') {
92
93 // TODO: virtual reset(){}
94 group = 0;
95 index = 0;
96 this->indexMax = INDEX_MAX;
97 set(s);
98 }
99
100 virtual
102
103 // ambiguous! index 5 could mean range 5:5 or 0:0xffff ?
104
105
106 virtual
107 //bool
108 void set(const std::string &s);
109
110
111 inline
112 bool isSingle() const {
113 return (!isIndexed()) || (index == indexMax);
114 }
115
116 inline
118 index = 0;
119 indexMax = INDEX_MAX;
120 group = g;
121 return *this;
122 }
123
124 inline
125 ODIMPathElemMatcher & operator=(const std::string &s){
126 index = 0;
127 indexMax = INDEX_MAX;
128 set(s);
129 return *this;
130 }
131
132
134 bool test(const ODIMPathElem & elem) const;
135
136 // Applied only as a upper limit in path matching.
137 index_t indexMax;
138
139 //drain::Flagger flags;
140 //drain::Flagger2<drain::MultiFlagger<group_t> > flags;
142 //drain::MultiFlagger<group_t> flags;
143
144 virtual
145 std::ostream & toStream(std::ostream & sstr) const;
146
147 // FUTURE extension:
149
150
151protected:
152
154 virtual
155 void extractIndex(const std::string &s);
156
157
158 virtual
159 bool extractPrefix(const std::string &s, bool indexed);
160
161
162};
163
164
165// Note: Elem, not MatcherElem
166template <>
167void ODIMPathElem::extractIndex(const std::string &s, ODIMPathElemMatcher::idx_range_t & idx);
168
169inline
170std::ostream & operator<<(std::ostream & ostr, const ODIMPathElemMatcher & p) {
171 p.toStream(ostr);
172 return ostr;
173}
174
175//template <>
176//char drain::TextReader::scanSegmentToValue(std::istream & istr, const std::string & endChars, ODIMPathElemMatcher::idx_range_t & dst);
177
178
179
181
186class ODIMPathMatcher : public drain::Path<ODIMPathElemMatcher> {
187
188public:
189
192 separator.acceptTrailing = true;
193 }
194
197 }
198
199 template<typename ... T>
200 ODIMPathMatcher(const ODIMPathElem & elem, const T &... rest){
201 separator.acceptTrailing = true;
202 setElems(elem, rest...);
203 //updateBean(); ?
204 }
205
207 ODIMPathMatcher(const std::string & path){
208 separator.acceptTrailing = true;
209 set(path);
210 //assign(path);
211 }
212
214 ODIMPathMatcher(const char * path){
215 separator.acceptTrailing = true;
216 set(path);
217 // assign(path);
218 }
219
220
222 // void parse(const std::string & path);
223
225 bool ensureLimitingSlash();
226
227
229 bool isLiteral() const;
230
232 void extractPath(ODIMPath & path) const;
233
235 bool match(const rack::ODIMPath & path) const;
236
238 bool matchHead(const rack::ODIMPath & path) const;
239
241 bool matchTail(const rack::ODIMPath & path) const;
242
244 bool matchElem(const rack::ODIMPathElem & elem, bool defaultValue = true) const;
245
246};
247
248
249
250} // namespace rack
251
252
253#endif
Definition Flags.h:220
Definition Path.h:112
Definition Range.h:52
Element in a chain in testing a path for a match.
Definition ODIMPathMatcher.h:49
virtual void set(const std::string &s)
Assign a string to this path element.
Definition ODIMPathMatcher.cpp:46
virtual std::ostream & toStream(std::ostream &sstr) const
For string presentation.
Definition ODIMPathMatcher.cpp:345
virtual bool extractPrefix(const std::string &s, bool indexed)
Given the non-numeric prefix of a group, like "dataset" or "data", set the group.
Definition ODIMPathMatcher.cpp:140
bool test(const ODIMPathElem &elem) const
Test if the elem has the same group, and elem.index is within [index,indexMax].
Definition ODIMPathMatcher.cpp:193
virtual void extractIndex(const std::string &s)
Extracts index range of type <index>[:<indexMax>].
Definition ODIMPathMatcher.cpp:94
Definition ODIMPath.h:82
static const group_t ROOT
Definition ODIMPath.h:102
unsigned int group_t
In H5, "groups" correspond to directories or folders in file system.
Definition ODIMPath.h:92
bool isIndexed() const
Abbreviation of (group == NONE)
Definition ODIMPath.h:321
Structure for testing if a path matches a given sequence of path elements.
Definition ODIMPathMatcher.h:186
bool isLiteral() const
Checks if corresponds to a single path, implying that all the index ranges are singletons.
Definition ODIMPathMatcher.cpp:232
ODIMPathMatcher(const std::string &path)
Almost copy constructor.
Definition ODIMPathMatcher.h:207
bool matchHead(const rack::ODIMPath &path) const
Match leading part of path.
Definition ODIMPathMatcher.cpp:270
bool ensureLimitingSlash()
Resolves "where|where".
Definition ODIMPathMatcher.cpp:69
bool matchTail(const rack::ODIMPath &path) const
Match trailing part of path.
Definition ODIMPathMatcher.cpp:298
ODIMPathMatcher(const char *path)
Almost copy constructor.
Definition ODIMPathMatcher.h:214
bool matchElem(const rack::ODIMPathElem &elem, bool defaultValue=true) const
Match single element. If matcher path does not contain it, return defaultValue.
Definition ODIMPathMatcher.cpp:325
ODIMPathMatcher()
Basic constructor.
Definition ODIMPathMatcher.h:191
ODIMPathMatcher(const ODIMPathMatcher &matcher)
Copy constructor.
Definition ODIMPathMatcher.h:196
void extractPath(ODIMPath &path) const
Convert to a single path, assuming uniqueness. Future option: extract all the enumerated paths.
Definition ODIMPathMatcher.cpp:243
bool match(const rack::ODIMPath &path) const
Match the leading part of path , if matcher starts with root. Else, match the trailing part.
Definition ODIMPathMatcher.cpp:256
Definition DataSelector.cpp:1277
Definition DataSelector.cpp:44
bool & acceptTrailing
If true, allow trailing empty elements (appearing as repeated separators)
Definition Path.h:79