real.hpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*=============================================================================
  2. Copyright (c) 2001-2014 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #if !defined(BOOST_SPIRIT_X3_REAL_APRIL_18_2006_0850AM)
  7. #define BOOST_SPIRIT_X3_REAL_APRIL_18_2006_0850AM
  8. #include <boost/spirit/home/x3/core/parser.hpp>
  9. #include <boost/spirit/home/x3/core/skip_over.hpp>
  10. #include <boost/spirit/home/x3/numeric/real_policies.hpp>
  11. #include <boost/spirit/home/x3/support/numeric_utils/extract_real.hpp>
  12. namespace boost { namespace spirit { namespace x3
  13. {
  14. template <typename T, typename RealPolicies = real_policies<T> >
  15. struct real_parser : parser<real_parser<T, RealPolicies> >
  16. {
  17. typedef T attribute_type;
  18. static bool const has_attribute = true;
  19. real_parser()
  20. : policies() {}
  21. real_parser(RealPolicies const& policies)
  22. : policies(policies) {}
  23. template <typename Iterator, typename Context>
  24. bool parse(Iterator& first, Iterator const& last
  25. , Context const& context, unused_type, T& attr_) const
  26. {
  27. x3::skip_over(first, last, context);
  28. return extract_real<T, RealPolicies>::parse(first, last, attr_, policies);
  29. }
  30. template <typename Iterator, typename Context, typename Attribute>
  31. bool parse(Iterator& first, Iterator const& last
  32. , Context const& context, unused_type, Attribute& attr_param) const
  33. {
  34. // this case is called when Attribute is not T
  35. T attr_;
  36. if (parse(first, last, context, unused, attr_))
  37. {
  38. traits::move_to(attr_, attr_param);
  39. return true;
  40. }
  41. return false;
  42. }
  43. RealPolicies policies;
  44. };
  45. typedef real_parser<float> float_type;
  46. float_type const float_ = {};
  47. typedef real_parser<double> double_type;
  48. double_type const double_ = {};
  49. typedef real_parser<long double> long_double_type;
  50. long_double_type const long_double = {};
  51. }}}
  52. #endif