3 * ref_ptr. A reference counted template class. Similar to the STL
4 * auto_ptr, but generalised for reference counting.
6 * Copyright (C) 2002 Barnaby Gray <barnaby@beedesign.co.uk>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 template <typename Object
>
33 /* note: the actual reference count is controlled on the object -
34 it turned out problematic having it stored in the ref_ptr,
35 for reasons I won't go into here ;-) */
42 ref_ptr(const ref_ptr
<Object
>& that
)
43 : m_instance(that
.m_instance
)
45 if (m_instance
!= NULL
)
46 ++(m_instance
->count
);
52 if (m_instance
!= NULL
)
53 ++(m_instance
->count
);
54 /* zeroed inside Contact now */
59 if (m_instance
!= NULL
&& --(m_instance
->count
) == 0)
68 const Object
* get() const
78 const Object
* operator->() const
88 const Object
& operator*() const
94 unsigned int count() const
96 return m_instance
->count
;
99 ref_ptr
& operator=(const ref_ptr
<Object
>& that
) {
100 if (m_instance
!= NULL
&& --(m_instance
->count
) == 0) {
103 m_instance
= that
.m_instance
;
104 if (m_instance
!= NULL
)
105 ++(m_instance
->count
);