]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - include/irefcount.h
set eol-style
[xonotic/netradiant.git] / include / irefcount.h
1 /*
2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21
22 #ifndef __IREFCOUNT_H__
23 #define __IREFCOUNT_H__
24
25 /*!
26 \class IRefCounted
27 \brief reference counted objects
28
29 general hints: how to use ref count properly
30   we consider ref counting as an extension of new and delete operators
31   the main issue is 'when to incref' 'when to decref'
32   in most cases connected to function calls
33   the general thinking about that:
34     - if you get a pointer to a refcounted object through a call to a function, assume that this object has been 'reserved' for you
35     already (i.e. allocated if you think this the new/delete way). so if you keep the object, you don't need to incref it, and if
36     you don't keep it you need to decref it.
37     - if you are called in a function and a refcounted object passed as parameter, then you should assume that this is an optional
38     object given to you FYI, which you don't need to decref if you don't keep / need to incref if you keep
39     
40   refcount is initialized to 1 in constructor. that serves for static objects and memory allocator
41   when you allocate in memory a ref counted object, it's default ref count will be 1, you should never delete it but just call DecRef on it
42   
43 define an interface and an implementation macro to make things easier
44 NOTE: we may have to provide a static library to go with that
45   in case we would move irecount.h out of here into libs/
46   
47 \todo functionality needed:
48 mostly enable/disable some features with compile time flags (independently from each other as much as possible)
49 - log the destructor calls with != 0 ref count
50 - log all incref/decref (with module info, and maybe even file/line number etc.?)
51 */
52 class IRefCounted
53 {
54   int refCount;
55 public:
56   IRefCounted() { refCount = 1; }
57   virtual ~IRefCounted() { }
58   void IncRef() { refCount++; }
59   void DecRef() { refCount--; if (refCount <= 0) delete this; }
60 };
61   
62 #endif // __ISYNAPSE_H__