-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathoptional.cpp
More file actions
48 lines (40 loc) · 1.14 KB
/
optional.cpp
File metadata and controls
48 lines (40 loc) · 1.14 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
#include "bobl/cbor/decode.hpp"
#include "bobl/cbor/encode.hpp"
#include <boost/optional.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <string>
#include <tuple>
#include <cstdint>
#include <cassert>
enum class Type { One, Two, Three };
struct Data
{
boost::optional<Type> type; //will be encoded as int
int id;
};
BOOST_FUSION_ADAPT_STRUCT(Data, type, id)
int main()
{
auto data = Data { {}, 123};
std::vector<std::uint8_t> encoded = bobl::cbor::encode(data);
auto begin = encoded.data();
auto end = begin + encoded.size();
//this will work as expected
auto decoded = bobl::cbor::decode<Data>(begin, end);
assert(!decoded.type);
assert(decoded.id == data.id);
begin = encoded.data();
auto decoded_tuple = bobl::cbor::decode<boost::optional<Type>, boost::optional<int>>(begin, end);
//this supposed to be broken please see README.md
assert(int(std::get<0>(decoded_tuple).get()) == 123);
assert(!std::get<1>(decoded_tuple));
try
{
auto begin = encoded.data();
bobl::cbor::decode<boost::optional<Type>, int>(begin, end);
assert(!"it should throw before it gets here");
}catch(bobl::InputToShort&)
{
}
return 0;
}