Skip to content

Commit

Permalink
decoupling pools and components
Browse files Browse the repository at this point in the history
  • Loading branch information
skypjack committed Aug 25, 2019
1 parent 643a20f commit 8713d0a
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 102 deletions.
37 changes: 37 additions & 0 deletions src/entt/entity/component.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef ENTT_ENTITY_COMPONENT_HPP
#define ENTT_ENTITY_COMPONENT_HPP


#include <type_traits>
#include "default_storage.hpp"


namespace entt {


template<typename>
struct component_traits;


template<typename Type, typename = std::void_t<>>
struct component_pool {
template<typename Entity>
using type = default_storage<Entity, Type>;
};


template<typename Type>
struct component_pool<Type, std::void_t<typename component_traits<Type>::pool_type>> {
template<typename Entity>
using type = typename component_traits<Type>::pool_type;
};


template<typename Entity, typename Type>
using component_pool_t = typename component_pool<Type>::template type<Entity>;


}


#endif // ENTT_ENTITY_COMPONENT_HPP
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef ENTT_ENTITY_STORAGE_HPP
#define ENTT_ENTITY_STORAGE_HPP
#ifndef ENTT_ENTITY_DEFAULT_STORAGE_HPP
#define ENTT_ENTITY_DEFAULT_STORAGE_HPP


