-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent.inl
More file actions
76 lines (60 loc) · 2.44 KB
/
Copy pathcomponent.inl
File metadata and controls
76 lines (60 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* File: /lib/ecs/component.inl
*
* This file is a part of cpplib project which is distributed under MIT License.
* See file LICENSE for full license details.
*
* Copyright (c) 2020-present Nikita Zuev (V.Slavski!) <[email protected]>
*/
#ifndef CPPLIB__lib__ecs__component__inl
#define CPPLIB__lib__ecs__component__inl
#include <cpp/lib_debug>
#include "../../lib/utils/enum.hpp"
namespace lib::ecs {
// DECLARATION lib::ecs::detail::Component<>
namespace detail {
template< class T, class Impl >
struct Component final : Impl {
constexpr Component() noexcept : Impl{ T::TYPE } {}
T object;
};
} // namespace detail
// INLINES lib::ecs::ComponentBase<>
template< ::cpp::Integral EntityId_, ::cpp::Enumeration ComponentType, class Component >
template< class T >
inline constexpr Ptr<Component>
ComponentBase<EntityId_, ComponentType, Component>::create() noexcept
{ return MkPtr< detail::Component<T, Component> >(); }
template< ::cpp::Integral EntityId_, ::cpp::Enumeration ComponentType, class Component >
inline constexpr ComponentBase<EntityId_, ComponentType, Component>::ComponentBase( ComponentType type ) noexcept
: type{ type }
{ CPP_ASSERT( utils::is_valid( type ) ); }
template< ::cpp::Integral EntityId_, ::cpp::Enumeration ComponentType, class Component >
template< class T >
inline constexpr T & ComponentBase<EntityId_, ComponentType, Component>::as() noexcept {
auto * ptr = object<T>();
CPP_ASSERT( ptr != nullptr );
return *ptr;
}
template< ::cpp::Integral EntityId_, ::cpp::Enumeration ComponentType, class Component >
template< class T >
inline constexpr const T & ComponentBase<EntityId_, ComponentType, Component>::as() const noexcept {
const auto * ptr = object<T>();
CPP_ASSERT( ptr != nullptr );
return *ptr;
}
template< ::cpp::Integral EntityId_, ::cpp::Enumeration ComponentType, class Component >
template< class T >
inline constexpr T * ComponentBase<EntityId_, ComponentType, Component>::object() noexcept {
if ( type != T::TYPE )
return nullptr;
return &static_cast< detail::Component<T, Component> * >( this )->object;
}
template< ::cpp::Integral EntityId_, ::cpp::Enumeration ComponentType, class Component >
template< class T >
inline constexpr const T * ComponentBase<EntityId_, ComponentType, Component> ::object() const noexcept {
if ( type != T::TYPE )
return nullptr;
return &static_cast< const detail::Component<T, Component> * >( this )->object;
}
} // namespace lib::ecs
#endif // CPPLIB__lib__ecs__component__inl