/
main_Factory.cpp
109 lines (94 loc) · 2.75 KB
/
main_Factory.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//! ========================================================================================
//! ExecutionGraph
//! Copyright (C) 2014 by Gabriel Nützi <gnuetzi (at) gmail (døt) com>
//!
//! @date Tue Jan 16 2018
//! @author Gabriel Nützi, gnuetzi (at) gmail (døt) com
//!
//! This Source Code Form is subject to the terms of the Mozilla Public
//! License, v. 2.0. If a copy of the MPL was not distributed with this
//! file, You can obtain one at http://mozilla.org/MPL/2.0/.
//! ========================================================================================
#include <iostream>
#include <memory>
#include <rttr/registration>
#include <rttr/type>
#include "TestFunctions.hpp"
#include "executionGraph/common/Factory.hpp"
using namespace executionGraph;
#ifdef __clang__
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wweak-vtables"
#endif
struct MyMessage2 final
{
RTTR_ENABLE()
public:
int a = 3;
void fromJSON();
void toJSON();
};
RTTR_REGISTRATION
{
using namespace rttr;
registration::class_<MyMessage2>("MyMessage2")
.constructor<>()(
policy::ctor::as_std_shared_ptr);
;
}
struct FunnyTable
{
FunnyTable(std::string a)
: a(a)
{
std::cout << "created FunnyTable " << a << std::endl;
}
std::string a;
};
struct CreatorA
{
using Key = int;
static FunnyTable create() { return {"int"}; }
};
struct CreatorB
{
using Key = double;
static FunnyTable create() { return {"double"}; }
};
struct CreatorC
{
using Key = float;
static FunnyTable create() { return {"float"}; }
};
template<typename T, typename K>
using comp = std::is_same<typename T::Key, K>;
MY_TEST(FactoryTest, StaticFactory)
{
using CreatorList = meta::list<CreatorA, CreatorB, CreatorC>;
using MySuperFactory = StaticFactory<CreatorList>;
// create at compile time
auto a = MySuperFactory::create<double>();
auto b = MySuperFactory::create<int>();
auto c = MySuperFactory::create<float>();
ASSERT_EQ(a.a, "double");
ASSERT_EQ(b.a, "int");
ASSERT_EQ(c.a, "float");
// Create if you have a rtti
auto d = MySuperFactory::create(rttr::type::get<double>());
auto e = MySuperFactory::create(rttr::type::get<int>());
auto f = MySuperFactory::create(rttr::type::get<float>());
ASSERT_EQ(d.a, "double");
ASSERT_EQ(e.a, "int");
ASSERT_EQ(f.a, "float");
// Messages
using varT = std::shared_ptr<MyMessage2>;
rttr::variant instance = rttr::type::get_by_name("MyMessage2").create();
ASSERT_TRUE(instance.is_type<varT>());
varT r = instance.get_value<varT>();
ASSERT_TRUE(r->a == 3);
}
int main(int argc, char** argv)
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}