refcnt.hh revision 1762
1/*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
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: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
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
29#ifndef __REFCNT_HH__
30#define __REFCNT_HH__
31
32class RefCounted
33{
34  private:
35    int count;
36
37  private:
38    RefCounted(const RefCounted &);
39
40  public:
41    RefCounted() : count(0) {}
42    virtual ~RefCounted() {}
43
44    void incref() { ++count; }
45    void decref() { if (--count <= 0) delete this; }
46};
47
48template <class T>
49class RefCountingPtr
50{
51  protected:
52    T *data;
53
54    void copy(T *d)
55    {
56        data = d;
57        if (data)
58            data->incref();
59    }
60    void del()
61    {
62        if (data)
63            data->decref();
64    }
65    void set(T *d)
66    {
67        if (data == d)
68            return;
69
70        del();
71        copy(d);
72    }
73
74
75  public:
76    RefCountingPtr() : data(NULL) {}
77    RefCountingPtr(T *data) { copy(data); }
78    RefCountingPtr(const RefCountingPtr &r) { copy(r.data); }
79    ~RefCountingPtr() { del(); }
80
81    T *operator->() { return data; }
82    T &operator*() { return *data; }
83    T *get() { return data; }
84
85    const T *operator->() const { return data; }
86    const T &operator*() const { return *data; }
87    const T *get() const { return data; }
88
89    RefCountingPtr &operator=(T *p) { set(p); return *this; }
90    RefCountingPtr &operator=(const RefCountingPtr &r)
91    { return operator=(r.data); }
92
93    bool operator!() const { return data == 0; }
94    operator bool() const { return data != 0; }
95};
96
97template<class T>
98bool operator==(const RefCountingPtr<T> &l, const RefCountingPtr<T> &r)
99{ return l.get() == r.get(); }
100
101template<class T>
102bool operator==(const RefCountingPtr<T> &l, const T *r)
103{ return l.get() == r; }
104
105template<class T>
106bool operator==(const T &l, const RefCountingPtr<T> &r)
107{ return l == r.get(); }
108
109template<class T>
110bool operator!=(const RefCountingPtr<T> &l, const RefCountingPtr<T> &r)
111{ return l.get() != r.get(); }
112
113template<class T>
114bool operator!=(const RefCountingPtr<T> &l, const T *r)
115{ return l.get() != r; }
116
117template<class T>
118bool operator!=(const T &l, const RefCountingPtr<T> &r)
119{ return l != r.get(); }
120
121#endif // __REFCNT_HH__
122