generated from cpp-best-practices/gui_starter_template
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSimpleReflectionExample.cpp
More file actions
65 lines (53 loc) · 2.53 KB
/
SimpleReflectionExample.cpp
File metadata and controls
65 lines (53 loc) · 2.53 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
#include <iostream>
#include <IoSerialiserYaS.hpp>
// SI units -- include what you need
#include <units/isq/si/electric_current.h>
#include <units/isq/si/energy.h>
#include <units/isq/si/thermodynamic_temperature.h>
using namespace units::isq::si; // for short-hand notation
struct className {
int field1;
float field2;
std::string field3;
// just good common practise to define some operators
bool operator==(const className &) const = default;
};
ENABLE_REFLECTION_FOR(className, field1, field2, field3)
using opencmw::Annotated;
struct otherClass {
Annotated<float, thermodynamic_temperature<kelvin>, "device specific temperature"> temperature = 23.2F;
Annotated<float, electric_current<ampere>, "this is the current from ..."> current = 42.F;
Annotated<float, energy<electronvolt>, "SIS18 energy at injection before being captured"> injectionEnergy = 8.44e6F;
// [..]
// just good common practise to define some operators
bool operator==(const otherClass &) const = default;
};
ENABLE_REFLECTION_FOR(otherClass, temperature, current, injectionEnergy)
using namespace std::string_literals;
using namespace opencmw;
int main() {
className a{ 1, 0.5F, "Hello World!" };
className b{ 1, 0.501F, "Γειά σου Κόσμε!" };
std::cout << std::format("class info a: {}\n", a);
std::cout << ClassInfoVerbose << "class info b: " << b << '\n';
diffView(std::cout, a, b);
// printout example for annotated class
otherClass c;
std::cout << "class info for annotated class: " << c << '\n';
// simple serialisation example:
IoBuffer buffer;
assert(a != b && "a & b should be unequal here"); // just checking
// serialise 'a' into the byte buffer
opencmw::serialise<opencmw::YaS>(buffer, a);
// de-serialise the byte buffer into 'b'
try {
opencmw::deserialise<opencmw::YaS, opencmw::ProtocolCheck::LENIENT>(buffer, b);
} catch (...) { // TODO: add protocol exception and field name/mismatch interface here
std::cout << "caught unknown exception " << std::endl;
}
assert(a == b && "a & b should be equal here"); // just checking
std::cout << std::format("finished simple serialise-deserialise identity -- IoBuffer required {} bytes\n", buffer.size());
diffView(std::cout, a, b);
// N.B. the buffer size is larger than the mere field sizes because of additional meta information that is required
// for safely transmitting the data via the network and between different programming languages.
}