#include <algorithm>
Expand All @@ -19,7 +19,7 @@ namespace entt {


/**
* @brief Basic storage implementation.
* @brief Default storage implementation.
*
* This class is a refinement of a sparse set that associates an object to an
* entity. The main purpose of this class is to extend sparse sets to store
Expand Down Expand Up @@ -48,13 +48,13 @@ namespace entt {
* @tparam Type Type of objects assigned to the entities.
*/
template<typename Entity, typename Type, typename = std::void_t<>>
class basic_storage: public sparse_set<Entity> {
class default_storage: public sparse_set<Entity> {
using underlying_type = sparse_set<Entity>;
using traits_type = entt_traits<std::underlying_type_t<Entity>>;

template<bool Const>
class iterator {
friend class basic_storage<Entity, Type>;
friend class default_storage<Entity, Type>;

using instance_type = std::conditional_t<Const, const std::vector<Type>, std::vector<Type>>;
using index_type = typename traits_type::difference_type;
Expand Down Expand Up @@ -490,14 +490,14 @@ class basic_storage: public sparse_set<Entity> {
};


/*! @copydoc basic_storage */
/*! @copydoc default_storage */
template<typename Entity, typename Type>
class basic_storage<Entity, Type, std::enable_if_t<std::is_empty_v<Type>>>: public sparse_set<Entity> {
class default_storage<Entity, Type, std::enable_if_t<std::is_empty_v<Type>>>: public sparse_set<Entity> {
using traits_type = entt_traits<std::underlying_type_t<Entity>>;
using underlying_type = sparse_set<Entity>;

class iterator {
friend class basic_storage<Entity, Type>;
friend class default_storage<Entity, Type>;

using index_type = typename traits_type::difference_type;

Expand Down Expand Up @@ -693,12 +693,7 @@ class basic_storage<Entity, Type, std::enable_if_t<std::is_empty_v<Type>>>: publ
}
};

/*! @copydoc basic_storage */
template<typename Entity, typename Type>
struct storage: basic_storage<Entity, Type> {};


}


#endif // ENTT_ENTITY_STORAGE_HPP
#endif // ENTT_ENTITY_DEFAULT_STORAGE_HPP
10 changes: 5 additions & 5 deletions src/entt/entity/group.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "../config/config.h"
#include "../core/type_traits.hpp"
#include "sparse_set.hpp"
#include "storage.hpp"
#include "component.hpp"
#include "utility.hpp"
#include "fwd.hpp"

Expand Down Expand Up @@ -72,10 +72,10 @@ class basic_group<Entity, exclude_t<Exclude...>, get_t<Get, Other...>> {
friend class basic_registry<Entity>;

template<typename Component>
using pool_type = std::conditional_t<std::is_const_v<Component>, const storage<Entity, std::remove_const_t<Component>>, storage<Entity, Component>>;
using pool_type = std::conditional_t<std::is_const_v<Component>, const component_pool_t<Entity, std::remove_const_t<Component>>, component_pool_t<Entity, Component>>;

// we could use pool_type<Type> *..., but vs complains about it and refuses to compile for unknown reasons (most likely a bug)
basic_group(sparse_set<Entity> *ref, storage<Entity, std::remove_const_t<Get>> *get, storage<Entity, std::remove_const_t<Other>> *... other) ENTT_NOEXCEPT
basic_group(sparse_set<Entity> *ref, pool_type<Get> *get, pool_type<Other> *... other) ENTT_NOEXCEPT
: handler{ref},
pools{get, other...}
{}
Expand Down Expand Up @@ -492,13 +492,13 @@ class basic_group<Entity, exclude_t<Exclude...>, get_t<Get...>, Owned, Other...>
friend class basic_registry<Entity>;

template<typename Component>
using pool_type = std::conditional_t<std::is_const_v<Component>, const storage<Entity, std::remove_const_t<Component>>, storage<Entity, Component>>;
using pool_type = std::conditional_t<std::is_const_v<Component>, const component_pool_t<Entity, std::remove_const_t<Component>>, component_pool_t<Entity, Component>>;

template<typename Component>
using component_iterator_type = decltype(std::declval<pool_type<Component>>().begin());

// we could use pool_type<Type> *..., but vs complains about it and refuses to compile for unknown reasons (most likely a bug)
basic_group(const typename basic_registry<Entity>::size_type *sz, storage<Entity, std::remove_const_t<Owned>> *owned, storage<Entity, std::remove_const_t<Other>> *... other, storage<Entity, std::remove_const_t<Get>> *... get) ENTT_NOEXCEPT
basic_group(const typename basic_registry<Entity>::size_type *sz, pool_type<Owned> *owned, pool_type<Other> *... other, pool_type<Get> *... get) ENTT_NOEXCEPT
: length{sz},
pools{owned, other..., get...}
{}
Expand Down
4 changes: 2 additions & 2 deletions src/entt/entity/observer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#include <type_traits>
#include "../config/config.h"
#include "../core/type_traits.hpp"
#include "default_storage.hpp"
#include "registry.hpp"
#include "storage.hpp"
#include "entity.hpp"
#include "fwd.hpp"

Expand Down Expand Up @@ -428,7 +428,7 @@ class basic_observer {
private:
basic_registry<entity_type> *target;
void(* release)(basic_observer &, basic_registry<entity_type> &);
storage<entity_type, payload_type> view;
default_storage<entity_type, payload_type> view;
};


Expand Down
20 changes: 10 additions & 10 deletions src/entt/entity/registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
#include "../signal/sigh.hpp"
#include "runtime_view.hpp"
#include "sparse_set.hpp"
#include "component.hpp"
#include "snapshot.hpp"
#include "storage.hpp"
#include "utility.hpp"
#include "entity.hpp"
#include "group.hpp"
Expand Down Expand Up @@ -51,13 +51,13 @@ class basic_registry {
};

template<typename Component>
struct pool_handler: storage<Entity, Component> {
struct pool_handler: component_pool_t<Entity, Component> {
group_type *group{};

pool_handler() ENTT_NOEXCEPT = default;

pool_handler(const storage<Entity, Component> &other)
: storage<Entity, Component>{other}
pool_handler(const component_pool_t<Entity, Component> &other)
: component_pool_t<Entity, Component>{other}
{}

auto on_construct() ENTT_NOEXCEPT {
Expand All @@ -75,19 +75,19 @@ class basic_registry {
template<typename... Args>
decltype(auto) assign(basic_registry &registry, const Entity entt, Args &&... args) {
if constexpr(std::is_empty_v<Component>) {
storage<Entity, Component>::construct(entt);
component_pool_t<Entity, Component>::construct(entt);
construction.publish(entt, registry, Component{});
return Component{std::forward<Args>(args)...};
} else {
auto &component = storage<Entity, Component>::construct(entt, std::forward<Args>(args)...);
auto &component = component_pool_t<Entity, Component>::construct(entt, std::forward<Args>(args)...);
construction.publish(entt, registry, component);
return component;
}
}

template<typename It, typename... Comp>
auto batch(basic_registry &registry, It first, It last, const Comp &... value) {
auto it = storage<Entity, Component>::batch(first, last, value...);
auto it = component_pool_t<Entity, Component>::batch(first, last, value...);

if(!construction.empty()) {
std::for_each(first, last, [this, &registry, it](const auto entt) mutable {
Expand All @@ -100,19 +100,19 @@ class basic_registry {

void remove(basic_registry &registry, const Entity entt) {
destruction.publish(entt, registry);
storage<Entity, Component>::destroy(entt);
component_pool_t<Entity, Component>::destroy(entt);
}

template<typename... Args>
decltype(auto) replace(basic_registry &registry, const Entity entt, Args &&... args) {
if constexpr(std::is_empty_v<Component>) {
ENTT_ASSERT((storage<Entity, Component>::has(entt)));
ENTT_ASSERT((component_pool_t<Entity, Component>::has(entt)));
update.publish(entt, registry, Component{});
return Component{std::forward<Args>(args)...};
} else {
Component component{std::forward<Args>(args)...};
update.publish(entt, registry, component);
return (storage<Entity, Component>::get(entt) = std::move(component));
return (component_pool_t<Entity, Component>::get(entt) = std::move(component));
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/entt/entity/view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "../config/config.h"
#include "../core/type_traits.hpp"
#include "sparse_set.hpp"
#include "storage.hpp"
#include "component.hpp"
#include "entity.hpp"
#include "fwd.hpp"

Expand Down Expand Up @@ -64,7 +64,7 @@ class basic_view {
friend class basic_registry<Entity>;

template<typename Comp>
using pool_type = std::conditional_t<std::is_const_v<Comp>, const storage<Entity, std::remove_const_t<Comp>>, storage<Entity, Comp>>;
using pool_type = std::conditional_t<std::is_const_v<Comp>, const component_pool_t<Entity, std::remove_const_t<Comp>>, component_pool_t<Entity, Comp>>;

template<typename Comp>
using component_iterator_type = decltype(std::declval<pool_type<Comp>>().begin());
Expand Down Expand Up @@ -145,7 +145,7 @@ class basic_view {
};

// we could use pool_type<Component> *..., but vs complains about it and refuses to compile for unknown reasons (likely a bug)
basic_view(storage<Entity, std::remove_const_t<Component>> *... ref) ENTT_NOEXCEPT
basic_view(pool_type<Component> *... ref) ENTT_NOEXCEPT
: pools{ref...}
{}

Expand Down Expand Up @@ -541,7 +541,7 @@ class basic_view<Entity, Component> {
/*! @brief A registry is allowed to create views. */
friend class basic_registry<Entity>;

using pool_type = std::conditional_t<std::is_const_v<Component>, const storage<Entity, std::remove_const_t<Component>>, storage<Entity, Component>>;
using pool_type = std::conditional_t<std::is_const_v<Component>, const component_pool_t<Entity, std::remove_const_t<Component>>, component_pool_t<Entity, Component>>;

basic_view(pool_type *ref) ENTT_NOEXCEPT
: pool{ref}
Expand Down
3 changes: 2 additions & 1 deletion src/entt/entt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "core/type_traits.hpp"
#include "core/utility.hpp"
#include "entity/actor.hpp"
#include "entity/component.hpp"
#include "entity/default_storage.hpp"
#include "entity/entity.hpp"
#include "entity/group.hpp"
#include "entity/helper.hpp"
Expand All @@ -14,7 +16,6 @@
#include "entity/runtime_view.hpp"
#include "entity/snapshot.hpp"
#include "entity/sparse_set.hpp"
#include "entity/storage.hpp"
#include "entity/utility.hpp"
#include "entity/view.hpp"
#include "locator/locator.hpp"
Expand Down
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ SETUP_AND_ADD_TEST(registry entt/entity/registry.cpp)
SETUP_AND_ADD_TEST(runtime_view entt/entity/runtime_view.cpp)
SETUP_AND_ADD_TEST(snapshot entt/entity/snapshot.cpp)
SETUP_AND_ADD_TEST(sparse_set entt/entity/sparse_set.cpp)
SETUP_AND_ADD_TEST(storage entt/entity/storage.cpp)
SETUP_AND_ADD_TEST(default_storage entt/entity/default_storage.cpp)
SETUP_AND_ADD_TEST(view entt/entity/view.cpp)

# Test locator
Expand Down
Loading

0 comments on commit 8713d0a

Please sign in to comment.