123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- #ifndef BOOST_SIGNALS2_DECONSTRUCT_PTR_HPP
- #define BOOST_SIGNALS2_DECONSTRUCT_PTR_HPP
- #include <boost/assert.hpp>
- #include <boost/checked_delete.hpp>
- #include <boost/signals2/postconstructible.hpp>
- #include <boost/signals2/predestructible.hpp>
- #include <boost/shared_ptr.hpp>
- namespace boost
- {
- namespace signals2
- {
- namespace detail
- {
- inline void do_postconstruct(const postconstructible *ptr)
- {
- postconstructible *nonconst_ptr = const_cast<postconstructible*>(ptr);
- nonconst_ptr->postconstruct();
- }
- inline void do_postconstruct(...)
- {
- }
- inline void do_predestruct(...)
- {
- }
- inline void do_predestruct(const predestructible *ptr)
- {
- try
- {
- predestructible *nonconst_ptr = const_cast<predestructible*>(ptr);
- nonconst_ptr->predestruct();
- }
- catch(...)
- {
- BOOST_ASSERT(false);
- }
- }
- }
- template<typename T> class predestructing_deleter
- {
- public:
- void operator()(const T *ptr) const
- {
- detail::do_predestruct(ptr);
- checked_delete(ptr);
- }
- };
- template<typename T>
- shared_ptr<T> deconstruct_ptr(T *ptr)
- {
- if(ptr == 0) return shared_ptr<T>(ptr);
- shared_ptr<T> shared(ptr, boost::signals2::predestructing_deleter<T>());
- detail::do_postconstruct(ptr);
- return shared;
- }
- template<typename T, typename D>
- shared_ptr<T> deconstruct_ptr(T *ptr, D deleter)
- {
- shared_ptr<T> shared(ptr, deleter);
- if(ptr == 0) return shared;
- detail::do_postconstruct(ptr);
- return shared;
- }
- }
- }
- #endif
|