backup_holder.hpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //-----------------------------------------------------------------------------
  2. // boost variant/detail/backup_holder.hpp header file
  3. // See http://www.boost.org for updates, documentation, and revision history.
  4. //-----------------------------------------------------------------------------
  5. //
  6. // Copyright (c) 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_DETAIL_BACKUP_HOLDER_HPP
  13. #define BOOST_VARIANT_DETAIL_BACKUP_HOLDER_HPP
  14. #include <boost/config.hpp>
  15. #include <boost/assert.hpp>
  16. namespace boost {
  17. namespace detail { namespace variant {
  18. template <typename T>
  19. class backup_holder
  20. {
  21. private: // representation
  22. T* backup_;
  23. public: // structors
  24. ~backup_holder() BOOST_NOEXCEPT
  25. {
  26. delete backup_;
  27. }
  28. explicit backup_holder(T* backup) BOOST_NOEXCEPT
  29. : backup_(backup)
  30. {
  31. }
  32. backup_holder(const backup_holder&);
  33. public: // modifiers
  34. backup_holder& operator=(const backup_holder& rhs)
  35. {
  36. *backup_ = rhs.get();
  37. return *this;
  38. }
  39. backup_holder& operator=(const T& rhs)
  40. {
  41. *backup_ = rhs;
  42. return *this;
  43. }
  44. void swap(backup_holder& rhs) BOOST_NOEXCEPT
  45. {
  46. T* tmp = rhs.backup_;
  47. rhs.backup_ = this->backup_;
  48. this->backup_ = tmp;
  49. }
  50. public: // queries
  51. T& get() BOOST_NOEXCEPT
  52. {
  53. return *backup_;
  54. }
  55. const T& get() const BOOST_NOEXCEPT
  56. {
  57. return *backup_;
  58. }
  59. };
  60. template <typename T>
  61. backup_holder<T>::backup_holder(const backup_holder&)
  62. : backup_(0)
  63. {
  64. // not intended for copy, but do not want to prohibit syntactically
  65. BOOST_ASSERT(false);
  66. }
  67. template <typename T>
  68. void swap(backup_holder<T>& lhs, backup_holder<T>& rhs) BOOST_NOEXCEPT
  69. {
  70. lhs.swap(rhs);
  71. }
  72. }} // namespace detail::variant
  73. } // namespace boost
  74. #endif // BOOST_VARIANT_DETAIL_BACKUP_HOLDER_HPP