3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #ifndef LEMON_REFPTR_H
20 #define LEMON_REFPTR_H
24 ///\brief A reference counted pointer implementation.
32 ///Reference counted pointer
34 ///This is a simple implementation of a reference counted pointer.
36 ///\warning Current implementation is far from being thread-safe.
40 mutable RefPtr *prev, *next;
46 void attach(RefPtr &r)
49 prev=&r; next=r.next; ref=r.ref;
53 void attach(const T *p)
55 prev=0; next=0; ref=p;
61 if(prev) { fr=false; prev->next=next; }
62 if(next) { fr=false; next->prev=prev; }
73 RefPtr(const RefPtr &r) {
75 attach(const_cast<RefPtr&>(r));
80 RefPtr(T *p) : prev(0), next(0), ref(p) {}
90 const RefPtr &operator=(const RefPtr &r) {
93 release(); attach(const_cast<RefPtr&>(r));
100 const RefPtr &operator=(const T* &p) {
101 if(ref!=p) { lock(); release(); attach(p); unlock(); }
106 void swap(RefPtr &r) {
110 p=prev; prev=r.prev; r.prev=p;
111 p=next; next=r.next; r.next=p;
112 tp=ref; ref=r.ref; r.ref=tp;
117 void clear() { lock(); release(); unlock(); }
120 T * operator->() { return ref; }
122 const T * operator->() const { return ref; }
124 operator T *() { return ref; }
126 operator const T *() const { return ref; }
129 bool operator<(const RefPtr &r) const { return this->ref < r.ref; }
131 bool operator<=(const RefPtr &r) const { return this->ref <= r.ref; }
133 bool operator==(const RefPtr &r) const { return this->ref == r.ref; }
135 bool operator>=(const RefPtr &r) const { return this->ref >= r.ref; }
137 bool operator>(const RefPtr &r) const { return this->ref > r.ref; }
139 bool operator!=(const RefPtr &r) const { return this->ref != r.ref; }
142 operator bool() const { return ref; }
145 const RefPtr &borrow(const T* &p) {
148 if(prev) prev->next=next;
149 if(next) next->prev=prev;
159 const RefPtr &borrow() {
161 if(prev) prev->next=next;
162 if(next) next->prev=prev;
168 }; //END OF CLASS REFPTR
170 } //END OF NAMESPACE LEMON