]> www.ginac.de Git - ginac.git/blob - ginac/ptr.h
Made class possymbol for positive symbols and documented it.
[ginac.git] / ginac / ptr.h
1 /** @file ptr.h
2  *
3  *  Reference-counted pointer template. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2005 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  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                 // NB1: Must first add reference to "other", since other might be *this.
84                 // NB2: Cache other.p, because if "other" is a subexpression of p,
85                 //      deleting p will also invalidate "other".
86                 T *otherp = other.p;
87                 otherp->add_reference();
88                 if (p->remove_reference() == 0)
89                         delete p;
90                 p = otherp;
91                 return *this;
92         }
93
94         T &operator*() const throw() { return *p; }
95         T *operator->() const throw() { return p; }
96
97         friend inline T *get_pointer(const ptr & x) throw() { return x.p; }
98
99         /** Announce your intention to modify the object bound to this ptr.
100          *  This ensures that the object is not shared by any other ptrs. */
101         void makewritable()
102         {
103                 if (p->get_refcount() > 1) {
104                         T *p2 = p->duplicate();
105                         p2->set_refcount(1);
106                         p->remove_reference();
107                         p = p2;
108                 }
109         }
110
111         /** Swap the bound object of this ptr with another ptr. */
112         void swap(ptr & other) throw()
113         {
114                 T *t = p;
115                 p = other.p;
116                 other.p = t;
117         }
118
119         // ptr<>s are always supposed to be bound to a valid object, so we don't
120         // provide support for "if (p)", "if (!p)", "if (p==0)" and "if (p!=0)".
121         // We do, however, provide support for comparing ptr<>s with other ptr<>s
122         // to different (probably derived) types and raw pointers.
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         bool operator!=(const ptr<U> & rhs) const throw() { return p != get_pointer(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 ptr & lhs, const U * rhs) throw() { return lhs.p != rhs; }
135
136         template <class U>
137         inline friend bool operator==(const U * lhs, const ptr & rhs) throw() { return lhs == rhs.p; }
138
139         template <class U>
140         inline friend bool operator!=(const U * lhs, const ptr & rhs) throw() { return lhs != rhs.p; }
141
142         inline friend std::ostream & operator<<(std::ostream & os, const ptr<T> & rhs)
143         {
144                 os << rhs.p;
145                 return os;
146         }
147
148 private:
149         T *p;
150 };
151
152 } // namespace GiNaC
153
154
155 namespace std {
156
157 /** Specialization of std::less for ptr<T> to enable ordering of ptr<T>
158  *  objects (e.g. for the use as std::map keys). */
159 template <class T> struct less< GiNaC::ptr<T> >
160  : public binary_function<GiNaC::ptr<T>, GiNaC::ptr<T>, bool> {
161         bool operator()(const GiNaC::ptr<T> &lhs, const GiNaC::ptr<T> &rhs) const
162         {
163                 return less<T*>()(lhs.p, rhs.p);
164         }
165 };
166
167 } // namespace std
168
169 #endif // ndef __GINAC_PTR_H__