Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 10:08:48

0001 // DEPRECATED in favor of adl_postconstruct and adl_predestruct with
0002 // deconstruct<T>().
0003 // A factory function for creating a shared_ptr that enhances the plain
0004 // shared_ptr constructors by adding support for postconstructors
0005 // and predestructors through the boost::signals2::postconstructible and
0006 // boost::signals2::predestructible base classes.
0007 //
0008 // Copyright Frank Mori Hess 2007-2008.
0009 //
0010 // Use, modification and
0011 // distribution is subject to the Boost Software License, Version
0012 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
0013 // http://www.boost.org/LICENSE_1_0.txt)
0014 
0015 #ifndef BOOST_SIGNALS2_DECONSTRUCT_PTR_HPP
0016 #define BOOST_SIGNALS2_DECONSTRUCT_PTR_HPP
0017 
0018 #include <boost/assert.hpp>
0019 #include <boost/core/checked_delete.hpp>
0020 #include <boost/core/no_exceptions_support.hpp>
0021 #include <boost/signals2/postconstructible.hpp>
0022 #include <boost/signals2/predestructible.hpp>
0023 #include <boost/shared_ptr.hpp>
0024 
0025 namespace boost
0026 {
0027   namespace signals2
0028   {
0029     namespace detail
0030     {
0031       inline void do_postconstruct(const postconstructible *ptr)
0032       {
0033         postconstructible *nonconst_ptr = const_cast<postconstructible*>(ptr);
0034         nonconst_ptr->postconstruct();
0035       }
0036       inline void do_postconstruct(...)
0037       {
0038       }
0039       inline void do_predestruct(...)
0040       {
0041       }
0042       inline void do_predestruct(const predestructible *ptr)
0043       {
0044         BOOST_TRY
0045         {
0046           predestructible *nonconst_ptr = const_cast<predestructible*>(ptr);
0047           nonconst_ptr->predestruct();
0048         }
0049         BOOST_CATCH(...)
0050         {
0051           BOOST_ASSERT(false);
0052         }
0053         BOOST_CATCH_END
0054       }
0055     }
0056 
0057     template<typename T> class predestructing_deleter
0058     {
0059     public:
0060       void operator()(const T *ptr) const
0061       {
0062         detail::do_predestruct(ptr);
0063         checked_delete(ptr);
0064       }
0065     };
0066 
0067     template<typename T>
0068     shared_ptr<T> deconstruct_ptr(T *ptr)
0069     {
0070       if(ptr == 0) return shared_ptr<T>(ptr);
0071       shared_ptr<T> shared(ptr, boost::signals2::predestructing_deleter<T>());
0072       detail::do_postconstruct(ptr);
0073       return shared;
0074     }
0075     template<typename T, typename D>
0076     shared_ptr<T> deconstruct_ptr(T *ptr, D deleter)
0077     {
0078       shared_ptr<T> shared(ptr, deleter);
0079       if(ptr == 0) return shared;
0080       detail::do_postconstruct(ptr);
0081       return shared;
0082     }
0083   }
0084 }
0085 
0086 #endif // BOOST_SIGNALS2_DECONSTRUCT_PTR_HPP