python api for ID property access by Joseph Eager, copied from blender 2.4x.
[blender.git] / intern / audaspace / intern / AUD_Reference.h
1 /*
2  * $Id$
3  *
4  * ***** BEGIN LGPL LICENSE BLOCK *****
5  *
6  * Copyright 2009 Jörg Hermann Müller
7  *
8  * This file is part of AudaSpace.
9  *
10  * AudaSpace is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * AudaSpace is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with AudaSpace.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * ***** END LGPL LICENSE BLOCK *****
24  */
25
26 #ifndef AUD_REFERENCE
27 #define AUD_REFERENCE
28
29 template <class T>
30 /**
31  * This class provides reference counting functionality.
32  */
33 class AUD_Reference
34 {
35 private:
36         /// The reference.
37         T* m_reference;
38         /// The reference counter.
39         int* m_refcount;
40 public:
41         /**
42          * Creates a new reference counter.
43          * \param reference The reference.
44          */
45         AUD_Reference(T* reference = 0)
46         {
47                 m_reference = reference;
48                 m_refcount = new int; AUD_NEW("int")
49                 *m_refcount = 1;
50         }
51
52         /**
53          * Copies a AUD_Reference object.
54          * \param ref The AUD_Reference object to copy.
55          */
56         AUD_Reference(const AUD_Reference& ref)
57         {
58                 m_reference = ref.m_reference;
59                 m_refcount = ref.m_refcount;
60                 (*m_refcount)++;
61         }
62
63         /**
64          * Destroys a AUD_Reference object, if there's no furthere reference on the
65          * reference, it is destroyed as well.
66          */
67         ~AUD_Reference()
68         {
69                 (*m_refcount)--;
70                 if(*m_refcount == 0)
71                 {
72                         if(m_reference != 0)
73                         {
74                                 delete m_reference; AUD_DELETE("buffer")
75                         }
76                         delete m_refcount; AUD_DELETE("int")
77                 }
78         }
79
80         /**
81          * Copies a AUD_Reference object.
82          * \param ref The AUD_Reference object to copy.
83          */
84         AUD_Reference& operator=(const AUD_Reference& ref)
85         {
86                 if(&ref == this)
87                         return *this;
88
89                 (*m_refcount)--;
90                 if(*m_refcount == 0)
91                 {
92                         if(m_reference != 0)
93                         {
94                                 delete m_reference; AUD_DELETE("buffer")
95                         }
96                         delete m_refcount; AUD_DELETE("int")
97                 }
98
99                 m_reference = ref.m_reference;
100                 m_refcount = ref.m_refcount;
101                 (*m_refcount)++;
102
103                 return *this;
104         }
105
106         /**
107          * Returns the reference.
108          */
109         T* get()
110         {
111                 return m_reference;
112         }
113 };
114
115 #endif // AUD_REFERENCE