3 * Reference-counted pointer template. */
6 * GiNaC Copyright (C) 1999-2011 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
26 #include "assertion.h"
28 #include <cstddef> // for size_t
34 /** Base class for reference-counted objects. */
37 refcounted() throw() : refcount(0) {}
39 unsigned int add_reference() throw() { return ++refcount; }
40 unsigned int remove_reference() throw() { return --refcount; }
41 unsigned int get_refcount() const throw() { return refcount; }
42 void set_refcount(unsigned int r) throw() { refcount = r; }
45 unsigned int refcount; ///< reference counter
49 /** Class of (intrusively) reference-counted pointers that support
50 * copy-on-write semantics.
53 * must support the refcounted interface (usually by being derived
55 * T* T::duplicate() member function (only if makewriteable() is used) */
56 template <class T> class ptr {
57 friend class std::less< ptr<T> >;
59 // NB: This implementation of reference counting is not thread-safe.
60 // The reference counter needs to be incremented/decremented atomically,
61 // and makewritable() requires locking.
64 // no default ctor: a ptr is never unbound
66 /** Bind ptr to newly created object, start reference counting. */
67 ptr(T *t) throw() : p(t) { GINAC_ASSERT(p); p->set_refcount(1); }
69 /** Bind ptr to existing reference-counted object. */
70 explicit ptr(T &t) throw() : p(&t) { p->add_reference(); }
72 ptr(const ptr & other) throw() : p(other.p) { p->add_reference(); }
76 if (p->remove_reference() == 0)
80 ptr &operator=(const ptr & other)
82 // NB1: Must first add reference to "other", since other might be *this.
83 // NB2: Cache other.p, because if "other" is a subexpression of p,
84 // deleting p will also invalidate "other".
86 otherp->add_reference();
87 if (p->remove_reference() == 0)
93 T &operator*() const throw() { return *p; }
94 T *operator->() const throw() { return p; }
96 friend inline T *get_pointer(const ptr & x) throw() { return x.p; }
98 /** Announce your intention to modify the object bound to this ptr.
99 * This ensures that the object is not shared by any other ptrs. */
102 if (p->get_refcount() > 1) {
103 T *p2 = p->duplicate();
105 p->remove_reference();
110 /** Swap the bound object of this ptr with another ptr. */
111 void swap(ptr & other) throw()
118 // ptr<>s are always supposed to be bound to a valid object, so we don't
119 // provide support for "if (p)", "if (!p)", "if (p==0)" and "if (p!=0)".
120 // We do, however, provide support for comparing ptr<>s with other ptr<>s
121 // to different (probably derived) types and raw pointers.
124 bool operator==(const ptr<U> & rhs) const throw() { return p == get_pointer(rhs); }
127 bool operator!=(const ptr<U> & rhs) const throw() { return p != get_pointer(rhs); }
130 inline friend bool operator==(const ptr & lhs, const U * rhs) throw() { return lhs.p == rhs; }
133 inline friend bool operator!=(const ptr & lhs, const U * rhs) throw() { return lhs.p != rhs; }
136 inline friend bool operator==(const U * lhs, const ptr & rhs) throw() { return lhs == rhs.p; }
139 inline friend bool operator!=(const U * lhs, const ptr & rhs) throw() { return lhs != rhs.p; }
141 inline friend std::ostream & operator<<(std::ostream & os, const ptr<T> & rhs)
156 /** Specialization of std::less for ptr<T> to enable ordering of ptr<T>
157 * objects (e.g. for the use as std::map keys). */
158 template <class T> struct less< GiNaC::ptr<T> >
159 : public binary_function<GiNaC::ptr<T>, GiNaC::ptr<T>, bool> {
160 bool operator()(const GiNaC::ptr<T> &lhs, const GiNaC::ptr<T> &rhs) const
162 return less<T*>()(lhs.p, rhs.p);
168 #endif // ndef GINAC_PTR_H