MPD 0.20.6

- Main Page
- Namespaces
- Data Structures
- Files

- File List
- Globals

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() {
81 return AllocatedPath(nullptr);
82 }
83
84 gcc_pure
85 operator Path() const {
86 return Path::FromFS(c_str());
87 }
88
92 gcc_pure gcc_nonnull_all
93 static AllocatedPath Build(const_pointer_type a, const_pointer_type b) {
94 return Build(a, PathTraitsFS::GetLength(a),
95 b, PathTraitsFS::GetLength(b));
96 }
97
98 gcc_pure gcc_nonnull_all
99 static AllocatedPath Build(Path a, const_pointer_type b) {
100 return Build(a.c_str(), b);
101 }
102
103 gcc_pure gcc_nonnull_all
104 static AllocatedPath Build(Path a, Path b) {
105 return Build(a, b.c_str());
106 }
107
108 gcc_pure gcc_nonnull_all
109 static AllocatedPath Build(const_pointer_type a, const AllocatedPath &b) {
110 return Build(a, PathTraitsFS::GetLength(a),
111 b.value.c_str(), b.value.size());
112 }
113
114 gcc_pure gcc_nonnull_all
115 static AllocatedPath Build(const AllocatedPath &a, const_pointer_type b) {
116 return Build(a.value.c_str(), a.value.size(),
117 b, PathTraitsFS::GetLength(b));
118 }
119
120 gcc_pure
121 static AllocatedPath Build(const AllocatedPath &a,
122 const AllocatedPath &b) {
123 return Build(a.value.c_str(), a.value.size(),
124 b.value.c_str(), b.value.size());
125 }
126
131 gcc_pure
132 static AllocatedPath FromFS(const_pointer_type fs) {
133 return AllocatedPath(fs);
134 }
135
136 gcc_pure
137 static AllocatedPath FromFS(const_pointer_type _begin,
138 const_pointer_type _end) {
139 return AllocatedPath(_begin, _end);
140 }
141
146 gcc_pure
147 static AllocatedPath FromFS(string &&fs) {
148 return AllocatedPath(std::move(fs));
149 }
150
155 gcc_pure gcc_nonnull_all
156 static AllocatedPath FromUTF8(const char *path_utf8);
157
162 gcc_pure gcc_nonnull_all
163 static AllocatedPath FromUTF8Throw(const char *path_utf8);
164
168 AllocatedPath &operator=(const AllocatedPath &) = default;
169
173 AllocatedPath &operator=(AllocatedPath &&other) {
174 value = std::move(other.value);
175 return *this;
176 }
177
178 gcc_pure
179 bool operator==(const AllocatedPath &other) const {
180 return value == other.value;
181 }
182
183 gcc_pure
184 bool operator!=(const AllocatedPath &other) const {
185 return value != other.value;
186 }
187
192 string &&Steal() {
193 return std::move(value);
194 }
195
200 bool IsNull() const {
201 return value.empty();
202 }
203
209 void SetNull() {
210 value.clear();
211 }
212
217 gcc_pure
218 size_t length() const {
219 return value.length();
220 }
221
227 gcc_pure
228 const_pointer_type c_str() const {
229 return value.c_str();
230 }
231
236 gcc_pure
237 const_pointer_type data() const {
238 return value.data();
239 }
240
246 gcc_pure
247 std::string ToUTF8() const;
248
253 gcc_pure
254 AllocatedPath GetDirectoryName() const;
255
262 gcc_pure
263 const_pointer_type Relative(Path other_fs) const {
264 return PathTraitsFS::Relative(c_str(), other_fs.c_str());
265 }
266
270 void ChopSeparators();
271
272 gcc_pure
273 bool IsAbsolute() const {
274 return PathTraitsFS::IsAbsolute(c_str());
275 }
276 };
277
278 #endif
check.h
AllocatedPath::Build
gcc_pure static gcc_nonnull_all AllocatedPath Build(Path a, const_pointer_type b)
Definition: AllocatedPath.hxx:99
AllocatedPath::operator=
AllocatedPath & operator=(const AllocatedPath &)=default
Copy an AllocatedPath object.
Traits.hxx
AllocatedPath::Relative
gcc_pure const_pointer_type Relative(Path other_fs) const
Determine the relative part of the given path to this object, not including the directory separator...
Definition: AllocatedPath.hxx:263
AllocatedPath::AllocatedPath
AllocatedPath(Path other)
Definition: AllocatedPath.hxx:69
gcc_nonnull_all
#define gcc_nonnull_all
Definition: Compiler.h:122
AllocatedPath::FromUTF8Throw
gcc_pure static gcc_nonnull_all AllocatedPath FromUTF8Throw(const char *path_utf8)
Convert a UTF-8 C string to an AllocatedPath instance.
AllocatedPath::SetNull
void SetNull()
Clear this object's value, make it "nulled".
Definition: AllocatedPath.hxx:209
AllocatedPath::FromUTF8
gcc_pure static gcc_nonnull_all AllocatedPath FromUTF8(const char *path_utf8)
Convert a UTF-8 C string to an AllocatedPath instance.
AllocatedPath
A path name in the native file system character set.
Definition: AllocatedPath.hxx:38
AllocatedPath::FromFS
static gcc_pure AllocatedPath FromFS(const_pointer_type _begin, const_pointer_type _end)
Definition: AllocatedPath.hxx:137
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:192
AllocatedPath::data
gcc_pure const_pointer_type data() const
Returns a pointer to the raw value, not necessarily null-terminated.
Definition: AllocatedPath.hxx:237
path_utf8
const Storage const char const char * path_utf8
Definition: DatabasePlaylist.hxx:31
AllocatedPath::length
gcc_pure size_t length() const
Definition: AllocatedPath.hxx:218
AllocatedPath::Build
gcc_pure static gcc_nonnull_all AllocatedPath Build(const_pointer_type a, const_pointer_type b)
Join two path components with the path separator.
Definition: AllocatedPath.hxx:93
PathTraitsFS::Relative
gcc_pure static gcc_nonnull_all const_pointer_type Relative(const_pointer_type base, const_pointer_type other)
Determine the relative part of the given path to this object, not including the directory separator...
PathTraitsFS::pointer_type
Pointer::pointer_type pointer_type
Definition: Traits.hxx:55
AllocatedPath::Build
static gcc_pure AllocatedPath Build(const AllocatedPath &a, const AllocatedPath &b)
Definition: AllocatedPath.hxx:121
AllocatedPath::ToUTF8
gcc_pure std::string ToUTF8() const
Convert the path to UTF-8.
AllocatedPath::IsAbsolute
gcc_pure bool IsAbsolute() const
Definition: AllocatedPath.hxx:273
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::operator=
AllocatedPath & operator=(AllocatedPath &&other)
Move an AllocatedPath object.
Definition: AllocatedPath.hxx:173
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
Compiler.h
AllocatedPath::AllocatedPath
AllocatedPath(AllocatedPath &&other)
Move an AllocatedPath object.
Definition: AllocatedPath.hxx:67
AllocatedPath::Null
static gcc_const AllocatedPath Null()
Return a "nulled" instance.
Definition: AllocatedPath.hxx:80
AllocatedPath::Build
gcc_pure static gcc_nonnull_all AllocatedPath Build(const_pointer_type a, const AllocatedPath &b)
Definition: AllocatedPath.hxx:109
AllocatedPath::operator==
gcc_pure bool operator==(const AllocatedPath &other) const
Definition: AllocatedPath.hxx:179
gcc_const
#define gcc_const
Definition: Compiler.h:109
Path.hxx
AllocatedPath::GetDirectoryName
gcc_pure AllocatedPath GetDirectoryName() const
Gets directory name of this path.
AllocatedPath::operator!=
gcc_pure bool operator!=(const AllocatedPath &other) const
Definition: AllocatedPath.hxx:184
AllocatedPath::c_str
gcc_pure const_pointer_type c_str() const
Returns the value as a const C string.
Definition: AllocatedPath.hxx:228
PathTraitsFS::GetLength
gcc_pure static gcc_nonnull_all size_t GetLength(const_pointer_type p)
Definition: Traits.hxx:113
AllocatedPath::FromFS
static gcc_pure AllocatedPath FromFS(const_pointer_type fs)
Convert a C string that is already in the filesystem character set to a Path instance.
Definition: AllocatedPath.hxx:132
AllocatedPath::ChopSeparators
void ChopSeparators()
Chop trailing directory separators.
AllocatedPath::~AllocatedPath
~AllocatedPath()
gcc_pure
#define gcc_pure
Definition: Compiler.h:116
PathTraitsFS::value_type
char_traits::char_type value_type
Definition: Traits.hxx:53
Path::c_str
gcc_pure const_pointer_type c_str() const
Returns the value as a const C string.
Definition: Path.hxx:107
PathTraitsFS::string
std::string string
Definition: Traits.hxx:50
PathTraitsFS::IsAbsolute
gcc_pure static gcc_nonnull_all bool IsAbsolute(const_pointer_type p)
Definition: Traits.hxx:99
AllocatedPath::IsNull
bool IsNull() const
Check if this is a "nulled" instance.
Definition: AllocatedPath.hxx:200
AllocatedPath::FromFS
static gcc_pure AllocatedPath FromFS(string &&fs)
Convert a C++ string that is already in the filesystem character set to a Path instance.
Definition: AllocatedPath.hxx:147
AllocatedPath::Build
gcc_pure static gcc_nonnull_all AllocatedPath Build(const AllocatedPath &a, const_pointer_type b)
Definition: AllocatedPath.hxx:115
AllocatedPath::Build
gcc_pure static gcc_nonnull_all AllocatedPath Build(Path a, Path b)
Definition: AllocatedPath.hxx:104
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)
Constructs the path from the given components.