1 #ifndef ENTT_ENTITY_PROTOTYPE_HPP 2 #define ENTT_ENTITY_PROTOTYPE_HPP 9 #include <unordered_map> 10 #include "../config/config.h" 11 #include "registry.hpp" 40 template<
typename Entity>
41 class basic_prototype {
42 using basic_fn_type = void(
const basic_prototype &, basic_registry<Entity> &,
const Entity);
45 template<
typename Component>
46 struct component_wrapper { Component component; };
48 struct component_handler {
54 if(reg->valid(entity)) {
93 : handlers{std::move(other.handlers)},
112 auto tmp{std::move(other)};
113 handlers.swap(tmp.handlers);
114 std::swap(reg, tmp.reg);
115 std::swap(entity, tmp.entity);
128 template<
typename Component,
typename... Args>
129 Component &
set(Args &&... args) {
130 component_handler handler;
133 const auto &wrapper = proto.reg->template get<component_wrapper<Component>>(proto.entity);
134 other.template assign_or_replace<Component>(dst, wrapper.component);
138 if(!other.template has<Component>(dst)) {
139 const auto &wrapper = proto.reg->template get<component_wrapper<Component>>(proto.entity);
140 other.template assign<Component>(dst, wrapper.component);
144 handlers[reg->template type<Component>()] = handler;
145 auto &wrapper = reg->template assign_or_replace<component_wrapper<Component>>(entity, Component{std::forward<Args>(args)...});
146 return wrapper.component;
153 template<
typename Component>
155 reg->template reset<component_wrapper<Component>>(entity);
156 handlers.erase(reg->template type<Component>());
164 template<
typename... Component>
165 bool has() const ENTT_NOEXCEPT {
166 return reg->template has<component_wrapper<Component>...>(entity);
181 template<
typename... Component>
182 decltype(
auto)
get() const ENTT_NOEXCEPT {
183 if constexpr(
sizeof...(Component) == 1) {
184 return (std::as_const(*reg).template
get<component_wrapper<Component...>>(entity).component);
186 return std::tuple<std::add_const_t<Component> &...>{get<Component>()...};
191 template<
typename... Component>
192 decltype(
auto)
get() ENTT_NOEXCEPT {
193 if constexpr(
sizeof...(Component) == 1) {
194 return (const_cast<Component &>(std::as_const(*this).template get<Component>()), ...);
196 return std::tuple<Component &...>{get<Component>()...};
205 template<
typename... Component>
207 if constexpr(
sizeof...(Component) == 1) {
208 const auto *wrapper = reg->template
try_get<component_wrapper<Component...>>(entity);
209 return wrapper ? &wrapper->component :
nullptr;
211 return std::tuple<std::add_const_t<Component> *...>{try_get<Component>()...};
216 template<
typename... Component>
218 if constexpr(
sizeof...(Component) == 1) {
219 return (const_cast<Component *>(std::as_const(*this).template try_get<Component>()), ...);
221 return std::tuple<Component *...>{try_get<Component>()...};
292 for(
auto &handler: handlers) {
293 handler.second.assign(*
this, other, dst);
341 for(
auto &handler: handlers) {
342 handler.second.assign_or_replace(*
this, other, dst);
470 return const_cast<registry_type &
>(std::as_const(*this).backend());
474 std::unordered_map<component_type, component_handler> handlers;
483 #endif // ENTT_ENTITY_PROTOTYPE_HPP basic_prototype(basic_prototype &&other)
Move constructor.
void assign_or_replace(const entity_type dst) const
Assigns or replaces the components of a prototype for an entity.
entity_type create(registry_type &other) const
Creates a new entity using a given prototype.
Entity entity_type
Underlying entity identifier.
std::size_t size_type
Unsigned integer type.
~basic_prototype()
Releases all its resources.
void operator()(const entity_type dst) const ENTT_NOEXCEPT
Assigns the components of a prototype to an entity.
decltype(auto) create()
Creates a new entity and returns it.
void assign(const entity_type dst) const
Assigns the components of a prototype to a given entity.
basic_prototype(registry_type &ref)
Constructs a prototype that is bound to a given registry.
void operator()(registry_type &other, const entity_type dst) const ENTT_NOEXCEPT
Assigns the components of a prototype to an entity.
entity_type create() const
Creates a new entity using a given prototype.
constexpr auto null
Null entity.
entity_type operator()() const ENTT_NOEXCEPT
Creates a new entity using a given prototype.
ENTT_ID_TYPE component_type
Unsigned integer type.
Fast and reliable entity-component system.
auto try_get() ENTT_NOEXCEPT
Returns pointers to the given components.
void unset() ENTT_NOEXCEPT
Removes the given component from a prototype.
void assign_or_replace(registry_type &other, const entity_type dst) const
Assigns or replaces the components of a prototype for an entity.
void assign(registry_type &other, const entity_type dst) const
Assigns the components of a prototype to a given entity.
Prototype container for concepts.
const registry_type & backend() const ENTT_NOEXCEPT
Returns a reference to the underlying registry.
registry_type & backend() ENTT_NOEXCEPT
Returns a reference to the underlying registry.
entity_type operator()(registry_type &other) const ENTT_NOEXCEPT
Creates a new entity using a given prototype.
decltype(auto) get() const ENTT_NOEXCEPT
Returns references to the given components.
auto try_get() const ENTT_NOEXCEPT
Returns pointers to the given components.
bool has() const ENTT_NOEXCEPT
Checks if a prototype owns all the given components.
basic_prototype & operator=(basic_prototype &&other)
Move assignment operator.