Skip to content

Commit

Permalink
libcore: Added PathRef utility class
Browse files Browse the repository at this point in the history
Similar to QStringRef, references a portion of an existing Path.
  • Loading branch information
skyjake committed Jun 30, 2014
1 parent 03a6155 commit faf8173
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
1 change: 1 addition & 0 deletions doomsday/libcore/include/de/PathRef
@@ -0,0 +1 @@
#include "data/path.h"
45 changes: 44 additions & 1 deletion doomsday/libcore/include/de/data/path.h
Expand Up @@ -286,6 +286,8 @@ class DENG2_PUBLIC Path : public ISerializable, public LogEntry::Arg::Base
*/
bool isEmpty() const;

bool isAbsolute() const;

/// Returns the length of the path.
int length() const;

Expand Down Expand Up @@ -447,8 +449,9 @@ class DENG2_PUBLIC Path : public ISerializable, public LogEntry::Arg::Base

/**
* Utility class for specifying paths that use a dot (.) as the path separator.
* @ingroup data
*/
class DotPath : public Path
class DENG2_PUBLIC DotPath : public Path
{
public:
DotPath(String const &path = "") : Path(path, '.') {}
Expand All @@ -466,6 +469,46 @@ class DotPath : public Path
}
};

/**
* Utility class for referring to a portion of an existing (immutable) path.
* @ingroup data
*/
class DENG2_PUBLIC PathRef
{
public:
PathRef(Path const &path)
: _path(path)
, _range(0, path.segmentCount()) {}
PathRef(Path const &path, Rangei const &segRange)
: _path(path)
, _range(segRange) {}

Path const &path() const { return _path; }
Rangei range() const { return _range; }

bool isEmpty() const { return _range.isEmpty(); }
bool isAbsolute() const {
return !isEmpty() && !firstSegment().size();
}
Path::Segment const &segment(int index) const {
return _path.segment(_range.start + index);
}
int segmentCount() const { return _range.size(); }
inline Path::Segment const &firstSegment() const {
return segment(0);
}
inline Path::Segment const &lastSegment() const {
return segment(segmentCount() - 1);
}
PathRef subPath(Rangei const &sub) const {
return PathRef(_path, sub + _range.start);
}

private:
Path const &_path;
Rangei _range;
};

} // namespace de

namespace std {
Expand Down
5 changes: 5 additions & 0 deletions doomsday/libcore/src/data/path.cpp
Expand Up @@ -399,6 +399,11 @@ bool Path::isEmpty() const
return d->path.isEmpty();
}

bool Path::isAbsolute() const
{
return !isEmpty() && !firstSegment().size();
}

int Path::length() const
{
return d->path.length();
Expand Down

0 comments on commit faf8173

Please sign in to comment.