co_ecs 0.9.0
Cobalt ECS
Loading...
Searching...
No Matches
type_meta.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <memory>
6#include <string_view>
7
8namespace co_ecs {
9
14template<typename T>
15constexpr static auto type_name() noexcept -> std::string_view {
16#if defined CO_ECS_PRETTY_FUNCTION
17 std::string_view pretty_function{ CO_ECS_PRETTY_FUNCTION };
18 auto prefix_pos = pretty_function.find_first_of(detail::PrettyFunctionPrefix);
19 auto suffix_pos = pretty_function.find_last_of(detail::prettyFunctionSuffix);
20 auto first = pretty_function.find_first_not_of(' ', prefix_pos + 1);
21 auto value = pretty_function.substr(first, suffix_pos - first);
22 return value;
23#else
24 return typeid(T).name();
25#endif
26}
27
29struct type_meta {
35 template<typename T>
36 static void copy_constructor(void* ptr, void* rhs) noexcept(std::is_nothrow_copy_constructible_v<T>) {
37 if constexpr (std::is_copy_constructible_v<T>) {
38 std::construct_at(static_cast<T*>(ptr), *static_cast<T*>(rhs));
39 } else {
40 throw std::invalid_argument("No copy constructor defined");
41 }
42 }
43
49 template<typename T>
50 static void move_constructor(void* ptr, void* rhs) noexcept {
51 std::construct_at(static_cast<T*>(ptr), std::move(*static_cast<T*>(rhs)));
52 }
53
59 template<typename T>
60 static void move_assignment(void* lhs, void* rhs) noexcept {
61 *static_cast<T*>(lhs) = std::move(*static_cast<T*>(rhs));
62 }
63
68 template<typename T>
69 static void destructor(void* ptr) noexcept {
70 static_cast<T*>(ptr)->~T();
71 }
72
77 template<typename T>
78 static auto of() noexcept -> const type_meta* {
79 static const type_meta meta{
80 sizeof(T),
81 alignof(T),
82 type_name<T>(),
83 &copy_constructor<T>,
84 &move_constructor<T>,
85 &move_assignment<T>,
86 &destructor<T>,
87 };
88 return &meta;
89 }
90
91 std::size_t size;
92 std::size_t align;
93 std::string_view name;
94 void (*copy_construct)(void*, void*);
95 void (*move_construct)(void*, void*);
96 void (*move_assign)(void*, void*);
97 void (*destruct)(void*);
98};
99
100
101} // namespace co_ecs
Definition archetype.hpp:11
STL namespace.
Type meta information.
Definition type_meta.hpp:29
static void move_assignment(void *lhs, void *rhs) noexcept
Move assignment callback for type T.
Definition type_meta.hpp:60
void(* copy_construct)(void *, void *)
Definition type_meta.hpp:94
std::size_t size
Definition type_meta.hpp:91
std::size_t align
Definition type_meta.hpp:92
void(* move_construct)(void *, void *)
Definition type_meta.hpp:95
static void destructor(void *ptr) noexcept
Destructor callback for type T.
Definition type_meta.hpp:69
static auto of() noexcept -> const type_meta *
Constructs type_meta for type T.
Definition type_meta.hpp:78
static void copy_constructor(void *ptr, void *rhs) noexcept(std::is_nothrow_copy_constructible_v< T >)
Copy constructor callback for type T.
Definition type_meta.hpp:36
void(* destruct)(void *)
Definition type_meta.hpp:97
static void move_constructor(void *ptr, void *rhs) noexcept
Move constructor callback for type T.
Definition type_meta.hpp:50
void(* move_assign)(void *, void *)
Definition type_meta.hpp:96
std::string_view name
Definition type_meta.hpp:93