Toast Engine
Toggle main menu visibility
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
21
using
json_t = nlohmann::json;
22
23
namespace
assets {
24
// clang-format off
25
enum class
SaveMode
: uint8_t {
26
editor
,
27
game
,
28
};
29
33
class
TOAST_API
ISaveable
{
34
public
:
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
45
class
TOAST_API Asset {
46
public
:
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
59
protected
:
60
void
addRef()
noexcept
;
61
void
release()
noexcept
;
62
63
friend
class
HandleBase;
64
65
private
:
66
std::atomic<uint32_t> m_ref_count {0};
67
};
68
72
class
TOAST_API HandleBase {
73
public
:
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
135
protected
:
136
Asset
* m_asset =
nullptr
;
137
toast::UID
m_uid;
138
std::string m_uri;
139
140
private
:
141
std::vector<std::function<void()>> m_callbacks;
142
void
dispatchOnChange();
143
};
144
148
template
<
typename
T>
149
class
Handle
:
public
HandleBase {
150
public
:
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
}
assets::Asset
Base class for all engine assets.
Definition
core_types.hpp:45
assets::HandleBase::onChangeCallback
void onChangeCallback(std::function< void()> &&callback)
Definition
types.cpp:76
assets::Handle
Type-safe smart pointer for assets.
Definition
core_types.hpp:149
assets::ISaveable
Inherit from this class to allow an asset to be saved.
Definition
core_types.hpp:33
assets::SaveMode
SaveMode
Definition
core_types.hpp:25
assets::SaveMode::game
@ game
compact binary format; used at runtime
Definition
core_types.hpp:27
export.hpp
editor
Definition
App.axaml.cs:9
toast::UID
Definition
uid.hpp:20
toast::UID::data
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
uid.hpp
Unique identifier for everything that exists in our engine.
engine
src
toast
assets
core_types.hpp
Generated by
1.17.0