-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtest.cpp
50 lines (37 loc) · 1.44 KB
/
test.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
#include <blobify/load.hpp>
#include <blobify/memory_storage.hpp>
#include <utility>
#include <cstring>
#include <iostream>
namespace Example {
struct InnerStruct {
std::uint32_t inner_member;
};
struct ExampleStruct {
int member1;
InnerStruct member2;
char member3;
};
inline constexpr auto properties(blob::tag<ExampleStruct>) {
auto props = blob::properties_t<ExampleStruct> { };
props.member<&ExampleStruct::member1>().expected_value = 10;
props.member<&ExampleStruct::member3>().expected_value = 'c';
return props;
}
} // namespace Example
int main() {
char data[256] = { 10, 0, 0, 0, 20, 0, 0, 0, 'c' };
{
auto example_value = blob::load<Example::ExampleStruct>(blob::memory_storage::OnArray(data));
std::cout << "Member 1 is 0x" << std::hex << example_value.member1 << std::endl;
std::cout << "Member 2 is 0x" << std::hex << example_value.member2.inner_member << std::endl;
std::cout << "Member 3 is '" << example_value.member3 << "'" << std::endl;
}
data[0] = 11;
try {
(void)blob::load<Example::ExampleStruct>(blob::memory_storage::OnArray(data));
std::cout << "This should not be printed: member1 was not 10 and hence load() should throw an exception!" << std::endl;
} catch (blob::unexpected_value_exception<&Example::ExampleStruct::member1>& err) {
std::cout << "Rightfully caught exception: member 1 was not 10!" << std::endl;
}
}