Advertisement
Guest User

Untitled

a guest
Sep 10th, 2012
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. #include <boost/config/warning_disable.hpp>
  2. #include <boost/spirit/include/karma.hpp>
  3. #include <boost/variant.hpp>
  4.  
  5. #include <iostream>
  6. #include <string>
  7.  
  8. class Value
  9. {
  10. public:
  11.  
  12.     enum ValueType
  13.     {
  14.         BoolType,
  15.         NumericType
  16.     };
  17.  
  18.     Value(bool b) : type_(BoolType), value_(b) {}
  19.     Value(const double d) : type_(NumericType), value_(d) {};
  20.  
  21.     ValueType type() { return type_; }
  22.  
  23.     operator bool() { return boost::get<bool>(value_); }
  24.     operator double() { return boost::get<double>(value_); }
  25.    
  26. private:
  27.     ValueType type_;
  28.     boost::variant<bool, double> value_;
  29.  
  30. };
  31.  
  32. namespace karma = boost::spirit::karma;
  33.  
  34. int main()
  35. {
  36.     using karma::bool_;
  37.     using karma::double_;
  38.     using karma::rule;
  39.     using karma::eps;
  40.  
  41.     std::string generated;
  42.     std::back_insert_iterator<std::string> sink(generated);
  43.  
  44.     rule<std::back_insert_iterator<std::string>, Value()> value_rule = bool_ | double_;
  45.     // rule<std::back_insert_iterator<std::string>, Value()> value_rule = (eps( ... ) << bool_) | (eps( ... ) << double_);
  46.  
  47.     Value bool_value = Value(true);
  48.     Value double_value = Value(5.0);
  49.  
  50.     karma::generate(sink, value_rule, bool_value);
  51.     std::cout << generated << "\n";
  52.  
  53.     generated.clear();
  54.  
  55.     karma::generate(sink, value_rule, double_value);
  56.     std::cout << generated << "\n";
  57.  
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement