recursive_wrapper.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //-----------------------------------------------------------------------------
  2. // boost variant/recursive_wrapper.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, Itay Maman
  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_RECURSIVE_WRAPPER_HPP
  13. #define BOOST_VARIANT_RECURSIVE_WRAPPER_HPP
  14. #include <boost/variant/recursive_wrapper_fwd.hpp>
  15. #include <boost/variant/detail/move.hpp>
  16. #include <boost/core/checked_delete.hpp>
  17. namespace boost {
  18. //////////////////////////////////////////////////////////////////////////
  19. // class template recursive_wrapper
  20. //
  21. // See docs and recursive_wrapper_fwd.hpp for more information.
  22. //
  23. template <typename T>
  24. class recursive_wrapper
  25. {
  26. public: // typedefs
  27. typedef T type;
  28. private: // representation
  29. T* p_;
  30. public: // structors
  31. ~recursive_wrapper();
  32. recursive_wrapper();
  33. recursive_wrapper(const recursive_wrapper& operand);
  34. recursive_wrapper(const T& operand);
  35. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  36. recursive_wrapper(recursive_wrapper&& operand);
  37. recursive_wrapper(T&& operand);
  38. #endif
  39. private: // helpers, for modifiers (below)
  40. void assign(const T& rhs);
  41. public: // modifiers
  42. recursive_wrapper& operator=(const recursive_wrapper& rhs)
  43. {
  44. assign( rhs.get() );
  45. return *this;
  46. }
  47. recursive_wrapper& operator=(const T& rhs)
  48. {
  49. assign( rhs );
  50. return *this;
  51. }
  52. void swap(recursive_wrapper& operand) BOOST_NOEXCEPT
  53. {
  54. T* temp = operand.p_;
  55. operand.p_ = p_;
  56. p_ = temp;
  57. }
  58. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  59. recursive_wrapper& operator=(recursive_wrapper&& rhs) BOOST_NOEXCEPT
  60. {
  61. swap(rhs);
  62. return *this;
  63. }
  64. recursive_wrapper& operator=(T&& rhs)
  65. {
  66. get() = detail::variant::move(rhs);
  67. return *this;
  68. }
  69. #endif
  70. public: // queries
  71. T& get() { return *get_pointer(); }
  72. const T& get() const { return *get_pointer(); }
  73. T* get_pointer() { return p_; }
  74. const T* get_pointer() const { return p_; }
  75. };
  76. template <typename T>
  77. recursive_wrapper<T>::~recursive_wrapper()
  78. {
  79. boost::checked_delete(p_);
  80. }
  81. template <typename T>
  82. recursive_wrapper<T>::recursive_wrapper()
  83. : p_(new T)
  84. {
  85. }
  86. template <typename T>
  87. recursive_wrapper<T>::recursive_wrapper(const recursive_wrapper& operand)
  88. : p_(new T( operand.get() ))
  89. {
  90. }
  91. template <typename T>
  92. recursive_wrapper<T>::recursive_wrapper(const T& operand)
  93. : p_(new T(operand))
  94. {
  95. }
  96. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  97. template <typename T>
  98. recursive_wrapper<T>::recursive_wrapper(recursive_wrapper&& operand)
  99. : p_(new T( detail::variant::move(operand.get()) ))
  100. {
  101. }
  102. template <typename T>
  103. recursive_wrapper<T>::recursive_wrapper(T&& operand)
  104. : p_(new T( detail::variant::move(operand) ))
  105. {
  106. }
  107. #endif
  108. template <typename T>
  109. void recursive_wrapper<T>::assign(const T& rhs)
  110. {
  111. this->get() = rhs;
  112. }
  113. // function template swap
  114. //
  115. // Swaps two recursive_wrapper<T> objects of the same type T.
  116. //
  117. template <typename T>
  118. inline void swap(recursive_wrapper<T>& lhs, recursive_wrapper<T>& rhs) BOOST_NOEXCEPT
  119. {
  120. lhs.swap(rhs);
  121. }
  122. } // namespace boost
  123. #endif // BOOST_VARIANT_RECURSIVE_WRAPPER_HPP