Cortex  10.0.0-a4
RefCounted.h
1 //
3 // Copyright (c) 2007-2014, Image Engine Design Inc. All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //
12 // * Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
15 //
16 // * Neither the name of Image Engine Design nor the names of any
17 // other contributors to this software may be used to endorse or
18 // promote products derived from this software without specific prior
19 // written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
34 
35 #ifndef IE_CORE_REFCOUNTED_H
36 #define IE_CORE_REFCOUNTED_H
37 
38 #include <cassert>
39 
40 #include "tbb/atomic.h"
41 
42 #include "IECore/Export.h"
43 #include "boost/noncopyable.hpp"
44 #include "boost/intrusive_ptr.hpp"
45 
46 namespace IECore
47 {
48 
49 #define IE_CORE_DECLAREPTR( TYPENAME ) \
50 typedef boost::intrusive_ptr< TYPENAME > TYPENAME ## Ptr; \
51 typedef boost::intrusive_ptr< const TYPENAME > Const ## TYPENAME ## Ptr; \
52 
53 #define IE_CORE_DECLAREMEMBERPTR( TYPENAME ) \
54  typedef boost::intrusive_ptr< TYPENAME > Ptr; \
55  typedef boost::intrusive_ptr< const TYPENAME > ConstPtr;
56 
59 #define IE_CORE_DECLAREMEMBERPTR2( PART1, PART2 ) \
60  typedef boost::intrusive_ptr< PART1, PART2 > Ptr; \
61  typedef boost::intrusive_ptr< const PART1, PART2 > ConstPtr;
62 
65 #define IE_CORE_DECLAREMEMBERPTR3( PART1, PART2, PART3 ) \
66  typedef boost::intrusive_ptr< PART1, PART2, PART3 > Ptr; \
67  typedef boost::intrusive_ptr< const PART1, PART2, PART3 > ConstPtr;
68 
69 #define IE_CORE_FORWARDDECLARE( TYPENAME ) \
70  class TYPENAME; \
71  IE_CORE_DECLAREPTR( TYPENAME ) \
72 
73 class IECORE_API RefCounted : private boost::noncopyable
125 {
126  public:
127 
128  IE_CORE_DECLAREMEMBERPTR( RefCounted );
129 
130  typedef size_t RefCount;
131 
132  RefCounted();
133 
135  inline void addRef() const { m_numRefs++; };
136 
138  inline void removeRef() const
139  {
140  assert( m_numRefs > 0 );
141  if( --m_numRefs==0 )
142  {
143  delete this;
144  }
145  };
146 
148  inline RefCount refCount() const { return m_numRefs; };
149 
150  protected:
151 
152  virtual ~RefCounted();
153 
154  private :
155 
156  mutable tbb::atomic<RefCount> m_numRefs;
157 
158 };
159 
160 IE_CORE_DECLAREPTR( RefCounted )
161 
162 IECORE_API inline void intrusive_ptr_add_ref( const IECore::RefCounted *r )
164 {
165  r->addRef();
166 }
167 
168 IECORE_API inline void intrusive_ptr_release(const IECore::RefCounted *r)
169 {
170  r->removeRef();
171 }
172 
175 template<typename T>
176 inline size_t tbb_hasher( const boost::intrusive_ptr<T> &ptr )
177 {
178  // This is the same as what tbb uses for raw pointers
179  const size_t h = reinterpret_cast<size_t>( ptr.get() );
180  return (h >> 3) ^ h;
181 }
182 
183 } // namespace IECore
184 
185 
186 #endif // IE_CORE_REFCOUNTED_H
size_t tbb_hasher(const boost::intrusive_ptr< T > &ptr)
Definition: RefCounted.h:176
void removeRef() const
Remove a reference from the current object.
Definition: RefCounted.h:138
RefCount refCount() const
Returns the current reference count.
Definition: RefCounted.h:148
IECORE_API void intrusive_ptr_add_ref(const IECore::RefCounted *r)
Functions required to allow use of RefCounted with boost::intrusive_ptr.
Definition: RefCounted.h:163
void addRef() const
Add a reference to the current object.
Definition: RefCounted.h:135
Definition: RefCounted.h:124
This namespace contains all components of the core library.
Definition: AddSmoothSkinningInfluencesOp.h:43