/Src/Dependencies/Boost/boost/variant/visitor_ptr.hpp

http://hadesmem.googlecode.com/ · C++ Header · 116 lines · 70 code · 29 blank · 17 comment · 0 complexity · 8b417e19326ec6049764111e99888711 MD5 · raw file

  1. //-----------------------------------------------------------------------------
  2. // boost variant/visitor_ptr.hpp header file
  3. // See http://www.boost.org for updates, documentation, and revision history.
  4. //-----------------------------------------------------------------------------
  5. //
  6. // Copyright (c) 2002-2003
  7. // Eric Friedman
  8. //
  9. // Distributed under the Boost Software License, Version 1.0. (See
  10. // accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. #ifndef BOOST_VARIANT_VISITOR_PTR_HPP
  13. #define BOOST_VARIANT_VISITOR_PTR_HPP
  14. #include "boost/variant/bad_visit.hpp"
  15. #include "boost/variant/static_visitor.hpp"
  16. #include "boost/mpl/eval_if.hpp"
  17. #include "boost/mpl/identity.hpp"
  18. #include "boost/type_traits/add_reference.hpp"
  19. #include "boost/type_traits/is_reference.hpp"
  20. #include "boost/type_traits/is_void.hpp"
  21. namespace boost {
  22. //////////////////////////////////////////////////////////////////////////
  23. // function template visitor_ptr
  24. //
  25. // Adapts a function pointer for use as visitor capable of handling
  26. // values of a single type. Throws bad_visit if inappropriately applied.
  27. //
  28. template <typename T, typename R>
  29. class visitor_ptr_t
  30. : public static_visitor<R>
  31. {
  32. private: // representation
  33. typedef R (*visitor_t)(T);
  34. visitor_t visitor_;
  35. public: // typedefs
  36. typedef R result_type;
  37. private: // private typedefs
  38. typedef typename mpl::eval_if<
  39. is_reference<T>
  40. , mpl::identity<T>
  41. , add_reference<const T>
  42. >::type argument_fwd_type;
  43. public: // structors
  44. explicit visitor_ptr_t(visitor_t visitor)
  45. : visitor_(visitor)
  46. {
  47. }
  48. public: // static visitor interfaces
  49. template <typename U>
  50. result_type operator()(const U&) const
  51. {
  52. throw bad_visit();
  53. }
  54. #if !defined(BOOST_NO_VOID_RETURNS)
  55. public: // static visitor interfaces, cont.
  56. result_type operator()(argument_fwd_type operand) const
  57. {
  58. return visitor_(operand);
  59. }
  60. #else // defined(BOOST_NO_VOID_RETURNS)
  61. private: // helpers, for static visitor interfaces (below)
  62. result_type execute_impl(argument_fwd_type operand, mpl::false_) const
  63. {
  64. return visitor_(operand);
  65. }
  66. BOOST_VARIANT_AUX_RETURN_VOID_TYPE
  67. execute_impl(argument_fwd_type operand, mpl::true_) const
  68. {
  69. visitor_(operand);
  70. BOOST_VARIANT_AUX_RETURN_VOID;
  71. }
  72. public: // static visitor interfaces, cont.
  73. BOOST_VARIANT_AUX_GENERIC_RESULT_TYPE(result_type)
  74. operator()(argument_fwd_type operand) const
  75. {
  76. typedef typename is_void<result_type>::type has_void_result;
  77. return execute_impl(operand, has_void_result());
  78. }
  79. #endif // BOOST_NO_VOID_RETURNS workaround
  80. };
  81. template <typename R, typename T>
  82. inline visitor_ptr_t<T,R> visitor_ptr(R (*visitor)(T))
  83. {
  84. return visitor_ptr_t<T,R>(visitor);
  85. }
  86. } // namespace boost
  87. #endif// BOOST_VISITOR_VISITOR_PTR_HPP