Add const fns to Manager, obey 80 char line limit

This commit is contained in:
Stephen Seo 2017-09-20 17:16:26 +09:00
parent bc70089822
commit 396ede1c76
13 changed files with 153 additions and 44 deletions

View file

@ -1,5 +1,6 @@
// This work derives from Vittorio Romeo's code used for cppcon 2015 licensed under the Academic Free License. // This work derives from Vittorio Romeo's code used for cppcon 2015 licensed
// under the Academic Free License.
// His code is available here: https://github.com/SuperV1234/cppcon2015 // His code is available here: https://github.com/SuperV1234/cppcon2015
@ -53,7 +54,8 @@ namespace EC
EC::Meta::forEach<Contents>([&bitset] (auto t) { EC::Meta::forEach<Contents>([&bitset] (auto t) {
if(EC::Meta::Contains<decltype(t), Combined>::value) if(EC::Meta::Contains<decltype(t), Combined>::value)
{ {
bitset[EC::Meta::IndexOf<decltype(t), Combined>::value] = true; bitset[EC::Meta::IndexOf<decltype(t), Combined>::value] =
true;
} }
}); });

View file

@ -1,5 +1,6 @@
// This work derives from Vittorio Romeo's code used for cppcon 2015 licensed under the Academic Free License. // This work derives from Vittorio Romeo's code used for cppcon 2015 licensed
// under the Academic Free License.
// His code is available here: https://github.com/SuperV1234/cppcon2015 // His code is available here: https://github.com/SuperV1234/cppcon2015

View file

@ -1,5 +1,6 @@
// This work derives from Vittorio Romeo's code used for cppcon 2015 licensed under the Academic Free License. // This work derives from Vittorio Romeo's code used for cppcon 2015 licensed
// under the Academic Free License.
// His code is available here: https://github.com/SuperV1234/cppcon2015 // His code is available here: https://github.com/SuperV1234/cppcon2015
@ -29,7 +30,8 @@ namespace EC
/*! /*!
\brief Manages an EntityComponent system. \brief Manages an EntityComponent system.
EC::Manager must be created with a list of all used Components and all used tags. EC::Manager must be created with a list of all used Components and all
used tags.
Note that all components must have a default constructor. Note that all components must have a default constructor.
@ -181,7 +183,8 @@ namespace EC
template <typename Component> template <typename Component>
Component& getEntityData(const std::size_t& index) Component& getEntityData(const std::size_t& index)
{ {
return std::get<std::vector<Component> >(componentsStorage).at(std::get<std::size_t>(entities.at(index))); return std::get<std::vector<Component> >(componentsStorage).at(
std::get<std::size_t>(entities.at(index)));
} }
/*! /*!
@ -202,6 +205,41 @@ namespace EC
return getEntityData<Component>(index); return getEntityData<Component>(index);
} }
/*!
\brief Returns a const reference to a component belonging to the
given Entity.
This function will return a const reference to a Component
regardless of whether or not the Entity actually owns the reference.
If the Entity doesn't own the Component, changes to the Component
will not affect any Entity. It is recommended to use hasComponent()
to determine if the Entity actually owns that Component.
*/
template <typename Component>
const Component& getEntityData(const std::size_t& index) const
{
return std::get<std::vector<Component> >(componentsStorage).at(
std::get<std::size_t>(entities.at(index)));
}
/*!
\brief Returns a const reference to a component belonging to the
given Entity.
Note that this function is the same as getEntityData() (const).
This function will return a const reference to a Component
regardless of whether or not the Entity actually owns the reference.
If the Entity doesn't own the Component, changes to the Component
will not affect any Entity. It is recommended to use hasComponent()
to determine if the Entity actually owns that Component.
*/
template <typename Component>
const Component& getEntityComponent(const std::size_t& index) const
{
return getEntityData<Component>(index);
}
/*! /*!
\brief Checks whether or not the given Entity has the given \brief Checks whether or not the given Entity has the given
Component. Component.
@ -240,7 +278,8 @@ namespace EC
result in entity IDs changing if some Entities were marked for result in entity IDs changing if some Entities were marked for
deletion. deletion.
<b>This function should be called periodically to correctly handle deletion of entities.</b> <b>This function should be called periodically to correctly handle
deletion of entities.</b>
*/ */
void cleanup() void cleanup()
{ {
@ -478,7 +517,8 @@ namespace EC
} }
private: private:
std::unordered_map<std::size_t, std::function<void()> > forMatchingFunctions; std::unordered_map<std::size_t, std::function<void()> >
forMatchingFunctions;
std::size_t functionIndex = 0; std::size_t functionIndex = 0;
public: public:
@ -664,9 +704,11 @@ namespace EC
std::size_t keepSomeMatchingFunctions(List list) std::size_t keepSomeMatchingFunctions(List list)
{ {
std::size_t deletedCount = 0; std::size_t deletedCount = 0;
for(auto iter = forMatchingFunctions.begin(); iter != forMatchingFunctions.end();) for(auto iter = forMatchingFunctions.begin();
iter != forMatchingFunctions.end();)
{ {
if(std::find(list.begin(), list.end(), iter->first) == list.end()) if(std::find(list.begin(), list.end(), iter->first)
== list.end())
{ {
iter = forMatchingFunctions.erase(iter); iter = forMatchingFunctions.erase(iter);
++deletedCount; ++deletedCount;

View file

@ -1,5 +1,6 @@
// This work derives from Vittorio Romeo's code used for cppcon 2015 licensed under the Academic Free License. // This work derives from Vittorio Romeo's code used for cppcon 2015 licensed
// under the Academic Free License.
// His code is available here: https://github.com/SuperV1234/cppcon2015 // His code is available here: https://github.com/SuperV1234/cppcon2015
@ -18,7 +19,10 @@ namespace EC
using type = TypeList<>; using type = TypeList<>;
}; };
template <template <typename...> class TTypeListA, template <typename...> class TTypeListB, typename... TypesA, typename... TypesB> template <
template <typename...> class TTypeListA,
template <typename...> class TTypeListB,
typename... TypesA, typename... TypesB>
struct CombineHelper<TTypeListA<TypesA...>, TTypeListB<TypesB...> > struct CombineHelper<TTypeListA<TypesA...>, TTypeListB<TypesB...> >
{ {
using type = TypeList<TypesA..., TypesB...>; using type = TypeList<TypesA..., TypesB...>;

View file

@ -1,5 +1,6 @@
// This work derives from Vittorio Romeo's code used for cppcon 2015 licensed under the Academic Free License. // This work derives from Vittorio Romeo's code used for cppcon 2015 licensed
// under the Academic Free License.
// His code is available here: https://github.com/SuperV1234/cppcon2015 // His code is available here: https://github.com/SuperV1234/cppcon2015
@ -19,7 +20,11 @@ namespace EC
{ {
}; };
template <typename T, template <typename...> class TTypeList, typename Type, typename... Types> template <
typename T,
template <typename...> class TTypeList,
typename Type,
typename... Types>
struct ContainsHelper<T, TTypeList<Type, Types...> > : struct ContainsHelper<T, TTypeList<Type, Types...> > :
std::conditional< std::conditional<
std::is_same<T, Type>::value, std::is_same<T, Type>::value,
@ -30,7 +35,8 @@ namespace EC
}; };
template <typename T, typename TTypeList> template <typename T, typename TTypeList>
using Contains = std::integral_constant<bool, ContainsHelper<T, TTypeList>::value>; using Contains = std::integral_constant<
bool, ContainsHelper<T, TTypeList>::value>;
} }
} }

