forked from mapbox/variant
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboost_spirit_karma.cpp
More file actions
58 lines (44 loc) · 1.34 KB
/
boost_spirit_karma.cpp
File metadata and controls
58 lines (44 loc) · 1.34 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
#include <iostream>
#include <cassert>
#include <mapbox/variant.hpp>
#include <mapbox/boost_spirit_karma.hpp>
#include <boost/spirit/include/karma.hpp>
typedef mapbox::util::variant<int, bool, std::string, float, long, uint64_t> frame;
int main() {
typedef std::back_insert_iterator<std::string> Iterator;
using namespace boost::spirit::karma;
rule<Iterator, std::string()> mystring = *char_;
rule<Iterator, frame()> r =
int_
| bool_
| mystring
;
{
frame frm = 12;
std::string buf;
Iterator iter(buf);
auto s = generate(iter, r, frm);
std::cout << (s ? " success " : " error ") << " - " << mapbox::util::get<int>(frm) << " == " << buf << std::endl;
assert(s);
assert("12" == buf);
}
{
frame frm = true;
std::string buf;
Iterator iter(buf);
auto s = generate(iter, r, frm);
std::cout << (s ? " success " : " error ") << " - " << mapbox::util::get<bool>(frm) << " == " << buf << std::endl;
assert(s);
assert("true" == buf);
}
{
frame frm = std::string{"this is a string"};
std::string buf;
Iterator iter(buf);
auto s = generate(iter, r, frm);
std::cout << (s ? " success " : " error ") << " - " << mapbox::util::get<std::string>(frm) << " == " << buf << std::endl;
assert(s);
assert("this is a string" == buf);
}
return EXIT_SUCCESS;
}