Blender V5.3
asset_index.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include "ED_asset_indexer.hh"
12
13#include <memory>
14#include <variant>
15
16namespace blender {
17struct AssetMetaData;
18class StringRefNull;
19namespace io::serialize {
20class DictionaryValue;
21class Value;
22} // namespace io::serialize
23} // namespace blender
24
26
28
29std::unique_ptr<io::serialize::Value> read_contents(StringRefNull filepath);
30
31AssetMetaData *asset_metadata_from_dictionary(const io::serialize::DictionaryValue &entry);
32
48template<typename T = std::monostate> class ReadingResult {
49 public:
50 enum class Type {
54 };
56 std::string failure_reason;
57 std::optional<T> success_value;
58
69
74 template<typename U = T>
76 requires(std::is_same_v<U, std::monostate>)
77 {
79 }
80
85 template<typename U = T>
86 static ReadingResult Success(T value)
87 requires(!std::is_same_v<U, std::monostate>)
88 {
90 result.success_value = std::move(value);
91 return result;
92 }
93
101 {
103 result.failure_reason = failure_reason;
104 return result;
105 }
106
113 {
115 }
116
126
128 bool is_success() const
129 {
130 return this->type == Type::Success;
131 }
132
133 bool is_failure() const
134 {
135 return this->type == Type::Failure;
136 }
137
138 bool is_cancelled() const
139 {
140 return this->type == Type::Cancelled;
141 }
142
143 bool has_warnings() const
144 {
145 return !this->warnings.is_empty();
146 }
147
151 template<typename U> void move_warnings_from(ReadingResult<U> &other)
152 {
153 BLI_assert_msg(is_success(), "Attempted to move warnings into a non-success ReadingResult");
154 for (std::string &warning : other.warnings) {
155 this->warnings.append(std::move(warning));
156 }
157 }
158
165 {
166 ReadingResult<> without_value(static_cast<ReadingResult<>::Type>(this->type));
167 without_value.success_value.reset();
168 without_value.failure_reason = this->failure_reason;
169 without_value.warnings.extend(this->warnings);
170 return without_value;
171 }
172
177 template<typename U = T>
179 requires(!std::is_same_v<U, std::monostate>)
180 {
181 BLI_assert_msg(is_success() || !success_value.has_value(),
182 "Attempted to access value of non-success ReadingResult");
183 return *success_value;
184 }
185
190 template<typename U = T>
191 const T &operator*() const
192 requires(!std::is_same_v<U, std::monostate>)
193 {
194 BLI_assert_msg(is_success() || !success_value.has_value(),
195 "Attempted to access value of non-success ReadingResult");
196 return *success_value;
197 }
198
203 template<typename U = T>
205 requires(!std::is_same_v<U, std::monostate>)
206 {
207 T &success_value = **this;
208 return &success_value;
209 }
210
215 template<typename U = T>
216 const T *operator->() const
217 requires(!std::is_same_v<U, std::monostate>)
218 {
219 const T &success_value = **this;
220 return &success_value;
221 }
222
226 template<typename U> ReadingResult(const ReadingResult<U> &other)
227 {
228 this->type = static_cast<ReadingResult<T>::Type>(other.type);
229 if (this->type == Type::Success) {
230 // Only allow if U is std::monostate.
231 static_assert(std::is_same<U, std::monostate>::value,
232 "Cannot convert a valued success to another type");
233 success_value.reset();
234 }
235 else {
236 // Failure or Cancelled can convert freely.
238 }
239 }
240};
241
242std::optional<bool> file_older_than_timestamp(const char *filepath, Timestamp timestamp);
243
248ReadingResult<> read_remote_listing_v1(
249 StringRefNull listing_root_dirpath,
251 RemoteListingWaitForPagesFn wait_fn = nullptr,
252 const std::optional<Timestamp> ignore_before_timestamp = std::nullopt);
253
254} // namespace blender::ed::asset::index
#define BLI_assert_msg(a, msg)
void append(const T &value)
bool is_empty() const
void move_warnings_from(ReadingResult< U > &other)
ReadingResult(const ReadingResult< U > &other)
ReadingResult without_success_value() const
static ReadingResult Success(T value)
static ReadingResult Failure(const StringRef failure_reason)
#define T
ReadingResult read_remote_listing_v1(StringRefNull listing_root_dirpath, RemoteListingEntryProcessFn process_fn, RemoteListingWaitForPagesFn wait_fn=nullptr, const std::optional< Timestamp > ignore_before_timestamp=std::nullopt)
std::filesystem::file_time_type Timestamp
FunctionRef< bool(RemoteListingAssetEntry &)> RemoteListingEntryProcessFn
FunctionRef< bool()> RemoteListingWaitForPagesFn
std::optional< bool > file_older_than_timestamp(const char *filepath, Timestamp timestamp)
AssetMetaData * asset_metadata_from_dictionary(const io::serialize::DictionaryValue &entry)
std::unique_ptr< io::serialize::Value > read_contents(StringRefNull filepath)
BMesh Iterators.
UserDef U
Definition blender.cc:58
The meta-data of an asset. By creating and giving this for a data-block (ID.asset_data),...