match_attr_traits.ipp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*=============================================================================
  2. Copyright (c) 1998-2003 Joel de Guzman
  3. http://spirit.sourceforge.net/
  4. Use, modification and distribution is subject to the Boost Software
  5. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. #if !defined(BOOST_SPIRIT_MATCH_ATTR_TRAITS_IPP)
  9. #define BOOST_SPIRIT_MATCH_ATTR_TRAITS_IPP
  10. #include <boost/optional.hpp>
  11. #include <boost/mpl/bool.hpp>
  12. #include <boost/mpl/or.hpp>
  13. #include <boost/type_traits/is_convertible.hpp>
  14. #include <boost/type_traits/is_same.hpp>
  15. namespace boost { namespace spirit {
  16. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  17. namespace impl
  18. {
  19. template <typename T>
  20. struct match_attr_traits
  21. {
  22. typedef typename
  23. boost::optional<T>::reference_const_type
  24. const_reference;
  25. // case where src *IS* convertible to T (dest)
  26. template <typename T2>
  27. static void
  28. convert(boost::optional<T>& dest, T2 const& src, mpl::true_)
  29. {
  30. dest.reset(src);
  31. }
  32. // case where src *IS NOT* convertible to T (dest)
  33. template <typename T2>
  34. static void
  35. convert(boost::optional<T>& dest, T2 const& /*src*/, mpl::false_)
  36. {
  37. dest.reset();
  38. }
  39. static void
  40. convert(boost::optional<T>& dest, nil_t/*src*/)
  41. {
  42. dest.reset();
  43. }
  44. template <typename T2>
  45. static void
  46. convert(boost::optional<T>& dest, T2 const& src)
  47. {
  48. convert(dest, src, is_convertible<T2, T>());
  49. }
  50. template <typename OtherMatchT>
  51. static void
  52. copy(boost::optional<T>& dest, OtherMatchT const& src)
  53. {
  54. if (src.has_valid_attribute())
  55. convert(dest, src.value());
  56. }
  57. template <typename OtherMatchT>
  58. static void
  59. assign(boost::optional<T>& dest, OtherMatchT const& src)
  60. {
  61. if (src.has_valid_attribute())
  62. convert(dest, src.value());
  63. else
  64. dest.reset();
  65. }
  66. // T is not reference
  67. template <typename ValueT>
  68. static void
  69. set_value(boost::optional<T>& dest, ValueT const& val, mpl::false_)
  70. {
  71. dest.reset(val);
  72. }
  73. // T is a reference
  74. template <typename ValueT>
  75. static void
  76. set_value(boost::optional<T>& dest, ValueT const& val, mpl::true_)
  77. {
  78. dest.get() = val;
  79. }
  80. };
  81. }
  82. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  83. }} // namespace boost::spirit::impl
  84. #endif