ADTF
Loading...
Searching...
No Matches
substream_name_filter.h
Go to the documentation of this file.
1
7#pragma once
8
9#include <stdint.h>
11
12#include <regex>
13
14namespace adtf
15{
16namespace filter
17{
19{
25{
26public:
30 enum class tFilterMode : uint8_t
31 {
32 Plain = 0,
33 PrefixTrim = 1,
34 Prefix = 2,
35 Wildcard = 3,
36 Regex = 4
37 };
38
39public:
44 {
45 protected:
51 tFilterBase(std::string_view strFilterString): strFilter(strFilterString)
52 {
53 }
54
56 std::string strFilter;
57
58 public:
64 virtual bool Matches(std::string_view strInput) const noexcept = 0;
65
66 public:
71 inline std::string_view GetFilterString() const noexcept
72 {
73 return strFilter;
74 }
75
80 virtual bool GetTrim() const noexcept
81 {
82 return false;
83 }
84
90 virtual bool PrefixMatches(std::string_view strPrefix) const noexcept
91 {
92 return strPrefix.substr(0, strFilter.length()) == GetFilterString().substr(0, strPrefix.length());
93 }
94 };
95
99 struct tPlain final : public tFilterBase
100 {
101 public:
106 tPlain(std::string_view strFilterString): tFilterBase(strFilterString)
107 {
108 }
109
111 inline bool Matches(std::string_view strInput) const noexcept override
112 {
113 return strFilter == strInput;
114 }
115
121 inline bool operator==(const tPlain& oOther) const noexcept
122 {
123 return strFilter == oOther.strFilter;
124 }
125 };
126
130 struct tPrefix final : public tFilterBase
131 {
132 protected:
134 bool bTrim;
135
136 public:
142 tPrefix(std::string_view strFilterString, bool bTrim): tFilterBase(strFilterString), bTrim(bTrim)
143 {
144 }
145
149 inline bool GetTrim() const noexcept override
150 {
151 return bTrim;
152 }
153
155 inline bool Matches(std::string_view strInput) const noexcept override
156 {
157 return strInput.substr(0, strFilter.length()) == strFilter;
158 }
159
165 inline bool operator==(const tPrefix& oOther) const noexcept
166 {
167 return strFilter == oOther.strFilter && bTrim == oOther.bTrim;
168 }
169 };
170
175 {
176 private:
178 std::regex oRegex;
179
180 protected:
185 tRegexFilterBase(std::string_view strFilterString, std::string_view strRegex):
186 tFilterBase(strFilterString), oRegex(strRegex.begin(), strRegex.end())
187 {
188 }
189
190 public:
192 inline bool Matches(std::string_view strInput) const noexcept final override
193 {
194 return std::regex_match(strInput.begin(), strInput.end(), oRegex);
195 }
196
198 inline bool PrefixMatches(std::string_view /* strPrefix */) const noexcept final override
199 {
200 return true;
201 }
202 };
203
207 struct tRegex final : public tRegexFilterBase
208 {
209 public:
214 tRegex(std::string_view strFilterString): tRegexFilterBase(strFilterString, strFilterString)
215 {
216 }
217
223 inline bool operator==(const tRegex& oOther) const noexcept
224 {
225 return strFilter == oOther.strFilter;
226 }
227 };
228
232 struct tWildcard final : public tRegexFilterBase
233 {
234 public:
239 tWildcard(std::string_view strFilterString):
240 tRegexFilterBase(strFilterString, cSubstreamNameFilter::WildcardToRegex(std::string(strFilterString)))
241 {
242 }
243
249 inline bool operator==(const tWildcard& oOther) const noexcept
250 {
251 return strFilter == oOther.strFilter;
252 }
253 };
254
256 using tFilter = std::variant<tPlain, tPrefix, tWildcard, tRegex>;
258 using tStreamAttributes = std::map<std::string, std::variant<bool, double, std::string>>;
259
264 {
269
271 inline std::string_view GetFilterString() const noexcept
272 {
273 return std::visit([](auto&& oFilter) noexcept -> std::string_view { return oFilter.GetFilterString(); },
274 xFilter);
275 }
276
278 inline bool GetTrim() const noexcept
279 {
280 return std::visit([](auto&& oFilter) noexcept -> bool { return oFilter.GetTrim(); }, xFilter);
281 }
282
284 inline bool Matches(std::string_view strInput) const noexcept
285 {
286 return std::visit([&](auto&& oFilter) noexcept -> bool { return oFilter.Matches(strInput); }, xFilter);
287 }
288
290 inline bool PrefixMatches(std::string_view strPrefix) const noexcept
291 {
292 return std::visit([&](auto&& oFilter) noexcept -> bool { return oFilter.PrefixMatches(strPrefix); },
293 xFilter);
294 }
295
301 inline bool HasAttribute(const std::string& strAttributeName) const noexcept
302 {
303 return oAttributes.find(strAttributeName) != oAttributes.end();
304 }
305
312 template<typename T>
313 inline T GetAttributeValue(const std::string& strAttributeName) const
314 {
315 if (auto it = oAttributes.find(strAttributeName); it != oAttributes.end())
316 {
317 if (std::holds_alternative<T>(it->second))
318 {
319 return std::get<T>(it->second);
320 }
321 else
322 {
323 THROW_ERROR_DESC(ERR_INVALID_TYPE, "There is no attribute with the requested type.");
324 }
325 }
326 else
327 {
328 THROW_ERROR_DESC(ERR_NOT_FOUND, "Attribute '%s' not found.", strAttributeName.c_str());
329 }
330 }
331 };
332
333private:
337 class ISubstreamNameFilterParser
338 {
339 public:
343 struct tContext
344 {
346 std::optional<tFilterMode> eFilterMode{};
347 };
348
349 public:
350 virtual std::vector<tStreamFilter> ReadFromFile(std::string_view strFileName,
351 const tContext& oContext) const = 0;
352 virtual void WriteToFile(std::string_view strFileName,
353 const std::vector<tStreamFilter>& oFilters,
354 const tContext& oContext) const = 0;
355 };
356
357 class cTextParser;
358 class cJsonParser;
359
360public:
365 static std::string WildcardToRegex(std::string_view strString);
366
372 static std::string EscapeRegularExpression(std::string_view strString);
373
392 cSubstreamNameFilter(std::string_view strFileName, std::optional<tFilterMode> oTextModeOverride);
397 cSubstreamNameFilter(std::vector<tStreamFilter> oStreamFilters) noexcept;
402
407 void Reset(std::vector<tStreamFilter> oStreamFilters = {}) noexcept;
408
414 void AddFilter(const tFilter& oFilter, const tStreamAttributes& oAttributes = {});
415
422 void AddFilterFile(std::string_view strFileName, std::optional<tFilterMode> oTextModeOverride);
423
431 void SaveFiltersToFile(std::string_view strFileName, std::optional<tFilterMode> oTextModeOverride);
432
437 tResult RemoveFilter(const tFilter& oFilter) noexcept;
438
442 std::vector<tStreamFilter> GetStreamFilters() const& noexcept;
443
447 std::vector<tStreamFilter>&& GetStreamFilters() && noexcept;
448
453 bool Matches(std::string_view strName) const noexcept;
454
459 bool PrefixMatches(std::string_view strName) const;
460
469 void Select(std::string_view strPrefix,
470 const adtf::streaming::ant::IStreamType& oType,
471 const std::function<void(std::string_view,
472 uint32_t,
473 const tStreamFilter&,
474 const adtf::ucom::ant::iobject_ptr<const adtf::streaming::ant::IStreamType>&)>&
475 fnSelectedCallback,
476 bool bInverseMatch = false) const;
477
485 void Select(const adtf::streaming::ant::IStreamType& oType,
486 const std::function<void(std::string_view,
487 uint32_t,
488 const tStreamFilter&,
489 const adtf::ucom::ant::iobject_ptr<const adtf::streaming::ant::IStreamType>&)>&
490 fnSelectedCallback,
491 bool bInverseMatch = false) const;
492
493private:
494 static const ISubstreamNameFilterParser& GetFileParserFor(std::string_view strExtension);
495
496private:
497 std::vector<tStreamFilter> m_oStreamFilters;
498};
499
500} // namespace private_implementations
501} // namespace filter
502} // namespace adtf
#define THROW_ERROR_DESC(_code,...)
throws a tResult exception
Definition result.h:85
A_UTILS_NS::cResult tResult
For backwards compatibility and to bring latest version into scope.
Definition result.h:736
void AddFilterFile(std::string_view strFileName, std::optional< tFilterMode > oTextModeOverride)
std::variant< tPlain, tPrefix, tWildcard, tRegex > tFilter
Variant holding the filter specialization.
Definition substream_name_filter.h:256
void Reset(std::vector< tStreamFilter > oStreamFilters={}) noexcept
static std::string EscapeRegularExpression(std::string_view strString)
std::vector< tStreamFilter > GetStreamFilters() const &noexcept
void Select(std::string_view strPrefix, const adtf::streaming::ant::IStreamType &oType, const std::function< void(std::string_view, uint32_t, const tStreamFilter &, const adtf::ucom::ant::iobject_ptr< const adtf::streaming::ant::IStreamType > &)> &fnSelectedCallback, bool bInverseMatch=false) const
tResult RemoveFilter(const tFilter &oFilter) noexcept
static std::string WildcardToRegex(std::string_view strString)
tFilterMode
Definition substream_name_filter.h:31
std::map< std::string, std::variant< bool, double, std::string > > tStreamAttributes
Storage type for additional stream attributes.
Definition substream_name_filter.h:258
bool Matches(std::string_view strName) const noexcept
void SaveFiltersToFile(std::string_view strFileName, std::optional< tFilterMode > oTextModeOverride)
void AddFilter(const tFilter &oFilter, const tStreamAttributes &oAttributes={})
bool PrefixMatches(std::string_view strName) const
Namespace for all functionality of the ADTF Filter SDK provided since v3.0.
Definition filter.h:26
Namespace for all internally used functionality implemented..
Definition configurable_runner.h:20
Namespace for the ADTF Filter SDK.
Definition configurable_runner.h:18
Namespace for the ADTF Streaming SDK.
Definition bindingproxyinport.h:14
Namespace for the ADTF uCOM SDK.
Definition adtf_system.h:324
Namespace for all functionality provided by ADTF and its SDKs.
Definition adtf_client_connector.h:14
std::optional< tFilterMode > eFilterMode
Filter mode used for specifying when reading from text files.
Definition substream_name_filter.h:346
virtual bool GetTrim() const noexcept
Definition substream_name_filter.h:80
virtual bool Matches(std::string_view strInput) const noexcept=0
std::string strFilter
The actual filter string.
Definition substream_name_filter.h:56
virtual bool PrefixMatches(std::string_view strPrefix) const noexcept
Definition substream_name_filter.h:90
tFilterBase(std::string_view strFilterString)
Definition substream_name_filter.h:51
std::string_view GetFilterString() const noexcept
Definition substream_name_filter.h:71
tPlain(std::string_view strFilterString)
Definition substream_name_filter.h:106
bool Matches(std::string_view strInput) const noexcept override
Definition substream_name_filter.h:111
bool operator==(const tPlain &oOther) const noexcept
Definition substream_name_filter.h:121
bool GetTrim() const noexcept override
Definition substream_name_filter.h:149
bool Matches(std::string_view strInput) const noexcept override
Definition substream_name_filter.h:155
tPrefix(std::string_view strFilterString, bool bTrim)
Definition substream_name_filter.h:142
bool operator==(const tPrefix &oOther) const noexcept
Definition substream_name_filter.h:165
bool bTrim
Whether to trim the output.
Definition substream_name_filter.h:134
bool PrefixMatches(std::string_view) const noexcept final override
Definition substream_name_filter.h:198
bool Matches(std::string_view strInput) const noexcept final override
Definition substream_name_filter.h:192
tRegexFilterBase(std::string_view strFilterString, std::string_view strRegex)
Definition substream_name_filter.h:185
tRegex(std::string_view strFilterString)
Definition substream_name_filter.h:214
bool operator==(const tRegex &oOther) const noexcept
Definition substream_name_filter.h:223
bool PrefixMatches(std::string_view strPrefix) const noexcept
Definition substream_name_filter.h:290
bool Matches(std::string_view strInput) const noexcept
Definition substream_name_filter.h:284
tFilter xFilter
The filter specifying which matching rules apply.
Definition substream_name_filter.h:266
tStreamAttributes oAttributes
Additional stream attributes.
Definition substream_name_filter.h:268
bool GetTrim() const noexcept
Definition substream_name_filter.h:278
std::string_view GetFilterString() const noexcept
Definition substream_name_filter.h:271
bool HasAttribute(const std::string &strAttributeName) const noexcept
Definition substream_name_filter.h:301
T GetAttributeValue(const std::string &strAttributeName) const
Definition substream_name_filter.h:313
tWildcard(std::string_view strFilterString)
Definition substream_name_filter.h:239
bool operator==(const tWildcard &oOther) const noexcept
Definition substream_name_filter.h:249