PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
VirtualPath.hpp
1#pragma once
2
3#include "PixelBullet/Serialization/Node.hpp"
4
5#include <string>
6#include <string_view>
7
8namespace PixelBullet
9{
11 {
12 public:
13 VirtualPath() = default;
14
15 VirtualPath(std::string_view path)
16 : m_LogicalPath(path)
17 {
18 }
19
20 VirtualPath(const char* path)
21 : m_LogicalPath(path)
22 {
23 }
24
25 VirtualPath(const VirtualPath&) = default;
26 VirtualPath(VirtualPath&&) noexcept = default;
27
28 VirtualPath& operator=(const VirtualPath&) = default;
29 VirtualPath& operator=(VirtualPath&&) noexcept = default;
30
31 ~VirtualPath() = default;
32
33 operator std::string() const
34 {
35 return GetResolved();
36 }
37 operator std::string_view() const noexcept
38 {
39 return GetResolved();
40 }
41 const char* c_str() const noexcept
42 {
43 return GetResolved().c_str();
44 }
45
46 [[nodiscard]] const std::string& LogicalPath() const noexcept
47 {
48 return m_LogicalPath;
49 }
50
51 [[nodiscard]] const std::string& GetResolved() const
52 {
53 if (!m_ResolvedComputed)
54 {
55 m_CachedResolved = Resolve();
56 m_ResolvedComputed = true;
57 }
58 return m_CachedResolved;
59 }
60
61 [[nodiscard]] std::string_view GetResolvedView() const noexcept
62 {
63 return GetResolved();
64 }
65
66 [[nodiscard]] std::wstring ToWString() const
67 {
68 const auto& resolved = GetResolved();
69 return std::wstring(resolved.begin(), resolved.end());
70 }
71
72 [[nodiscard]] bool HasExtension(std::string_view ext) const noexcept
73 {
74 return extension() == ext;
75 }
76
77 [[nodiscard]] std::string_view extension() const noexcept
78 {
79 const auto& resolved = GetResolved();
80 size_t pos = resolved.rfind('.');
81 if (pos != std::string::npos)
82 {
83 return std::string_view(resolved).substr(pos);
84 }
85 return std::string_view{};
86 }
87
88 std::string Resolve() const;
89
90 private:
91 std::string m_LogicalPath;
92 mutable bool m_ResolvedComputed = false;
93 mutable std::string m_CachedResolved;
94 };
95
96 inline VirtualPath operator/(const VirtualPath& lhs, const std::string& rhs)
97 {
98 std::string result = lhs.GetResolved();
99 if (!result.empty() && result.back() != '/' && !rhs.empty() && rhs.front() != '/')
100 {
101 result += '/';
102 }
103 result += rhs;
104 return VirtualPath(result);
105 }
106
107 inline VirtualPath operator/(const VirtualPath& lhs, const char* rhs)
108 {
109 return lhs / std::string(rhs);
110 }
111
112 inline VirtualPath operator"" _vp(const char* str, size_t len)
113 {
114 return VirtualPath(std::string_view(str, len));
115 }
116
117 inline const Node& operator>>(const Node& node, VirtualPath& path)
118 {
119 path = VirtualPath(node.GetValue());
120 return node;
121 }
122
123 inline Node& operator<<(Node& node, const VirtualPath& path)
124 {
125 node.SetValue(path.LogicalPath());
126 node.SetType(NodeType::String);
127 return node;
128 }
129} // namespace PixelBullet
Definition VirtualPath.hpp:11