refcnt.hh revision 8220
110993Sjungma@eit.uni-kl.de/*
210993Sjungma@eit.uni-kl.de * Copyright (c) 2002-2005 The Regents of The University of Michigan
310993Sjungma@eit.uni-kl.de * All rights reserved.
410993Sjungma@eit.uni-kl.de *
510993Sjungma@eit.uni-kl.de * Redistribution and use in source and binary forms, with or without
610993Sjungma@eit.uni-kl.de * modification, are permitted provided that the following conditions are
710993Sjungma@eit.uni-kl.de * met: redistributions of source code must retain the above copyright
810993Sjungma@eit.uni-kl.de * notice, this list of conditions and the following disclaimer;
910993Sjungma@eit.uni-kl.de * redistributions in binary form must reproduce the above copyright
1010993Sjungma@eit.uni-kl.de * notice, this list of conditions and the following disclaimer in the
1110993Sjungma@eit.uni-kl.de * documentation and/or other materials provided with the distribution;
1210993Sjungma@eit.uni-kl.de * neither the name of the copyright holders nor the names of its
1310993Sjungma@eit.uni-kl.de * contributors may be used to endorse or promote products derived from
1410993Sjungma@eit.uni-kl.de * this software without specific prior written permission.
1510993Sjungma@eit.uni-kl.de *
1610993Sjungma@eit.uni-kl.de * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1710993Sjungma@eit.uni-kl.de * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1810993Sjungma@eit.uni-kl.de * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1910993Sjungma@eit.uni-kl.de * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2010993Sjungma@eit.uni-kl.de * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2110993Sjungma@eit.uni-kl.de * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2210993Sjungma@eit.uni-kl.de * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2310993Sjungma@eit.uni-kl.de * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2410993Sjungma@eit.uni-kl.de * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2510993Sjungma@eit.uni-kl.de * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2610993Sjungma@eit.uni-kl.de * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2710993Sjungma@eit.uni-kl.de *
2810993Sjungma@eit.uni-kl.de * Authors: Nathan Binkert
2910993Sjungma@eit.uni-kl.de */
3010993Sjungma@eit.uni-kl.de
3110993Sjungma@eit.uni-kl.de#ifndef __BASE_REFCNT_HH__
3210993Sjungma@eit.uni-kl.de#define __BASE_REFCNT_HH__
3310993Sjungma@eit.uni-kl.de
3410993Sjungma@eit.uni-kl.declass RefCounted
3510993Sjungma@eit.uni-kl.de{
3611099Sabdul.mutaal@gmail.com  private:
3710993Sjungma@eit.uni-kl.de    mutable int count;
3810993Sjungma@eit.uni-kl.de
3910993Sjungma@eit.uni-kl.de  private:
4010993Sjungma@eit.uni-kl.de    // Don't allow a default copy constructor or copy operator on
4110993Sjungma@eit.uni-kl.de    // these objects because the default operation will copy the
4210993Sjungma@eit.uni-kl.de    // reference count as well and we certainly don't want that.
4310993Sjungma@eit.uni-kl.de    RefCounted(const RefCounted &);
4410993Sjungma@eit.uni-kl.de    RefCounted &operator=(const RefCounted &);
4510993Sjungma@eit.uni-kl.de
4610993Sjungma@eit.uni-kl.de  public:
4710993Sjungma@eit.uni-kl.de    RefCounted() : count(0) {}
4810993Sjungma@eit.uni-kl.de    virtual ~RefCounted() {}
4910993Sjungma@eit.uni-kl.de
5010993Sjungma@eit.uni-kl.de    void incref() { ++count; }
5110993Sjungma@eit.uni-kl.de    void decref() { if (--count <= 0) delete this; }
5210993Sjungma@eit.uni-kl.de};
5310993Sjungma@eit.uni-kl.de
5410993Sjungma@eit.uni-kl.detemplate <class T>
5510993Sjungma@eit.uni-kl.declass RefCountingPtr
5610993Sjungma@eit.uni-kl.de{
5710993Sjungma@eit.uni-kl.de  protected:
5810993Sjungma@eit.uni-kl.de    T *data;
5910993Sjungma@eit.uni-kl.de
6010993Sjungma@eit.uni-kl.de    void copy(T *d)
6110993Sjungma@eit.uni-kl.de    {
6210993Sjungma@eit.uni-kl.de        data = d;
6310993Sjungma@eit.uni-kl.de        if (data)
6410993Sjungma@eit.uni-kl.de            data->incref();
6510993Sjungma@eit.uni-kl.de    }
6610993Sjungma@eit.uni-kl.de    void del()
6710993Sjungma@eit.uni-kl.de    {
6810993Sjungma@eit.uni-kl.de        if (data)
6910993Sjungma@eit.uni-kl.de            data->decref();
7010993Sjungma@eit.uni-kl.de    }
7110993Sjungma@eit.uni-kl.de    void set(T *d)
7210993Sjungma@eit.uni-kl.de    {
7310993Sjungma@eit.uni-kl.de        if (data == d)
7410993Sjungma@eit.uni-kl.de            return;
7510993Sjungma@eit.uni-kl.de
7610993Sjungma@eit.uni-kl.de        del();
7710993Sjungma@eit.uni-kl.de        copy(d);
7810993Sjungma@eit.uni-kl.de    }
7910993Sjungma@eit.uni-kl.de
8010993Sjungma@eit.uni-kl.de
8110993Sjungma@eit.uni-kl.de  public:
8210993Sjungma@eit.uni-kl.de    RefCountingPtr() : data(0) {}
8310993Sjungma@eit.uni-kl.de    RefCountingPtr(T *data) { copy(data); }
8410993Sjungma@eit.uni-kl.de    RefCountingPtr(const RefCountingPtr &r) { copy(r.data); }
8510993Sjungma@eit.uni-kl.de    ~RefCountingPtr() { del(); }
8610993Sjungma@eit.uni-kl.de
8710993Sjungma@eit.uni-kl.de    T *operator->() const { return data; }
8810993Sjungma@eit.uni-kl.de    T &operator*() const { return *data; }
8910993Sjungma@eit.uni-kl.de    T *get() const { return data; }
9010993Sjungma@eit.uni-kl.de
9110993Sjungma@eit.uni-kl.de    const RefCountingPtr &operator=(T *p) { set(p); return *this; }
9210993Sjungma@eit.uni-kl.de    const RefCountingPtr &operator=(const RefCountingPtr &r)
9310993Sjungma@eit.uni-kl.de    { return operator=(r.data); }
9410993Sjungma@eit.uni-kl.de
9510993Sjungma@eit.uni-kl.de    bool operator!() const { return data == 0; }
9610993Sjungma@eit.uni-kl.de    operator bool() const { return data != 0; }
97};
98
99template<class T>
100inline bool operator==(const RefCountingPtr<T> &l, const RefCountingPtr<T> &r)
101{ return l.get() == r.get(); }
102
103template<class T>
104inline bool operator==(const RefCountingPtr<T> &l, const T *r)
105{ return l.get() == r; }
106
107template<class T>
108inline bool operator==(const T *l, const RefCountingPtr<T> &r)
109{ return l == r.get(); }
110
111template<class T>
112inline bool operator!=(const RefCountingPtr<T> &l, const RefCountingPtr<T> &r)
113{ return l.get() != r.get(); }
114
115template<class T>
116inline bool operator!=(const RefCountingPtr<T> &l, const T *r)
117{ return l.get() != r; }
118
119template<class T>
120inline bool operator!=(const T *l, const RefCountingPtr<T> &r)
121{ return l != r.get(); }
122
123#endif // __BASE_REFCNT_HH__
124