View file

@ -1,5 +1,6 @@
// This work derives from Vittorio Romeo's code used for cppcon 2015 licensed under the Academic Free License. // This work derives from Vittorio Romeo's code used for cppcon 2015 licensed
// under the Academic Free License.
// His code is available here: https://github.com/SuperV1234/cppcon2015 // His code is available here: https://github.com/SuperV1234/cppcon2015
@ -19,7 +20,11 @@ namespace EC
{ {
}; };
template <template <typename...> class TTypeListA, typename Type, typename... Types, typename TTypeListB> template <
template <typename...> class TTypeListA,
typename Type,
typename... Types,
typename TTypeListB>
struct ContainsAllHelper<TTypeListA<Type, Types...>, TTypeListB> : struct ContainsAllHelper<TTypeListA<Type, Types...>, TTypeListB> :
std::conditional< std::conditional<
Contains<Type, TTypeListB>::value, Contains<Type, TTypeListB>::value,
@ -30,7 +35,8 @@ namespace EC
}; };
template <typename TTypeListA, typename TTypeListB> template <typename TTypeListA, typename TTypeListB>
using ContainsAll = std::integral_constant<bool, ContainsAllHelper<TTypeListA, TTypeListB>::value>; using ContainsAll = std::integral_constant<
bool, ContainsAllHelper<TTypeListA, TTypeListB>::value>;
} }
} }

View file

