auto_ptr.hpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #ifndef BOOST_CONTRACT_DETAIL_AUTO_PTR_HPP_
  2. #define BOOST_CONTRACT_DETAIL_AUTO_PTR_HPP_
  3. // Copyright (C) 2008-2018 Lorenzo Caminiti
  4. // Distributed under the Boost Software License, Version 1.0 (see accompanying
  5. // file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
  6. // See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html
  7. #include <boost/contract/detail/operator_safe_bool.hpp>
  8. #include <boost/contract/detail/debug.hpp>
  9. #include <boost/config.hpp>
  10. namespace boost { namespace contract { namespace detail {
  11. // Using this instead of std::auto_ptr because std::auto_ptr will be removed in
  12. // C++17 (this library always uses release() to avoid ownership issues).
  13. template<typename T>
  14. class auto_ptr { // Copyable (using default copy operations).
  15. public:
  16. explicit auto_ptr(T* ptr = 0) : ptr_(ptr) {}
  17. ~auto_ptr() BOOST_NOEXCEPT_IF(false) { delete ptr_; }
  18. T* release() {
  19. T* ptr = ptr_;
  20. ptr_ = 0;
  21. return ptr;
  22. }
  23. T& operator*() {
  24. BOOST_CONTRACT_DETAIL_DEBUG(ptr_);
  25. return *ptr_;
  26. }
  27. T const& operator*() const {
  28. BOOST_CONTRACT_DETAIL_DEBUG(ptr_);
  29. return *ptr_;
  30. }
  31. T* operator->() { return ptr_; }
  32. T const* operator->() const { return ptr_; }
  33. BOOST_CONTRACT_DETAIL_OPERATOR_SAFE_BOOL(auto_ptr<T>, !!ptr_)
  34. private:
  35. T* ptr_;
  36. };
  37. } } } // namespace
  38. #endif // #include guard