refcnt.hh revision 2
111527Sdavid.guillen@arm.com/* 212546Sanouk.vanlaer@arm.com * Copyright (c) 2003 The Regents of The University of Michigan 311527Sdavid.guillen@arm.com * All rights reserved. 411527Sdavid.guillen@arm.com * 511527Sdavid.guillen@arm.com * Redistribution and use in source and binary forms, with or without 611527Sdavid.guillen@arm.com * modification, are permitted provided that the following conditions are 711527Sdavid.guillen@arm.com * met: redistributions of source code must retain the above copyright 811527Sdavid.guillen@arm.com * notice, this list of conditions and the following disclaimer; 911527Sdavid.guillen@arm.com * redistributions in binary form must reproduce the above copyright 1011527Sdavid.guillen@arm.com * notice, this list of conditions and the following disclaimer in the 1111527Sdavid.guillen@arm.com * documentation and/or other materials provided with the distribution; 1211527Sdavid.guillen@arm.com * neither the name of the copyright holders nor the names of its 1311527Sdavid.guillen@arm.com * contributors may be used to endorse or promote products derived from 1411527Sdavid.guillen@arm.com * this software without specific prior written permission. 1511527Sdavid.guillen@arm.com * 1611527Sdavid.guillen@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1711527Sdavid.guillen@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1811527Sdavid.guillen@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1911527Sdavid.guillen@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2011527Sdavid.guillen@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2111527Sdavid.guillen@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2211527Sdavid.guillen@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2311527Sdavid.guillen@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2411527Sdavid.guillen@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2511527Sdavid.guillen@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2611527Sdavid.guillen@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2711527Sdavid.guillen@arm.com */ 2811527Sdavid.guillen@arm.com 2911527Sdavid.guillen@arm.com#ifndef __REFCNT_HH__ 3011527Sdavid.guillen@arm.com#define __REFCNT_HH__ 3111527Sdavid.guillen@arm.com 3211527Sdavid.guillen@arm.comclass RefCounted 3311527Sdavid.guillen@arm.com{ 3411527Sdavid.guillen@arm.com private: 3511527Sdavid.guillen@arm.com int count; 3611527Sdavid.guillen@arm.com 3711527Sdavid.guillen@arm.com private: 3811527Sdavid.guillen@arm.com RefCounted(const RefCounted &); 3911527Sdavid.guillen@arm.com 4011527Sdavid.guillen@arm.com public: 4111527Sdavid.guillen@arm.com RefCounted() : count(0) {} 4211527Sdavid.guillen@arm.com virtual ~RefCounted() {} 4311527Sdavid.guillen@arm.com 4411527Sdavid.guillen@arm.com void incref() { ++count; } 4511800Sbrandon.potter@amd.com void decref() { if (--count <= 0) delete this; } 4611527Sdavid.guillen@arm.com}; 4711527Sdavid.guillen@arm.com 4811527Sdavid.guillen@arm.comtemplate <class T> 4911527Sdavid.guillen@arm.comclass RefCountingPtr 5011527Sdavid.guillen@arm.com{ 5111527Sdavid.guillen@arm.com private: 5211527Sdavid.guillen@arm.com T *data; 5311527Sdavid.guillen@arm.com 5411527Sdavid.guillen@arm.com void copy(T *d) { 5512546Sanouk.vanlaer@arm.com data = d; 5611527Sdavid.guillen@arm.com if (data) 5711527Sdavid.guillen@arm.com data->incref(); 5811527Sdavid.guillen@arm.com } 5911527Sdavid.guillen@arm.com void del() { 6011527Sdavid.guillen@arm.com if (data) 6111527Sdavid.guillen@arm.com data->decref(); 6211527Sdavid.guillen@arm.com } 6311527Sdavid.guillen@arm.com 6411527Sdavid.guillen@arm.com public: 6511527Sdavid.guillen@arm.com RefCountingPtr() : data(NULL) {} 6611527Sdavid.guillen@arm.com RefCountingPtr(T *data) { copy(data); } 6711527Sdavid.guillen@arm.com RefCountingPtr(const RefCountingPtr& r) { copy(r.data); } 6811527Sdavid.guillen@arm.com ~RefCountingPtr() { del(); } 6911527Sdavid.guillen@arm.com 7011527Sdavid.guillen@arm.com T *operator->() { return data; } 7111527Sdavid.guillen@arm.com T &operator*() { return *data; } 7211527Sdavid.guillen@arm.com T *get() { return data; } 7311527Sdavid.guillen@arm.com 7411527Sdavid.guillen@arm.com const T *operator->() const { return data; } 7511527Sdavid.guillen@arm.com const T &operator*() const { return *data; } 7611527Sdavid.guillen@arm.com const T *get() const { return data; } 7711527Sdavid.guillen@arm.com 7811527Sdavid.guillen@arm.com RefCountingPtr &operator=(T *p) { 7911527Sdavid.guillen@arm.com if (data != p) { 8011527Sdavid.guillen@arm.com del(); 8111527Sdavid.guillen@arm.com copy(p); 8211527Sdavid.guillen@arm.com } 8311527Sdavid.guillen@arm.com return *this; 8411527Sdavid.guillen@arm.com } 8511527Sdavid.guillen@arm.com 8611527Sdavid.guillen@arm.com RefCountingPtr &operator=(const RefCountingPtr& r) { 8711527Sdavid.guillen@arm.com if (data != r.data) { 8811527Sdavid.guillen@arm.com del(); 8911527Sdavid.guillen@arm.com copy(r.data); 9011527Sdavid.guillen@arm.com } 9111527Sdavid.guillen@arm.com return *this; 9211527Sdavid.guillen@arm.com } 9311527Sdavid.guillen@arm.com 9411527Sdavid.guillen@arm.com bool operator!() const { return data == 0; } 9511527Sdavid.guillen@arm.com operator bool() const { return data != 0; } 9611527Sdavid.guillen@arm.com}; 9712546Sanouk.vanlaer@arm.com 9812546Sanouk.vanlaer@arm.comtemplate<class T> 9912546Sanouk.vanlaer@arm.combool operator==(const RefCountingPtr<T> &l, const RefCountingPtr<T> &r) 10012546Sanouk.vanlaer@arm.com{ return l.get() == r.get(); } 10111527Sdavid.guillen@arm.com 10211527Sdavid.guillen@arm.comtemplate<class T> 10311527Sdavid.guillen@arm.combool operator==(const RefCountingPtr<T> &l, const T *r) 10411527Sdavid.guillen@arm.com{ return l.get() == r; } 10511527Sdavid.guillen@arm.com 10611527Sdavid.guillen@arm.comtemplate<class T> 10711527Sdavid.guillen@arm.combool operator==(const T &l, const RefCountingPtr<T> &r) 10811527Sdavid.guillen@arm.com{ return l == r.get(); } 10911527Sdavid.guillen@arm.com 11011527Sdavid.guillen@arm.comtemplate<class T> 11111527Sdavid.guillen@arm.combool operator!=(const RefCountingPtr<T> &l, const RefCountingPtr<T> &r) 11211527Sdavid.guillen@arm.com{ return l.get() != r.get(); } 11311527Sdavid.guillen@arm.com 11411527Sdavid.guillen@arm.comtemplate<class T> 11511527Sdavid.guillen@arm.combool operator!=(const RefCountingPtr<T> &l, const T *r) 11611527Sdavid.guillen@arm.com{ return l.get() != r; } 11711527Sdavid.guillen@arm.com 11811527Sdavid.guillen@arm.comtemplate<class T> 11911527Sdavid.guillen@arm.combool operator!=(const T &l, const RefCountingPtr<T> &r) 12011527Sdavid.guillen@arm.com{ return l != r.get(); } 12111527Sdavid.guillen@arm.com 12211527Sdavid.guillen@arm.com#endif // __REFCNT_HH__ 12311527Sdavid.guillen@arm.com