• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*=============================================================================
3     Copyright (c) 2019 Tom Tan
4 
5     Distributed under the Boost Software License, Version 1.0. (See accompanying
6     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 =============================================================================*/
8 #include <boost/config/warning_disable.hpp>
9 #include <boost/spirit/home/x3.hpp>
10 
11 #include <boost/fusion/adapted/std_tuple.hpp>
12 
13 #include <iostream>
14 #include <tuple>
15 #include <string>
16 
17 //
18 // X3 does not support more than one attribute anymore in the parse function,
19 // this example show how to wrap multiple attributes into one leveraging std::tuple.
20 //
21 
parse_message_prefix_revision(const std::string & s)22 std::tuple<uint32_t, uint32_t, uint32_t> parse_message_prefix_revision(const std::string &s)
23 {
24 
25   namespace x3 = boost::spirit::x3;
26 
27   auto const uint_3_digits = x3::uint_parser<std::uint32_t, 10, 3, 3>{};
28   auto const uint_4_digits = x3::uint_parser<std::uint32_t, 10, 4, 4>{};
29 
30   auto iter = s.cbegin();
31   auto end_iter = s.cend();
32 
33   std::tuple<uint32_t, uint32_t, uint32_t> length_id_revision;
34 
35   x3::parse(iter, end_iter,
36             uint_4_digits >> uint_4_digits >> uint_3_digits,
37             length_id_revision);
38 
39   return length_id_revision;
40 }
41 
main()42 int main()
43 {
44   std::string s = "00200060001";
45 
46   std::cout << "parsing " << s << '\n';
47   auto [len, id, rev] = parse_message_prefix_revision(s);
48   std::cout << "length = " << len << '\n'
49             << "id = " << id << '\n'
50             << "revision =" << rev;
51 }