variant_find_substitute.hpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*=============================================================================
  2. Copyright (c) 2001-2014 Joel de Guzman
  3. http://spirit.sourceforge.net/
  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. =============================================================================*/
  7. #if !defined(BOOST_SPIRIT_X3_VARIANT_FIND_SUBSTITUTE_APR_18_2014_930AM)
  8. #define BOOST_SPIRIT_X3_VARIANT_FIND_SUBSTITUTE_APR_18_2014_930AM
  9. #include <boost/spirit/home/x3/support/traits/is_substitute.hpp>
  10. #include <boost/mpl/find.hpp>
  11. namespace boost { namespace spirit { namespace x3 { namespace traits
  12. {
  13. template <typename Variant, typename Attribute>
  14. struct variant_find_substitute
  15. {
  16. // Get the type from the variant that can be a substitute for Attribute.
  17. // If none is found, just return Attribute
  18. typedef Variant variant_type;
  19. typedef typename variant_type::types types;
  20. typedef typename mpl::end<types>::type end;
  21. typedef typename mpl::find<types, Attribute>::type iter_1;
  22. typedef typename
  23. mpl::eval_if<
  24. is_same<iter_1, end>,
  25. mpl::find_if<types, traits::is_substitute<mpl::_1, Attribute> >,
  26. mpl::identity<iter_1>
  27. >::type
  28. iter;
  29. typedef typename
  30. mpl::eval_if<
  31. is_same<iter, end>,
  32. mpl::identity<Attribute>,
  33. mpl::deref<iter>
  34. >::type
  35. type;
  36. };
  37. template <typename Variant>
  38. struct variant_find_substitute<Variant, Variant>
  39. : mpl::identity<Variant> {};
  40. }}}}
  41. #endif