]> www.ginac.de Git - ginac.git/blob - ginac/ptr.h
fixed typos
[ginac.git] / ginac / ptr.h
1 /** @file ptr.h
2  *
3  *  Reference-counted pointer template. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2004 Johannes Gutenberg University Mainz, Germany
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program 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
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #ifndef __GINAC_PTR_H__
24 #define __GINAC_PTR_H__
25
26 #include <cstddef> // for size_t
27 #include <functional>
28 #include <iosfwd>
29
30 #include "assertion.h"
31
32 namespace GiNaC {
33
34
35 /** Base class for reference-counted objects. */
36 class refcounted {
37 public:
38         refcounted() throw() : refcount(0) {}
39
40         size_t add_reference() throw() { return ++refcount; }
41         size_t remove_reference() throw() { return --refcount; }
42         size_t get_refcount() const throw() { return refcount; }
43         void set_refcount(size_t r) throw() { refcount = r; }
44
45 private:
46         size_t refcount; ///< reference counter
47 };
48
49
50 /** Class of (intrusively) reference-counted pointers that support
51  *  copy-on-write semantics.
52  *
53  *  Requirements for T:
54  *    must support the refcounted interface (usually by being derived
55  *      from refcounted)
56  *    T* T::duplicate() member function (only if makewriteable() is used) */
57 template <class T> class ptr {
58         friend class std::less< ptr<T> >;
59
60         // NB: This implementation of reference counting is not thread-safe.
61         // The reference counter needs to be incremented/decremented atomically,
62         // and makewritable() requires locking.
63
64 public:
65     // no default ctor: a ptr is never unbound
66
67         /** Bind ptr to newly created object, start reference counting. */
68         ptr(T *t) throw() : p(t) { GINAC_ASSERT(p); p->set_refcount(1); }
69
70         /** Bind ptr to existing reference-counted object. */
71         explicit ptr(T &t) throw() : p(&t) { p->add_reference(); }
72
73         ptr(const ptr & other) throw() : p(other.p) { p->add_reference(); }
74
75         ~ptr()
76         {
77                 if (p->remove_reference() == 0)
78                         delete p;
79         }
80
81         ptr &operator=(const ptr & other)
82         {
83                 // NB: must first add reference to "other", since other might be *this.
84                 other.p->add_reference();
85                 if (p->remove_reference() == 0)
86                         delete p;
87                 p = other.p;
88                 return *this;
89         }
90
91         T &operator*() const throw() { return *p; }
92         T *operator->() const throw() { return p; }
93
94         friend inline T *get_pointer(const ptr & x) throw() { return x.p; }
95
96         /** Announce your intention to modify the object bound to this ptr.
97          *  This ensures that the object is not shared by any other ptrs. */
98         void makewritable()
99         {
100                 if (p->get_refcount() > 1) {
101                         T *p2 = p->duplicate();
102                         p2->set_refcount(1);
103                         p->remove_reference();
104                         p = p2;
105                 }
106         }
107
108         /** Swap the bound object of this ptr with another ptr. */
109         void swap(ptr & other) throw()
110         {
111                 T *t = p;
112                 p = other.p;
113                 other.p = t;
114         }
115
116         // ptr<>s are always supposed to be bound to a valid object, so we don't
117         // provide support for "if (p)", "if (!p)", "if (p==0)" and "if (p!=0)".
118         // We do, however, provide support for comparing ptr<>s with other ptr<>s
119         // to different (probably derived) types and raw pointers.
120
121         template <class U>
122         bool operator==(const ptr<U> & rhs) const throw() { return p == get_pointer(rhs); }
123
124         template <class U>
125         bool operator!=(const ptr<U> & rhs) const throw() { return p != get_pointer(rhs); }
126
127         template <class U>
128         inline friend bool operator==(const ptr & lhs, const U * rhs) throw() { return lhs.p == rhs; }
129
130         template <class U>
131         inline friend bool operator!=(const ptr & lhs, const U * rhs) throw() { return lhs.p != rhs; }
132
133         template <class U>
134         inline friend bool operator==(const U * lhs, const ptr & rhs) throw() { return lhs == rhs.p; }
135
136         template <class U>
137         inline friend bool operator!=(const U * lhs, const ptr & rhs) throw() { return lhs != rhs.p; }
138
139         inline friend std::ostream & operator<<(std::ostream & os, const ptr<T> & rhs)
140         {
141                 os << rhs.p;
142         }
143
144 private:
145         T *p;
146 };
147
148 } // namespace GiNaC
149
150
151 namespace std {
152
153 /** Specialization of std::less for ptr<T> to enable ordering of ptr<T>
154  *  objects (e.g. for the use as std::map keys). */
155 template <class T> struct less< GiNaC::ptr<T> >
156  : public binary_function<GiNaC::ptr<T>, GiNaC::ptr<T>, bool> {
157         bool operator()(const GiNaC::ptr<T> &lhs, const GiNaC::ptr<T> &rhs) const
158         {
159                 return less<T*>()(lhs.p, rhs.p);
160         }
161 };
162
163 } // namespace std
164
165 #endif // ndef __GINAC_PTR_H__