OpenImageIO
 All Classes Files Friends Macros Pages
refcnt.h
Go to the documentation of this file.
1 /*
2  Copyright 2008 Larry Gritz and the other authors and contributors.
3  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  * Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13  * Neither the name of the software's owners nor the names of its
14  contributors may be used to endorse or promote products derived from
15  this software without specific prior written permission.
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28  (This is the Modified BSD License)
29 */
30 
31 
37 
38 
39 #ifndef OPENIMAGEIO_REFCNT_H
40 #define OPENIMAGEIO_REFCNT_H
41 
42 #include "thread.h"
43 #include "version.h"
44 
45 // Use Boost for shared pointers
46 #include <boost/shared_ptr.hpp>
47 #include <boost/intrusive_ptr.hpp>
48 
49 
50 OIIO_NAMESPACE_ENTER
51 {
52 
53 using boost::shared_ptr;
54 using boost::intrusive_ptr;
55 
56 
59 class RefCnt {
60 protected:
61  // Declare RefCnt constructors and destructors protected because they
62  // should only be called implicitly from within child class constructors or
63  // destructors. In particular, this prevents users from deleting a RefCnt*
64  // which is important because the destructor is non-virtual.
65 
66  RefCnt () { m_refcnt = 0; }
67 
70  RefCnt (RefCnt&) { m_refcnt = 0; }
71 
72  ~RefCnt () {}
73 
74 public:
77  void _incref () const { ++m_refcnt; }
78 
81  bool _decref () const { return (--m_refcnt) == 0; }
82 
85  const RefCnt & operator= (const RefCnt&) const { return *this; }
86 
87 private:
88  mutable atomic_int m_refcnt;
89 };
90 
91 
92 
95 template <class T>
96 inline void intrusive_ptr_add_ref (T *x)
97 {
98  x->_incref ();
99 }
100 
103 template <class T>
104 inline void intrusive_ptr_release (T *x)
105 {
106  if (x->_decref ())
107  delete x;
108 }
109 
110 // Note that intrusive_ptr_add_ref and intrusive_ptr_release MUST be a
111 // templated on the full type, so that they pass the right address to
112 // 'delete' and destroy the right type. If you try to just
113 // 'inline void intrusive_ptr_release (RefCnt *x)', that might seem
114 // clever, but it will end up getting the address of (and destroying)
115 // just the inherited RefCnt sub-object, not the full subclass you
116 // meant to delete and destroy.
117 
118 
119 }
120 OIIO_NAMESPACE_EXIT
121 
122 #endif // OPENIMAGEIO_REFCNT_H