@ -1,5 +1,6 @@
// This work derives from Vittorio Romeo's code used for cppcon 2015 licensed under the Academic Free License. // This work derives from Vittorio Romeo's code used for cppcon 2015 licensed
// under the Academic Free License.
// His code is available here: https://github.com/SuperV1234/cppcon2015 // His code is available here: https://github.com/SuperV1234/cppcon2015
@ -15,9 +16,11 @@ namespace EC
namespace Meta namespace Meta
{ {
template <typename Function, typename TTuple, std::size_t... Indices> template <typename Function, typename TTuple, std::size_t... Indices>
constexpr void forEachHelper(Function&& function, TTuple tuple, std::index_sequence<Indices...>) constexpr void forEachHelper(
Function&& function, TTuple tuple, std::index_sequence<Indices...>)
{ {
return (void)std::initializer_list<int>{(function(std::move(std::get<Indices>(tuple))), 0)...}; return (void)std::initializer_list<int>{(function(std::move(
std::get<Indices>(tuple))), 0)...};
} }
template <typename TTypeList, typename Function> template <typename TTypeList, typename Function>
@ -27,7 +30,8 @@ namespace EC
using TTupleSize = std::tuple_size<TTuple>; using TTupleSize = std::tuple_size<TTuple>;
using IndexSeq = std::make_index_sequence<TTupleSize::value>; using IndexSeq = std::make_index_sequence<TTupleSize::value>;
return forEachHelper(std::forward<Function>(function), TTuple{}, IndexSeq{}); return forEachHelper(
std::forward<Function>(function), TTuple{}, IndexSeq{});
} }
} }
} }

View file

@ -1,5 +1,6 @@
// This work derives from Vittorio Romeo's code used for cppcon 2015 licensed under the Academic Free License. // This work derives from Vittorio Romeo's code used for cppcon 2015 licensed
// under the Academic Free License.
// His code is available here: https://github.com/SuperV1234/cppcon2015 // His code is available here: https://github.com/SuperV1234/cppcon2015
@ -17,13 +18,20 @@ namespace EC
{ {
}; };
template <typename T, template <typename...> class TTypeList, typename... Types> template <
typename T,
template <typename...> class TTypeList,
typename... Types>
struct IndexOf<T, TTypeList<T, Types...> > : struct IndexOf<T, TTypeList<T, Types...> > :
std::integral_constant<std::size_t, 0> std::integral_constant<std::size_t, 0>
{ {
}; };
template <typename T, template <typename...> class TTypeList, typename Type, typename... Types> template <
typename T,
template <typename...> class TTypeList,
typename Type,
typename... Types>
struct IndexOf<T, TTypeList<Type, Types...> > : struct IndexOf<T, TTypeList<Type, Types...> > :
std::integral_constant<std::size_t, 1 + std::integral_constant<std::size_t, 1 +
IndexOf<T, TTypeList<Types...> >::value IndexOf<T, TTypeList<Types...> >::value

View file

