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