#ifndef BOOST_SCOPED_PTR_HPP_INCLUDED #define BOOST_SCOPED_PTR_HPP_INCLUDED // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999. // Copyright (c) 2001, 2002 Peter Dimov // // 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) // // http://www.boost.org/libs/smart_ptr/scoped_ptr.htm // #include #include #include #ifndef BOOST_NO_AUTO_PTR # include // for std::auto_ptr #endif namespace boost { // Debug hooks #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS) void sp_scalar_constructor_hook(void * p); void sp_scalar_destructor_hook(void * p); #endif //default_deleter mimics the original behaviour of scoped_ptr //It uses boost::checked_delete to free the pointer and adds //no new functionality. The scoped_ptr remains the same size //as before (at least on Visual Studio 8) template class default_deleter { protected: typedef int delete_parm_type; //We don't care what type this is; we just need something default_deleter() {} //Protected constructor because this class to worthless on its own ~default_deleter() {} //Protected destructor to ensure scoped_ptr must be properly destroyed void policy_delete (T* ptr) { boost::checked_delete (ptr); } void swap(default_deleter & b) // never throws { } private: default_deleter (delete_parm_type); //Declare this private in an attempt to improve an error message //Declare these private to prevent copying default_deleter(default_deleter const &); default_deleter & operator=(default_deleter const &); }; //assignable_deleter adds an option to set a custom deleter for //a scoped_ptr. It adds the following member functions: // void set_deleter (delete_fn_type d): Causes the pointer to // use the specified deleter // void clear_deleter(): Causes the pointer to use the default // deleter (::boost::checked_delete ) // //It also accepts the two-parameter constructor and reset function //defined by scoped_ptr. The additional parameter is a pointer to //the deleter. // //The deleter is a global function of the form: // void delete (T* ptr); template class assignable_deleter { public: typedef void (*delete_fn_type) (T* ptr); void set_deleter (delete_fn_type d) { BOOST_ASSERT (d != 0); delete_fn = d; } void clear_deleter() { delete_fn = ::boost::checked_delete ; } protected: typedef delete_fn_type delete_parm_type; assignable_deleter(): delete_fn(::boost::checked_delete ) {} assignable_deleter (delete_parm_type d): delete_fn (d) {BOOST_ASSERT (d != 0);} ~assignable_deleter() {} void policy_delete (T* ptr) { delete_fn (ptr); //delete_fn WILL be valid, even if it defaults to ::boost::checked_delete } void swap(assignable_deleter & b) // never throws { //Swap the pointers' deleters delete_fn_type tmp = b.delete_fn; b.delete_fn = delete_fn; delete_fn = tmp; } private: delete_fn_type delete_fn; assignable_deleter(assignable_deleter const &); assignable_deleter & operator=(assignable_deleter const &); }; // scoped_ptr mimics a built-in pointer except that it guarantees deletion // of the object pointed to, either on destruction of the scoped_ptr or via // an explicit reset(). scoped_ptr is a simple solution for simple needs; // use shared_ptr or std::auto_ptr if your needs are more complex. template class deleter = ::boost::default_deleter > class scoped_ptr: public deleter // noncopyable { private: T * ptr; scoped_ptr(scoped_ptr const &); scoped_ptr & operator=(scoped_ptr const &); typedef scoped_ptr this_type; public: typedef T element_type; explicit scoped_ptr(T * p = 0): ptr(p) // never throws { #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS) boost::sp_scalar_constructor_hook(ptr); #endif } //This constructor takes whatever parameter the deleter policy will accept //Not all deleter policies will support this constructor. Those that don't raise a compiler error. //This relies on incomplete instantiation (the ability to declare a member function that may or may //not compile, depending on tempalte arguments). Do all compilers support incomplete instantiation? //Is there a macro that will indicate those that don't? scoped_ptr(T * p, typename deleter::delete_parm_type d): deleter (d), ptr(p) // never throws { #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS) boost::sp_scalar_constructor_hook(ptr); #endif } #ifndef BOOST_NO_AUTO_PTR explicit scoped_ptr(std::auto_ptr p): ptr(p.release()) // never throws { #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS) boost::sp_scalar_constructor_hook(ptr); #endif } #endif ~scoped_ptr() // never throws { #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS) boost::sp_scalar_destructor_hook(ptr); #endif policy_delete(ptr); } void reset(T * p = 0) // never throws { BOOST_ASSERT(p == 0 || p != ptr); // catch self-reset errors this_type(p).swap(*this); } //This reset function takes whatever parameter the deleter policy will accept //Not all deleter policies will support this constructor. Those that don't raise a compiler error. //This relies on incomplete instantiation (the ability to declare a member function that may or may //not compile, depending on tempalte arguments). Do all compilers support incomplete instantiation? //Is there a macro that will indicate those that don't? void reset(T * p, typename deleter::delete_parm_type d) // never throws { BOOST_ASSERT(p == 0 || p != ptr); // catch self-reset errors this_type(p, d).swap(*this); } T & operator*() const // never throws { BOOST_ASSERT(ptr != 0); return *ptr; } T * operator->() const // never throws { BOOST_ASSERT(ptr != 0); return ptr; } T * get() const // never throws { return ptr; } // implicit conversion to "bool" #if defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x530) operator bool () const { return ptr != 0; } #elif defined(__MWERKS__) && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003)) typedef T * (this_type::*unspecified_bool_type)() const; operator unspecified_bool_type() const // never throws { return ptr == 0? 0: &this_type::get; } #else typedef T * this_type::*unspecified_bool_type; operator unspecified_bool_type() const // never throws { return ptr == 0? 0: &this_type::ptr; } #endif bool operator! () const // never throws { return ptr == 0; } void swap(scoped_ptr & b) // never throws { T * tmp = b.ptr; b.ptr = ptr; ptr = tmp; deleter::swap (b); } }; template class D> inline void swap(scoped_ptr & a, scoped_ptr & b) // never throws { a.swap(b); } // get_pointer(p) is a generic way to say p.get() template class D> inline T * get_pointer(scoped_ptr const & p) { return p.get(); } } // namespace boost #endif // #ifndef BOOST_SCOPED_PTR_HPP_INCLUDED