MPD  0.20.6
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() {
81  return AllocatedPath(nullptr);
82  }
83 
84  gcc_pure
85  operator Path() const {
86  return Path::FromFS(c_str());
87  }
88 
93  static AllocatedPath Build(const_pointer_type a, const_pointer_type b) {
94  return Build(a, PathTraitsFS::GetLength(a),
96  }
97 
99  static AllocatedPath Build(Path a, const_pointer_type b) {
100  return Build(a.c_str(), b);
101  }
102 
104  static AllocatedPath Build(Path a, Path b) {
105  return Build(a, b.c_str());
106  }
107 
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 
115  static AllocatedPath Build(const AllocatedPath &a, const_pointer_type b) {
116  return Build(a.value.c_str(), a.value.size(),
118  }
119 
120  gcc_pure
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 
156  static AllocatedPath FromUTF8(const char *path_utf8);
157 
163  static AllocatedPath FromUTF8Throw(const char *path_utf8);
164 
168  AllocatedPath &operator=(const AllocatedPath &) = default;
169 
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
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 {
275  }
276 };
277 
278 #endif
gcc_pure static gcc_nonnull_all AllocatedPath Build(Path a, const_pointer_type b)
AllocatedPath & operator=(const AllocatedPath &)=default
Copy an AllocatedPath object.
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...
AllocatedPath(Path other)
#define gcc_nonnull_all
Definition: Compiler.h:122
gcc_pure static gcc_nonnull_all AllocatedPath FromUTF8Throw(const char *path_utf8)
Convert a UTF-8 C string to an AllocatedPath instance.
void SetNull()
Clear this object's value, make it "nulled".
gcc_pure static gcc_nonnull_all AllocatedPath FromUTF8(const char *path_utf8)
Convert a UTF-8 C string to an AllocatedPath instance.
A path name in the native file system character set.
static gcc_pure AllocatedPath FromFS(const_pointer_type _begin, const_pointer_type _end)
string && Steal()
Allows the caller to "steal" the internal value by providing a rvalue reference to the std::string at...
gcc_pure const_pointer_type data() const
Returns a pointer to the raw value, not necessarily null-terminated.
const Storage const char const char * path_utf8
gcc_pure size_t length() const
gcc_pure static gcc_nonnull_all AllocatedPath Build(const_pointer_type a, const_pointer_type b)
Join two path components with the path separator.
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...
Pointer::pointer_type pointer_type
Definition: Traits.hxx:55
static gcc_pure AllocatedPath Build(const AllocatedPath &a, const AllocatedPath &b)
gcc_pure std::string ToUTF8() const
Convert the path to UTF-8.
gcc_pure bool IsAbsolute() const
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 &&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
AllocatedPath(AllocatedPath &&other)
Move an AllocatedPath object.
static gcc_const AllocatedPath Null()
Return a "nulled" instance.
gcc_pure static gcc_nonnull_all AllocatedPath Build(const_pointer_type a, const AllocatedPath &b)
gcc_pure bool operator==(const AllocatedPath &other) const
#define gcc_const
Definition: Compiler.h:109
gcc_pure AllocatedPath GetDirectoryName() const
Gets directory name of this path.
gcc_pure bool operator!=(const AllocatedPath &other) const
gcc_pure const_pointer_type c_str() const
Returns the value as a const C string.
gcc_pure static gcc_nonnull_all size_t GetLength(const_pointer_type p)
Definition: Traits.hxx:113
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.
void ChopSeparators()
Chop trailing directory separators.
#define gcc_pure
Definition: Compiler.h:116
char_traits::char_type value_type
Definition: Traits.hxx:53
gcc_pure const_pointer_type c_str() const
Returns the value as a const C string.
Definition: Path.hxx:107
std::string string
Definition: Traits.hxx:50
gcc_pure static gcc_nonnull_all bool IsAbsolute(const_pointer_type p)
Definition: Traits.hxx:99
bool IsNull() const
Check if this is a "nulled" instance.
static gcc_pure AllocatedPath FromFS(string &&fs)
Convert a C++ string that is already in the filesystem character set to a Path instance.
gcc_pure static gcc_nonnull_all AllocatedPath Build(const AllocatedPath &a, const_pointer_type b)
gcc_pure static gcc_nonnull_all AllocatedPath Build(Path a, Path b)
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.