Basic meta stuff working

This commit is contained in:
Stephen Seo 2016-02-25 12:08:02 +09:00
parent 0b39f8bc93
commit 8ffacb16b1
4 changed files with 106 additions and 6 deletions

View file

@ -1,11 +1,11 @@
cmake_minimum_required(VERSION 2.6) cmake_minimum_required(VERSION 3.0)
project(EntityComponentSystem) project(EntityComponentSystem)
set(EntityComponentSystem_SOURCES set(EntityComponentSystem_HEADERS
) EC/Meta.hpp)
add_library(EntityComponentSystem add_library(EntityComponentSystem INTERFACE)
${EntityComponentSystem_SOURCES}) target_include_directories(EntityComponentSystem INTERFACE ${CMAKE_SOURCE_DIR})
include_directories(${CMAKE_SOURCE_DIR}) include_directories(${CMAKE_SOURCE_DIR})
@ -34,6 +34,19 @@ if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
endif() endif()
install(TARGETS EntityComponentSystem DESTINATION lib) install(FILES ${EntityComponentSystem_HEADERS} DESTINATION include/EC)
find_package(GTest)
if(GTEST_FOUND)
set(UnitTests_SOURCES
test/MetaTest.cpp
test/Main.cpp)
add_executable(UnitTests ${UnitTests_SOURCES})
target_link_libraries(UnitTests EntityComponentSystem ${GTEST_LIBRARIES})
enable_testing()
add_test(NAME UnitTests COMMAND UnitTests)
endif()

40
src/EC/Meta.hpp Normal file
View file

@ -0,0 +1,40 @@
#ifndef EC_META_HPP
#define EC_META_HPP
#include <type_traits>
namespace EC
{
namespace Meta
{
template <typename... Types>
struct TypeList
{
static constexpr std::size_t size{sizeof...(Types)};
};
template <typename T, typename... Types>
struct ContainsHelper :
std::false_type
{
};
template <typename T, typename Type, typename... Types>
struct ContainsHelper<T, TypeList<Type, Types...> > :
std::conditional<
std::is_same<T, Type>::value,
std::true_type,
ContainsHelper<T, TypeList<Types...> >
>::type
{
};
template <typename T, typename TTypeList>
using Contains = std::integral_constant<bool, ContainsHelper<T, TTypeList>::value >;
}
}
#endif

8
src/test/Main.cpp Normal file
View file

@ -0,0 +1,8 @@
#include <gtest/gtest.h>
int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

39
src/test/MetaTest.cpp Normal file
View file

@ -0,0 +1,39 @@
#include <gtest/gtest.h>
#include <EC/Meta.hpp>
TEST(Meta, Contains)
{
struct C0 {};
struct C1 {};
struct C2 {};
struct C3 {};
using listAll = EC::Meta::TypeList<C0, C1, C2, C3>;
int size = listAll::size;
EXPECT_EQ(size, 4);
bool result = EC::Meta::Contains<C0, listAll>::value;
EXPECT_TRUE(result);
result = EC::Meta::Contains<C1, listAll>::value;
EXPECT_TRUE(result);
result = EC::Meta::Contains<C2, listAll>::value;
EXPECT_TRUE(result);
result = EC::Meta::Contains<C3, listAll>::value;
EXPECT_TRUE(result);
using listSome = EC::Meta::TypeList<C1, C3>;
size = listSome::size;
EXPECT_EQ(size, 2);
result = EC::Meta::Contains<C0, listSome>::value;
EXPECT_FALSE(result);
result = EC::Meta::Contains<C1, listSome>::value;
EXPECT_TRUE(result);
result = EC::Meta::Contains<C2, listSome>::value;
EXPECT_FALSE(result);
result = EC::Meta::Contains<C3, listSome>::value;
EXPECT_TRUE(result);
}