1#ifndef ENTT_POLY_POLY_HPP
2#define ENTT_POLY_POLY_HPP
4#include "../core/any.hpp"
5#include "../core/concepts.hpp"
6#include "../core/type_info.hpp"
7#include "../core/type_traits.hpp"
8#include "../stl/concepts.hpp"
9#include "../stl/cstddef.hpp"
10#include "../stl/functional.hpp"
11#include "../stl/tuple.hpp"
12#include "../stl/type_traits.hpp"
13#include "../stl/utility.hpp"
24 template<
typename Type>
25 operator Type &&()
const;
34 template<stl::size_t Member,
typename... Args>
38 template<stl::size_t Member,
typename... Args>
48template<
typename Concept, stl::
size_t Len, stl::
size_t Align>
52 template<
typename Ret,
typename Clazz,
typename... Args>
53 requires stl::derived_from<inspector, stl::remove_const_t<Clazz>>
54 static auto vtable_entry(Ret (*)(Clazz &, Args...))
57 template<
typename Ret,
typename... Args>
58 static auto vtable_entry(Ret (*)(Args...))
61 template<
typename Ret,
typename Clazz,
typename... Args>
62 requires stl::derived_from<inspector, Clazz>
63 static auto vtable_entry(Ret (Clazz::*)(Args...))
66 template<
typename Ret,
typename Clazz,
typename... Args>
67 requires stl::derived_from<inspector, Clazz>
68 static auto vtable_entry(Ret (Clazz::*)(Args...)
const)
71 template<
auto... Candidate>
73 ->
decltype(stl::make_tuple(vtable_entry(Candidate)...));
75 template<
typename... Func>
77 if constexpr(
sizeof...(Func) == 0u) {
78 return decltype(make_vtable(
typename Concept::template impl<inspector>{})){};
79 }
else if constexpr((stl::is_function_v<Func> && ...)) {
80 return decltype(stl::make_tuple(vtable_entry(stl::declval<Func inspector::*>())...)){};
84 template<
typename Type,
auto Candidate,
typename Ret,
typename Any,
typename... Args>
85 static void fill_vtable_entry(Ret (*&entry)(Any &, Args...))
noexcept {
86 if constexpr(stl::is_invocable_r_v<Ret,
decltype(Candidate), Args...>) {
87 entry = +[](Any &, Args... args) -> Ret {
88 return stl::invoke(Candidate, stl::forward<Args>(args)...);
91 entry = +[](Any &
instance, Args... args) -> Ret {
97 template<
typename Type,
auto... Index>
98 [[nodiscard]]
static auto fill_vtable(stl::index_sequence<Index...>)
noexcept {
100 (fill_vtable_entry<Type, value_list_element_v<Index, typename Concept::template impl<Type>>>(stl::get<Index>(impl)), ...);
104 using vtable_type =
decltype(make_vtable(Concept{}));
105 static constexpr bool is_mono = stl::tuple_size_v<vtable_type> == 1u;
109 using type = stl::conditional_t<is_mono, stl::tuple_element_t<0u, vtable_type>,
const vtable_type *>;
116 template<cvref_unqualified Type>
118 static const vtable_type vtable = fill_vtable<Type>(stl::make_index_sequence<Concept::template impl<Type>::size>{});
120 if constexpr(is_mono) {
121 return stl::get<0>(vtable);
132template<
typename Poly>
142 template<stl::size_t Member,
typename... Args>
144 const auto &
poly =
static_cast<const Poly &
>(self);
146 if constexpr(stl::is_function_v<stl::remove_pointer_t<
decltype(
poly.vtable)>>) {
147 return poly.vtable(
poly.storage, stl::forward<Args>(args)...);
149 return stl::get<Member>(*
poly.vtable)(
poly.storage, stl::forward<Args>(args)...);
154 template<stl::size_t Member,
typename... Args>
156 auto &
poly =
static_cast<Poly &
>(self);
158 if constexpr(stl::is_function_v<stl::remove_pointer_t<
decltype(
poly.vtable)>>) {
159 static_assert(Member == 0u,
"Unknown member");
160 return poly.vtable(
poly.storage, stl::forward<Args>(args)...);
162 return stl::get<Member>(*
poly.vtable)(
poly.storage, stl::forward<Args>(args)...);
176template<stl::size_t Member,
typename Poly,
typename... Args>
178 return stl::forward<Poly>(self).template
invoke<Member>(self, stl::forward<Args>(args)...);
196template<
typename Concept, stl::
size_t Len, stl::
size_t Align>
197class basic_poly:
private Concept::template
type<poly_base<basic_poly<Concept, Len, Align>>> {
215 template<typename Type, typename... Args>
217 : storage{stl::in_place_type<Type>, stl::forward<Args>(args)...},
225 template<
typename Type>
226 requires (!stl::same_as<stl::remove_cvref_t<Type>,
basic_poly>)
228 :
basic_poly{stl::in_place_type<stl::remove_cvref_t<Type>>, stl::forward<Type>(value)} {}
235 return storage.info();
242 [[nodiscard]]
const void *
data() const noexcept {
243 return storage.data();
247 [[nodiscard]]
void *
data() noexcept {
248 return storage.data();
257 template<
typename Type,
typename... Args>
273 [[nodiscard]]
explicit operator bool() const noexcept {
274 return static_cast<bool>(storage);
296 ref.storage = storage.as_ref();
304 ref.storage = storage.as_ref();
A SBO friendly, type-safe container for single values of any type.
poly_vtable< Concept, Len, Align >::type vtable_type
basic_poly as_ref() noexcept
Aliasing constructor.
void * data() noexcept
Returns an opaque pointer to the contained instance.
void reset()
Destroys contained object.
concept_type * operator->() noexcept
Returns a pointer to the underlying concept.
const type_info & info() const noexcept
Returns the object type info if any, type_id<void>() otherwise.
void emplace(Args &&...args)
Replaces the contained object by creating a new instance directly.
basic_poly as_ref() const noexcept
Aliasing constructor.
const concept_type * operator->() const noexcept
Returns a pointer to the underlying concept.
Concept::template type< poly_base< basic_poly > > concept_type
const void * data() const noexcept
Returns an opaque pointer to the contained instance.
basic_poly() noexcept=default
Default constructor.
basic_poly(Type &&value) noexcept
Constructs a poly from a given value.
Static virtual table factory.
stl::conditional_t< is_mono, stl::tuple_element_t< 0u, vtable_type >, const vtable_type * > type
Virtual table type.
static type instance() noexcept
Returns a static virtual table for a specific concept and type.
Custom EnTT namespace for the standard template library.
decltype(auto) poly_call(Poly &&self, Args &&...args)
Shortcut for calling poly_base<Type>::invoke.
stl::remove_const_t< Type > any_cast(const basic_any< Len, Align > &data) noexcept
Performs type-safe access to the contained object.
basic_poly< Concept > poly
Alias declaration for the most common use case.
void invoke(Registry ®, const typename Registry::entity_type entt)
Helper to create a listener that directly invokes a member function.
@ ref
Aliasing mode, non-const reference.
constness_as< To, From >::type constness_as_t
Alias template to facilitate the transcription of the constness.
basic_storage< Type > storage
Alias declaration for the most common use case.
Poly base class used to inject functionalities into concepts.
decltype(auto) invoke(const poly_base &self, Args &&...args) const
Invokes a function from the static virtual table.
decltype(auto) invoke(poly_base &self, Args &&...args)
Invokes a function from the static virtual table.
Inspector class used to infer the type of the virtual table.
poly_inspector invoke(Args &&...args) const
Dummy invocation function (definition only).
poly_inspector invoke(Args &&...args)
Dummy invocation function (definition only).
Implementation specific information about a type.
A class to use to push around lists of types, nothing more.
A class to use to push around lists of constant values, nothing more.