34 #include "CLHEP/Utility/defs.h"
35 #include "CLHEP/Utility/noncopyable.h"
36 #include "CLHEP/Utility/type_traits.h"
55 template<
typename T >
class shared_ptr;
56 template<
typename T >
class weak_ptr;
57 template<
typename T >
class enable_shared_from_this;
58 template<
typename T >
class enable_shared_from_this2;
66 :
public std::exception
69 inline virtual char const *
what()
const throw();
76 return "bad_weak_ptr";
101 virtual void *
get_deleter( std::type_info
const & ti ) = 0;
104 virtual void dispose()
throw() = 0;
105 inline virtual void destroy()
throw();
120 assert( n_shared_ptrs == 0 || n_weak_ptrs >= 1 );
146 return n_shared_ptrs ? ++n_shared_ptrs :
false;
160 if( 0 == --n_shared_ptrs )
168 if( 0 == --n_weak_ptrs )
175 assert( n_weak_ptrs == 0 );
183 return n_shared_ptrs;
194 template<
typename P >
205 inline void *
operator new ( std::size_t );
206 inline void operator delete (
void * );
209 inline virtual void *
get_deleter( std::type_info
const & );
210 inline virtual void dispose()
throw();
218 template<
typename P >
224 template<
typename P >
228 template<
typename P >
235 template<
typename P >
242 template<
typename P >
246 return std::allocator<this_type>().allocate( 1 );
249 template<
typename P >
253 std::allocator<this_type>().deallocate( static_cast<this_type*>(p), 1 );
269 inline void *
operator new ( std::size_t );
270 inline void operator delete (
void * );
273 inline virtual void *
get_deleter( std::type_info
const & );
274 inline virtual void dispose()
throw();
284 template<
typename P,
typename D >
291 template<
typename P,
typename D >
295 template<
typename P,
typename D >
302 template<
typename P,
typename D >
306 return ti ==
typeid(
D) ? &reinterpret_cast<char&>(
deleter ) : 0;
309 template<
typename P,
typename D >
313 return std::allocator<this_type>().allocate( 1 );
316 template<
typename P,
typename D >
320 std::allocator<this_type>().deallocate( static_cast<this_type*>(p), 1 );
337 inline virtual void *
get_deleter( std::type_info
const & );
338 inline virtual void dispose()
throw();
339 inline virtual void destroy()
throw();
350 template<
typename P,
typename D,
typename A >
358 template<
typename P,
typename D,
typename A >
362 template<
typename P,
typename D,
typename A >
369 template<
typename P,
typename D,
typename A >
373 typename A::template rebind< this_type >::other this_allocator( allocator );
375 this_allocator.destroy(
this );
376 this_allocator.deallocate(
this, 1 );
379 template<
typename P,
typename D,
typename A >
383 return ti ==
typeid(
D ) ? &reinterpret_cast<char&>(
deleter ) : 0;
402 template< typename P >
405 template< typename P, typename
D >
407 template< typename P, typename D, typename A >
409 template< typename P >
426 inline
void *
get_deleter( std::type_info const & ) const;
427 inline
bool unique() const throw();
428 inline
bool empty() const throw();
449 template<
typename P >
464 template<
typename P,
typename D >
479 template<
typename P,
typename D,
typename A >
485 typedef typename A::template rebind<ctrl_block>::other
486 ctrl_block_allocator;
487 ctrl_block_allocator cba( a );
491 acb_ptr = cba.allocate( 1 );
492 new(
static_cast<void*
>(acb_ptr) ) ctrl_block(p, d, a);
498 cba.deallocate( static_cast<ctrl_block*>( acb_ptr ), 1 );
503 template<
typename P >
520 other.acb_ptr = acb_ptr;
525 : acb_ptr( other.acb_ptr )
539 if( acb_ptr != 0 ) acb_ptr->release();
567 return acb_ptr == 0 ? 0L : acb_ptr->
use_count();
573 return lhs.acb_ptr == rhs.acb_ptr;
579 return std::less<abstract_ctrl_block*>()( lhs.acb_ptr, rhs.acb_ptr );
601 inline
bool empty() const throw();
623 : acb_ptr( other.acb_ptr )
639 other.acb_ptr = acb_ptr;
644 : acb_ptr( other.acb_ptr )
689 return acb_ptr == 0 ? 0L : acb_ptr->
use_count();
695 return lhs.acb_ptr == rhs.acb_ptr;
701 return std::less<abstract_ctrl_block*>()( lhs.acb_ptr, rhs.acb_ptr );
705 : acb_ptr( other.acb_ptr )
713 : acb_ptr( other.acb_ptr )
734 template<
typename T >
769 template<
typename X,
typename Y,
typename T >
773 , enable_shared_from_this<T>
const * pe
777 pe->_internal_accept_owner( ppx, const_cast<Y*>( py ) );
780 template<
typename X,
typename Y,
typename T >
784 , enable_shared_from_this2<T>
const * pe
788 pe->_internal_accept_owner( ppx, const_cast<Y*>( py ) );
802 template<
typename P >
816 template< typename P2 >
819 template< typename P2, typename
D >
821 template< typename P2, typename D, typename A >
829 template< typename P2 >
832 template< typename P2 >
834 template< typename P2 >
836 template< typename P2 >
838 template< typename P2 >
840 template< typename P2 >
842 template< typename P2 >
844 template< typename P2 >
847 template< typename AP >
852 template< typename P2 >
857 template< typename P2 >
859 template< typename P2 >
860 inline
shared_ptr & operator = ( std::auto_ptr<P2> & );
861 template< typename AP >
866 inline
void reset() throw();
867 template< typename P2 >
868 inline
void reset( P2 * );
869 template< typename P2, typename D >
870 inline
void reset( P2 *, D );
871 template< typename P2, typename D, typename A >
872 inline
void reset( P2 *, D, A );
873 template< typename P2 >
877 inline operator
bool () const throw();
878 inline
reference operator * () const throw();
879 inline P * operator -> () const throw();
882 inline P *
get() const throw();
883 inline
bool unique() const throw();
887 template< typename P2 >
895 sp::shared_ctrl_handle pn;
899 template< typename P, typename P2 >
901 template< typename P, typename P2 >
903 template< typename P, typename P2 >
906 template< typename P >
909 template< typename P, typename P2 >
911 template< typename P, typename P2 >
913 template< typename P, typename P2 >
916 template< typename P >
918 template< typename D, typename P >
921 template< typename C, typename T, typename P >
922 inline std::basic_ostream<C,T> & operator << ( std::basic_ostream<C,T> &
926 template< typename P >
932 template<
typename P >
933 template<
typename P2 >
941 template<
typename P >
942 template<
typename P2,
typename D >
950 template<
typename P >
951 template<
typename P2,
typename D,
typename A >
959 template<
typename P >
967 template<
typename P >
975 template<
typename P >
976 template<
typename P2 >
984 template<
typename P >
985 template<
typename P2 >
990 , pn( other.pn, sp::sp_nothrow_tag() )
996 template<
typename P >
997 template<
typename P2 >
1005 template<
typename P >
1006 template<
typename P2 >
1014 template<
typename P >
1015 template<
typename P2 >
1023 template<
typename P >
1024 template<
typename P2 >
1035 template<
typename P >
1036 template<
typename P2 >
1044 throw std::bad_cast();
1047 template<
typename P >
1048 template<
typename P2 >
1053 P2 * tmp = other.get();
1058 template<
typename P >
1059 template<
typename AP >
1066 typename AP::element_type * tmp = other.get();
1071 template<
typename P >
1072 template<
typename P2 >
1080 template<
typename P >
1081 template<
typename P2 >
1089 template<
typename P >
1090 template<
typename P2 >
1098 template<
typename P >
1099 template<
typename AP >
1107 template<
typename P >
1114 template<
typename P >
1115 template<
typename P2 >
1119 assert( p == 0 || p != px );
1123 template<
typename P >
1124 template<
typename P2,
typename D >
1131 template<
typename P >
1132 template<
typename P2,
typename D,
typename A >
1139 template<
typename P >
1140 template<
typename P2 >
1147 template<
typename P >
1153 template<
typename P >
1162 template<
typename P >
1170 template<
typename P >
1177 template<
typename P >
1184 template<
typename P >
1188 return pn.use_count();
1191 template<
typename P >
1192 template<
typename P2 >
1199 template<
typename P >
1203 return pn.get_deleter( ti );
1206 template<
typename P >
1210 return px == other.px && pn == other.pn;
1213 template<
typename P,
typename P2 >
1217 return a.
get() == b.
get();
1220 template<
typename P,
typename P2 >
1224 return a.
get() != b.
get();
1227 template<
typename P,
typename P2 >
1231 return a._internal_less(
b);
1234 template<
typename P >
1241 template<
typename P,
typename P2 >
1248 template<
typename P,
typename P2 >
1255 template<
typename P,
typename P2 >
1262 template<
typename P >
1269 template<
typename D,
typename P >
1276 template<
typename C,
typename T,
typename P >
1277 std::basic_ostream<C,T> &
1289 template<
typename P >
1305 template< typename P2 >
1310 template< typename P2 >
1315 template< typename P2 >
1317 template< typename P2 >
1323 inline
bool expired() const throw();
1324 inline
bool _empty() const;
1325 inline
void reset() throw();
1328 inline
void _internal_assign( P * px2, sp::shared_ctrl_handle const & pn2 );
1329 template< typename P2 >
1334 sp::weak_ctrl_handle pn;
1338 template< typename P, typename P2 >
1341 template< typename P >
1344 template< typename P >
1350 template<
typename P >
1351 template<
typename P2 >
1355 : px( r.
lock().get() )
1359 template<
typename P >
1360 template<
typename P2 >
1368 template<
typename P >
1369 template<
typename P2 >
1373 px = r.lock().get();
1378 template<
typename P >
1379 template<
typename P2 >
1388 template<
typename P >
1395 template<
typename P >
1402 template<
typename P >
1409 template<
typename P >
1416 template<
typename P >
1423 template<
typename P >
1431 template<
typename P >
1439 template<
typename P >
1440 template<
typename P2 >
1447 template<
typename P,
typename P2 >
1451 return a._internal_less(
b);
1454 template<
typename P >
1467 inline void operator () (
void const * )
const;
1480 #endif // CLHEP_MEMORY_H
1489 template<
typename T >
1490 class enable_shared_from_this
1493 enable_shared_from_this()
1496 ~enable_shared_from_this()
1499 enable_shared_from_this( enable_shared_from_this
const & )
1502 enable_shared_from_this &
1503 operator = ( enable_shared_from_this
const & )
1512 shared_ptr<T> p( weak_this_ );
1513 assert( p.get() == this );
1518 shared_from_this()
const
1520 shared_ptr<T const> p( weak_this_ );
1521 assert( p.get() == this );
1528 template<
typename X,
typename Y >
1530 _internal_accept_owner( shared_ptr<X>
const * ppx,
Y * py )
const
1532 if( weak_this_.expired() )
1533 weak_this_ = shared_ptr<T>( *ppx, py );
1537 mutable weak_ptr<T> weak_this_;
1546 class esft2_deleter_wrapper
1549 shared_ptr<void> deleter_;
1552 esft2_deleter_wrapper()
1555 template<
typename T >
1557 set_deleter( shared_ptr<T>
const &
deleter )
1562 template<
typename T >
1566 assert( deleter_.use_count() <= 1 );
1573 template<
typename T >
1574 class enable_shared_from_this2
1578 enable_shared_from_this2()
1581 enable_shared_from_this2( enable_shared_from_this2
const & )
1584 enable_shared_from_this2 & operator = ( enable_shared_from_this2
const & )
1589 ~enable_shared_from_this2()
1591 assert( shared_this_.use_count() <= 1 );
1595 mutable weak_ptr<T> weak_this_;
1596 mutable shared_ptr<T> shared_this_;
1604 return shared_ptr<T>( weak_this_ );
1608 shared_from_this()
const
1611 return shared_ptr<T>( weak_this_ );
1616 void init_weak_once()
const
1618 if( weak_this_._empty() )
1620 shared_this_.reset( static_cast< T* >( 0 )
1621 , detail::esft2_deleter_wrapper()
1623 weak_this_ = shared_this_;
1630 template<
typename X,
typename Y >
1632 _internal_accept_owner( shared_ptr<X> * ppx,
Y * py )
const
1636 if( weak_this_.use_count() == 0 )
1637 weak_this_ = shared_ptr<T>( *ppx, py );
1638 else if( shared_this_.use_count() != 0 )
1640 assert( ppx->unique() );
1642 detail::esft2_deleter_wrapper * pd
1643 = boost::get_deleter<detail::esft2_deleter_wrapper>( shared_this_ );
1646 pd->set_deleter( *ppx );
1648 ppx->reset( shared_this_, ppx->get() );
1649 shared_this_.reset();