refcnt.hh revision 3877
12SN/A/* 21762SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan 32SN/A * All rights reserved. 42SN/A * 52SN/A * Redistribution and use in source and binary forms, with or without 62SN/A * modification, are permitted provided that the following conditions are 72SN/A * met: redistributions of source code must retain the above copyright 82SN/A * notice, this list of conditions and the following disclaimer; 92SN/A * redistributions in binary form must reproduce the above copyright 102SN/A * notice, this list of conditions and the following disclaimer in the 112SN/A * documentation and/or other materials provided with the distribution; 122SN/A * neither the name of the copyright holders nor the names of its 132SN/A * contributors may be used to endorse or promote products derived from 142SN/A * this software without specific prior written permission. 152SN/A * 162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272665Ssaidi@eecs.umich.edu * 282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert 292SN/A */ 302SN/A 313877Sbinkertn@umich.edu#ifndef __BASE_REFCNT_HH__ 323877Sbinkertn@umich.edu#define __BASE_REFCNT_HH__ 332147SN/A 342SN/Aclass RefCounted 352SN/A{ 362SN/A private: 372SN/A int count; 382SN/A 392SN/A private: 402SN/A RefCounted(const RefCounted &); 412SN/A 422SN/A public: 432SN/A RefCounted() : count(0) {} 442SN/A virtual ~RefCounted() {} 452SN/A 462SN/A void incref() { ++count; } 472SN/A void decref() { if (--count <= 0) delete this; } 482SN/A}; 492SN/A 502SN/Atemplate <class T> 512SN/Aclass RefCountingPtr 522SN/A{ 531078SN/A protected: 542SN/A T *data; 552SN/A 561114SN/A void copy(T *d) 571114SN/A { 582SN/A data = d; 592SN/A if (data) 602SN/A data->incref(); 612SN/A } 621114SN/A void del() 631114SN/A { 642SN/A if (data) 652SN/A data->decref(); 662SN/A } 671114SN/A void set(T *d) 681114SN/A { 691114SN/A if (data == d) 701114SN/A return; 711114SN/A 721114SN/A del(); 731114SN/A copy(d); 741114SN/A } 751114SN/A 762SN/A 772SN/A public: 783877Sbinkertn@umich.edu RefCountingPtr() : data(0) {} 792SN/A RefCountingPtr(T *data) { copy(data); } 80502SN/A RefCountingPtr(const RefCountingPtr &r) { copy(r.data); } 812SN/A ~RefCountingPtr() { del(); } 822SN/A 832SN/A T *operator->() { return data; } 842SN/A T &operator*() { return *data; } 852SN/A T *get() { return data; } 862SN/A 872SN/A const T *operator->() const { return data; } 882SN/A const T &operator*() const { return *data; } 892SN/A const T *get() const { return data; } 902SN/A 913877Sbinkertn@umich.edu const RefCountingPtr &operator=(T *p) { set(p); return *this; } 923877Sbinkertn@umich.edu const RefCountingPtr &operator=(const RefCountingPtr &r) 931114SN/A { return operator=(r.data); } 942SN/A 952SN/A bool operator!() const { return data == 0; } 962SN/A operator bool() const { return data != 0; } 972SN/A}; 982SN/A 992SN/Atemplate<class T> 1002SN/Abool operator==(const RefCountingPtr<T> &l, const RefCountingPtr<T> &r) 1012SN/A{ return l.get() == r.get(); } 1022SN/A 1032SN/Atemplate<class T> 1042SN/Abool operator==(const RefCountingPtr<T> &l, const T *r) 1052SN/A{ return l.get() == r; } 1062SN/A 1072SN/Atemplate<class T> 1082SN/Abool operator==(const T &l, const RefCountingPtr<T> &r) 1092SN/A{ return l == r.get(); } 1102SN/A 1112SN/Atemplate<class T> 1122SN/Abool operator!=(const RefCountingPtr<T> &l, const RefCountingPtr<T> &r) 1132SN/A{ return l.get() != r.get(); } 1142SN/A 1152SN/Atemplate<class T> 1162SN/Abool operator!=(const RefCountingPtr<T> &l, const T *r) 1172SN/A{ return l.get() != r; } 1182SN/A 1192SN/Atemplate<class T> 1202SN/Abool operator!=(const T &l, const RefCountingPtr<T> &r) 1212SN/A{ return l != r.get(); } 1222SN/A 1233877Sbinkertn@umich.edu#endif // __BASE_REFCNT_HH__ 124