optional_traits.hpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*=============================================================================
  2. Copyright (c) 2001-2014 Joel de Guzman
  3. Copyright (c) 2001-2011 Hartmut Kaiser
  4. http://spirit.sourceforge.net/
  5. Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. #if !defined(BOOST_SPIRIT_X3_OPTIONAL_TRAITS_FEBRUARY_06_2007_1001AM)
  9. #define BOOST_SPIRIT_X3_OPTIONAL_TRAITS_FEBRUARY_06_2007_1001AM
  10. #include <boost/spirit/home/x3/support/unused.hpp>
  11. #include <boost/optional/optional.hpp>
  12. #include <boost/mpl/identity.hpp>
  13. namespace boost { namespace spirit { namespace x3 { namespace traits
  14. {
  15. ///////////////////////////////////////////////////////////////////////////
  16. template <typename T, typename Enable = void>
  17. struct is_optional
  18. : mpl::false_
  19. {};
  20. template <typename T>
  21. struct is_optional<boost::optional<T>>
  22. : mpl::true_
  23. {};
  24. ///////////////////////////////////////////////////////////////////////////
  25. // build_optional
  26. //
  27. // Build a boost::optional from T. Return unused_type if T is unused_type.
  28. ///////////////////////////////////////////////////////////////////////////
  29. template <typename T>
  30. struct build_optional
  31. {
  32. typedef boost::optional<T> type;
  33. };
  34. template <typename T>
  35. struct build_optional<boost::optional<T> >
  36. {
  37. typedef boost::optional<T> type;
  38. };
  39. template <>
  40. struct build_optional<unused_type>
  41. {
  42. typedef unused_type type;
  43. };
  44. ///////////////////////////////////////////////////////////////////////////
  45. // optional_value
  46. //
  47. // Get the optional's value_type. Handles unused_type as well.
  48. ///////////////////////////////////////////////////////////////////////////
  49. template <typename T>
  50. struct optional_value : mpl::identity<T> {};
  51. template <typename T>
  52. struct optional_value<boost::optional<T> >
  53. : mpl::identity<T> {};
  54. template <>
  55. struct optional_value<unused_type>
  56. : mpl::identity<unused_type> {};
  57. template <>
  58. struct optional_value<unused_type const>
  59. : mpl::identity<unused_type> {};
  60. }}}}
  61. #endif