opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
node_path.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <iterator>
5#include <string>
6#include <string_view>
7
8namespace osc
9{
10 // Represents a pre-parsed path to a node in a tree.
12 public:
13 static constexpr std::string_view::value_type separator = '/';
14
16 public:
18 using value_type = std::string_view;
19 using pointer = const std::string_view*;
20 using reference = const std::string_view&;
21 using iterator_category = std::forward_iterator_tag;
22
23 reference operator*() const { return current_; }
24
25 pointer operator->() const { return &current_; }
26
28 {
29 current_ = remaining_.substr(0, remaining_.find(separator));
30 remaining_ = current_.size() == remaining_.size() ? std::string_view{} : remaining_.substr(current_.size()+1);
31 return *this;
32 }
33
35 {
36 Iterator copy = *this;
37 ++(*this);
38 return copy;
39 }
40
41 friend bool operator==(const Iterator&, const Iterator&) = default;
42
43 private:
44 friend class NodePath;
45
46 Iterator() = default;
47
48 explicit Iterator(std::string_view path) :
49 current_{path.substr(0, path.find(separator))},
50 remaining_{current_.size() == path.size() ? std::string_view{} : path.substr(current_.size()+1)}
51 {}
52
53 std::string_view current_;
54 std::string_view remaining_;
55 };
56
57 using value_type = std::string_view;
60
61 NodePath() = default;
62 explicit NodePath(std::string_view);
63
64 [[nodiscard]] bool empty() const
65 {
66 return parsed_path_.empty();
67 }
68
69 bool is_absolute() const
70 {
71 return !parsed_path_.empty() and parsed_path_.front() == separator;
72 }
73
74 operator std::string_view () const
75 {
76 return parsed_path_;
77 }
78
80 {
81 return Iterator{is_absolute() ? std::string_view{parsed_path_}.substr(1) : parsed_path_};
82 }
83
84 Iterator end() const
85 {
86 return Iterator{};
87 }
88
89 friend bool operator==(const NodePath&, const NodePath&) = default;
90 private:
91 std::string parsed_path_;
92 };
93}
94
95template<>
96struct std::hash<osc::NodePath> final {
97 size_t operator()(const osc::NodePath& node_path) const noexcept
98 {
99 return std::hash<std::string_view>{}(node_path);
100 }
101};
Definition node_path.h:15
std::forward_iterator_tag iterator_category
Definition node_path.h:21
reference operator*() const
Definition node_path.h:23
const std::string_view & reference
Definition node_path.h:20
std::string_view value_type
Definition node_path.h:18
friend bool operator==(const Iterator &, const Iterator &)=default
ptrdiff_t difference_type
Definition node_path.h:17
Iterator & operator++()
Definition node_path.h:27
const std::string_view * pointer
Definition node_path.h:19
Iterator operator++(int)
Definition node_path.h:34
pointer operator->() const
Definition node_path.h:25
Definition node_path.h:11
NodePath()=default
bool is_absolute() const
Definition node_path.h:69
Iterator begin() const
Definition node_path.h:79
Iterator end() const
Definition node_path.h:84
bool empty() const
Definition node_path.h:64
operator std::string_view() const
Definition node_path.h:74
NodePath(std::string_view)
static constexpr std::string_view::value_type separator
Definition node_path.h:13
std::string_view value_type
Definition node_path.h:57
friend bool operator==(const NodePath &, const NodePath &)=default
Definition custom_decoration_generator.h:5
constexpr U to(T &&value)
Definition conversion.h:56
and
Definition algorithms.h:158
size_t operator()(const osc::NodePath &node_path) const noexcept
Definition node_path.h:97