Added EC/Bitset.hpp and EC/Meta/ContainsAll.hpp

Also added EC/EC.hpp and EC/Meta/Meta.hpp that loads all
headers in that directory.
This commit is contained in:
Stephen Seo 2016-03-04 21:12:37 +09:00
parent c318121dc8
commit 12bc0d9f8f
8 changed files with 109 additions and 5 deletions

View file

@ -4,7 +4,11 @@ project(EntityComponentSystem)
set(EntityComponentSystem_HEADERS
EC/Meta/TypeList.hpp
EC/Meta/Contains.hpp
EC/Meta/IndexOf.hpp)
EC/Meta/ContainsAll.hpp
EC/Meta/IndexOf.hpp
EC/Meta/Meta.hpp
EC/Bitset.hpp
EC/EC.hpp)
add_library(EntityComponentSystem INTERFACE)
target_include_directories(EntityComponentSystem INTERFACE ${CMAKE_SOURCE_DIR})

29
src/EC/Bitset.hpp Normal file
View file

@ -0,0 +1,29 @@
#ifndef EC_BITSET_HPP
#define EC_BITSET_HPP
#include <bitset>
#include "Meta/TypeList.hpp"
namespace EC
{
template <typename ComponentsList, typename TagsList>
struct Bitset :
public std::bitset<ComponentsList::size + TagsList::size>
{
template <typename Component>
constexpr auto getComponentBit()
{
return (*this)[EC::Meta::IndexOf<Component, ComponentsList>::value];
}
template <typename Tag>
constexpr auto getTagBit()
{
return (*this)[ComponentsList::size + EC::Meta::IndexOf<Tag, TagsList>::value];
}
};
}
#endif

3
src/EC/EC.hpp Normal file
View file

@ -0,0 +1,3 @@
#include "Bitset.hpp"

View file

@ -2,6 +2,7 @@
#ifndef EC_META_CONTAINS_HPP
#define EC_META_CONTAINS_HPP
#include <type_traits>
#include "TypeList.hpp"
namespace EC
@ -25,7 +26,7 @@ namespace EC
};
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

@ -0,0 +1,34 @@
#ifndef EC_META_CONTAINS_ALL_HPP
#define EC_META_CONTAINS_ALL_HPP
#include "TypeList.hpp"
#include "Contains.hpp"
namespace EC
{
namespace Meta
{
template <typename TTypeListA, typename TTypeListB>
struct ContainsAllHelper :
std::true_type
{
};
template <typename Type, typename... Types, typename TTypeListB>
struct ContainsAllHelper<TypeList<Type, Types...>, TTypeListB> :
std::conditional<
Contains<Type, TTypeListB>::value,
ContainsAllHelper<TypeList<Types...>, TTypeListB>,
std::false_type
>::type
{
};
template <typename TTypeListA, typename TTypeListB>
using ContainsAll = std::integral_constant<bool, ContainsAllHelper<TTypeListA, TTypeListB>::value>;
}
}
#endif

View file

@ -1,5 +1,6 @@
#include "TypeList.hpp"
#include "Contains.hpp"
#include "ContainsAll.hpp"
#include "IndexOf.hpp"

View file

@ -2,8 +2,6 @@
#ifndef EC_META_TYPE_LIST_HPP
#define EC_META_TYPE_LIST_HPP
#include <type_traits>
namespace EC
{
namespace Meta

View file

@ -2,18 +2,23 @@
#include <gtest/gtest.h>
#include <EC/Meta/Meta.hpp>
#include <EC/EC.hpp>
struct C0 {};
struct C1 {};
struct C2 {};
struct C3 {};
struct T0 {};
struct T1 {};
using listAll = EC::Meta::TypeList<C0, C1, C2, C3>;
using listSome = EC::Meta::TypeList<C1, C3>;
using listTagsAll = EC::Meta::TypeList<T0, T1>;
TEST(Meta, Contains)
{
int size = listAll::size;
EXPECT_EQ(size, 4);
@ -39,6 +44,18 @@ TEST(Meta, Contains)
EXPECT_TRUE(result);
}
TEST(Meta, ContainsAll)
{
bool contains = EC::Meta::ContainsAll<listSome, listAll>::value;
EXPECT_TRUE(contains);
contains = EC::Meta::ContainsAll<listAll, listSome>::value;
EXPECT_FALSE(contains);
contains = EC::Meta::ContainsAll<listAll, listAll>::value;
EXPECT_TRUE(contains);
}
TEST(Meta, IndexOf)
{
int index = EC::Meta::IndexOf<C0, listAll>::value;
@ -56,3 +73,20 @@ TEST(Meta, IndexOf)
EXPECT_EQ(index, 1);
}
TEST(Meta, Bitset)
{
EC::Bitset<listAll, listTagsAll> bitset;
EXPECT_EQ(bitset.size(), listAll::size + listTagsAll::size);
bitset[1] = true;
EXPECT_TRUE(bitset.getComponentBit<C1>());
bitset.flip();
EXPECT_FALSE(bitset.getComponentBit<C1>());
bitset.reset();
bitset[4] = true;
EXPECT_TRUE(bitset.getTagBit<T0>());
bitset.flip();
EXPECT_FALSE(bitset.getTagBit<T0>());
}