11 #include "CppTools.hpp"
13 #if BOGUS_HAS_CPP11 && !defined(BOGUS_SHARED_PTR_NS)
14 #define BOGUS_SHARED_PTR_NS std
18 #ifdef BOGUS_SHARED_PTR_NS
19 #ifndef BOGUS_SHARED_PTR
21 #define BOGUS_SHARED_PTR( Type, Name ) BOGUS_SHARED_PTR_NS::shared_ptr< Type > Name
25 #ifndef BOGUS_NAIVE_SHARED_PTR_HPP
26 #define BOGUS_NAIVE_SHARED_PTR_HPP
28 #ifndef BOGUS_SHARED_PTR
29 #define BOGUS_SHARED_PTR( Type, Name ) NaiveSharedPtr< Type > Name
32 #if defined( _MSC_VER ) && !defined( BOGUS_DONT_USE_BUILTIN_ATOMICS )
33 #define BOGUS_DONT_USE_BUILTIN_ATOMICS
46 template <
typename T >
81 void reset( T * instance = BOGUS_NULL_PTR(T) )
89 T* instance = BOGUS_NULL_PTR(T) ;
90 std::swap( m_instance, instance ) ;
93 if( instance && 0 == sync_add< -1 > () )
103 T
const& operator * ( )
const {
109 T
const* operator -> ( )
const {
115 const T*
get()
const {
119 operator bool()
const {
120 return BOGUS_NULL_PTR(T) != m_instance ;
127 T* instance = m_instance ;
129 if( instance && sync_add< 1 >() <= 1 )
134 instance = BOGUS_NULL_PTR(T) ;
142 m_refCount = rhs.m_refCount ;
143 m_instance = rhs.add_ref() ;
146 void acquire( T* instance )
149 m_refCount =
new int ;
152 m_instance = instance ;
157 inline int sync_add()
const
160 #ifndef BOGUS_DONT_USE_BUILTIN_ATOMICS
161 return __sync_add_and_fetch( m_refCount, val );
166 #pragma omp atomic capture
168 #ifndef BOGUS_DONT_PARALLELIZE
169 #pragma omp critical (BogusNaiveSharedPtr)
172 { *m_refCount += val ; t = *m_refCount ; }
Naive reference-counting Shared Pointer.
Definition: NaiveSharedPtr.hpp:47