Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
os-canon-path.hh
Go to the documentation of this file.
1#pragma once
3
6
7#include <filesystem>
8#include <optional>
9
10namespace nix {
11
29class OsCanonPath
30{
31 std::filesystem::path p;
32
33 void validate() const;
34
39 OsCanonPath(std::filesystem::path p)
40 : p(std::move(p))
41 {
42 }
43
44public:
48 OsCanonPath() = default;
49
55 static OsCanonPath fromPathAsserting(std::filesystem::path p)
56 {
57 auto ret = OsCanonPath{std::move(p)};
58 ret.validate();
59 return ret;
60 }
61
69 : p(name.path())
70 {
71 }
72
80 OsCanonPath(const CanonPath & canonPath);
81
95 CanonPath toPortable() const;
96
97 const std::filesystem::path & path() const noexcept
98 {
99 return p;
100 }
101
102 const std::filesystem::path::value_type * c_str() const noexcept
103 {
104 return p.c_str();
105 }
106
110 bool empty() const noexcept
111 {
112 return p.empty();
113 }
114
118 void pop();
119
124 std::optional<OsCanonPath> parent() const;
125
129 bool isWithin(const OsCanonPath & prefix) const;
130
137 OsCanonPath removePrefix(const OsCanonPath & prefix) const;
138
153 class Iterator
154 {
155 std::filesystem::path::const_iterator it;
156
157 explicit Iterator(std::filesystem::path::const_iterator it)
158 : it(std::move(it))
159 {
160 }
161
162 friend class OsCanonPath;
163
164 public:
165 using value_type = OsFilename;
166 using reference = const OsFilename &;
167 using pointer = const OsFilename *;
168 using difference_type = std::ptrdiff_t;
169 /* Stashed references rule out `LegacyForwardIterator`, which
170 requires references to outlive the iterator. */
171 using iterator_concept = std::bidirectional_iterator_tag;
172 using iterator_category = std::input_iterator_tag;
173
174 Iterator() = default;
175
176 const OsFilename & operator*() const
177 {
178 /* Go through `operator->` rather than `operator*` of the
179 underlying iterator: libc++'s `operator*` returns the
180 component by value, so a reference to it would dangle as soon
181 as this function returns. `operator->` points at the component
182 stored in the iterator (libc++) or in the path itself
183 (libstdc++), which lives at least as long as `it`. */
184 return OsFilename::refFromPathUnchecked(*it.operator->());
185 }
186
187 const OsFilename * operator->() const
188 {
189 return &**this;
190 }
191
192 Iterator & operator++()
193 {
194 ++it;
195 return *this;
196 }
197
198 Iterator operator++(int)
199 {
200 auto tmp = *this;
201 ++it;
202 return tmp;
203 }
204
205 Iterator & operator--()
206 {
207 --it;
208 return *this;
209 }
210
211 Iterator operator--(int)
212 {
213 auto tmp = *this;
214 --it;
215 return tmp;
216 }
217
218 bool operator==(const Iterator &) const = default;
219 };
220
221 Iterator begin() const
222 {
223 return Iterator{p.begin()};
224 }
225
226 Iterator end() const
227 {
228 return Iterator{p.end()};
229 }
230
231 auto operator<=>(const OsCanonPath &) const = default;
232
236 OsCanonPath operator/(const OsCanonPath & other) const;
237
241 OsCanonPath operator/(const OsFilename & name) const;
242
246 friend OsCanonPath operator/(const OsFilename & name, const OsCanonPath & path);
247
254 friend OsCanonPath operator/(const OsFilename & a, const OsFilename & b);
255};
256
257OsCanonPath operator/(const OsFilename & a, const OsFilename & b);
258
259} // namespace nix
Definition canon-path.hh:50
Definition os-canon-path.hh:154
Definition os-canon-path.hh:30
OsCanonPath operator/(const OsCanonPath &other) const
Definition os-canon-path.cc:60
OsCanonPath()=default
OsCanonPath(OsFilename name)
Definition os-canon-path.hh:68
bool isWithin(const OsCanonPath &prefix) const
Definition os-canon-path.cc:116
std::optional< OsCanonPath > parent() const
Definition os-canon-path.cc:108
static OsCanonPath fromPathAsserting(std::filesystem::path p)
Definition os-canon-path.hh:55
OsCanonPath removePrefix(const OsCanonPath &prefix) const
Definition os-canon-path.cc:121
bool empty() const noexcept
Definition os-canon-path.hh:110
CanonPath toPortable() const
Definition os-canon-path.cc:135
Definition os-filename.hh:22
OsCanonPath operator/(const OsFilename &a, const OsFilename &b)
Definition os-canon-path.cc:93
std::optional< typename T::value_type > pop(T &c)
Definition util.hh:375