Unravel Engine C++ Reference
Loading...
Searching...
No Matches
context.hpp
Go to the documentation of this file.
1
#ifndef HPP_CONTEXT
2
#define HPP_CONTEXT
3
4
#include <hpp/type_index.hpp>
5
6
#include <iostream>
7
#include <map>
8
#include <memory>
9
10
namespace
rtti
11
{
12
13
struct
context
14
{
15
template
<
typename
T,
typename
D = T,
typename
... Args>
16
auto
add
(Args&&... args) -> T&
17
{
18
const
auto
id
= hpp::type_id<T>();
19
// std::cout << "context::" << __func__ << " < " << hpp::type_name_str<T>() << " >() -> " << index <<
20
// std::endl;
21
22
std::shared_ptr<T> obj = std::make_shared<D>(std::forward<Args>(args)...);
23
objects_[id] = obj;
24
return
*obj;
25
}
26
27
template
<
typename
T>
28
auto
has
() const ->
bool
29
{
30
const
auto
id
= hpp::type_id<T>();
31
return
objects_.find(
id
) != objects_.end();
32
}
33
34
template
<
typename
T>
35
auto
get
() -> T&
36
{
37
const
auto
id
= hpp::type_id<T>();
38
return
*
reinterpret_cast<
T*
>
(objects_.at(
id
).
get
());
39
}
40
41
template
<
typename
T>
42
auto
get
() const -> const T&
43
{
44
const
auto
id
= hpp::type_id<T>();
45
return
*
reinterpret_cast<
const
T*
>
(objects_.at(
id
).
get
());
46
}
47
48
template
<
typename
T>
49
auto
get_cached
() -> T&
50
{
51
static
T& cached =
get<T>
();
52
return
cached;
53
}
54
55
template
<
typename
T>
56
auto
get_cached
() const -> const T&
57
{
58
static
const
T& cached =
get<T>
();
59
return
cached;
60
}
61
62
63
template
<
typename
T>
64
auto
get_or_empalce
() -> T&
65
{
66
const
auto
id
= hpp::type_id<T>();
67
auto
it = objects_.find(
id
);
68
if
(it == objects_.end())
69
{
70
return
add<T>
();
71
}
72
73
74
return
*
reinterpret_cast<
T*
>
(it->second.get());
75
}
76
77
template
<
typename
T>
78
void
remove
()
79
{
80
const
auto
id
= hpp::type_id<T>();
81
// std::cout << "context::" << __func__ << " < " << hpp::type_name_str<T>() << " >() -> " << index <<
82
// std::endl;
83
objects_.erase(
id
);
84
}
85
86
auto
empty
() const ->
bool
87
{
88
return
objects_.empty();
89
}
90
91
void
print_types
()
const
92
{
93
for
(
const
auto
& kvp : objects_)
94
{
95
std::cout <<
" < "
<< kvp.first.name() <<
" >() -> "
<< kvp.first.hash_code() << std::endl;
96
}
97
}
98
99
private
:
100
std::map<hpp::type_index, std::shared_ptr<void>> objects_;
101
};
102
103
}
// namespace rtti
104
#endif
rtti
Definition
context.hpp:11
rtti::context
Definition
context.hpp:14
rtti::context::empty
auto empty() const -> bool
Definition
context.hpp:86
rtti::context::print_types
void print_types() const
Definition
context.hpp:91
rtti::context::get_cached
auto get_cached() -> T &
Definition
context.hpp:49
rtti::context::has
auto has() const -> bool
Definition
context.hpp:28
rtti::context::add
auto add(Args &&... args) -> T &
Definition
context.hpp:16
rtti::context::get_cached
auto get_cached() const -> const T &
Definition
context.hpp:56
rtti::context::get_or_empalce
auto get_or_empalce() -> T &
Definition
context.hpp:64
rtti::context::get
auto get() const -> const T &
Definition
context.hpp:42
rtti::context::get
auto get() -> T &
Definition
context.hpp:35
rtti::context::remove
void remove()
Definition
context.hpp:78
UnravelEngine
UnravelEngine
engine
core
context
context.hpp
Generated by
1.12.0