35 #if __GNUC__ > 3 && __GNUC_MINOR__ > 6 36 #pragma GCC diagnostic push 37 #pragma GCC diagnostic ignored "-Wdelete-non-virtual-dtor" 41 #pragma clang diagnostic push 42 #pragma clang diagnostic ignored "-Wdelete-non-virtual-dtor" 45 #include "CLHEP/Utility/defs.h" 46 #include "CLHEP/Utility/noncopyable.h" 47 #include "CLHEP/Utility/type_traits.h" 66 template<
typename T >
class shared_ptr;
67 template<
typename T >
class weak_ptr;
68 template<
typename T >
class enable_shared_from_this;
69 template<
typename T >
class enable_shared_from_this2;
77 :
public std::exception
80 inline virtual char const *
what()
const throw();
85 bad_weak_ptr::
what() const throw()
87 return "bad_weak_ptr";
98 class abstract_ctrl_block
102 inline void class_invariant()
const throw();
105 inline abstract_ctrl_block();
106 inline virtual ~abstract_ctrl_block() throw();
109 inline
void add_ref();
110 inline
bool add_ref_lock();
111 inline
void weak_add_ref() throw();
113 inline
void release() throw();
114 inline
void weak_release() throw();
115 virtual
void dispose() throw() = 0;
116 inline virtual
void destroy() throw();
119 inline
long use_count() const throw();
129 abstract_ctrl_block::class_invariant() const throw()
131 assert( n_shared_ptrs == 0 || n_weak_ptrs >= 1 );
134 abstract_ctrl_block::abstract_ctrl_block()
141 abstract_ctrl_block::~abstract_ctrl_block() throw()
147 abstract_ctrl_block::add_ref()
154 abstract_ctrl_block::add_ref_lock()
157 return n_shared_ptrs ? ++n_shared_ptrs :
false;
161 abstract_ctrl_block::weak_add_ref() throw()
168 abstract_ctrl_block::release() throw()
171 if( 0 == --n_shared_ptrs )
172 dispose(), weak_release();
176 abstract_ctrl_block::weak_release() throw()
179 if( 0 == --n_weak_ptrs )
184 abstract_ctrl_block::destroy() throw()
186 assert( n_weak_ptrs == 0 );
191 abstract_ctrl_block::use_count()
const throw()
194 return n_shared_ptrs;
205 template<
typename P >
207 :
public abstract_ctrl_block
209 typedef ctrl_block_p<P> this_type;
212 inline explicit ctrl_block_p( P * );
213 inline ~ctrl_block_p() throw();
216 inline
void * operator new (
std::
size_t );
217 inline
void operator delete (
void * );
221 inline virtual
void dispose() throw();
229 template< typename P >
230 ctrl_block_p<P>::ctrl_block_p( P * p )
231 : abstract_ctrl_block( )
235 template<
typename P >
236 ctrl_block_p<P>::~ctrl_block_p() throw()
239 template<
typename P >
241 ctrl_block_p<P>::dispose() throw()
246 template<
typename P >
253 template<
typename P >
255 ctrl_block_p<P>::operator
new ( std::size_t )
257 return std::allocator<this_type>().allocate( 1 );
260 template<
typename P >
262 ctrl_block_p<P>::operator
delete (
void * p )
264 std::allocator<this_type>().deallocate( static_cast<this_type*>(p), 1 );
271 :
public abstract_ctrl_block
273 typedef ctrl_block_pd<P,D> this_type;
276 inline ctrl_block_pd( P *,
D );
277 inline ~ctrl_block_pd() throw();
280 inline
void * operator new (
std::
size_t );
281 inline
void operator delete (
void * );
285 inline virtual
void dispose() throw();
295 template< typename P, typename
D >
296 ctrl_block_pd<P,
D>::ctrl_block_pd( P * p,
D d )
297 : abstract_ctrl_block( )
302 template<
typename P,
typename D >
303 ctrl_block_pd<P,D>::~ctrl_block_pd() throw()
306 template<
typename P,
typename D >
308 ctrl_block_pd<P,D>::dispose() throw()
313 template<
typename P,
typename D >
317 return ti ==
typeid(
D) ? &reinterpret_cast<char&>(
deleter ) : 0;
320 template<
typename P,
typename D >
322 ctrl_block_pd<P,D>::operator
new ( std::size_t )
324 return std::allocator<this_type>().allocate( 1 );
327 template<
typename P,
typename D >
329 ctrl_block_pd<P,D>::operator
delete (
void * p )
331 std::allocator<this_type>().deallocate( static_cast<this_type*>(p), 1 );
339 :
public abstract_ctrl_block
341 typedef ctrl_block_pda<P,D,A> this_type;
344 inline ctrl_block_pda( P *, D, A );
345 inline ~ctrl_block_pda() throw();
349 inline virtual
void dispose() throw();
350 inline virtual
void destroy() throw();
361 template< typename P, typename D, typename A >
362 ctrl_block_pda<P,D,A>::ctrl_block_pda( P * p, D d, A
a )
363 : abstract_ctrl_block( )
369 template<
typename P,
typename D,
typename A >
370 ctrl_block_pda<P,D,A>::~ctrl_block_pda() throw()
373 template<
typename P,
typename D,
typename A >
375 ctrl_block_pda<P,D,A>::dispose() throw()
380 template<
typename P,
typename D,
typename A >
382 ctrl_block_pda<P,D,A>::destroy() throw()
384 typename A::template rebind< this_type >::other this_allocator( allocator );
386 this_allocator.destroy(
this );
387 this_allocator.deallocate(
this, 1 );
390 template<
typename P,
typename D,
typename A >
394 return ti ==
typeid( D ) ? &reinterpret_cast<char&>(
deleter ) : 0;
402 class shared_ctrl_handle;
403 class weak_ctrl_handle;
405 struct sp_nothrow_tag { };
407 class shared_ctrl_handle
409 friend class weak_ctrl_handle;
412 inline shared_ctrl_handle() throw();
413 template< typename P >
415 shared_ctrl_handle( P * );
416 template< typename P, typename D >
417 inline shared_ctrl_handle( P *, D );
418 template< typename P, typename D, typename A >
419 inline shared_ctrl_handle( P *, D, A );
420 template< typename P >
422 shared_ctrl_handle(
std::auto_ptr<P> & );
423 inline ~shared_ctrl_handle() throw();
426 inline
void swap( shared_ctrl_handle & ) throw();
427 inline shared_ctrl_handle( shared_ctrl_handle const & ) throw();
428 inline shared_ctrl_handle &
429 operator = ( shared_ctrl_handle const & ) throw();
433 shared_ctrl_handle( weak_ctrl_handle const & );
434 inline shared_ctrl_handle( weak_ctrl_handle const &, sp_nothrow_tag );
438 inline
bool unique() const throw();
439 inline
bool empty() const throw();
440 inline
long use_count() const throw();
445 operator == ( shared_ctrl_handle const &, shared_ctrl_handle const & );
448 operator < ( shared_ctrl_handle const &, shared_ctrl_handle const & );
452 abstract_ctrl_block * acb_ptr;
456 shared_ctrl_handle::shared_ctrl_handle() throw()
460 template<
typename P >
461 shared_ctrl_handle::shared_ctrl_handle( P * p )
467 acb_ptr =
new ctrl_block_p<P>(p);
475 template<
typename P,
typename D >
476 shared_ctrl_handle::shared_ctrl_handle( P * p, D d )
482 acb_ptr =
new ctrl_block_pd<P,D>(p, d);
490 template<
typename P,
typename D,
typename A >
491 shared_ctrl_handle::shared_ctrl_handle( P * p, D d, A
a )
494 typedef ctrl_block_pda<P,D,A>
496 typedef typename A::template rebind<ctrl_block>::other
497 ctrl_block_allocator;
498 ctrl_block_allocator cba( a );
502 acb_ptr = cba.allocate( 1 );
503 new(
static_cast<void*
>(acb_ptr) ) ctrl_block(p, d, a);
509 cba.deallocate( static_cast<ctrl_block*>( acb_ptr ), 1 );
514 template<
typename P >
515 shared_ctrl_handle::shared_ctrl_handle( std::auto_ptr<P> & p )
516 : acb_ptr( new ctrl_block_p<P>( p.get() ) )
521 shared_ctrl_handle::~shared_ctrl_handle() throw()
530 abstract_ctrl_block * tmp = other.acb_ptr;
531 other.acb_ptr = acb_ptr;
535 shared_ctrl_handle::shared_ctrl_handle( shared_ctrl_handle
const & other )
throw()
536 : acb_ptr( other.acb_ptr )
543 shared_ctrl_handle::operator = ( shared_ctrl_handle
const & other )
throw()
545 abstract_ctrl_block * tmp = other.acb_ptr;
549 if( tmp != 0 ) tmp->add_ref();
550 if( acb_ptr != 0 ) acb_ptr->release();
560 return acb_ptr ? acb_ptr->get_deleter( ti ) : 0;
564 shared_ctrl_handle::unique()
const throw()
566 return 1L == use_count();
570 shared_ctrl_handle::empty()
const throw()
576 shared_ctrl_handle::use_count()
const throw()
578 return acb_ptr == 0 ? 0L : acb_ptr->use_count();
582 operator == ( shared_ctrl_handle
const & lhs, shared_ctrl_handle
const & rhs )
584 return lhs.acb_ptr == rhs.acb_ptr;
588 operator < ( shared_ctrl_handle
const & lhs, shared_ctrl_handle
const & rhs )
590 return std::less<abstract_ctrl_block*>()( lhs.acb_ptr, rhs.acb_ptr );
593 class weak_ctrl_handle
595 friend class shared_ctrl_handle;
599 inline weak_ctrl_handle() throw();
600 inline weak_ctrl_handle( shared_ctrl_handle const & ) throw();
601 inline ~weak_ctrl_handle() throw();
604 inline
void swap( weak_ctrl_handle & ) throw();
605 inline weak_ctrl_handle( weak_ctrl_handle const & ) throw();
606 inline weak_ctrl_handle & operator = ( shared_ctrl_handle const & ) throw();
609 inline weak_ctrl_handle & operator = ( weak_ctrl_handle const & ) throw();
612 inline
bool empty() const throw();
613 inline
long use_count() const throw();
618 operator == ( weak_ctrl_handle const &, weak_ctrl_handle const & );
621 operator < ( weak_ctrl_handle const &, weak_ctrl_handle const & );
625 abstract_ctrl_block * acb_ptr;
629 weak_ctrl_handle::weak_ctrl_handle() throw()
633 weak_ctrl_handle::weak_ctrl_handle( shared_ctrl_handle
const & other )
throw()
634 : acb_ptr( other.acb_ptr )
637 acb_ptr->weak_add_ref();
640 weak_ctrl_handle::~weak_ctrl_handle() throw()
643 acb_ptr->weak_release();
649 abstract_ctrl_block * tmp = other.acb_ptr;
650 other.acb_ptr = acb_ptr;
654 weak_ctrl_handle::weak_ctrl_handle( weak_ctrl_handle
const & other )
throw()
655 : acb_ptr( other.acb_ptr )
658 acb_ptr->weak_add_ref();
662 weak_ctrl_handle::operator = ( shared_ctrl_handle
const & other )
throw()
664 abstract_ctrl_block * tmp = other.acb_ptr;
668 if( tmp != 0 ) tmp->weak_add_ref();
669 if( acb_ptr != 0 ) acb_ptr->weak_release();
677 weak_ctrl_handle::operator = ( weak_ctrl_handle
const & other )
throw()
679 abstract_ctrl_block * tmp = other.acb_ptr;
683 if( tmp != 0 ) tmp->weak_add_ref();
684 if( acb_ptr != 0 ) acb_ptr->weak_release();
692 weak_ctrl_handle::empty()
const throw()
698 weak_ctrl_handle::use_count()
const throw()
700 return acb_ptr == 0 ? 0L : acb_ptr->use_count();
704 operator == ( weak_ctrl_handle
const & lhs, weak_ctrl_handle
const & rhs )
706 return lhs.acb_ptr == rhs.acb_ptr;
710 operator < ( weak_ctrl_handle
const & lhs, weak_ctrl_handle
const & rhs )
712 return std::less<abstract_ctrl_block*>()( lhs.acb_ptr, rhs.acb_ptr );
715 shared_ctrl_handle::shared_ctrl_handle( weak_ctrl_handle
const & other )
716 : acb_ptr( other.acb_ptr )
718 if( acb_ptr == 0 || ! acb_ptr->add_ref_lock() )
719 throw bad_weak_ptr();
722 shared_ctrl_handle::shared_ctrl_handle( weak_ctrl_handle
const & other
724 : acb_ptr( other.acb_ptr )
726 if( acb_ptr != 0 && ! acb_ptr->add_ref_lock() )
735 struct static_cast_tag { };
736 struct const_cast_tag { };
737 struct dynamic_cast_tag { };
738 struct polymorphic_cast_tag { };
745 template<
typename T >
746 struct shared_ptr_traits
780 template<
typename X,
typename Y,
typename T >
788 pe->_internal_accept_owner( ppx, const_cast<Y*>( py ) );
791 template<
typename X,
typename Y,
typename T >
799 pe->_internal_accept_owner( ppx, const_cast<Y*>( py ) );
813 template<
typename P >
827 template< typename P2 >
830 template< typename P2, typename D >
832 template< typename P2, typename D, typename A >
840 template< typename P2 >
843 template< typename P2 >
845 template< typename P2 >
847 template< typename P2 >
849 template< typename P2 >
851 template< typename P2 >
853 template< typename P2 >
855 template< typename P2 >
858 template< typename AP >
863 template< typename P2 >
868 template< typename P2 >
870 template< typename P2 >
872 template< typename AP >
877 inline
void reset() throw();
878 template< typename P2 >
879 inline
void reset( P2 * );
880 template< typename P2, typename D >
881 inline
void reset( P2 *, D );
882 template< typename P2, typename D, typename A >
883 inline
void reset( P2 *, D, A );
884 template< typename P2 >
885 inline
void reset(
shared_ptr<P2> const &, P * );
888 inline operator
bool () const throw();
889 inline reference operator * () const throw();
890 inline P * operator -> () const throw();
893 inline P * get() const throw();
894 inline
bool unique() const throw();
895 inline
long use_count() const throw();
898 template< typename P2 >
899 inline
bool _internal_less(
shared_ptr<P2> const & ) const;
900 inline
void * _internal_get_deleter(
std::type_info const & ) const;
901 inline
bool _internal_equiv(
shared_ptr const & ) const;
906 sp::shared_ctrl_handle pn;
910 template< typename P, typename P2 >
912 template< typename P, typename P2 >
914 template< typename P, typename P2 >
917 template< typename P >
920 template< typename P, typename P2 >
922 template< typename P, typename P2 >
924 template< typename P, typename P2 >
927 template< typename P >
929 template< typename D, typename P >
932 template< typename C, typename T, typename P >
933 inline
std::basic_ostream<C,T> & operator << (
std::basic_ostream<C,T> &
937 template< typename P >
943 template<
typename P >
944 template<
typename P2 >
952 template<
typename P >
953 template<
typename P2,
typename D >
961 template<
typename P >
962 template<
typename P2,
typename D,
typename A >
970 template<
typename P >
978 template<
typename P >
982 this_type( other ).
swap( *
this );
986 template<
typename P >
987 template<
typename P2 >
995 template<
typename P >
996 template<
typename P2 >
1007 template<
typename P >
1008 template<
typename P2 >
1016 template<
typename P >
1017 template<
typename P2 >
1021 : px( static_cast<element_type*>( other.px ) )
1025 template<
typename P >
1026 template<
typename P2 >
1030 : px( const_cast<element_type*>( other.px ) )
1034 template<
typename P >
1035 template<
typename P2 >
1039 : px( dynamic_cast<element_type*>( other.px ) )
1046 template<
typename P >
1047 template<
typename P2 >
1051 : px( dynamic_cast<element_type*>( other.px ) )
1055 throw std::bad_cast();
1058 template<
typename P >
1059 template<
typename P2 >
1064 P2 * tmp = other.get();
1069 template<
typename P >
1070 template<
typename AP >
1077 typename AP::element_type * tmp = other.get();
1082 template<
typename P >
1083 template<
typename P2 >
1091 template<
typename P >
1092 template<
typename P2 >
1096 this_type( other ).
swap( *
this );
1100 template<
typename P >
1101 template<
typename P2 >
1105 this_type( other ).
swap( *
this );
1109 template<
typename P >
1110 template<
typename AP >
1114 this_type( other ).
swap( *
this );
1118 template<
typename P >
1122 this_type().
swap( *
this );
1125 template<
typename P >
1126 template<
typename P2 >
1130 assert( p == 0 || p != px );
1131 this_type( p ).
swap( *
this );
1134 template<
typename P >
1135 template<
typename P2,
typename D >
1139 this_type( p, d ).
swap( *
this );
1142 template<
typename P >
1143 template<
typename P2,
typename D,
typename A >
1147 this_type( p, d, a ).
swap( *
this );
1150 template<
typename P >
1151 template<
typename P2 >
1155 this_type( other, p ).
swap( *
this );
1158 template<
typename P >
1164 template<
typename P >
1173 template<
typename P >
1181 template<
typename P >
1188 template<
typename P >
1195 template<
typename P >
1202 template<
typename P >
1203 template<
typename P2 >
1210 template<
typename P >
1214 return pn.get_deleter( ti );
1217 template<
typename P >
1221 return px == other.px && pn == other.pn;
1224 template<
typename P,
typename P2 >
1228 return a.
get() == b.
get();
1231 template<
typename P,
typename P2 >
1235 return a.
get() != b.
get();
1238 template<
typename P,
typename P2 >
1242 return a._internal_less(b);
1245 template<
typename P >
1252 template<
typename P,
typename P2 >
1259 template<
typename P,
typename P2 >
1266 template<
typename P,
typename P2 >
1273 template<
typename P >
1280 template<
typename D,
typename P >
1287 template<
typename C,
typename T,
typename P >
1288 std::basic_ostream<C,T> &
1289 operator << ( std::basic_ostream<C,T> & os,
shared_ptr<P> const & p )
1300 template<
typename P >
1306 template<
typename >
friend class weak_ptr;
1315 inline
void swap( this_type & other ) throw();
1316 template< typename P2 >
1321 template< typename P2 >
1326 template< typename P2 >
1328 template< typename P2 >
1333 inline
long use_count() const throw();
1334 inline
bool expired() const throw();
1335 inline
bool _empty() const;
1336 inline
void reset() throw();
1339 inline
void _internal_assign( P * px2, sp::shared_ctrl_handle const & pn2 );
1340 template< typename P2 >
1341 inline
bool _internal_less(
weak_ptr<P2> const & rhs ) const;
1345 sp::weak_ctrl_handle pn;
1349 template< typename P, typename P2 >
1352 template< typename P >
1355 template< typename P >
1361 template<
typename P >
1362 template<
typename P2 >
1366 : px( r.
lock().get() )
1370 template<
typename P >
1371 template<
typename P2 >
1379 template<
typename P >
1380 template<
typename P2 >
1384 px = r.
lock().get();
1389 template<
typename P >
1390 template<
typename P2 >
1399 template<
typename P >
1406 template<
typename P >
1413 template<
typename P >
1420 template<
typename P >
1427 template<
typename P >
1431 this_type().
swap(*
this);
1434 template<
typename P >
1442 template<
typename P >
1450 template<
typename P >
1451 template<
typename P2 >
1458 template<
typename P,
typename P2 >
1462 return a._internal_less(b);
1465 template<
typename P >
1478 inline void operator () (
void const * )
const;
1482 do_nothing_deleter::operator () (
void const * )
const 1489 #if defined __GNUC__ 1490 #if __GNUC__ > 3 && __GNUC_MINOR__ > 6 1491 #pragma GCC diagnostic pop 1495 #pragma clang diagnostic pop 1500 #endif // CLHEP_MEMORY_H 1509 template<
typename T >
1533 assert( p.
get() == this );
1538 shared_from_this()
const 1541 assert( p.
get() == this );
1548 template<
typename X,
typename Y >
1550 _internal_accept_owner(
shared_ptr<X> const * ppx,
Y * py )
const 1552 if( weak_this_.expired() )
1566 class esft2_deleter_wrapper
1572 esft2_deleter_wrapper()
1575 template<
typename T >
1582 template<
typename T >
1593 template<
typename T >
1611 assert( shared_this_.use_count() <= 1 );
1628 shared_from_this()
const 1636 void init_weak_once()
const 1638 if( weak_this_.
_empty() )
1640 shared_this_.
reset( static_cast< T* >( 0 )
1641 , detail::esft2_deleter_wrapper()
1643 weak_this_ = shared_this_;
1650 template<
typename X,
typename Y >
1658 else if( shared_this_.
use_count() != 0 )
1660 assert( ppx->unique() );
1662 detail::esft2_deleter_wrapper * pd
1663 = boost::get_deleter<detail::esft2_deleter_wrapper>( shared_this_ );
1666 pd->set_deleter( *ppx );
1668 ppx->reset( shared_this_, ppx->get() );
1669 shared_this_.
reset();
shared_ptr< P > dynamic_pointer_cast(shared_ptr< P2 > const &)
shared_ptr< P > lock() const
bool operator<(const HepRotation &r, const HepLorentzRotation <)
void * _internal_get_deleter(std::type_info const &) const
void sp_enable_shared_from_this(shared_ptr< X > const *ppx, Y const *py, enable_shared_from_this< T > const *pe)
P * get_pointer(shared_ptr< P > const &)
void swap(shared_ptr< P > &)
void swap(this_type &other)
shared_ptr< P > const_pointer_cast(shared_ptr< P2 > const &)
bool operator!=(const HepRotation &r, const HepLorentzRotation <)
D * get_deleter(shared_ptr< P > const &)
shared_ptr< P > static_pointer_cast(shared_ptr< P2 > const &)
virtual char const * what() const
void swap(shared_ptr< P > &, shared_ptr< P > &)
bool operator==(const HepRotation &r, const HepLorentzRotation <)
Product operator*(const Function &op1, const Function &op2)