MPD: fs/AllocatedPath.hxx Source File

MPD 0.20.15

AllocatedPath.hxx
Go to the documentation of this file.
1 /*
2 * Copyright 2003-2017 The Music Player Daemon Project
3 * http://www.musicpd.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef MPD_FS_ALLOCATED_PATH_HXX
21 #define MPD_FS_ALLOCATED_PATH_HXX
22
23 #include "check.h"
24 #include "Compiler.h"
25 #include "Traits.hxx"
26 #include "Path.hxx"
27
28 #include <cstddef>
29 #include <utility>
30 #include <string>
31
38 class AllocatedPath {
39 typedef PathTraitsFS::string string;
40 typedef PathTraitsFS::value_type value_type;
41 typedef PathTraitsFS::pointer_type pointer_type;
42 typedef PathTraitsFS::const_pointer_type const_pointer_type;
43
44 string value;
45
46 AllocatedPath(std::nullptr_t):value() {}
47 explicit AllocatedPath(const_pointer_type _value):value(_value) {}
48
49 AllocatedPath(const_pointer_type _begin, const_pointer_type _end)
50 :value(_begin, _end) {}
51
52 AllocatedPath(string &&_value):value(std::move(_value)) {}
53
54 static AllocatedPath Build(const_pointer_type a, size_t a_size,
55 const_pointer_type b, size_t b_size) {
56 return AllocatedPath(PathTraitsFS::Build(a, a_size, b, b_size));
57 }
58 public:
62 AllocatedPath(const AllocatedPath &) = default;
63
67 AllocatedPath(AllocatedPath &&other):value(std::move(other.value)) {}
68
69 explicit AllocatedPath(Path other):value(other.c_str()) {}
70
71 ~AllocatedPath();
72
79 gcc_const
80 static AllocatedPath Null() noexcept {
81 return AllocatedPath(nullptr);
82 }
83
84 gcc_pure
85 operator Path() const noexcept {
86 return Path::FromFS(c_str());
87 }
88
92 gcc_pure gcc_nonnull_all
93 static AllocatedPath Build(const_pointer_type a,
94 const_pointer_type b) noexcept {
95 return Build(a, PathTraitsFS::GetLength(a),
96 b, PathTraitsFS::GetLength(b));
97 }
98
99 gcc_pure gcc_nonnull_all
100 static AllocatedPath Build(Path a, const_pointer_type b) noexcept {
101 return Build(a.c_str(), b);
102 }
103
104 gcc_pure gcc_nonnull_all
105 static AllocatedPath Build(Path a, Path b) noexcept {
106 return Build(a, b.c_str());
107 }
108
109 gcc_pure gcc_nonnull_all
110 static AllocatedPath Build(const_pointer_type a,
111 const AllocatedPath &b) noexcept {
112 return Build(a, PathTraitsFS::GetLength(a),
113 b.value.c_str(), b.value.size());
114 }
115
116 gcc_pure gcc_nonnull_all
117 static AllocatedPath Build(const AllocatedPath &a,
118 const_pointer_type b) noexcept {
119 return Build(a.value.c_str(), a.value.size(),
120 b, PathTraitsFS::GetLength(b));
121 }
122
123 gcc_pure
124 static AllocatedPath Build(const AllocatedPath &a,
125 const AllocatedPath &b) noexcept {
126 return Build(a.value.c_str(), a.value.size(),
127 b.value.c_str(), b.value.size());
128 }
129
134 gcc_pure
135 static AllocatedPath FromFS(const_pointer_type fs) noexcept {
136 return AllocatedPath(fs);
137 }
138
139 gcc_pure
140 static AllocatedPath FromFS(const_pointer_type _begin,
141 const_pointer_type _end) noexcept {
142 return AllocatedPath(_begin, _end);
143 }
144
149 gcc_pure
150 static AllocatedPath FromFS(string &&fs) noexcept {
151 return AllocatedPath(std::move(fs));
152 }
153
158 gcc_pure gcc_nonnull_all
159 static AllocatedPath FromUTF8(const char *path_utf8) noexcept;
160
165 gcc_nonnull_all
166 static AllocatedPath FromUTF8Throw(const char *path_utf8);
167
171 AllocatedPath &operator=(const AllocatedPath &) = default;
172
176 AllocatedPath &operator=(AllocatedPath &&other) {
177 value = std::move(other.value);
178 return *this;
179 }
180
181 gcc_pure
182 bool operator==(const AllocatedPath &other) const noexcept {
183 return value == other.value;
184 }
185
186 gcc_pure
187 bool operator!=(const AllocatedPath &other) const noexcept {
188 return value != other.value;
189 }
190
195 string &&Steal() {
196 return std::move(value);
197 }
198
203 bool IsNull() const noexcept {
204 return value.empty();
205 }
206
212 void SetNull() noexcept {
213 value.clear();
214 }
215
220 gcc_pure
221 size_t length() const noexcept {
222 return value.length();
223 }
224
230 gcc_pure
231 const_pointer_type c_str() const noexcept {
232 return value.c_str();
233 }
234
239 gcc_pure
240 const_pointer_type data() const noexcept {
241 return value.data();
242 }
243
249 gcc_pure
250 std::string ToUTF8() const noexcept;
251
256 gcc_pure
257 AllocatedPath GetDirectoryName() const noexcept;
258
265 gcc_pure
266 const_pointer_type Relative(Path other_fs) const noexcept {
267 return PathTraitsFS::Relative(c_str(), other_fs.c_str());
268 }
269
273 void ChopSeparators() noexcept;
274
275 gcc_pure
276 bool IsAbsolute() const noexcept {
277 return PathTraitsFS::IsAbsolute(c_str());
278 }
279 };
280
281 #endif
AllocatedPath::FromFS
static gcc_pure AllocatedPath FromFS(string &&fs) noexcept
Convert a C++ string that is already in the filesystem character set to a Path instance.
Definition: AllocatedPath.hxx:150
check.h
PathTraitsFS::Build
gcc_pure static gcc_nonnull_all string Build(const_pointer_type a, size_t a_size, const_pointer_type b, size_t b_size) noexcept
Constructs the path from the given components.
AllocatedPath::operator=
AllocatedPath & operator=(const AllocatedPath &)=default
Copy an AllocatedPath object.
AllocatedPath::data
gcc_pure const_pointer_type data() const noexcept
Returns a pointer to the raw value, not necessarily null-terminated.
Definition: AllocatedPath.hxx:240
Traits.hxx
AllocatedPath::IsAbsolute
gcc_pure bool IsAbsolute() const noexcept
Definition: AllocatedPath.hxx:276
AllocatedPath::AllocatedPath
AllocatedPath(Path other)
Definition: AllocatedPath.hxx:69
AllocatedPath::c_str
gcc_pure const_pointer_type c_str() const noexcept
Returns the value as a const C string.
Definition: AllocatedPath.hxx:231
gcc_nonnull_all
#define gcc_nonnull_all
Definition: Compiler.h:122
AllocatedPath::length
gcc_pure size_t length() const noexcept
Definition: AllocatedPath.hxx:221
AllocatedPath::Build
gcc_pure static gcc_nonnull_all AllocatedPath Build(Path a, const_pointer_type b) noexcept
Definition: AllocatedPath.hxx:100
AllocatedPath::Build
gcc_pure static gcc_nonnull_all AllocatedPath Build(const_pointer_type a, const_pointer_type b) noexcept
Join two path components with the path separator.
Definition: AllocatedPath.hxx:93
AllocatedPath::operator!=
gcc_pure bool operator!=(const AllocatedPath &other) const noexcept
Definition: AllocatedPath.hxx:187
AllocatedPath
A path name in the native file system character set.
Definition: AllocatedPath.hxx:38
AllocatedPath::Steal
string && Steal()
Allows the caller to "steal" the internal value by providing a rvalue reference to the std::string at...
Definition: AllocatedPath.hxx:195
AllocatedPath::Null
static gcc_const AllocatedPath Null() noexcept
Return a "nulled" instance.
Definition: AllocatedPath.hxx:80
AllocatedPath::GetDirectoryName
gcc_pure AllocatedPath GetDirectoryName() const noexcept
Gets directory name of this path.
PathTraitsFS::Relative
gcc_pure static gcc_nonnull_all const_pointer_type Relative(const_pointer_type base, const_pointer_type other) noexcept
Determine the relative part of the given path to this object, not including the directory separator...
path_utf8
const Storage const char const char * path_utf8
Definition: DatabasePlaylist.hxx:31
AllocatedPath::Build
gcc_pure static gcc_nonnull_all AllocatedPath Build(const_pointer_type a, const AllocatedPath &b) noexcept
Definition: AllocatedPath.hxx:110
AllocatedPath::Build
static gcc_pure AllocatedPath Build(const AllocatedPath &a, const AllocatedPath &b) noexcept
Definition: AllocatedPath.hxx:124
PathTraitsFS::pointer_type
Pointer::pointer_type pointer_type
Definition: Traits.hxx:55
Path::FromFS
static constexpr Path FromFS(const_pointer_type fs)
Create a new instance pointing to the specified path string.
Definition: Path.hxx:64
AllocatedPath::ToUTF8
gcc_pure std::string ToUTF8() const noexcept
Convert the path to UTF-8.
AllocatedPath::FromFS
static gcc_pure AllocatedPath FromFS(const_pointer_type _begin, const_pointer_type _end) noexcept
Definition: AllocatedPath.hxx:140
AllocatedPath::IsNull
bool IsNull() const noexcept
Check if this is a "nulled" instance.
Definition: AllocatedPath.hxx:203
PathTraitsFS::IsAbsolute
gcc_pure static gcc_nonnull_all bool IsAbsolute(const_pointer_type p) noexcept
Definition: Traits.hxx:99
AllocatedPath::operator=
AllocatedPath & operator=(AllocatedPath &&other)
Move an AllocatedPath object.
Definition: AllocatedPath.hxx:176
PathTraitsFS::const_pointer_type
Pointer::const_pointer_type const_pointer_type
Definition: Traits.hxx:56
Path
A path name in the native file system character set.
Definition: Path.hxx:39
AllocatedPath::ChopSeparators
void ChopSeparators() noexcept
Chop trailing directory separators.
Compiler.h
AllocatedPath::AllocatedPath
AllocatedPath(AllocatedPath &&other)
Move an AllocatedPath object.
Definition: AllocatedPath.hxx:67
gcc_const
#define gcc_const
Definition: Compiler.h:109
Path.hxx
PathTraitsFS::GetLength
gcc_pure static gcc_nonnull_all size_t GetLength(const_pointer_type p) noexcept
Definition: Traits.hxx:113
AllocatedPath::FromFS
static gcc_pure AllocatedPath FromFS(const_pointer_type fs) noexcept
Convert a C string that is already in the filesystem character set to a Path instance.
Definition: AllocatedPath.hxx:135
AllocatedPath::FromUTF8Throw
static gcc_nonnull_all AllocatedPath FromUTF8Throw(const char *path_utf8)
Convert a UTF-8 C string to an AllocatedPath instance.
AllocatedPath::Relative
gcc_pure const_pointer_type Relative(Path other_fs) const noexcept
Determine the relative part of the given path to this object, not including the directory separator...
Definition: AllocatedPath.hxx:266
AllocatedPath::~AllocatedPath
~AllocatedPath()
AllocatedPath::operator==
gcc_pure bool operator==(const AllocatedPath &other) const noexcept
Definition: AllocatedPath.hxx:182
AllocatedPath::FromUTF8
gcc_pure static gcc_nonnull_all AllocatedPath FromUTF8(const char *path_utf8) noexcept
Convert a UTF-8 C string to an AllocatedPath instance.
gcc_pure
#define gcc_pure
Definition: Compiler.h:116
AllocatedPath::Build
gcc_pure static gcc_nonnull_all AllocatedPath Build(const AllocatedPath &a, const_pointer_type b) noexcept
Definition: AllocatedPath.hxx:117
PathTraitsFS::value_type
char_traits::char_type value_type
Definition: Traits.hxx:53
AllocatedPath::Build
gcc_pure static gcc_nonnull_all AllocatedPath Build(Path a, Path b) noexcept
Definition: AllocatedPath.hxx:105
PathTraitsFS::string
std::string string
Definition: Traits.hxx:50
AllocatedPath::SetNull
void SetNull() noexcept
Clear this object&#39;s value, make it "nulled".
Definition: AllocatedPath.hxx:212