fs/Path: rename to AllocatedPath
The new class Path only holds a string pointer without being responsible for allocation/deallocation. The FileSystem.hxx library accepts Path arguments instead of AllocatedPath, to avoid forcing callers to allocate another string object.
This commit is contained in:
106
src/fs/Path.hxx
106
src/fs/Path.hxx
@@ -24,50 +24,33 @@
|
||||
#include "Compiler.h"
|
||||
#include "Traits.hxx"
|
||||
|
||||
#ifdef WIN32
|
||||
#include <glib.h>
|
||||
#endif
|
||||
|
||||
#include <utility>
|
||||
#include <string>
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
class Error;
|
||||
|
||||
/**
|
||||
* A path name in the native file system character set.
|
||||
*
|
||||
* This class manages a pointer to an existing path string. While an
|
||||
* instance lives, the string must not be invalidated.
|
||||
*/
|
||||
class Path {
|
||||
typedef std::string string;
|
||||
|
||||
typedef PathTraits::value_type value_type;
|
||||
typedef PathTraits::pointer pointer;
|
||||
typedef PathTraits::const_pointer const_pointer;
|
||||
|
||||
string value;
|
||||
const char *value;
|
||||
|
||||
struct Donate {};
|
||||
|
||||
/**
|
||||
* Donate the allocated pointer to a new #Path object.
|
||||
*/
|
||||
Path(Donate, pointer _value);
|
||||
|
||||
Path(const_pointer _value):value(_value) {}
|
||||
constexpr Path(const_pointer _value):value(_value) {}
|
||||
|
||||
public:
|
||||
/**
|
||||
* Copy a #Path object.
|
||||
*/
|
||||
Path(const Path &) = default;
|
||||
|
||||
/**
|
||||
* Move a #Path object.
|
||||
*/
|
||||
Path(Path &&other):value(std::move(other.value)) {}
|
||||
|
||||
~Path();
|
||||
constexpr Path(const Path &) = default;
|
||||
|
||||
/**
|
||||
* Return a "nulled" instance. Its IsNull() method will
|
||||
@@ -75,70 +58,29 @@ public:
|
||||
*
|
||||
* @see IsNull()
|
||||
*/
|
||||
gcc_const
|
||||
static Path Null() {
|
||||
return Path("");
|
||||
static constexpr Path Null() {
|
||||
return Path(nullptr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Join two path components with the path separator.
|
||||
* Create a new instance pointing to the specified path
|
||||
* string.
|
||||
*/
|
||||
gcc_pure gcc_nonnull_all
|
||||
static Path Build(const_pointer a, const_pointer b);
|
||||
|
||||
gcc_pure gcc_nonnull_all
|
||||
static Path Build(const_pointer a, const Path &b) {
|
||||
return Build(a, b.c_str());
|
||||
}
|
||||
|
||||
gcc_pure gcc_nonnull_all
|
||||
static Path Build(const Path &a, const_pointer b) {
|
||||
return Build(a.c_str(), b);
|
||||
}
|
||||
|
||||
gcc_pure
|
||||
static Path Build(const Path &a, const Path &b) {
|
||||
return Build(a.c_str(), b.c_str());
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a C string that is already in the filesystem
|
||||
* character set to a #Path instance.
|
||||
*/
|
||||
gcc_pure
|
||||
static Path FromFS(const_pointer fs) {
|
||||
static constexpr Path FromFS(const_pointer fs) {
|
||||
return Path(fs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a UTF-8 C string to a #Path instance.
|
||||
* Returns return a "nulled" instance on error.
|
||||
*/
|
||||
gcc_pure gcc_nonnull_all
|
||||
static Path FromUTF8(const char *path_utf8);
|
||||
|
||||
gcc_pure gcc_nonnull_all
|
||||
static Path FromUTF8(const char *path_utf8, Error &error);
|
||||
|
||||
/**
|
||||
* Copy a #Path object.
|
||||
*/
|
||||
Path &operator=(const Path &) = default;
|
||||
|
||||
/**
|
||||
* Move a #Path object.
|
||||
*/
|
||||
Path &operator=(Path &&other) {
|
||||
value = std::move(other.value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this is a "nulled" instance. A "nulled" instance
|
||||
* must not be used.
|
||||
*/
|
||||
bool IsNull() const {
|
||||
return value.empty();
|
||||
return value == nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -147,7 +89,7 @@ public:
|
||||
* @see IsNull()
|
||||
*/
|
||||
void SetNull() {
|
||||
value.clear();
|
||||
value = nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -156,7 +98,9 @@ public:
|
||||
*/
|
||||
gcc_pure
|
||||
size_t length() const {
|
||||
return value.length();
|
||||
assert(value != nullptr);
|
||||
|
||||
return strlen(value);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -166,7 +110,7 @@ public:
|
||||
*/
|
||||
gcc_pure
|
||||
const_pointer c_str() const {
|
||||
return value.c_str();
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -175,7 +119,7 @@ public:
|
||||
*/
|
||||
gcc_pure
|
||||
const_pointer data() const {
|
||||
return value.data();
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -186,13 +130,6 @@ public:
|
||||
gcc_pure
|
||||
std::string ToUTF8() const;
|
||||
|
||||
/**
|
||||
* Gets directory name of this path.
|
||||
* Returns a "nulled" instance on error.
|
||||
*/
|
||||
gcc_pure
|
||||
Path GetDirectoryName() const;
|
||||
|
||||
/**
|
||||
* Determine the relative part of the given path to this
|
||||
* object, not including the directory separator. Returns an
|
||||
@@ -202,11 +139,6 @@ public:
|
||||
gcc_pure
|
||||
const char *RelativeFS(const char *other_fs) const;
|
||||
|
||||
/**
|
||||
* Chop trailing directory separators.
|
||||
*/
|
||||
void ChopSeparators();
|
||||
|
||||
gcc_pure
|
||||
bool IsAbsolute() {
|
||||
return PathTraits::IsAbsoluteFS(c_str());
|
||||
|
||||
Reference in New Issue
Block a user