PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
virtual_path.h
1#pragma once
2
3#include <cstddef>
4#include <string>
5#include <string_view>
6
7namespace pixelbullet
8{
10{
11public:
12 VirtualPath() = default;
13
14 VirtualPath(std::string_view path)
15 : logical_path_(path)
16 {
17 }
18
19 VirtualPath(const char* path)
20 : logical_path_(path)
21 {
22 }
23
24 VirtualPath(const VirtualPath&) = default;
25 VirtualPath(VirtualPath&&) noexcept = default;
26
27 VirtualPath& operator=(const VirtualPath&) = default;
28 VirtualPath& operator=(VirtualPath&&) noexcept = default;
29
30 ~VirtualPath() = default;
31
32 operator std::string() const
33 {
34 return logical_path_;
35 }
36 operator std::string_view() const noexcept
37 {
38 return logical_path_;
39 }
40 const char* CString() const noexcept
41 {
42 return logical_path_.c_str();
43 }
44
45 [[nodiscard]] const std::string& LogicalPath() const noexcept
46 {
47 return logical_path_;
48 }
49
50 [[nodiscard]] bool Empty() const noexcept
51 {
52 return logical_path_.empty();
53 }
54
55 [[nodiscard]] std::wstring ToWString() const
56 {
57 return std::wstring(logical_path_.begin(), logical_path_.end());
58 }
59
60 [[nodiscard]] bool HasExtension(std::string_view ext) const noexcept
61 {
62 return Extension() == ext;
63 }
64
65 [[nodiscard]] std::string_view Extension() const noexcept
66 {
67 const std::size_t pos = logical_path_.rfind('.');
68 if (pos != std::string::npos)
69 {
70 return std::string_view(logical_path_).substr(pos);
71 }
72 return std::string_view{};
73 }
74
75private:
76 std::string logical_path_;
77};
78
79inline VirtualPath operator/(const VirtualPath& lhs, const std::string& rhs)
80{
81 std::string result = lhs.LogicalPath();
82 if (!result.empty() && result.back() != '/' && !rhs.empty() && rhs.front() != '/')
83 {
84 result += '/';
85 }
86 result += rhs;
87 return VirtualPath(result);
88}
89
90inline VirtualPath operator/(const VirtualPath& lhs, const char* rhs)
91{
92 return lhs / std::string(rhs);
93}
94
95inline VirtualPath operator"" _vp(const char* str, size_t len)
96{
97 return VirtualPath(std::string_view(str, len));
98}
99} // namespace pixelbullet
Definition virtual_path.h:10