refcnt.hh revision 2665
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
312SN/A#ifndef __REFCNT_HH__
322SN/A#define __REFCNT_HH__
332SN/A
342147SN/A#include <stddef.h> //For the NULL macro definition
352147SN/A
362SN/Aclass RefCounted
372SN/A{
382SN/A  private:
392SN/A    int count;
402SN/A
412SN/A  private:
422SN/A    RefCounted(const RefCounted &);
432SN/A
442SN/A  public:
452SN/A    RefCounted() : count(0) {}
462SN/A    virtual ~RefCounted() {}
472SN/A
482SN/A    void incref() { ++count; }
492SN/A    void decref() { if (--count <= 0) delete this; }
502SN/A};
512SN/A
522SN/Atemplate <class T>
532SN/Aclass RefCountingPtr
542SN/A{
551078SN/A  protected:
562SN/A    T *data;
572SN/A
581114SN/A    void copy(T *d)
591114SN/A    {
602SN/A        data = d;
612SN/A        if (data)
622SN/A            data->incref();
632SN/A    }
641114SN/A    void del()
651114SN/A    {
662SN/A        if (data)
672SN/A            data->decref();
682SN/A    }
691114SN/A    void set(T *d)
701114SN/A    {
711114SN/A        if (data == d)
721114SN/A            return;
731114SN/A
741114SN/A        del();
751114SN/A        copy(d);
761114SN/A    }
771114SN/A
782SN/A
792SN/A  public:
802SN/A    RefCountingPtr() : data(NULL) {}
812SN/A    RefCountingPtr(T *data) { copy(data); }
82502SN/A    RefCountingPtr(const RefCountingPtr &r) { copy(r.data); }
832SN/A    ~RefCountingPtr() { del(); }
842SN/A
852SN/A    T *operator->() { return data; }
862SN/A    T &operator*() { return *data; }
872SN/A    T *get() { return data; }
882SN/A
892SN/A    const T *operator->() const { return data; }
902SN/A    const T &operator*() const { return *data; }
912SN/A    const T *get() const { return data; }
922SN/A
931114SN/A    RefCountingPtr &operator=(T *p) { set(p); return *this; }
941114SN/A    RefCountingPtr &operator=(const RefCountingPtr &r)
951114SN/A    { return operator=(r.data); }
962SN/A
972SN/A    bool operator!() const { return data == 0; }
982SN/A    operator bool() const { return data != 0; }
992SN/A};
1002SN/A
1012SN/Atemplate<class T>
1022SN/Abool operator==(const RefCountingPtr<T> &l, const RefCountingPtr<T> &r)
1032SN/A{ return l.get() == r.get(); }
1042SN/A
1052SN/Atemplate<class T>
1062SN/Abool operator==(const RefCountingPtr<T> &l, const T *r)
1072SN/A{ return l.get() == r; }
1082SN/A
1092SN/Atemplate<class T>
1102SN/Abool operator==(const T &l, const RefCountingPtr<T> &r)
1112SN/A{ return l == r.get(); }
1122SN/A
1132SN/Atemplate<class T>
1142SN/Abool operator!=(const RefCountingPtr<T> &l, const RefCountingPtr<T> &r)
1152SN/A{ return l.get() != r.get(); }
1162SN/A
1172SN/Atemplate<class T>
1182SN/Abool operator!=(const RefCountingPtr<T> &l, const T *r)
1192SN/A{ return l.get() != r; }
1202SN/A
1212SN/Atemplate<class T>
1222SN/Abool operator!=(const T &l, const RefCountingPtr<T> &r)
1232SN/A{ return l != r.get(); }
1242SN/A
1252SN/A#endif // __REFCNT_HH__
126