]> Creatis software - gdcm.git/blob - src/gdcmRefCounter.h
* Add Command and CommandManager to have possible callback
[gdcm.git] / src / gdcmRefCounter.h
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmRefCounter.h,v $
5   Language:  C++
6   Date:      $Date: 2005/11/28 15:20:34 $
7   Version:   $Revision: 1.9 $
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
24 namespace gdcm 
25 {
26 //-----------------------------------------------------------------------------
27 /**
28  * \brief Integration of reference counting with a destruction of the
29  *        object only when the reference is to zero
30  */
31 class GDCM_EXPORT RefCounter : public Base
32 {
33    gdcmTypeMacro(RefCounter);
34
35 public:
36 // Allocator / Unallocator
37    /// \brief Delete the object
38    /// \remarks The object is deleted only if its reference counting is to zero
39    void Delete() { Unregister(); }
40
41 // Reference count
42    /// \brief Register the object
43    /// \remarks It increments the reference counting
44    void Register() { RefCount++; }
45
46    /// \brief Unregister the object
47    /// \remarks It decrements the reference counting
48    void Unregister()
49    {
50       RefCount--;
51       if(RefCount<=0)
52          delete this;
53    }
54    /// \brief Get the reference counting
55    /// \return Reference count
56    const unsigned long &GetRefCount() const
57    {
58       return RefCount;
59    }
60
61 protected:
62    /// Constructor
63    RefCounter() { RefCount = 1; }
64    /// Destructor
65    virtual ~RefCounter() {}
66
67 private:
68    /// \brief Reference count
69    unsigned long RefCount;
70 };
71 } // end namespace gdcm
72
73 //-----------------------------------------------------------------------------
74 #endif