MPD  0.20.18
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 
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 
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 
93  static AllocatedPath Build(const_pointer_type a,
94  const_pointer_type b) noexcept {
95  return Build(a, PathTraitsFS::GetLength(a),
97  }
98 
100  static AllocatedPath Build(Path a, const_pointer_type b) noexcept {
101  return Build(a.c_str(), b);
102  }
103 
105  static AllocatedPath Build(Path a, Path b) noexcept {
106  return Build(a, b.c_str());
107  }
108 
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 
118  const_pointer_type b) noexcept {
119  return Build(a.value.c_str(), a.value.size(),
121  }
122 
123  gcc_pure
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 
159  static AllocatedPath FromUTF8(const char *path_utf8) noexcept;
160 
166  static AllocatedPath FromUTF8Throw(const char *path_utf8);
167 
171  AllocatedPath &operator=(const AllocatedPath &) = default;
172 
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 {
278  }
279 };
280 
281 #endif
static gcc_pure AllocatedPath FromFS(string &&fs) noexcept
Convert a C++ string that is already in the filesystem character set to a Path instance.
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=(const AllocatedPath &)=default
Copy an AllocatedPath object.
gcc_pure const_pointer_type data() const noexcept
Returns a pointer to the raw value, not necessarily null-terminated.
gcc_pure bool IsAbsolute() const noexcept
AllocatedPath(Path other)
gcc_pure const_pointer_type c_str() const noexcept
Returns the value as a const C string.
#define gcc_nonnull_all
Definition: Compiler.h:122
gcc_pure size_t length() const noexcept
gcc_pure static gcc_nonnull_all AllocatedPath Build(Path a, const_pointer_type b) noexcept
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.
gcc_pure bool operator!=(const AllocatedPath &other) const noexcept
A path name in the native file system character set.
string && Steal()
Allows the caller to "steal" the internal value by providing a rvalue reference to the std::string at...
static gcc_const AllocatedPath Null() noexcept
Return a "nulled" instance.
gcc_pure AllocatedPath GetDirectoryName() const noexcept
Gets directory name of this path.
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...
const Storage const char const char * path_utf8
gcc_pure static gcc_nonnull_all AllocatedPath Build(const_pointer_type a, const AllocatedPath &b) noexcept
static gcc_pure AllocatedPath Build(const AllocatedPath &a, const AllocatedPath &b) noexcept
Pointer::pointer_type pointer_type
Definition: Traits.hxx:55
static constexpr Path FromFS(const_pointer_type fs)
Create a new instance pointing to the specified path string.
Definition: Path.hxx:64
gcc_pure std::string ToUTF8() const noexcept
Convert the path to UTF-8.
static gcc_pure AllocatedPath FromFS(const_pointer_type _begin, const_pointer_type _end) noexcept
bool IsNull() const noexcept
Check if this is a "nulled" instance.
gcc_pure static gcc_nonnull_all bool IsAbsolute(const_pointer_type p) noexcept
Definition: Traits.hxx:99
AllocatedPath & operator=(AllocatedPath &&other)
Move an AllocatedPath object.
Pointer::const_pointer_type const_pointer_type
Definition: Traits.hxx:56
A path name in the native file system character set.
Definition: Path.hxx:39
void ChopSeparators() noexcept
Chop trailing directory separators.
AllocatedPath(AllocatedPath &&other)
Move an AllocatedPath object.
#define gcc_const
Definition: Compiler.h:109
gcc_pure static gcc_nonnull_all size_t GetLength(const_pointer_type p) noexcept
Definition: Traits.hxx:113
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.
static gcc_nonnull_all AllocatedPath FromUTF8Throw(const char *path_utf8)
Convert a UTF-8 C string to an AllocatedPath instance.
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...
gcc_pure bool operator==(const AllocatedPath &other) const noexcept
gcc_pure static gcc_nonnull_all AllocatedPath FromUTF8(const char *path_utf8) noexcept
Convert a UTF-8 C string to an AllocatedPath instance.
#define gcc_pure
Definition: Compiler.h:116
gcc_pure static gcc_nonnull_all AllocatedPath Build(const AllocatedPath &a, const_pointer_type b) noexcept
char_traits::char_type value_type
Definition: Traits.hxx:53
gcc_pure static gcc_nonnull_all AllocatedPath Build(Path a, Path b) noexcept
std::string string
Definition: Traits.hxx:50
void SetNull() noexcept
Clear this object&#39;s value, make it "nulled".