3 * Reference-counted pointer template. */
6 * GiNaC Copyright (C) 1999-2007 Johannes Gutenberg University Mainz, Germany
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.
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.
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
23 #ifndef __GINAC_PTR_H__
24 #define __GINAC_PTR_H__
26 #include <cstddef> // for size_t
30 #include "assertion.h"
35 /** Base class for reference-counted objects. */
38 refcounted() throw() : refcount(0) {}
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; }
46 size_t refcount; ///< reference counter
50 /** Class of (intrusively) reference-counted pointers that support
51 * copy-on-write semantics.
54 * must support the refcounted interface (usually by being derived
56 * T* T::duplicate() member function (only if makewriteable() is used) */
57 template <class T> class ptr {
58 friend class std::less< ptr<T> >;
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.
65 // no default ctor: a ptr is never unbound
67 /** Bind ptr to newly created object, start reference counting. */
68 ptr(T *t) throw() : p(t) { GINAC_ASSERT(p); p->set_refcount(1); }
70 /** Bind ptr to existing reference-counted object. */
71 explicit ptr(T &t) throw() : p(&t) { p->add_reference(); }
73 ptr(const ptr & other) throw() : p(other.p) { p->add_reference(); }
77 if (p->remove_reference() == 0)
81 ptr &operator=(const ptr & other)
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".
87 otherp->add_reference();
88 if (p->remove_reference() == 0)
94 T &operator*() const throw() { return *p; }
95 T *operator->() const throw() { return p; }
97 friend inline T *get_pointer(const ptr & x) throw() { return x.p; }
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. */
103 if (p->get_refcount() > 1) {
104 T *p2 = p->duplicate();
106 p->remove_reference();
111 /** Swap the bound object of this ptr with another ptr. */
112 void swap(ptr & other) throw()
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.
125 bool operator==(const ptr<U> & rhs) const throw() { return p == get_pointer(rhs); }
128 bool operator!=(const ptr<U> & rhs) const throw() { return p != get_pointer(rhs); }
131 inline friend bool operator==(const ptr & lhs, const U * rhs) throw() { return lhs.p == rhs; }
134 inline friend bool operator!=(const ptr & lhs, const U * rhs) throw() { return lhs.p != rhs; }
137 inline friend bool operator==(const U * lhs, const ptr & rhs) throw() { return lhs == rhs.p; }
140 inline friend bool operator!=(const U * lhs, const ptr & rhs) throw() { return lhs != rhs.p; }
142 inline friend std::ostream & operator<<(std::ostream & os, const ptr<T> & rhs)
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
163 return less<T*>()(lhs.p, rhs.p);
169 #endif // ndef __GINAC_PTR_H__