]> Creatis software - gdcm.git/blob - src/gdcmRefCounter.h
* Add a RefCounter object that is deleted only when it's reference count is
[gdcm.git] / src / gdcmRefCounter.h
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmRefCounter.h,v $
5   Language:  C++
6   Date:      $Date: 2005/10/20 15:24:10 $
7   Version:   $Revision: 1.1 $
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 public:
35    gdcmTypeMacro(RefCounter);
36
37 public:
38 // Allocator / Unallocator
39    /// Delete the object
40    /// \remarks The object is deleted only if its reference counting is to zero
41    inline virtual void Delete(void)
42    {
43       Unregister();
44    }
45
46 // Reference count
47    /// Register the object
48    /// \remarks It increments the reference counting
49    inline void Register(void)
50    {
51       RefCount++;
52    }
53    /// Unregister the object
54    /// \remarks It decrements the reference counting
55    inline void Unregister(void)
56    {
57       RefCount--;
58       if(RefCount==0)
59       {
60          delete this;
61       }
62    }
63    /// Get the reference counting
64    /// \return Reference count
65    inline const unsigned long &GetRefCount(void) const
66    {
67       return(RefCount);
68    }
69
70 protected:
71    /// Constructor
72    RefCounter() { RefCount = 1; }
73    /// Destructor
74    virtual ~RefCounter() {}
75
76 private:
77    /// \brief Reference count
78    unsigned long RefCount;
79 };
80 } // end namespace gdcm
81
82 //-----------------------------------------------------------------------------
83 #endif