-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplain_struct.cpp
More file actions
89 lines (76 loc) · 2.01 KB
/
Copy pathplain_struct.cpp
File metadata and controls
89 lines (76 loc) · 2.01 KB
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
// Sutter's "plain_struct" metaclass:
// - All fields public
// - No functions
// - No hierarchy
//
// In P0707 this would be:
// plain_struct Config { int width; int height; bool fullscreen; };
//
// Here, a plain_struct is a TypeTable with empty Functions and Hierarchy.
// The concept enforces: no behavior, no inheritance, just data.
#include <iostream>
#include <tuple>
#include <ctp/ctp.h>
using namespace ctp;
namespace Config
{
struct Width { int value; };
struct Height { int value; };
struct Fullscreen { bool value; };
using Type =
TypeTable<
Hierarchy<>,
Functions<>,
DataFields<Width, Height, Fullscreen>
>;
}
namespace Color
{
struct R { float value; };
struct G { float value; };
struct B { float value; };
struct A { float value; };
using Type =
TypeTable<
Hierarchy<>,
Functions<>,
DataFields<R, G, B, A>
>;
}
int main()
{
constexpr auto config =
Config::Type{
Config::Width{ 1920 },
Config::Height{ 1080 },
Config::Fullscreen{ true }
};
config.access(
[](const auto& _this)
{
std::cout
<< "Config: "
<< _this.template get<Config::Width>().value << "x"
<< _this.template get<Config::Height>().value
<< ((_this.template get<Config::Fullscreen>().value)
? " fullscreen" : " windowed")
<< "\n";
});
constexpr auto color =
Color::Type{
Color::R{ 0.2f },
Color::G{ 0.4f },
Color::B{ 0.8f },
Color::A{ 1.0f }
};
color.access(
[](const auto& _this)
{
std::cout
<< "Color: ("
<< _this.template get<Color::R>().value << ", "
<< _this.template get<Color::G>().value << ", "
<< _this.template get<Color::B>().value << ", "
<< _this.template get<Color::A>().value << ")\n";
});
}