Unravel Engine C++ Reference
Loading...
Searching...
No Matches
asset_handle.h
Go to the documentation of this file.
1#pragma once
2
3#include <engine/engine_export.h>
4#include <hpp/filesystem.hpp>
5#include <logging/logging.h>
6#include <memory>
7#include <string>
8#include <uuid/uuid.h>
9
11
12template<typename T>
13using task_future = tpp::job_shared_future<T>;
14
20template<typename T>
22{
24 using weak_asset_t = std::weak_ptr<T>;
25
27 hpp::uuid uid{};
29 std::string id{};
34};
35
41template<typename T>
43{
45
51 auto operator==(const asset_handle& rhs) const -> bool
52 {
53 return uid() == rhs.uid() && id() == rhs.id() && is_valid() == rhs.is_valid();
54 }
55
56 auto version() const -> uintptr_t
57 {
58 return uintptr_t(get_cached_asset().get());
59 }
60
65 operator bool() const
66 {
67 return is_valid();
68 }
69
74 auto id() const -> const std::string&
75 {
76 if(link_)
77 {
78 return link_->id;
79 }
80
81 static const std::string empty;
82 return empty;
83 }
84
89 auto uid() const -> const hpp::uuid&
90 {
91 if(link_)
92 {
93 return link_->uid;
94 }
95
96 static const hpp::uuid empty;
97 return empty;
98 }
99
104 auto name() const -> std::string
105 {
106 return fs::path(id()).stem().string();
107 }
108
109 auto extension() const -> std::string
110 {
111 return fs::path(id()).extension().string();
112 }
113
119 auto get(bool wait = true) const -> std::shared_ptr<T>
120 {
121 if(auto cached_asset = get_cached_asset())
122 {
123 return cached_asset;
124 }
125
126 bool valid = is_valid();
127 bool ready = is_ready();
128 bool should_get = ready || (!ready && wait);
129
130 if(valid && should_get)
131 {
132 auto task = link_->task;
133 if(!ready)
134 {
135 task.change_priority(tpp::priority::high());
136 }
137
138 auto value = task.get();
139
140 if(value)
141 {
142 link_->weak_asset = value;
143 return value;
144 }
145 }
146
147 static const std::shared_ptr<T> empty = std::make_shared<T>();
148 return empty;
149 }
150
155 auto is_valid() const -> bool
156 {
157 return link_ && link_->task.valid();
158 }
159
164 auto is_ready() const -> bool
165 {
166 return is_valid() && link_->task.is_ready();
167 }
168
169
174 auto task_id() const
175 {
176 if(link_)
177 {
178 return link_->task.id;
179 }
180
181 return tpp::job_id{};
182 }
183
189 {
190 ensure();
191 link_->task = future;
192 link_->weak_asset = {};
193 }
194
200 void set_internal_ids(const hpp::uuid& internal_uid, const std::string& internal_id = get_empty_id())
201 {
202 ensure();
203 link_->uid = internal_uid;
204 link_->id = internal_id;
205 }
206
211 void set_internal_id(const std::string& internal_id = get_empty_id())
212 {
213 ensure();
214 link_->id = internal_id;
215 }
216
221 {
222 if(is_valid())
223 {
224 auto task_count = link_->task.use_count();
225 if(task_count > 1)
226 {
227 APPLOG_TRACE("{} - task leak use_count {}", id(), task_count);
228 }
229 }
232 }
233
238 static auto get_empty() -> const asset_handle&
239 {
240 static const asset_handle none_asset = []()
241 {
242 asset_handle asset;
243 asset.set_internal_ids({});
244 return asset;
245 }();
246 return none_asset;
247 }
248
253 static auto get_empty_id() -> const std::string&
254 {
255 static const std::string empty{"None"};
256 return empty;
257 }
258
262 void ensure()
263 {
264 static_assert(sizeof(asset_link_t) >= 1, "Type must be fully defined");
265 if(!link_)
266 {
267 link_ = std::make_shared<asset_link_t>();
268 }
269 }
270
271private:
272 auto get_cached_asset() const -> std::shared_ptr<T>
273 {
274 if(link_)
275 {
276 return link_->weak_asset.lock();
277 }
278
279 return nullptr;
280 }
282 std::shared_ptr<asset_link_t> link_;
283};
tpp::job_shared_future< T > task_future
#define APPLOG_TRACE(...)
Definition logging.h:17
Represents a handle to an asset, providing access and management functions.
static auto get_empty_id() -> const std::string &
Gets an empty string identifier.
auto is_valid() const -> bool
Checks if the handle is valid.
auto operator==(const asset_handle &rhs) const -> bool
Equality operator for asset handles.
void set_internal_job(const typename asset_link_t::task_future_t &future)
Sets the internal job future.
void invalidate()
Invalidates the handle, resetting its state.
auto uid() const -> const hpp::uuid &
Gets the unique identifier of the asset.
static auto get_empty() -> const asset_handle &
Gets an empty asset handle.
void set_internal_ids(const hpp::uuid &internal_uid, const std::string &internal_id=get_empty_id())
Sets the internal IDs.
auto get(bool wait=true) const -> std::shared_ptr< T >
Gets the shared pointer to the asset.
asset_link< T > asset_link_t
auto extension() const -> std::string
auto name() const -> std::string
Gets the name of the asset derived from its path.
auto is_ready() const -> bool
Checks if the task is ready.
auto task_id() const
Gets the task ID.
void ensure()
Ensures the asset link is initialized.
auto id() const -> const std::string &
Gets the string identifier of the asset.
auto version() const -> uintptr_t
void set_internal_id(const std::string &internal_id=get_empty_id())
Sets the internal string identifier.