163 lines
2.7 KiB
C++
Executable File
163 lines
2.7 KiB
C++
Executable File
// License: Modified MIT (NON-AI)
|
|
// See the LICENSE file in the root directory for license information.
|
|
// Copyright 2025 Timothy Prepscius
|
|
|
|
#include "RTTI.hpp"
|
|
#include <tjp/core/testing/catch.hpp>
|
|
#include <tjp/core/algorithm/unused.hpp>
|
|
|
|
#include <tjp/core/log/Log.h>
|
|
#include <tjp/core/log/LogOf.h>
|
|
|
|
namespace tjp::core::rtti {
|
|
namespace {
|
|
|
|
struct A {
|
|
ENABLE_RTTI_BASE;
|
|
|
|
} ;
|
|
|
|
struct B : A {
|
|
ENABLE_RTTI(A);
|
|
} ;
|
|
|
|
struct C_different_base {
|
|
ENABLE_RTTI_BASE;
|
|
} ;
|
|
|
|
//struct D_no_inherit_shouldn_t_compile {
|
|
// ENABLE_RTTI(A);
|
|
//} ;
|
|
|
|
struct E : A {
|
|
ENABLE_RTTI(A);
|
|
} ;
|
|
|
|
struct B_A : B {
|
|
ENABLE_RTTI(B);
|
|
} ;
|
|
|
|
struct X0 {};
|
|
struct X1 {};
|
|
|
|
SCENARIO("core::rtti")
|
|
{
|
|
GIVEN("some structures")
|
|
{
|
|
A a_;
|
|
B b_;
|
|
C_different_base c_;
|
|
// D_no_inherit d_;
|
|
B_A b_a_;
|
|
|
|
auto *a = &a_;
|
|
unused(a);
|
|
|
|
auto *b = &b_;
|
|
unused(a);
|
|
|
|
auto *c = &c_;
|
|
unused(c);
|
|
|
|
// auto *d = &d_;
|
|
|
|
auto *b_a = &b_a_;
|
|
|
|
static_assert(has_rtti_method<int>::value == false);
|
|
static_assert(has_rtti_method<A>::value == true);
|
|
|
|
sLogTest("testing",
|
|
logVar(type_id<A>().v)
|
|
<< logVar(type_id<B>().v)
|
|
<< logVar(type_id<C_different_base>().v)
|
|
// << logVar(type_id<D>().v)
|
|
<< logVar(type_id<E>().v)
|
|
<< logVar(type_id<B_A>().v)
|
|
);
|
|
|
|
sLogTest("testing",
|
|
logVar(has_rtti_method<int>::value)
|
|
<< logVar(has_rtti_method<A>::value)
|
|
<< logVar(has_rtti_method<B>::value)
|
|
<< logVar(has_rtti_method<C_different_base>::value)
|
|
// << logVar(has_rtti_method<D>::value)
|
|
<< logVar(has_rtti_method<E>::value)
|
|
<< logVar(has_rtti_method<B_A>::value)
|
|
);
|
|
|
|
{
|
|
auto cast_a = dynamic_cast_ptr<A>(b);
|
|
auto cast_b = dynamic_cast_ptr<B>(cast_a);
|
|
REQUIRE(cast_b == b);
|
|
}
|
|
|
|
{
|
|
auto cast_a = dynamic_cast_ptr<A>(b);
|
|
auto cast_e = dynamic_cast_ptr<E>(cast_a);
|
|
REQUIRE(cast_e == nullptr);
|
|
}
|
|
|
|
{
|
|
auto cast_a = dynamic_cast_ptr<A>(b_a);
|
|
auto cast_b = dynamic_cast_ptr<B>(cast_a);
|
|
REQUIRE(cast_b == b_a);
|
|
auto cast_b_a = dynamic_cast_ptr<B_A>(cast_a);
|
|
REQUIRE(cast_b_a == b_a);
|
|
|
|
auto cast_b_a_2 = dynamic_cast_ptr<B_A>(cast_b);
|
|
REQUIRE(cast_b_a_2 == b_a);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
GIVEN("some datatypes")
|
|
{
|
|
int i_;
|
|
char c_;
|
|
float f_;
|
|
|
|
X0 x0_;
|
|
X1 x1_;
|
|
|
|
auto *i = &i_;
|
|
auto *c = &c_;
|
|
unused(c);
|
|
|
|
auto *f = &f_;
|
|
|
|
auto *x0 = &x0_;
|
|
auto *x1 = &x1_;
|
|
unused(x1);
|
|
|
|
{
|
|
auto x = dynamic_cast_ptr<char>(i);
|
|
REQUIRE(x == nullptr);
|
|
|
|
auto y = dynamic_cast_ptr<int>(i);
|
|
REQUIRE(y == i);
|
|
}
|
|
|
|
{
|
|
auto x = dynamic_cast_ptr<char>(f);
|
|
REQUIRE(x == nullptr);
|
|
|
|
auto y = dynamic_cast_ptr<float>(f);
|
|
REQUIRE(y == f);
|
|
}
|
|
|
|
{
|
|
auto x = dynamic_cast_ptr<X1>(x0);
|
|
REQUIRE(x == nullptr);
|
|
|
|
auto y = dynamic_cast_ptr<X0>(x0);
|
|
REQUIRE(y == x0);
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace
|