0a56b0680f8729ef252e6051681db7567dafccb2
[supertux.git] / src / refcounter.hpp
1 //  $Id$
2 //
3 //  Windstille - A Jump'n Shoot Game
4 //  Copyright (C) 2005 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 #ifndef __REFCOUNTER_HPP__
20 #define __REFCOUNTER_HPP__
21
22 #include <assert.h>
23
24 /**
25  * A base class that provides reference counting facilities
26  */
27 class RefCounter
28 {
29 public:
30   RefCounter()
31     : refcount(0)
32   { }
33
34   /** increases reference count */
35   void ref()
36   {
37     refcount++;
38   }
39   /** decreases reference count. Destroys the object if the reference count
40    * reaches 0
41    */
42   void unref()
43   {
44     refcount--;
45     if(refcount <= 0) {
46       delete this;
47       return;
48     }
49   }
50
51 protected:
52   virtual ~RefCounter()
53   {
54     assert(refcount == 0);
55   }
56
57 private:
58   int refcount;
59 };
60
61 #endif