forked from bitekas/libproperty
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusage_test.cpp
More file actions
101 lines (89 loc) · 3.08 KB
/
usage_test.cpp
File metadata and controls
101 lines (89 loc) · 3.08 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
90
91
92
93
94
95
96
97
98
99
100
101
#include "../libproperty/property.hpp"
#include <cassert>
#include <cstddef>
#include <utility>
#include <iostream>
#include <string>
using namespace meta;
/** consumes 8 bytes, because of alignment */
template <int I>
struct property_test {
using self_ = property_test;
public:
int value_;
int const& get_value() const { return value_; }
int const& set_value(int const& x) { return value_ = x; }
LIBPROPERTY_PROPERTY(prop1, self_, get_value, set_value);
LIBPROPERTY_PROPERTY(prop2, self_, get_value, set_value);
LIBPROPERTY_PROPERTY(prop3, self_, get_value, set_value);
};
/** consumes 8 bytes, because of alignment */
static_assert(sizeof(property_test<0>) == sizeof(int) + sizeof(int),
"External property tester is supposed to be only its value and"
" the property alignment!");
template <typename T>
struct property_with_storage_test {
using self_ = property_with_storage_test;
public:
T const& get_value() const { return prop1.value; }
T const& set_value(T const& x) { return prop1.value = x; }
LIBPROPERTY_PROPERTY_WITH_STORAGE(T, prop1, self_, get_value, set_value);
};
static_assert(sizeof(property_with_storage_test<char>) == sizeof(char),
"Supposed to be equal in size as what it's storing!");
static_assert(sizeof(property_with_storage_test<int>) == sizeof(int),
"Supposed to be equal in size as what it's storing!");
static_assert(sizeof(property_with_storage_test<long>) == sizeof(long),
"Supposed to be equal in size as what it's storing!");
struct my_class {
int const& my_getter() const { return property.value; }
int const& my_setter(std::string const& x) {
return property.value = atoi(x.c_str());
}
int const& my_int_setter(int x) { return property.value = x; }
LIBPROPERTY_PROPERTY_WITH_STORAGE(
int, property, my_class, my_getter, my_setter);
LIBPROPERTY_PROPERTY(as_int, my_class, my_getter, my_int_setter);
};
struct offset_of_test {
int i;
size_t j;
};
auto y = [](auto x) {
std::cout << x << std::endl;
};
int main() {
{
using namespace libproperty::impl;
assert(offset_of<offset_of_test>(&offset_of_test::j) ==
offsetof(offset_of_test, j));
assert(offset_of<offset_of_test>(&offset_of_test::i) ==
offsetof(offset_of_test, i));
}
{
property_test<0> x;
property_test<0> y;
x.prop1 = 5;
y = x;
std::cout << "sizeof property_test: " << sizeof(x) << " " << x.prop1 << " "
<< y.prop2 << " prop1 offset:"
<< libproperty::impl::offset_of_property<decltype(x)>(&x.prop1)
<< " prop2 offset: "
<< libproperty::impl::offset_of_property<decltype(x)>(&x.prop2)
<< '\n';
}
{
property_with_storage_test<int> x;
x.prop1 = 5;
auto y = x;
std::cout << "sizeof property_test: " << sizeof(x) << " "
<< x.prop1 << " " << y.prop1 << '\n';
}
{
my_class a;
a.property = "5";
std::cout << "should print 8: " << a.property + 3 << '\n';
std::cout << "sizeof(a): " << sizeof(a)
<< " == sizeof(int): " << sizeof(int) << '\n';
}
}