]> Creatis software - gdcm.git/blob - src/gdcmRefCounter.h
* Fix compilation error for the Python part
[gdcm.git] / src / gdcmRefCounter.h
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmRefCounter.h,v $
5   Language:  C++
6   Date:      $Date: 2005/10/21 11:50:04 $
7   Version:   $Revision: 1.5 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18
19 #ifndef GDCMREFCOUNTER_H
20 #define GDCMREFCOUNTER_H
21
22 #include "gdcmBase.h"
23 #include "gdcmDebug.h"
24
25 namespace gdcm 
26 {
27 //-----------------------------------------------------------------------------
28 /**
29  * \brief Integration of reference counting with a destruction of the
30  *        object only when the reference is to zero
31  */
32 class GDCM_EXPORT RefCounter : public Base
33 {
34    gdcmTypeMacro(RefCounter)
35
36 public:
37 // Allocator / Unallocator
38    /// Delete the object
39    /// \remarks The object is deleted only if its reference counting is to zero
40    inline void Delete(void)
41    {
42       Unregister();
43    }
44
45 // Reference count
46    /// Register the object
47    /// \remarks It increments the reference counting
48    inline void Register(void)
49    {
50       RefCount++;
51    }
52    /// Unregister the object
53    /// \remarks It decrements the reference counting
54    inline void Unregister(void)
55    {
56       RefCount--;
57       if(RefCount==0)
58       {
59          delete this;
60       }
61    }
62    /// Get the reference counting
63    /// \return Reference count
64    inline const unsigned long &GetRefCount(void) const
65    {
66       return(RefCount);
67    }
68
69 protected:
70    /// Constructor
71    RefCounter() { RefCount = 1; }
72    /// Destructor
73    virtual ~RefCounter() {}
74
75 private:
76    /// \brief Reference count
77    unsigned long RefCount;
78 };
79 } // end namespace gdcm
80
81 //-----------------------------------------------------------------------------
82 #endif