Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:51:40

0001 
0002 //  helper code for dealing with tracking non-boost shared_ptr/weak_ptr
0003 
0004 // Copyright Frank Mori Hess 2009.
0005 // Distributed under the Boost Software License, Version
0006 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
0007 // http://www.boost.org/LICENSE_1_0.txt)
0008 
0009 // See http://www.boost.org/libs/signals2 for library home page.
0010 
0011 #ifndef BOOST_SIGNALS2_FOREIGN_PTR_HPP
0012 #define BOOST_SIGNALS2_FOREIGN_PTR_HPP
0013 
0014 #include <algorithm>
0015 #include <boost/config.hpp>
0016 #include <boost/core/invoke_swap.hpp>
0017 #include <boost/assert.hpp>
0018 #include <boost/scoped_ptr.hpp>
0019 #include <boost/smart_ptr/bad_weak_ptr.hpp>
0020 
0021 #ifndef BOOST_NO_CXX11_SMART_PTR
0022 #include <memory>
0023 #endif
0024 
0025 namespace boost
0026 {
0027   template<typename T> class shared_ptr;
0028   template<typename T> class weak_ptr;
0029 
0030   namespace signals2
0031   {
0032     template<typename WeakPtr> struct weak_ptr_traits
0033     {};
0034     template<typename T> struct weak_ptr_traits<boost::weak_ptr<T> >
0035     {
0036       typedef boost::shared_ptr<T> shared_type;
0037     };
0038 #ifndef BOOST_NO_CXX11_SMART_PTR
0039     template<typename T> struct weak_ptr_traits<std::weak_ptr<T> >
0040     {
0041       typedef std::shared_ptr<T> shared_type;
0042     };
0043 #endif
0044 
0045     template<typename SharedPtr> struct shared_ptr_traits
0046     {};
0047 
0048     template<typename T> struct shared_ptr_traits<boost::shared_ptr<T> >
0049     {
0050       typedef boost::weak_ptr<T> weak_type;
0051     };
0052 #ifndef BOOST_NO_CXX11_SMART_PTR
0053     template<typename T> struct shared_ptr_traits<std::shared_ptr<T> >
0054     {
0055       typedef std::weak_ptr<T> weak_type;
0056     };
0057 #endif
0058 
0059     namespace detail
0060     {
0061       struct foreign_shared_ptr_impl_base
0062       {
0063         virtual ~foreign_shared_ptr_impl_base() {}
0064         virtual foreign_shared_ptr_impl_base * clone() const = 0;
0065       };
0066 
0067       template<typename FSP>
0068       class foreign_shared_ptr_impl: public foreign_shared_ptr_impl_base
0069       {
0070       public:
0071         foreign_shared_ptr_impl(const FSP &p): _p(p)
0072         {}
0073         virtual foreign_shared_ptr_impl * clone() const
0074         {
0075           return new foreign_shared_ptr_impl(*this);
0076         }
0077       private:
0078         FSP _p;
0079       };
0080 
0081       class foreign_void_shared_ptr
0082       {
0083       public:
0084         foreign_void_shared_ptr():
0085           _p(0)
0086         {}
0087         foreign_void_shared_ptr(const foreign_void_shared_ptr &other):
0088           _p(other._p->clone())
0089         {}
0090         template<typename FSP>
0091         explicit foreign_void_shared_ptr(const FSP &fsp):
0092           _p(new foreign_shared_ptr_impl<FSP>(fsp))
0093         {}
0094         ~foreign_void_shared_ptr()
0095         {
0096           delete _p;
0097         }
0098         foreign_void_shared_ptr & operator=(const foreign_void_shared_ptr &other)
0099         {
0100           if(&other == this) return *this;
0101           foreign_void_shared_ptr(other).swap(*this);
0102           return *this;
0103         }
0104         void swap(foreign_void_shared_ptr &other)
0105         {
0106           boost::core::invoke_swap(_p, other._p);
0107         }
0108       private:
0109         foreign_shared_ptr_impl_base *_p;
0110       };
0111 
0112       struct foreign_weak_ptr_impl_base
0113       {
0114         virtual ~foreign_weak_ptr_impl_base() {}
0115         virtual foreign_void_shared_ptr lock() const = 0;
0116         virtual bool expired() const = 0;
0117         virtual foreign_weak_ptr_impl_base * clone() const = 0;
0118       };
0119 
0120       template<typename FWP>
0121       class foreign_weak_ptr_impl: public foreign_weak_ptr_impl_base
0122       {
0123       public:
0124         foreign_weak_ptr_impl(const FWP &p): _p(p)
0125         {}
0126         virtual foreign_void_shared_ptr lock() const
0127         {
0128           return foreign_void_shared_ptr(_p.lock());
0129         }
0130         virtual bool expired() const
0131         {
0132           return _p.expired();
0133         }
0134         virtual foreign_weak_ptr_impl * clone() const
0135         {
0136           return new foreign_weak_ptr_impl(*this);
0137         }
0138       private:
0139         FWP _p;
0140       };
0141 
0142       class foreign_void_weak_ptr
0143       {
0144       public:
0145         foreign_void_weak_ptr()
0146         {}
0147         foreign_void_weak_ptr(const foreign_void_weak_ptr &other):
0148           _p(other._p->clone())
0149         {}
0150         template<typename FWP>
0151         explicit foreign_void_weak_ptr(const FWP &fwp):
0152           _p(new foreign_weak_ptr_impl<FWP>(fwp))
0153         {}
0154         foreign_void_weak_ptr & operator=(const foreign_void_weak_ptr &other)
0155         {
0156           if(&other == this) return *this;
0157           foreign_void_weak_ptr(other).swap(*this);
0158           return *this;
0159         }
0160         void swap(foreign_void_weak_ptr &other)
0161         {
0162           boost::core::invoke_swap(_p, other._p);
0163         }
0164         foreign_void_shared_ptr lock() const
0165         {
0166           return _p->lock();
0167         }
0168         bool expired() const
0169         {
0170           return _p->expired();
0171         }
0172       private:
0173         boost::scoped_ptr<foreign_weak_ptr_impl_base> _p;
0174       };
0175     } // namespace detail
0176 
0177   } // namespace signals2
0178 } // namespace boost
0179 
0180 #endif  // BOOST_SIGNALS2_FOREIGN_PTR_HPP