#ifndef BOOST_SMART_PTR_UNIQUE_PTR_HPP_INCLUDED #define BOOST_SMART_PTR_UNIQUE_PTR_HPP_INCLUDED // unique_ptr.hpp // // Copyright 2009 (C) Dean Michael Berris // // Distributed under the Boost Software License, Version 1.0 (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // // See http://www.boost.org/libs/smart_ptr/unique_ptr.html for documentation. // #include namespace boost { namespace detail { template struct unique_ptr_traits { typedef T & reference; }; template <> struct unique_ptr_traits { typedef void reference; }; } // namespace detail template class unique_ptr; template class unique_ptr { private: typedef unique_ptr this_type; unique_ptr(unique_ptr const &); public: typedef T element_type; typedef T value_type; typedef T * pointer; typedef typename boost::detail::unique_ptr_traits::reference reference; unique_ptr() : px(0) {} template explicit unique_ptr(Y* p) : px(p) {} unique_ptr(unique_ptr && o) : px(o.px) { o.px = 0; } ~unique_ptr () { if (px) delete px; } template explicit unique_ptr(Y && r) : px(&r) {} T * get() const { return px; } T * operator->() const { BOOST_ASSERT(px != 0); return *px; } reference operator * () const { BOOST_ASSERT(px != 0); return *px; } unique_ptr & operator=( unique_ptr && r) { this_type( std::move(r) ).swap(*this); return *this; } template unique_ptr & operator=(unique_ptr && r) { this_type( std::move(r) ).swap(*this); return *this; } void reset() { this_type().swap(*this); } template void reset( Y * p) { BOOST_ASSERT(p == 0 || p != px); this_type(p).swap(*this); } void swap(unique_ptr & other) { std::swap(px, other.px); other.px = 0; } private: pointer px; }; } #endif