Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <boost/config/warning_disable.hpp>
- #include <boost/spirit/include/karma.hpp>
- #include <boost/variant.hpp>
- #include <iostream>
- #include <string>
- class Value
- {
- public:
- enum ValueType
- {
- BoolType,
- NumericType
- };
- Value(bool b) : type_(BoolType), value_(b) {}
- Value(const double d) : type_(NumericType), value_(d) {};
- ValueType type() { return type_; }
- operator bool() { return boost::get<bool>(value_); }
- operator double() { return boost::get<double>(value_); }
- private:
- ValueType type_;
- boost::variant<bool, double> value_;
- };
- namespace karma = boost::spirit::karma;
- int main()
- {
- using karma::bool_;
- using karma::double_;
- using karma::rule;
- using karma::eps;
- std::string generated;
- std::back_insert_iterator<std::string> sink(generated);
- rule<std::back_insert_iterator<std::string>, Value()> value_rule = bool_ | double_;
- // rule<std::back_insert_iterator<std::string>, Value()> value_rule = (eps( ... ) << bool_) | (eps( ... ) << double_);
- Value bool_value = Value(true);
- Value double_value = Value(5.0);
- karma::generate(sink, value_rule, bool_value);
- std::cout << generated << "\n";
- generated.clear();
- karma::generate(sink, value_rule, double_value);
- std::cout << generated << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement