| 1 | #include <boost/uuid/uuid.hpp>
|
|---|
| 2 | #include <boost/spirit/include/qi.hpp>
|
|---|
| 3 | #include <boost/spirit/include/qi_uint.hpp>
|
|---|
| 4 | #include <boost/spirit/include/phoenix_core.hpp>
|
|---|
| 5 | #include <boost/spirit/include/phoenix_operator.hpp>
|
|---|
| 6 |
|
|---|
| 7 | namespace qi = boost::spirit::qi;
|
|---|
| 8 | namespace px = boost::phoenix;
|
|---|
| 9 |
|
|---|
| 10 | static qi::uint_parser<uint8_t, 16, 2, 2> hx;
|
|---|
| 11 |
|
|---|
| 12 | boost::uuids::uuid from_string(const std::string& rep)
|
|---|
| 13 | {
|
|---|
| 14 | boost::uuids::uuid u;
|
|---|
| 15 |
|
|---|
| 16 | std::string::const_iterator rep_begin = rep.begin();
|
|---|
| 17 | std::string::const_iterator rep_end = rep.end();
|
|---|
| 18 |
|
|---|
| 19 | const bool okay = boost::spirit::qi::parse( rep_begin, rep_end,
|
|---|
| 20 | hx[ px::ref(u.data[0]) = qi::_1 ] >
|
|---|
| 21 | hx[ px::ref(u.data[1]) = qi::_1 ] >
|
|---|
| 22 | hx[ px::ref(u.data[2]) = qi::_1 ] >
|
|---|
| 23 | hx[ px::ref(u.data[3]) = qi::_1 ] >
|
|---|
| 24 | '-' >
|
|---|
| 25 | hx[ px::ref(u.data[4]) = qi::_1 ] >
|
|---|
| 26 | hx[ px::ref(u.data[5]) = qi::_1 ] >
|
|---|
| 27 | '-' >
|
|---|
| 28 | hx[ px::ref(u.data[6]) = qi::_1 ] >
|
|---|
| 29 | hx[ px::ref(u.data[7]) = qi::_1 ] >
|
|---|
| 30 | '-' >
|
|---|
| 31 | hx[ px::ref(u.data[8]) = qi::_1 ] >
|
|---|
| 32 | hx[ px::ref(u.data[9]) = qi::_1 ] > // enable this line (or any other line below) and you'll see the error
|
|---|
| 33 | '-' >
|
|---|
| 34 | // hx[ px::ref(u.data[10]) = qi::_1 ] >
|
|---|
| 35 | // hx[ px::ref(u.data[11]) = qi::_1 ] >
|
|---|
| 36 | // hx[ px::ref(u.data[12]) = qi::_1 ] >
|
|---|
| 37 | // hx[ px::ref(u.data[13]) = qi::_1 ] >
|
|---|
| 38 | // hx[ px::ref(u.data[14]) = qi::_1 ] >
|
|---|
| 39 | hx[ px::ref(u.data[15]) = qi::_1 ]
|
|---|
| 40 |
|
|---|
| 41 | );
|
|---|
| 42 |
|
|---|
| 43 | if(!okay) throw std::runtime_error("cannot parse string \"" + rep + "\" as a UUID");
|
|---|
| 44 | return u;
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 | int main()
|
|---|
| 49 | {
|
|---|
| 50 | boost::uuids::uuid u = from_string("f0cbb6f6-3c39-42e0-bfac-5a34a2900f1d");
|
|---|
| 51 | return u.data[0];
|
|---|
| 52 | }
|
|---|