3
* Copyright 2004--2005, Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
8
* 1. Redistributions of source code must retain the above copyright notice,
9
* this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
13
* 3. The name of the author may not be used to endorse or promote products
14
* derived from this software without specific prior written permission.
16
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
* linked_ptr - simple reference linked pointer
30
* (like reference counting, just using a linked list of the references
31
* instead of their count.)
33
* The implementation stores three pointers for every linked_ptr, but
34
* does not allocate anything on the free store.
37
#ifndef TALK_BASE_LINKED_PTR_H__
38
#define TALK_BASE_LINKED_PTR_H__
42
/* For ANSI-challenged compilers, you may want to #define
43
* NO_MEMBER_TEMPLATES, explicit or mutable */
44
#define NO_MEMBER_TEMPLATES
46
template <class X> class linked_ptr
50
#ifndef NO_MEMBER_TEMPLATES
51
# define TEMPLATE_FUNCTION template <class Y>
52
TEMPLATE_FUNCTION friend class linked_ptr<Y>;
54
# define TEMPLATE_FUNCTION
58
typedef X element_type;
60
explicit linked_ptr(X* p = 0) throw()
61
: itsPtr(p) {itsPrev = itsNext = this;}
64
linked_ptr(const linked_ptr& r) throw()
66
linked_ptr& operator=(const linked_ptr& r)
75
#ifndef NO_MEMBER_TEMPLATES
76
template <class Y> friend class linked_ptr<Y>;
77
template <class Y> linked_ptr(const linked_ptr<Y>& r) throw()
79
template <class Y> linked_ptr& operator=(const linked_ptr<Y>& r)
87
#endif // NO_MEMBER_TEMPLATES
89
X& operator*() const throw() {return *itsPtr;}
90
X* operator->() const throw() {return itsPtr;}
91
X* get() const throw() {return itsPtr;}
92
bool unique() const throw() {return itsPrev ? itsPrev==this : true;}
96
mutable const linked_ptr* itsPrev;
97
mutable const linked_ptr* itsNext;
99
void acquire(const linked_ptr& r) throw()
100
{ // insert this to the list
103
itsNext->itsPrev = this;
107
#else // for ANSI-challenged compilers
108
(const_cast<linked_ptr<X>*>(&r))->itsNext = this;
112
#ifndef NO_MEMBER_TEMPLATES
113
template <class Y> void acquire(const linked_ptr<Y>& r) throw()
114
{ // insert this to the list
117
itsNext->itsPrev = this;
121
#else // for ANSI-challenged compilers
122
(const_cast<linked_ptr<X>*>(&r))->itsNext = this;
125
#endif // NO_MEMBER_TEMPLATES
128
{ // erase this from the list, delete if unique
129
if (unique()) delete itsPtr;
131
itsPrev->itsNext = itsNext;
132
itsNext->itsPrev = itsPrev;
133
itsPrev = itsNext = 0;
139
} // namespace talk_base
141
#endif // TALK_BASE_LINKED_PTR_H__