|
||||
File indexing completed on 2025-01-18 09:58:39
0001 // 0002 // ******************************************************************** 0003 // * License and Disclaimer * 0004 // * * 0005 // * The Geant4 software is copyright of the Copyright Holders of * 0006 // * the Geant4 Collaboration. It is provided under the terms and * 0007 // * conditions of the Geant4 Software License, included in the file * 0008 // * LICENSE and available at http://cern.ch/geant4/license . These * 0009 // * include a list of copyright holders. * 0010 // * * 0011 // * Neither the authors of this software system, nor their employing * 0012 // * institutes,nor the agencies providing financial support for this * 0013 // * work make any representation or warranty, express or implied, * 0014 // * regarding this software system or assume any liability for its * 0015 // * use. Please see the license in the file LICENSE and URL above * 0016 // * for the full disclaimer and the limitation of liability. * 0017 // * * 0018 // * This code implementation is the result of the scientific and * 0019 // * technical work of the GEANT4 collaboration. * 0020 // * By using, copying, modifying or distributing the software (or * 0021 // * any work based on the software) you agree to acknowledge its * 0022 // * use in resulting scientific publications, and indicate your * 0023 // * acceptance of all terms of the Geant4 Software license. * 0024 // ******************************************************************** 0025 // 0026 #ifndef G4SHARED_PTR_HH_ 0027 #define G4SHARED_PTR_HH_ 0028 0029 //#if __cplusplus >= 201103L 0030 #include <memory> 0031 0032 #define G4shared_ptr std::shared_ptr 0033 #define G4weak_ptr std::weak_ptr 0034 #define G4static_pointer_cast std::static_pointer_cast 0035 #define G4const_pointer_cast std::const_pointer_cast 0036 #define G4dynamic_pointer_cast std::dynamic_pointer_cast 0037 #define G4enable_shared_from_this std::enable_shared_from_this 0038 #define G4enable_shared_from_this2 std::enable_shared_from_this 0039 /* 0040 #else 0041 #include "CLHEP/Utility/memory.h" 0042 0043 #define G4shared_ptr G4::shared_ptr 0044 #define G4weak_ptr G4::weak_ptr 0045 #define G4static_pointer_cast G4::static_pointer_cast 0046 #define G4const_pointer_cast G4::const_pointer_cast 0047 #define G4dynamic_pointer_cast G4::dynamic_pointer_cast 0048 #define G4enable_shared_from_this G4::enable_shared_from_this 0049 #define G4enable_shared_from_this2 G4::enable_shared_from_this2 0050 0051 namespace G4 0052 { 0053 using CLHEP::shared_ptr; 0054 using CLHEP::weak_ptr; 0055 using CLHEP::static_pointer_cast; 0056 using CLHEP::const_pointer_cast; 0057 using CLHEP::dynamic_pointer_cast; 0058 } 0059 0060 namespace CLHEP 0061 { 0062 0063 template< typename T > 0064 class enable_shared_from_this 0065 { 0066 protected: 0067 enable_shared_from_this() 0068 { } 0069 0070 ~enable_shared_from_this() 0071 { } 0072 0073 enable_shared_from_this( enable_shared_from_this const & ) 0074 { } 0075 0076 enable_shared_from_this & 0077 operator = ( enable_shared_from_this const & ) 0078 { 0079 return *this; 0080 } 0081 0082 public: 0083 shared_ptr<T> 0084 shared_from_this() 0085 { 0086 shared_ptr<T> p( weak_this_ ); 0087 // assert( p.get() == this ); 0088 return p; 0089 } 0090 0091 shared_ptr<T const> 0092 shared_from_this() const 0093 { 0094 shared_ptr<T const> p( weak_this_ ); 0095 // assert( p.get() == this ); 0096 return p; 0097 } 0098 0099 public: // actually private, but avoids compiler template friendship issues 0100 0101 // Note: invoked automatically by shared_ptr; do not call 0102 template< typename X, typename Y > 0103 void 0104 _internal_accept_owner( shared_ptr<X> const * ppx, Y * py ) const 0105 { 0106 if( weak_this_.expired() ) 0107 weak_this_ = shared_ptr<T>( *ppx, py ); 0108 } 0109 0110 private: 0111 mutable weak_ptr<T> weak_this_; 0112 }; // enable_shared_from_this<> 0113 0114 0115 // enable_shared_from_this2.hpp 0116 0117 namespace detail 0118 { 0119 0120 class esft2_deleter_wrapper 0121 { 0122 private: 0123 shared_ptr<void> deleter_; 0124 0125 public: 0126 esft2_deleter_wrapper(){ } 0127 0128 template< typename T > 0129 void 0130 set_deleter( shared_ptr<T> const & deleter ) 0131 { 0132 deleter_ = deleter; 0133 } 0134 0135 template< typename T > 0136 void 0137 operator () ( T* ) 0138 { 0139 assert( deleter_.use_count() <= 1 ); 0140 deleter_.reset(); 0141 } 0142 }; 0143 0144 } // namespace detail 0145 0146 template< typename T > 0147 class enable_shared_from_this2 0148 { 0149 protected: 0150 0151 enable_shared_from_this2(){ } 0152 0153 enable_shared_from_this2( enable_shared_from_this2 const & ) 0154 { } 0155 0156 enable_shared_from_this2 & operator = ( enable_shared_from_this2 const & ) 0157 { 0158 return *this; 0159 } 0160 0161 ~enable_shared_from_this2() 0162 { 0163 assert( shared_this_.use_count() <= 1 ); // ensure no dangling shared_ptrs 0164 } 0165 0166 private: 0167 mutable weak_ptr<T> weak_this_; 0168 mutable shared_ptr<T> shared_this_; 0169 0170 public: 0171 0172 shared_ptr<T> 0173 shared_from_this() 0174 { 0175 init_weak_once(); 0176 return shared_ptr<T>( weak_this_ ); 0177 } 0178 0179 shared_ptr<T const> 0180 shared_from_this() const 0181 { 0182 init_weak_once(); 0183 return shared_ptr<T>( weak_this_ ); 0184 } 0185 0186 private: 0187 0188 void init_weak_once() const 0189 { 0190 if( weak_this_._empty() ) 0191 { 0192 shared_this_.reset( static_cast< T* >( 0 ) 0193 , detail::esft2_deleter_wrapper() 0194 ); 0195 weak_this_ = shared_this_; 0196 } 0197 } 0198 0199 public: // actually private, but avoids compiler template friendship issues 0200 0201 // Note: invoked automatically by shared_ptr; do not call 0202 template< typename X, typename Y > 0203 void 0204 _internal_accept_owner( shared_ptr<X> * ppx, Y * py ) const 0205 { 0206 assert( ppx != 0 ); 0207 0208 if( weak_this_.use_count() == 0 ) 0209 weak_this_ = shared_ptr<T>( *ppx, py ); 0210 else if( shared_this_.use_count() != 0 ) 0211 { 0212 assert( ppx->unique() ); // no weak_ptrs should exist either, but there's no way to check that 0213 0214 detail::esft2_deleter_wrapper * pd 0215 = //boost:: 0216 get_deleter<detail::esft2_deleter_wrapper>( shared_this_ ); 0217 assert( pd != 0 ); 0218 0219 pd->set_deleter( *ppx ); 0220 0221 ppx->reset( shared_this_, ppx->get() ); 0222 shared_this_.reset(); 0223 } 0224 } 0225 }; // enable_shared_from_this2<> 0226 } 0227 0228 namespace G4 0229 { 0230 using CLHEP::enable_shared_from_this; 0231 using CLHEP::enable_shared_from_this2; 0232 } 0233 #endif 0234 */ 0235 0236 #endif /* G4SHARED_PTR_HH_ */
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |