-
Notifications
You must be signed in to change notification settings - Fork 1
/
custom_struct.cpp
93 lines (79 loc) · 2.19 KB
/
custom_struct.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
#include <runtypes/runtypes.hpp>
#include <iostream>
#define INDENT_STR std::string(indent, ' ')
#define INDENT(VALUE) indent = VALUE;
#define LOG(VALUE) std::cout << INDENT_STR << "==== " VALUE " ====" << std::endl;
int indent;
struct A
{
A(float value) : value_(value), c_(++c)
{
std::cout << INDENT_STR << "+ Build: " << c_ << "º, address: " << reinterpret_cast<uintptr_t>(this) << std::endl;
};
A(const A& other) : value_(other.value_), c_(++c)
{
std::cout << INDENT_STR << "+ Copy: " << c_ << "º, address: " << reinterpret_cast<uintptr_t>(this) << std::endl;
};
~A()
{
std::cout << INDENT_STR << "- Destroy: " << c_ << "º, address: " << reinterpret_cast<uintptr_t>(this) << std::endl;
};
static int c;
float value_;
int c_;
};
int A::c = 0;
int main()
{
std::cout << "###### create and copy method ######" << std::endl;
{
INDENT(0);
LOG("Struct definition");
rt::Struct t1("my_type_1");
t1.add_member("a", A(8.7f)); //copy way
{
INDENT(4);
LOG("Data instantiation");
rt::Data d1(t1);
LOG("Set");
d1["a"].set(A(9.5f));
{
INDENT(8);
LOG("Data copy");
rt::Data d2(d1);
LOG("End data copy");
}
INDENT(4);
LOG("End data instantation");
}
INDENT(0);
LOG("End struct definition");
}
std::cout << std::endl;
A::c = 0;
std::cout << "###### emplace method ######" << std::endl;
{
INDENT(0);
LOG("Struct definition");
rt::Struct t1("my_type_1");
t1.add_member<A>("a", 8.6f); //emplace way
{
INDENT(4);
LOG("Data instantiation");
rt::Data d1(t1);
LOG("Set");
d1["a"].set(A(9.5f));
{
INDENT(8);
LOG("Data copy");
rt::Data d2(d1);
LOG("End data copy");
}
INDENT(4);
LOG("End data instantation");
}
INDENT(0);
LOG("End struct definition");
}
return 0;
}