RTC Toolkit 4.0.1
Loading...
Searching...
No Matches
dataPointPath.hpp
Go to the documentation of this file.
1
13#ifndef RTCTK_COMPONENTFRAMEWORK_DATAPOINTPATH_HPP
14#define RTCTK_COMPONENTFRAMEWORK_DATAPOINTPATH_HPP
15
16#include <algorithm>
17#include <iostream>
18#include <regex>
19#include <stdexcept>
20#include <string>
21#include <string_view>
22#include <type_traits>
23#include <utility>
24#include <vector>
25
26#include <fmt/format.h>
27#include <mal/utility/Uri.hpp>
28
31
33
74public:
80 public:
81 explicit InvalidPathException(const std::string& path);
82 };
83
84 DataPointPath() = default;
85 DataPointPath(const DataPointPath&) = default;
89 virtual ~DataPointPath() = default;
90
97 explicit DataPointPath(const std::string& new_path);
98
105 explicit DataPointPath(std::string&& new_path);
106
113 explicit DataPointPath(const char* const new_path) : DataPointPath(std::string(new_path)) {
114 }
115
119 const std::string& ToString() const noexcept;
120
127 DataPointPath& operator=(const std::string& new_path);
128
135 DataPointPath& operator=(std::string&& new_path);
136
143 operator std::string() const noexcept;
144
145 // Append Operators
154 DataPointPath& operator+=(const DataPointPath& rhs);
166 DataPointPath& operator/=(const DataPointPath& rhs);
167
176 friend DataPointPath operator+(DataPointPath lhs, const DataPointPath& rhs);
177
188 friend DataPointPath operator/(DataPointPath lhs, const DataPointPath& rhs);
189
193 friend std::ostream& operator<<(std::ostream& out, const DataPointPath& rhs);
194
195 // Comparison operators for DataPointPath, char * and string
196 friend bool operator==(const DataPointPath& lhs, const char* rhs) noexcept;
197 friend bool operator!=(const DataPointPath& lhs, const char* rhs) noexcept {
198 return !(lhs == rhs);
199 }
200 friend bool operator<(const DataPointPath& lhs, const char* rhs) noexcept;
201 friend bool operator<=(const DataPointPath& lhs, const char* rhs) noexcept;
202 friend bool operator>(const DataPointPath& lhs, const char* rhs) noexcept;
203 friend bool operator>=(const DataPointPath& lhs, const char* rhs) noexcept;
204
205 friend bool operator==(const DataPointPath& lhs, const std::string& rhs) noexcept;
206 friend bool operator!=(const DataPointPath& lhs, const std::string& rhs) noexcept {
207 return !(lhs == rhs);
208 }
209 friend bool operator<(const DataPointPath& lhs, const std::string& rhs) noexcept;
210 friend bool operator<=(const DataPointPath& lhs, const std::string& rhs) noexcept;
211 friend bool operator>(const DataPointPath& lhs, const std::string& rhs) noexcept;
212 friend bool operator>=(const DataPointPath& lhs, const std::string& rhs) noexcept;
213
214 friend bool operator==(const DataPointPath& lhs, const DataPointPath& rhs) noexcept;
215 friend bool operator!=(const DataPointPath& lhs, const DataPointPath& rhs) noexcept {
216 return !(lhs == rhs);
217 }
218 friend bool operator<(const DataPointPath& lhs, const DataPointPath& rhs) noexcept;
219 friend bool operator<=(const DataPointPath& lhs, const DataPointPath& rhs) noexcept;
220 friend bool operator>(const DataPointPath& lhs, const DataPointPath& rhs) noexcept;
221 friend bool operator>=(const DataPointPath& lhs, const DataPointPath& rhs) noexcept;
222
223 // symmetry
224 friend bool operator==(const char* lhs, const DataPointPath& rhs) noexcept {
225 return rhs == lhs;
226 }
227 friend bool operator!=(const char* lhs, const DataPointPath& rhs) noexcept {
228 return !(rhs == lhs);
229 }
230 friend bool operator<(const char* lhs, const DataPointPath& rhs) noexcept {
231 return rhs > lhs;
232 }
233 friend bool operator<=(const char* lhs, const DataPointPath& rhs) noexcept {
234 return rhs >= lhs;
235 }
236 friend bool operator>(const char* lhs, const DataPointPath& rhs) noexcept {
237 return rhs < lhs;
238 }
239 friend bool operator>=(const char* lhs, const DataPointPath& rhs) noexcept {
240 return rhs <= lhs;
241 }
242
243 friend bool operator==(const std::string& lhs, const DataPointPath& rhs) noexcept {
244 return rhs == lhs;
245 }
246 friend bool operator!=(const std::string& lhs, const DataPointPath& rhs) noexcept {
247 return !(rhs == lhs);
248 }
249 friend bool operator<(const std::string& lhs, const DataPointPath& rhs) noexcept {
250 return rhs > lhs;
251 }
252 friend bool operator<=(const std::string& lhs, const DataPointPath& rhs) noexcept {
253 return rhs >= lhs;
254 }
255 friend bool operator>(const std::string& lhs, const DataPointPath& rhs) noexcept {
256 return rhs < lhs;
257 }
258 friend bool operator>=(const std::string& lhs, const DataPointPath& rhs) noexcept {
259 return rhs <= lhs;
260 }
261
265 friend std::istream& operator>>(std::istream& input, DataPointPath& path);
276 elt::mal::Uri ToRepositoryURI(const elt::mal::Uri& base_uri) const;
277
287 std::pair<DataPointPath, DataPointPath> Split() const;
288
295 DataPointPath BasePath() const;
296
304
312 std::vector<DataPointPath> SplitAll() const;
313
327 static elt::mal::Uri AppendSuffixToUriPath(const std::string& suffix, const elt::mal::Uri& uri);
328
329private:
335 static bool ValidPath(const std::string& path);
336
337 std::string m_path;
338
340 const static char VALID_CHARACTERS[];
341
343 const static std::regex REGEX_VALID_CHARS;
344}; // class DataPointPath
345
346DataPointPath operator"" _dppath(const char* str, std::size_t len);
347
348// inline implementations
349
350inline bool operator==(const DataPointPath& lhs, const char* rhs) noexcept {
351 return lhs.m_path == rhs;
352}
353
354inline bool operator<(const DataPointPath& lhs, const char* rhs) noexcept {
355 return lhs.m_path < rhs;
356}
357
358inline bool operator<=(const DataPointPath& lhs, const char* rhs) noexcept {
359 return lhs.m_path <= rhs;
360}
361
362inline bool operator>(const DataPointPath& lhs, const char* rhs) noexcept {
363 return lhs.m_path > rhs;
364}
365
366inline bool operator>=(const DataPointPath& lhs, const char* rhs) noexcept {
367 return lhs.m_path >= rhs;
368}
369
370inline bool operator==(const DataPointPath& lhs, const std::string& rhs) noexcept {
371 return lhs.m_path == rhs;
372}
373
374inline bool operator<(const DataPointPath& lhs, const std::string& rhs) noexcept {
375 return lhs.m_path < rhs;
376}
377
378inline bool operator<=(const DataPointPath& lhs, const std::string& rhs) noexcept {
379 return lhs.m_path <= rhs;
380}
381
382inline bool operator>(const DataPointPath& lhs, const std::string& rhs) noexcept {
383 return lhs.m_path > rhs;
384}
385
386inline bool operator>=(const DataPointPath& lhs, const std::string& rhs) noexcept {
387 return lhs.m_path >= rhs;
388}
389
390inline bool operator==(const DataPointPath& lhs, const DataPointPath& rhs) noexcept {
391 return lhs.m_path == rhs.m_path;
392}
393
394inline bool operator<(const DataPointPath& lhs, const DataPointPath& rhs) noexcept {
395 return lhs.m_path < rhs.m_path;
396}
397
398inline bool operator<=(const DataPointPath& lhs, const DataPointPath& rhs) noexcept {
399 return lhs.m_path <= rhs.m_path;
400}
401
402inline bool operator>(const DataPointPath& lhs, const DataPointPath& rhs) noexcept {
403 return lhs.m_path > rhs.m_path;
404}
405
406inline bool operator>=(const DataPointPath& lhs, const DataPointPath& rhs) noexcept {
407 return lhs.m_path >= rhs.m_path;
408}
409
411 lhs += rhs;
412 if (not DataPointPath::ValidPath(lhs)) {
414 }
415 return lhs;
416}
417
419 lhs /= rhs;
420 return lhs;
421}
422
423inline const std::string& DataPointPath::ToString() const noexcept {
424 return this->m_path;
425}
426
428 this->m_path += rhs.m_path;
429 if (not DataPointPath::ValidPath(*this)) {
430 CII_THROW(DataPointPath::InvalidPathException, *this);
431 }
432 return *this;
433}
434
436 std::string::value_type this_path_back = this->m_path.empty() ? '0' : this->m_path.back();
437 std::string::value_type rhs_path_front = rhs.m_path.empty() ? '0' : rhs.m_path.front();
438 if (this_path_back != '/' and rhs_path_front != '/') {
439 // If we do not end or start with a slash then add one.
440 this->m_path += "/" + rhs.m_path;
441 } else if (this_path_back == '/' and rhs_path_front == '/') {
442 // If we end and start with a slash then ignore one of them.
443 this->m_path.append(rhs.m_path, 1);
444 } else {
445 this->m_path += rhs.m_path;
446 }
447 return *this;
448}
449
450inline DataPointPath operator"" _dppath(const char* str, std::size_t len) {
451 return DataPointPath(std::string(str));
452}
453
454inline DataPointPath::operator std::string() const noexcept {
455 return m_path;
456}
457
458inline std::ostream& operator<<(std::ostream& out, const DataPointPath& rhs) {
459 out << rhs.m_path;
460 return out;
461}
462
463} // namespace rtctk::componentFramework
464
468template <>
469struct fmt::formatter<rtctk::componentFramework::DataPointPath> : fmt::formatter<std::string> {
470 auto format(const rtctk::componentFramework::DataPointPath& path, fmt::format_context& ctx)
471 -> fmt::format_context::iterator;
472};
473
475// The naming convention for the following two helper functions is defined by the nlohmann_json
476// library.
477// NOLINTNEXTLINE(readability-identifier-naming)
478inline void to_json(JsonPayload& j, const std::vector<DataPointPath>& v) {
479 std::vector<std::string> string_vec;
480 string_vec.reserve(v.size());
481 for (auto& path : v) {
482 string_vec.emplace_back(path.ToString());
483 }
484 j = JsonPayload{string_vec};
485}
486
487// NOLINTNEXTLINE(readability-identifier-naming)
488inline void from_json(const JsonPayload& j, std::vector<DataPointPath>& v) {
489 auto string_vec = j.get<std::vector<std::string>>();
490 v.reserve(string_vec.size());
491 for (auto& str : string_vec) {
492 v.emplace_back(str);
493 }
494}
495} // namespace rtctk::componentFramework
496
497#endif // RTCTK_COMPONENTFRAMEWORK_DATAPOINTPATH_HPP
Exception class used when an invalid character is used in a DataPointPath.
Definition: dataPointPath.hpp:79
This class provides a wrapper for a data point path.
Definition: dataPointPath.hpp:73
friend bool operator<(const DataPointPath &lhs, const char *rhs) noexcept
Definition: dataPointPath.hpp:354
friend bool operator!=(const DataPointPath &lhs, const DataPointPath &rhs) noexcept
Definition: dataPointPath.hpp:215
friend bool operator<=(const std::string &lhs, const DataPointPath &rhs) noexcept
Definition: dataPointPath.hpp:252
DataPointPath LastComponent() const
Returns the last component of the path.
Definition: dataPointPath.cpp:101
friend bool operator>=(const DataPointPath &lhs, const char *rhs) noexcept
Definition: dataPointPath.hpp:366
elt::mal::Uri ToRepositoryURI(const elt::mal::Uri &base_uri) const
Creates and returns a complete URI.
Definition: dataPointPath.cpp:68
friend bool operator>=(const char *lhs, const DataPointPath &rhs) noexcept
Definition: dataPointPath.hpp:239
friend bool operator>(const char *lhs, const DataPointPath &rhs) noexcept
Definition: dataPointPath.hpp:236
std::pair< DataPointPath, DataPointPath > Split() const
Splits the path into a base path and last component pair.
Definition: dataPointPath.cpp:72
friend bool operator<(const char *lhs, const DataPointPath &rhs) noexcept
Definition: dataPointPath.hpp:230
friend bool operator!=(const std::string &lhs, const DataPointPath &rhs) noexcept
Definition: dataPointPath.hpp:246
friend bool operator>=(const std::string &lhs, const DataPointPath &rhs) noexcept
Definition: dataPointPath.hpp:258
friend bool operator!=(const char *lhs, const DataPointPath &rhs) noexcept
Definition: dataPointPath.hpp:227
friend bool operator!=(const DataPointPath &lhs, const std::string &rhs) noexcept
Definition: dataPointPath.hpp:206
friend bool operator<=(const char *lhs, const DataPointPath &rhs) noexcept
Definition: dataPointPath.hpp:233
DataPointPath BasePath() const
Returns the base component of the path.
Definition: dataPointPath.cpp:89
friend bool operator<(const std::string &lhs, const DataPointPath &rhs) noexcept
Definition: dataPointPath.hpp:249
DataPointPath & operator/=(const DataPointPath &rhs)
Append another DataPointPath to this DataPointPath with a path seperator (/) in between if needed.
Definition: dataPointPath.hpp:435
const std::string & ToString() const noexcept
Get string representing the DataPointPath.
Definition: dataPointPath.hpp:423
friend std::istream & operator>>(std::istream &input, DataPointPath &path)
Read into DataPointPath from istream.
Definition: dataPointPath.cpp:170
friend bool operator>(const std::string &lhs, const DataPointPath &rhs) noexcept
Definition: dataPointPath.hpp:255
operator std::string() const noexcept
Cast operator to string (implicit).
Definition: dataPointPath.hpp:454
DataPointPath(const char *const new_path)
Create DataPointPath from a null terminated character string.
Definition: dataPointPath.hpp:113
DataPointPath & operator=(DataPointPath &&)=default
DataPointPath & operator=(const DataPointPath &)=default
DataPointPath(DataPointPath &&)=default
friend bool operator==(const std::string &lhs, const DataPointPath &rhs) noexcept
Definition: dataPointPath.hpp:243
friend bool operator==(const char *lhs, const DataPointPath &rhs) noexcept
Definition: dataPointPath.hpp:224
friend bool operator<=(const DataPointPath &lhs, const char *rhs) noexcept
Definition: dataPointPath.hpp:358
DataPointPath(const DataPointPath &)=default
std::vector< DataPointPath > SplitAll() const
Splits the path into a list of individual components.
Definition: dataPointPath.cpp:115
friend bool operator==(const DataPointPath &lhs, const char *rhs) noexcept
Definition: dataPointPath.hpp:350
friend bool operator>(const DataPointPath &lhs, const char *rhs) noexcept
Definition: dataPointPath.hpp:362
static elt::mal::Uri AppendSuffixToUriPath(const std::string &suffix, const elt::mal::Uri &uri)
Appends a suffix string to the path of a URI.
Definition: dataPointPath.cpp:148
DataPointPath & operator+=(const DataPointPath &rhs)
Append another DataPointPath to this DataPointPath without adding a path seperator,...
Definition: dataPointPath.hpp:427
The RtctkException class is the base class for all Rtctk exceptions.
Definition: exceptions.hpp:237
Provides macros and utilities for exception handling.
Defines the JSON payload type JsonPayload.
Definition: commandReplier.cpp:22
DataPointPath operator/(DataPointPath lhs, const DataPointPath &rhs)
Definition: dataPointPath.hpp:418
void to_json(JsonPayload &j, const std::vector< DataPointPath > &v)
Definition: dataPointPath.hpp:478
bool operator<(const DataPointPath &lhs, const char *rhs) noexcept
Definition: dataPointPath.hpp:354
void from_json(const JsonPayload &j, std::vector< DataPointPath > &v)
Definition: dataPointPath.hpp:488
std::ostream & operator<<(std::ostream &os, AlertServiceIf::AlertStatus const &status)
Definition: alertServiceIf.cpp:60
bool operator>=(const DataPointPath &lhs, const char *rhs) noexcept
Definition: dataPointPath.hpp:366
nlohmann::json JsonPayload
Type requirements:
Definition: jsonPayload.hpp:25
bool operator<=(const DataPointPath &lhs, const char *rhs) noexcept
Definition: dataPointPath.hpp:358
bool operator==(const DataPointPath &lhs, const char *rhs) noexcept
Definition: dataPointPath.hpp:350
DataPointPath operator+(DataPointPath lhs, const DataPointPath &rhs)
Definition: dataPointPath.hpp:410
bool operator>(const DataPointPath &lhs, const char *rhs) noexcept
Definition: dataPointPath.hpp:362
Definition: commandReplier.cpp:22
DataPointPath m_path
Definition: populateConfig.cpp:165