1/*
2 * libjingle
3 * Copyright 2004--2005, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
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.
15 *
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.
26 */
27
28/*
29 * linked_ptr - simple reference linked pointer
30 * (like reference counting, just using a linked list of the references
31 * instead of their count.)
32 *
33 * The implementation stores three pointers for every linked_ptr, but
34 * does not allocate anything on the free store.
35 */
36
37#ifndef TALK_BASE_LINKED_PTR_H__
38#define TALK_BASE_LINKED_PTR_H__
39
40namespace talk_base {
41
42/* For ANSI-challenged compilers, you may want to #define
43 * NO_MEMBER_TEMPLATES, explicit or mutable */
44#define NO_MEMBER_TEMPLATES
45
46template <class X> class linked_ptr
47{
48public:
49
50#ifndef NO_MEMBER_TEMPLATES
51#   define TEMPLATE_FUNCTION template <class Y>
52    TEMPLATE_FUNCTION friend class linked_ptr<Y>;
53#else
54#   define TEMPLATE_FUNCTION
55    typedef X Y;
56#endif
57
58    typedef X element_type;
59
60    explicit linked_ptr(X* p = 0) throw()
61        : itsPtr(p) {itsPrev = itsNext = this;}
62    ~linked_ptr()
63        {release();}
64    linked_ptr(const linked_ptr& r) throw()
65        {acquire(r);}
66    linked_ptr& operator=(const linked_ptr& r)
67    {
68        if (this != &r) {
69            release();
70            acquire(r);
71        }
72        return *this;
73    }
74
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()
78        {acquire(r);}
79    template <class Y> linked_ptr& operator=(const linked_ptr<Y>& r)
80    {
81        if (this != &r) {
82            release();
83            acquire(r);
84        }
85        return *this;
86    }
87#endif // NO_MEMBER_TEMPLATES
88
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;}
93
94private:
95    X*                          itsPtr;
96    mutable const linked_ptr*   itsPrev;
97    mutable const linked_ptr*   itsNext;
98
99    void acquire(const linked_ptr& r) throw()
100    { // insert this to the list
101        itsPtr = r.itsPtr;
102        itsNext = r.itsNext;
103        itsNext->itsPrev = this;
104        itsPrev = &r;
105#ifndef mutable
106        r.itsNext = this;
107#else // for ANSI-challenged compilers
108        (const_cast<linked_ptr<X>*>(&r))->itsNext = this;
109#endif
110    }
111
112#ifndef NO_MEMBER_TEMPLATES
113    template <class Y> void acquire(const linked_ptr<Y>& r) throw()
114    { // insert this to the list
115        itsPtr = r.itsPtr;
116        itsNext = r.itsNext;
117        itsNext->itsPrev = this;
118        itsPrev = &r;
119#ifndef mutable
120        r.itsNext = this;
121#else // for ANSI-challenged compilers
122        (const_cast<linked_ptr<X>*>(&r))->itsNext = this;
123#endif
124    }
125#endif // NO_MEMBER_TEMPLATES
126
127    void release()
128    { // erase this from the list, delete if unique
129        if (unique()) delete itsPtr;
130        else {
131            itsPrev->itsNext = itsNext;
132            itsNext->itsPrev = itsPrev;
133            itsPrev = itsNext = 0;
134        }
135        itsPtr = 0;
136    }
137};
138
139} // namespace talk_base
140
141#endif // TALK_BASE_LINKED_PTR_H__
142
143