-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathadapt.cpp
More file actions
51 lines (41 loc) · 1.13 KB
/
Copy pathadapt.cpp
File metadata and controls
51 lines (41 loc) · 1.13 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
#include "bobl/cbor/decode.hpp"
#include "bobl/cbor/encode.hpp"
#include "bobl/cbor/adapter.hpp"
#include <cassert>
class X
{
public:
explicit X(int persistent) : persistent_{ persistent } {}
int persistent() const { return persistent_;}
int not_persistent() const { return notso_;}
void not_persistent(int val) { notso_ = val;}
private:
int persistent_;
int notso_ = 0;
};
namespace bobl { namespace cbor {
template<>
class Adapter<X, boost::mpl::true_>
{
public:
using type = int;
X operator()(int x) const { return X{ x }; }
int operator()(X const& x) const { return x.persistent(); }
};
} /*namespace cbor*/ } /*namespace bobl*/
struct Data
{
X x;
};
BOOST_FUSION_ADAPT_STRUCT(Data, x)
int main()
{
static_assert(bobl::utility::Adaptable<X, bobl::cbor::Adapter<X>>::value, "seems bobl::utility::utility::decode::Adaptable doesn't work as expected");
auto data = Data{ X { 101 } };
std::vector<std::uint8_t> encoded = bobl::cbor::encode(data);
auto begin = encoded.data();
auto end = begin + encoded.size();
auto decoded = bobl::cbor::decode<X>(begin, end);
assert(decoded.persistent() == 101);
return 0;
}