Toast Engine
Loading...
Searching...
No Matches
core_types.hpp
Go to the documentation of this file.
1
11
12#pragma once
13
14#include <atomic>
15#include <nlohmann/json.hpp>
16#include <toast/export.hpp>
17#include <toast/uid.hpp>
18#include <toml++/toml.hpp>
19#include <vector>
20
21using json_t = nlohmann::json;
22
23namespace assets {
24// clang-format off
25enum class SaveMode : uint8_t {
28};
29
33class TOAST_API ISaveable {
34public:
35 [[nodiscard]]
36 virtual auto serialize(SaveMode mode) const -> std::vector<uint8_t> = 0;
37 virtual ~ISaveable() = default;
38};
39
40// clang-format on
41
45class TOAST_API Asset {
46public:
47 Asset() = default;
48 virtual ~Asset() = default;
49
50 Asset(const Asset&) = delete;
51 auto operator=(const Asset&) -> Asset& = delete;
52
53 [[nodiscard]]
54 auto refCount() const noexcept -> uint32_t;
55
56 [[nodiscard]]
57 virtual auto type() const -> std::string_view = 0;
58
59protected:
60 void addRef() noexcept;
61 void release() noexcept;
62
63 friend class HandleBase;
64
65private:
66 std::atomic<uint32_t> m_ref_count {0};
67};
68
72class TOAST_API HandleBase {
73public:
74 HandleBase() = default;
75 explicit HandleBase(Asset* asset);
76
84 HandleBase(Asset* asset, toast::UID uid, std::string_view uri);
85
86 virtual ~HandleBase();
87
88 HandleBase(const HandleBase& other);
89 auto operator=(const HandleBase& other) -> HandleBase&;
90
91 HandleBase(HandleBase&& other) noexcept;
92 auto operator=(HandleBase&& other) noexcept -> HandleBase&;
93
98 void onChangeCallback(std::function<void()>&& callback);
99
100 [[nodiscard]]
101 auto hasValue() const noexcept -> bool;
102
103 [[nodiscard]]
104 auto uid() const noexcept -> toast::UID {
105 return m_uid;
106 }
107
108 [[nodiscard]]
109 auto path() const noexcept -> std::string_view {
110 return m_uri;
111 }
112
113 [[nodiscard]]
114 auto get() noexcept -> Asset&;
115
116 [[nodiscard]]
117 auto get() const noexcept -> const Asset&;
118
119 [[nodiscard]]
120 auto operator->() noexcept -> Asset*;
121
122 [[nodiscard]]
123 auto operator->() const noexcept -> const Asset*;
124
125 [[nodiscard]]
126 auto operator==(const HandleBase& other) const -> bool {
127 return m_uid.data() == other.m_uid.data();
128 }
129
130 [[nodiscard]]
131 auto operator<(const HandleBase& other) const -> bool {
132 return m_uid.data() < other.m_uid.data();
133 }
134
135protected:
136 Asset* m_asset = nullptr;
137 toast::UID m_uid;
138 std::string m_uri;
139
140private:
141 std::vector<std::function<void()>> m_callbacks;
142 void dispatchOnChange();
143};
144
148template<typename T>
149class Handle : public HandleBase {
150public:
151 using asset_type = T;
152 using HandleBase::HandleBase;
153
154 [[nodiscard]]
155 auto operator->() noexcept -> T* {
156 return static_cast<T*>(this->m_asset);
157 }
158
159 [[nodiscard]]
160 auto operator->() const noexcept -> const T* {
161 return static_cast<const T*>(this->m_asset);
162 }
163
164 [[nodiscard]]
165 auto operator*() noexcept -> T& {
166 return *static_cast<T*>(this->m_asset);
167 }
168
169 [[nodiscard]]
170 auto operator*() const noexcept -> const T& {
171 return *static_cast<const T*>(this->m_asset);
172 }
173
174 [[nodiscard]]
175 auto get() noexcept -> T& {
176 return *static_cast<T*>(this->m_asset);
177 }
178
179 [[nodiscard]]
180 auto get() const noexcept -> const T& {
181 return *static_cast<const T*>(this->m_asset);
182 }
183
184 template<typename U>
185 auto as() -> Handle<U> {
186 return Handle<U>(this->m_asset, this->m_uid, this->m_uri);
187 }
188};
189}
Base class for all engine assets.
Definition core_types.hpp:45
void onChangeCallback(std::function< void()> &&callback)
Definition types.cpp:76
Type-safe smart pointer for assets.
Definition core_types.hpp:149
Inherit from this class to allow an asset to be saved.
Definition core_types.hpp:33
SaveMode
Definition core_types.hpp:25
@ game
compact binary format; used at runtime
Definition core_types.hpp:27
Definition App.axaml.cs:9
Definition uid.hpp:20
auto data() const noexcept -> uint64_t
Returns the raw 64-bit integer; useful for hashing or serialization that owns the encoding.
Definition uid.cpp:136
Unique identifier for everything that exists in our engine.