@ -1,5 +1,6 @@
// This work derives from Vittorio Romeo's code used for cppcon 2015 licensed under the Academic Free License. // This work derives from Vittorio Romeo's code used for cppcon 2015 licensed
// under the Academic Free License.
// His code is available here: https://github.com/SuperV1234/cppcon2015 // His code is available here: https://github.com/SuperV1234/cppcon2015
@ -19,32 +20,54 @@ namespace EC
using type = TypeList<>; using type = TypeList<>;
}; };
template <typename TTypeListA, typename TTypeListB, typename... Matching> template <typename TTypeListA, typename TTypeListB,
typename... Matching>
struct MatchingHelperHelper struct MatchingHelperHelper
{ {
}; };
template <typename TTypeListA, typename TTypeListB, typename... Matching> template <typename TTypeListA, typename TTypeListB,
struct MatchingHelperHelper<TTypeListA, TTypeListB, TypeList<Matching...> > typename... Matching>
struct MatchingHelperHelper<
TTypeListA, TTypeListB, TypeList<Matching...> >
{ {
using type = TypeList<Matching...>; using type = TypeList<Matching...>;
}; };
template <template <typename...> class TTypeListA, typename TTypeListB, typename Type, typename... Types, typename... Matching> template <
struct MatchingHelperHelper<TTypeListA<Type, Types...>, TTypeListB, TypeList<Matching...> > : template <typename...> class TTypeListA,
typename TTypeListB,
typename Type,
typename... Types,
typename... Matching>
struct MatchingHelperHelper<
TTypeListA<Type, Types...>,
TTypeListB,
TypeList<Matching...> > :
std::conditional< std::conditional<
Contains<Type, TTypeListB>::value, Contains<Type, TTypeListB>::value,
MatchingHelperHelper<TTypeListA<Types...>, TTypeListB, TypeList<Matching..., Type> >, MatchingHelperHelper<
MatchingHelperHelper<TTypeListA<Types...>, TTypeListB, TypeList<Matching...> > TTypeListA<Types...>,
TTypeListB,
TypeList<Matching..., Type> >,
MatchingHelperHelper<
TTypeListA<Types...>,
TTypeListB,
TypeList<Matching...> >
>::type >::type
{ {
}; };
template <template <typename...> class TTypeListA, typename TTypeListB, typename Type, typename... Types> template <
template <typename...> class TTypeListA,
typename TTypeListB,
typename Type,
typename... Types>
struct MatchingHelper<TTypeListA<Type, Types...>, TTypeListB> : struct MatchingHelper<TTypeListA<Type, Types...>, TTypeListB> :
std::conditional< std::conditional<
Contains<Type, TTypeListB>::value, Contains<Type, TTypeListB>::value,
MatchingHelperHelper<TTypeListA<Types...>, TTypeListB, TypeList<Type> >, MatchingHelperHelper<
TTypeListA<Types...>, TTypeListB, TypeList<Type> >,
MatchingHelper<TTypeListA<Types...>, TTypeListB> MatchingHelper<TTypeListA<Types...>, TTypeListB>
>::type >::type
{ {

View file

@ -1,5 +1,6 @@
// This work derives from Vittorio Romeo's code used for cppcon 2015 licensed under the Academic Free License. // This work derives from Vittorio Romeo's code used for cppcon 2015 licensed
// under the Academic Free License.
// His code is available here: https://github.com/SuperV1234/cppcon2015 // His code is available here: https://github.com/SuperV1234/cppcon2015

View file

@ -1,5 +1,6 @@
// This work derives from Vittorio Romeo's code used for cppcon 2015 licensed under the Academic Free License. // This work derives from Vittorio Romeo's code used for cppcon 2015 licensed
// under the Academic Free License.
// His code is available here: https://github.com/SuperV1234/cppcon2015 // His code is available here: https://github.com/SuperV1234/cppcon2015
@ -17,7 +18,10 @@ namespace EC
{ {
}; };
template <template <typename...> class TypeA, template <typename...> class TypeB, typename... TypesA, typename... TypesB> template <
template <typename...> class TypeA,
template <typename...> class TypeB,
typename... TypesA, typename... TypesB>
struct MorphHelper<TypeA<TypesA...>, TypeB<TypesB...> > struct MorphHelper<TypeA<TypesA...>, TypeB<TypesB...> >
{ {
using type = TypeB<TypesA...>; using type = TypeB<TypesA...>;

View file

@ -1,5 +1,6 @@
// This work derives from Vittorio Romeo's code used for cppcon 2015 licensed under the Academic Free License. // This work derives from Vittorio Romeo's code used for cppcon 2015 licensed
// under the Academic Free License.
// His code is available here: https://github.com/SuperV1234/cppcon2015 // His code is available here: https://github.com/SuperV1234/cppcon2015
@ -14,7 +15,6 @@ namespace EC
struct TypeList struct TypeList
{ {
static constexpr std::size_t size{sizeof...(Types)}; static constexpr std::size_t size{sizeof...(Types)};
}; };
} }
} }

View file

@ -1,5 +1,6 @@
// This work derives from Vittorio Romeo's code used for cppcon 2015 licensed under the Academic Free License. // This work derives from Vittorio Romeo's code used for cppcon 2015 licensed
// under the Academic Free License.
// His code is available here: https://github.com/SuperV1234/cppcon2015 // His code is available here: https://github.com/SuperV1234/cppcon2015
@ -21,19 +22,26 @@ namespace EC
using type = TTypeList; using type = TTypeList;
}; };
template <typename TTypeList, template <typename...> class TTTypeList, unsigned int Index, typename Type, typename... Rest> template <
typename TTypeList,
template <typename...> class TTTypeList,
unsigned int Index,
typename Type,
typename... Rest>
struct TypeListGetHelper<TTypeList, TTTypeList<Type, Rest...>, Index> struct TypeListGetHelper<TTypeList, TTTypeList<Type, Rest...>, Index>
{ {
using type = using type =
typename std::conditional< typename std::conditional<
Index == EC::Meta::IndexOf<Type, TTypeList>::value, Index == EC::Meta::IndexOf<Type, TTypeList>::value,
Type, Type,
typename TypeListGetHelper<TTypeList, TTTypeList<Rest...>, Index>::type typename TypeListGetHelper<
TTypeList, TTTypeList<Rest...>, Index>::type
>::type; >::type;
}; };
template <typename TTypeList, unsigned int Index> template <typename TTypeList, unsigned int Index>
using TypeListGet = typename TypeListGetHelper<TTypeList, TTypeList, Index>::type; using TypeListGet =
typename TypeListGetHelper<TTypeList, TTypeList, Index>::type;
} }
} }