Add tests
This commit is contained in:
@@ -0,0 +1,8 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.5)
|
||||||
|
|
||||||
|
project(FastRTTI VERSION 1.0.0)
|
||||||
|
|
||||||
|
file(GLOB_RECURSE SRC "*.cpp" "*.h")
|
||||||
|
|
||||||
|
add_subdirectory(RTTI)
|
||||||
|
add_subdirectory(Tests)
|
||||||
@@ -1,2 +1,13 @@
|
|||||||
# Fast-RTTI
|
# Fast-RTTI
|
||||||
This library implements fast RTTI for C++.
|
This library implements fast RTTI for C++.
|
||||||
|
|
||||||
|
Tests:
|
||||||
|
[Direct type casting]
|
||||||
|
dynami_cast: 202ms
|
||||||
|
RTTI::dyn_cast: 54ms
|
||||||
|
[Possible type casting]
|
||||||
|
dynami_cast: 193ms
|
||||||
|
RTTI::dyn_cast: 41ms
|
||||||
|
[Not possible type casting]
|
||||||
|
dynami_cast: 489ms
|
||||||
|
RTTI::dyn_cast: 43ms
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
file(GLOB SRC "*.cpp" "*.h")
|
||||||
|
|
||||||
|
add_library(${CMAKE_PROJECT_NAME} STATIC ${SRC})
|
||||||
|
|
||||||
|
if(CMAKE_BUILD_TYPE STREQUAL "Release")
|
||||||
|
message(STATUS "Release mode: enabling optimizations")
|
||||||
|
|
||||||
|
if(MSVC)
|
||||||
|
# /O2 - высокая оптимизация
|
||||||
|
# /GL - link-time optimization
|
||||||
|
# /DNDEBUG - отключить assert()
|
||||||
|
# /GR- - отключить RTTI
|
||||||
|
target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE /O2 /GL /DNDEBUG /GR-)
|
||||||
|
target_link_options(${CMAKE_PROJECT_NAME} PRIVATE /LTCG)
|
||||||
|
else()
|
||||||
|
# GCC / Clang
|
||||||
|
target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE
|
||||||
|
-O3
|
||||||
|
-march=native
|
||||||
|
-flto
|
||||||
|
-fno-rtti
|
||||||
|
-DNDEBUG
|
||||||
|
)
|
||||||
|
target_link_options(${CMAKE_PROJECT_NAME} PRIVATE -flto)
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
+1
-1
@@ -13,5 +13,5 @@ template<class T> struct Meta;
|
|||||||
// Enum type classes
|
// Enum type classes
|
||||||
enum class Classes
|
enum class Classes
|
||||||
{
|
{
|
||||||
IRTTI
|
IRTTI, ClassA, ClassB, ClassC, ClassD, ClassE
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1,55 @@
|
|||||||
|
file(GLOB test_default_src "test_default_rtti.cpp" "ClassesDefault/*.cpp" "ClassesDefault/*.h")
|
||||||
|
|
||||||
|
add_executable(test_defalt_rtti ${test_default_src})
|
||||||
|
|
||||||
|
if(CMAKE_BUILD_TYPE STREQUAL "Release")
|
||||||
|
message(STATUS "Release mode: enabling optimizations")
|
||||||
|
|
||||||
|
if(MSVC)
|
||||||
|
# /O2 - высокая оптимизация
|
||||||
|
# /GL - link-time optimization
|
||||||
|
# /DNDEBUG - отключить assert()
|
||||||
|
target_compile_options(test_defalt_rtti PRIVATE /O2 /GL /DNDEBUG)
|
||||||
|
target_link_options(test_defalt_rtti PRIVATE /LTCG)
|
||||||
|
else()
|
||||||
|
# GCC / Clang
|
||||||
|
target_compile_options(test_defalt_rtti PRIVATE
|
||||||
|
-O3
|
||||||
|
-march=native
|
||||||
|
-flto
|
||||||
|
-DNDEBUG
|
||||||
|
)
|
||||||
|
target_link_options(Tests PRIVATE -flto)
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
file(GLOB test_fast_src "test_fast_rtti.cpp" "ClassesFast/*.cpp" "ClassesFast/*.h")
|
||||||
|
|
||||||
|
add_executable(test_fast_rtti ${test_fast_src})
|
||||||
|
target_link_libraries(test_fast_rtti PRIVATE FastRTTI)
|
||||||
|
target_include_directories(test_fast_rtti PRIVATE ${PROJECT_SOURCE_DIR}/RTTI)
|
||||||
|
|
||||||
|
if(CMAKE_BUILD_TYPE STREQUAL "Release")
|
||||||
|
message(STATUS "Release mode: enabling optimizations")
|
||||||
|
|
||||||
|
if(MSVC)
|
||||||
|
# /O2 - высокая оптимизация
|
||||||
|
# /GL - link-time optimization
|
||||||
|
# /DNDEBUG - отключить assert()
|
||||||
|
# /GR- - отключить RTTI
|
||||||
|
target_compile_options(test_fast_rtti PRIVATE /O2 /GL /DNDEBUG /GR-)
|
||||||
|
target_link_options(test_fast_rtti PRIVATE /LTCG)
|
||||||
|
else()
|
||||||
|
# GCC / Clang
|
||||||
|
target_compile_options(test_fast_rtti PRIVATE
|
||||||
|
-O3
|
||||||
|
-march=native
|
||||||
|
-flto
|
||||||
|
-fno-rtti
|
||||||
|
-DNDEBUG
|
||||||
|
)
|
||||||
|
target_link_options(test_fast_rtti PRIVATE -flto)
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
#include "ClassA.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int ClassA::foo()
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
class ClassA {
|
||||||
|
public:
|
||||||
|
virtual int foo();
|
||||||
|
};
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
#include "ClassB.h"
|
||||||
|
|
||||||
|
int ClassB::foo() {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "ClassA.h"
|
||||||
|
|
||||||
|
class ClassB : public ClassA{
|
||||||
|
public:
|
||||||
|
virtual int foo();
|
||||||
|
};
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
#include "ClassC.h"
|
||||||
|
|
||||||
|
int ClassC::foo()
|
||||||
|
{
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "ClassB.h"
|
||||||
|
|
||||||
|
class ClassC : public ClassB {
|
||||||
|
public:
|
||||||
|
virtual int foo();
|
||||||
|
};
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
#include "ClassD.h"
|
||||||
|
|
||||||
|
int ClassD::foo()
|
||||||
|
{
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "ClassC.h"
|
||||||
|
|
||||||
|
class ClassD : public ClassC {
|
||||||
|
public:
|
||||||
|
virtual int foo();
|
||||||
|
};
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
#include "ClassE.h"
|
||||||
|
|
||||||
|
int ClassE::foo()
|
||||||
|
{
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "ClassD.h"
|
||||||
|
|
||||||
|
class ClassE : public ClassD
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual int foo();
|
||||||
|
};
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
#include "ClassA.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int ClassA::foo()
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "RTTI.h"
|
||||||
|
|
||||||
|
GENERATE_META(ClassA)
|
||||||
|
class ClassA : public IRTTI {
|
||||||
|
public:
|
||||||
|
virtual int foo();
|
||||||
|
};
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
#include "ClassB.h"
|
||||||
|
|
||||||
|
int ClassB::foo() {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "ClassA.h"
|
||||||
|
|
||||||
|
GENERATE_META(ClassB)
|
||||||
|
class ClassB : public ClassA{
|
||||||
|
public:
|
||||||
|
virtual int foo();
|
||||||
|
};
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
#include "ClassC.h"
|
||||||
|
|
||||||
|
int ClassC::foo()
|
||||||
|
{
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "ClassB.h"
|
||||||
|
|
||||||
|
GENERATE_META(ClassC)
|
||||||
|
class ClassC : public ClassB {
|
||||||
|
public:
|
||||||
|
virtual int foo();
|
||||||
|
};
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
#include "ClassD.h"
|
||||||
|
|
||||||
|
int ClassD::foo()
|
||||||
|
{
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "ClassC.h"
|
||||||
|
|
||||||
|
GENERATE_META(ClassD)
|
||||||
|
class ClassD : public ClassC {
|
||||||
|
public:
|
||||||
|
virtual int foo();
|
||||||
|
};
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
#include "ClassE.h"
|
||||||
|
|
||||||
|
int ClassE::foo()
|
||||||
|
{
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "ClassD.h"
|
||||||
|
|
||||||
|
GENERATE_META(ClassE)
|
||||||
|
class ClassE : public ClassD
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual int foo();
|
||||||
|
};
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "ClassesDefault/ClassA.h"
|
||||||
|
#include "ClassesDefault/ClassB.h"
|
||||||
|
#include "ClassesDefault/ClassC.h"
|
||||||
|
#include "ClassesDefault/ClassD.h"
|
||||||
|
#include "ClassesDefault/ClassE.h"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ClassA* a = new ClassD();
|
||||||
|
|
||||||
|
{
|
||||||
|
std::cout << "[Direct type casting]\n";
|
||||||
|
|
||||||
|
ClassD* d1;
|
||||||
|
clock_t first, second;
|
||||||
|
clock_t realTime = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < 100; ++i)
|
||||||
|
{
|
||||||
|
first = clock();
|
||||||
|
for (unsigned long long i = 0; i < 10000000; ++i)
|
||||||
|
d1 = dynamic_cast<ClassD*>(a);
|
||||||
|
second = clock();
|
||||||
|
realTime += second - first;
|
||||||
|
}
|
||||||
|
std::cout << "dynami_cast: " << realTime / 100 << "ms\n";
|
||||||
|
realTime = 0;
|
||||||
|
|
||||||
|
if (d1 != nullptr)
|
||||||
|
d1->foo();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
{
|
||||||
|
std::cout << "[Possible type casting]\n";
|
||||||
|
|
||||||
|
ClassC* d1;
|
||||||
|
clock_t first, second;
|
||||||
|
clock_t realTime = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < 100; ++i)
|
||||||
|
{
|
||||||
|
first = clock();
|
||||||
|
for (unsigned long long i = 0; i < 10000000; ++i)
|
||||||
|
d1 = dynamic_cast<ClassC*>(a);
|
||||||
|
second = clock();
|
||||||
|
realTime += second - first;
|
||||||
|
}
|
||||||
|
std::cout << "dynami_cast: " << realTime / 100 << "ms\n";
|
||||||
|
realTime = 0;
|
||||||
|
|
||||||
|
if (d1 != nullptr)
|
||||||
|
d1->foo();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
{
|
||||||
|
std::cout << "[Not possible type casting]\n";
|
||||||
|
|
||||||
|
ClassE* d1;
|
||||||
|
clock_t first, second;
|
||||||
|
clock_t realTime = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < 100; ++i)
|
||||||
|
{
|
||||||
|
first = clock();
|
||||||
|
for (unsigned long long i = 0; i < 10000000; ++i)
|
||||||
|
d1 = dynamic_cast<ClassE*>(a);
|
||||||
|
second = clock();
|
||||||
|
realTime += second - first;
|
||||||
|
}
|
||||||
|
std::cout << "dynami_cast: " << realTime / 100 << "ms\n";
|
||||||
|
realTime = 0;
|
||||||
|
|
||||||
|
if (d1 != nullptr)
|
||||||
|
d1->foo();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "ClassesFast/ClassA.h"
|
||||||
|
#include "ClassesFast/ClassB.h"
|
||||||
|
#include "ClassesFast/ClassC.h"
|
||||||
|
#include "ClassesFast/ClassD.h"
|
||||||
|
#include "ClassesFast/ClassE.h"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ClassA* a = new ClassD();
|
||||||
|
|
||||||
|
{
|
||||||
|
std::cout << "[Direct type casting]\n";
|
||||||
|
|
||||||
|
ClassD* d1;
|
||||||
|
clock_t first, second;
|
||||||
|
clock_t realTime = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < 100; ++i)
|
||||||
|
{
|
||||||
|
first = clock();
|
||||||
|
for (unsigned long long i = 0; i < 10000000; ++i)
|
||||||
|
d1 = RTTI::dyn_cast<ClassD>(a);
|
||||||
|
second = clock();
|
||||||
|
realTime += second - first;
|
||||||
|
}
|
||||||
|
std::cout << "RTTI::dyn_cast: " << realTime / 100 << "ms\n";
|
||||||
|
|
||||||
|
if (d1 != nullptr)
|
||||||
|
d1->foo();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
{
|
||||||
|
std::cout << "[Possible type casting]\n";
|
||||||
|
|
||||||
|
ClassC* d1;
|
||||||
|
clock_t first, second;
|
||||||
|
clock_t realTime = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < 100; ++i)
|
||||||
|
{
|
||||||
|
first = clock();
|
||||||
|
for (unsigned long long i = 0; i < 10000000; ++i)
|
||||||
|
d1 = RTTI::dyn_cast<ClassC>(a);
|
||||||
|
second = clock();
|
||||||
|
realTime += second - first;
|
||||||
|
}
|
||||||
|
std::cout << "RTTI::dyn_cast: " << realTime / 100 << "ms\n";
|
||||||
|
|
||||||
|
if (d1 != nullptr)
|
||||||
|
d1->foo();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
{
|
||||||
|
std::cout << "[Not possible type casting]\n";
|
||||||
|
|
||||||
|
ClassE* d1;
|
||||||
|
clock_t first, second;
|
||||||
|
clock_t realTime = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < 100; ++i)
|
||||||
|
{
|
||||||
|
first = clock();
|
||||||
|
for (unsigned long long i = 0; i < 10000000; ++i)
|
||||||
|
d1 = RTTI::dyn_cast<ClassE>(a);
|
||||||
|
second = clock();
|
||||||
|
realTime += second - first;
|
||||||
|
}
|
||||||
|
std::cout << "RTTI::dyn_cast: " << realTime / 100 << "ms\n";
|
||||||
|
|
||||||
|
if (d1 != nullptr)
|
||||||
|
d1->foo();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user