-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathucast_example.cpp
58 lines (44 loc) · 1.67 KB
/
ucast_example.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*******************************************************************************
* This file is part of the "https://github.com/blackmatov/meta.hpp"
* For conditions of distribution and use, see copyright notice in LICENSE.md
* Copyright (C) 2021-2025, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#include <meta.hpp/meta_all.hpp>
#include <doctest/doctest.h>
#include <fmt/core.h>
namespace
{
// we should add META_HPP_ENABLE_POLY_INFO macro inside every class
// to add polymorphic information to our inheritance hierarchy
// after this we will be able to use the library's polymorphic functions
// like `ucast` or `resolve_type(T&&)`
struct A {
A() = default;
A(const A&) = default;
virtual ~A() = default;
META_HPP_ENABLE_POLY_INFO()
};
struct B {
B() = default;
B(const B&) = default;
virtual ~B() = default;
META_HPP_ENABLE_POLY_INFO()
};
struct C : A, B {
META_HPP_ENABLE_POLY_INFO(A, B)
};
}
TEST_CASE("meta/meta_examples/ucast") {
namespace meta = meta_hpp;
C c{};
A& a = c;
B& b = c;
// functions `ucast` and `resolve_type(T&&)` rely on polymorphic information
// are provided in META_HPP_ENABLE_POLY_INFO macro
// we can use polymorphic type resolving to get most derived class type
CHECK(meta::resolve_type(a) == meta::resolve_type<C>());
// and cast our pointers and references like dynamic_cast does
CHECK(&c == &meta::ucast<C&>(a));
// `ucast` supports cross casts, multiple and virtual inheritance as well
CHECK(&a == meta::ucast<A*>(&b));
}