attributes.hpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (c) 2001-2011 Hartmut Kaiser
  2. // Copyright (c) 2001-2011 Joel de Guzman
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #if !defined(SPIRIT_KARMA_DETAIL_ATTRIBUTES_APR_18_2010_0453PM)
  7. #define SPIRIT_KARMA_DETAIL_ATTRIBUTES_APR_18_2010_0453PM
  8. #include <boost/spirit/home/karma/domain.hpp>
  9. #include <boost/spirit/home/support/attributes_fwd.hpp>
  10. #include <boost/spirit/home/support/attributes.hpp>
  11. ///////////////////////////////////////////////////////////////////////////////
  12. namespace boost { namespace spirit { namespace karma
  13. {
  14. template <typename Exposed, typename Transformed, typename Enable = void>
  15. struct transform_attribute
  16. {
  17. typedef Transformed type;
  18. static Transformed pre(Exposed& val)
  19. {
  20. return Transformed(traits::extract_from<Transformed>(val, unused));
  21. }
  22. // Karma only, no post() and no fail() required
  23. };
  24. template <typename Exposed, typename Transformed>
  25. struct transform_attribute<boost::optional<Exposed> const, Transformed
  26. , typename disable_if<is_same<boost::optional<Exposed>, Transformed> >::type>
  27. {
  28. typedef Transformed const& type;
  29. static Transformed const& pre(boost::optional<Exposed> const& val)
  30. {
  31. return boost::get<Transformed>(val);
  32. }
  33. };
  34. template <typename Attribute>
  35. struct transform_attribute<Attribute const, Attribute>
  36. {
  37. typedef Attribute const& type;
  38. static Attribute const& pre(Attribute const& val) { return val; }
  39. // Karma only, no post() and no fail() required
  40. };
  41. // unused_type needs some special handling as well
  42. template <>
  43. struct transform_attribute<unused_type, unused_type>
  44. {
  45. typedef unused_type type;
  46. static unused_type pre(unused_type) { return unused; }
  47. };
  48. template <>
  49. struct transform_attribute<unused_type const, unused_type>
  50. : transform_attribute<unused_type, unused_type>
  51. {};
  52. template <typename Attribute>
  53. struct transform_attribute<Attribute, unused_type>
  54. : transform_attribute<unused_type, unused_type>
  55. {};
  56. template <typename Attribute>
  57. struct transform_attribute<Attribute const, unused_type>
  58. : transform_attribute<unused_type, unused_type>
  59. {};
  60. }}}
  61. ///////////////////////////////////////////////////////////////////////////////
  62. namespace boost { namespace spirit { namespace traits { namespace detail
  63. {
  64. template <typename Exposed, typename Transformed>
  65. struct transform_attribute_base<Exposed, Transformed, karma::domain>
  66. : karma::transform_attribute<Exposed, Transformed>
  67. {};
  68. }}}}
  69. #endif