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)
48 prev=&r; next=r.next; ref=r.ref;
51 void attach(const T *p)
53 prev=0; next=0; ref=p;
59 if(prev) { fr=false; prev->next=next; }
60 if(next) { fr=false; next->prev=prev; }
71 RefPtr(const RefPtr &r) {
73 attach(const_cast<RefPtr&>(r));
78 RefPtr(T *p) : prev(0), next(0), ref(p) {}
88 const RefPtr &operator=(const RefPtr &r) {
91 release(); attach(const_cast<RefPtr&>(r));
98 const RefPtr &operator=(const T* &p) {
99 if(ref!=p) { lock(); release(); attach(p); unlock(); }
104 void swap(RefPtr &r) {
108 p=prev; prev=r.prev; r.prev=p;
109 p=next; next=r.next; r.next=p;
110 tp=ref; ref=r.ref; r.ref=tp;
115 void clear() { lock(); release(); unlock(); }
118 T * operator->() { return ref; }
120 const T * operator->() const { return ref; }
122 operator T *() { return ref; }
124 operator const T *() const { return ref; }
127 bool operator<(const RefPtr &r) const { return this->ref < r.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; }
140 operator bool() const { return ref; }
142 }; //END OF CLASS REFPTR
144 } //END OF NAMESPACE LEMON