PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
asset_path.h
1#pragma once
2
4#include "pixelbullet/filesystem/virtual_path.h"
5
6#include <cstddef>
7#include <optional>
8#include <string>
9#include <string_view>
10
11namespace pixelbullet::filesystem
12{
14{
15 std::string_view alias;
16 std::string_view remainder;
17};
18
19[[nodiscard]] bool is_known_asset_alias(std::string_view alias) noexcept;
20[[nodiscard]] bool is_valid_asset_alias_remainder(std::string_view remainder);
21[[nodiscard]] std::optional<ParsedAssetAliasPath> parse_asset_alias_path(std::string_view logical_path);
22[[nodiscard]] bool is_valid_asset_logical_path(std::string_view logical_path);
23} // namespace pixelbullet::filesystem
24
25namespace pixelbullet
26{
27class AssetPath
28{
29public:
30 AssetPath() = default;
31
32 AssetPath(std::string_view path)
33 {
34 AssignTrusted(path);
35 }
36
37 AssetPath(const char* path)
38 : AssetPath(std::string_view(path ? path : ""))
39 {
40 }
41
42 AssetPath(const std::string& path)
43 : AssetPath(std::string_view(path))
44 {
45 }
46
47 AssetPath(const VirtualPath& path)
48 : AssetPath(path.logical_path())
49 {
50 }
51
52 [[nodiscard]] static bool IsValid(std::string_view path)
53 {
54 return filesystem::is_valid_asset_logical_path(path);
55 }
56
57 [[nodiscard]] static std::optional<AssetPath> TryCreate(std::string_view path)
58 {
59 if (!IsValid(path))
60 {
61 return std::nullopt;
62 }
63
64 return AssetPath(path, TrustedTag{});
65 }
66
67 [[nodiscard]] const char* c_str() const noexcept
68 {
69 return logical_path_.c_str();
70 }
71
72 [[nodiscard]] const std::string& logical_path() const noexcept
73 {
74 return logical_path_;
75 }
76
77 [[nodiscard]] bool empty() const noexcept
78 {
79 return logical_path_.empty();
80 }
81
82 [[nodiscard]] std::string_view extension() const noexcept
83 {
84 const std::size_t last_forward_slash = logical_path_.rfind('/');
85 const std::size_t segment_start = last_forward_slash == std::string::npos ? 0u : last_forward_slash + 1u;
86 const std::size_t dot_position = logical_path_.rfind('.');
87 if (dot_position != std::string::npos && dot_position >= segment_start)
88 {
89 return std::string_view(logical_path_).substr(dot_position);
90 }
91 return std::string_view{};
92 }
93
94 [[nodiscard]] bool has_extension(const std::string_view ext) const noexcept
95 {
96 return extension() == ext;
97 }
98
99 [[nodiscard]] VirtualPath AsVirtualPath() const
100 {
101 return VirtualPath(logical_path_);
102 }
103
104 [[nodiscard]] operator VirtualPath() const
105 {
106 return AsVirtualPath();
107 }
108
109private:
110 struct TrustedTag
111 {
112 };
113
114 AssetPath(std::string_view path, TrustedTag)
115 : logical_path_(path)
116 {
117 }
118
119 void AssignTrusted(std::string_view path)
120 {
121 ASSERT(IsValid(path), "Invalid authored asset path '{}'", path);
122 logical_path_ = path;
123 }
124
125private:
126 std::string logical_path_;
127};
128
129[[nodiscard]] inline bool operator==(const AssetPath& lhs, const AssetPath& rhs) noexcept
130{
131 return lhs.logical_path() == rhs.logical_path();
132}
133
134[[nodiscard]] inline bool operator!=(const AssetPath& lhs, const AssetPath& rhs) noexcept
135{
136 return !(lhs == rhs);
137}
138
139[[nodiscard]] inline bool operator==(const AssetPath& lhs, const VirtualPath& rhs) noexcept
140{
141 return lhs.logical_path() == rhs.logical_path();
142}
143
144[[nodiscard]] inline bool operator==(const VirtualPath& lhs, const AssetPath& rhs) noexcept
145{
146 return lhs.logical_path() == rhs.logical_path();
147}
148
149[[nodiscard]] inline bool operator!=(const AssetPath& lhs, const VirtualPath& rhs) noexcept
150{
151 return !(lhs == rhs);
152}
153
154[[nodiscard]] inline bool operator!=(const VirtualPath& lhs, const AssetPath& rhs) noexcept
155{
156 return !(lhs == rhs);
157}
158
159[[nodiscard]] inline AssetPath operator/(const AssetPath& lhs, const std::string_view rhs)
160{
161 std::string result = lhs.logical_path();
162 if (!result.empty() && result.back() != '/' && !rhs.empty() && rhs.front() != '/')
163 {
164 result += '/';
165 }
166 result += rhs;
167 return AssetPath(result);
168}
169
170[[nodiscard]] inline AssetPath operator/(const AssetPath& lhs, const char* rhs)
171{
172 return lhs / std::string_view(rhs ? rhs : "");
173}
174} // namespace pixelbullet
Provides assertion and panic mechanisms with optional custom formatting.
#define ASSERT(condition,...)
Asserts that a condition is true.
Definition assert.h:142
Definition asset_path.h:28
Definition virtual_path.h:10