From 6a7c2fbcb5bfc240a3b9875ad4836f5dc058e069 Mon Sep 17 00:00:00 2001 From: regrain Date: Thu, 20 Oct 2005 15:24:05 +0000 Subject: [PATCH] * Add a RefCounter object that is deleted only when it's reference count is to 0. * DicomEntry (thus, DictEntry) inherits from RefCounter... next stage to do, all classes inherit from RefCounter * Remove the VirtualDictEntry use, now useless due to the reference counting. So, there will be no memory leaks. -- BeNours --- Testing/TestDataEntry.cxx | 100 ++++++++++++++++++------------------ Testing/TestDict.cxx | 8 +-- gdcmPython/gdcm.i | 2 + src/CMakeLists.txt | 1 + src/gdcmBase.cxx | 28 +--------- src/gdcmBase.h | 10 ++-- src/gdcmCommon.h | 5 +- src/gdcmDefaultDicts.cxx.in | 8 +-- src/gdcmDicomEntry.h | 18 ++++--- src/gdcmDict.cxx | 48 +++++++++-------- src/gdcmDict.h | 10 ++-- src/gdcmDictEntry.cxx | 21 ++++++-- src/gdcmDictEntry.h | 18 +++++-- src/gdcmDictSet.cxx | 56 +++----------------- src/gdcmDictSet.h | 19 ++----- src/gdcmDocEntry.cxx | 17 +++++- src/gdcmDocEntry.h | 7 ++- src/gdcmDocEntrySet.cxx | 43 +++++++--------- src/gdcmDocEntrySet.h | 10 +--- src/gdcmDocument.cxx | 5 +- src/gdcmFile.cxx | 9 ++-- src/gdcmMacro.h | 29 +++++++++++ src/gdcmRefCounter.cxx | 40 +++++++++++++++ src/gdcmRefCounter.h | 83 ++++++++++++++++++++++++++++++ 24 files changed, 356 insertions(+), 239 deletions(-) create mode 100644 src/gdcmMacro.h create mode 100644 src/gdcmRefCounter.cxx create mode 100644 src/gdcmRefCounter.h diff --git a/Testing/TestDataEntry.cxx b/Testing/TestDataEntry.cxx index d57dd732..7c2977c6 100644 --- a/Testing/TestDataEntry.cxx +++ b/Testing/TestDataEntry.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: TestDataEntry.cxx,v $ Language: C++ - Date: $Date: 2005/10/20 08:58:15 $ - Version: $Revision: 1.4 $ + Date: $Date: 2005/10/20 15:24:05 $ + Version: $Revision: 1.5 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -40,7 +40,7 @@ int TestDataEntry(int , char *[]) gdcm::DataEntry *entry; //------------------------------------------------------------------ - dict = new gdcm::DictEntry(0x0000,0x0000); + dict = gdcm::DictEntry::New(0x0000,0x0000); entry = new gdcm::DataEntry(dict); dict->SetVR("US"); @@ -56,7 +56,7 @@ int TestDataEntry(int , char *[]) << " Number of content values is incorrect" << std::endl << " Found: " << entry->GetValueCount() << " - Must be: 1" << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } @@ -71,7 +71,7 @@ int TestDataEntry(int , char *[]) << " Number of content values is incorrect" << std::endl << " Found: " << entry->GetValueCount() << " - Must be: 2" << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } @@ -86,17 +86,17 @@ int TestDataEntry(int , char *[]) << " Number of content values is incorrect" << std::endl << " Found: " << entry->GetValueCount() << " - Must be: 0" << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } std::cout << std::endl; - delete dict; + dict->Delete(); delete entry; //------------------------------------------------------------------ - dict = new gdcm::DictEntry(0x0000,0x0000); + dict = gdcm::DictEntry::New(0x0000,0x0000); entry = new gdcm::DataEntry(dict); dict->SetVR("LT"); @@ -111,7 +111,7 @@ int TestDataEntry(int , char *[]) << " Size of string is incorrect" << std::endl << " Found: " << entry->GetLength() << " - Must be: " << strlen(data) + strlen(data)%2 << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } @@ -121,7 +121,7 @@ int TestDataEntry(int , char *[]) << " Number of content values is incorrect" << std::endl << " Found: " << entry->GetValueCount() << " - Must be: " << nbvalue << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } @@ -129,7 +129,7 @@ int TestDataEntry(int , char *[]) { std::cout << " Failed" << std::endl << " Content of bin area is incorrect" << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } @@ -139,7 +139,7 @@ int TestDataEntry(int , char *[]) << " Content of string is incorrect" << std::endl << " Found: " << entry->GetString().c_str() << " - Must be: " << data << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } @@ -151,18 +151,18 @@ int TestDataEntry(int , char *[]) << " Content of entry's values is incorrect : id " << i << std::endl << " Found " << entry->GetValue(i) << " - Must be " << svalue[i] << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } } std::cout << std::endl; - delete dict; + dict->Delete(); delete entry; //------------------------------------------------------------------ - dict = new gdcm::DictEntry(0x0000,0x0000); + dict = gdcm::DictEntry::New(0x0000,0x0000); entry = new gdcm::DataEntry(dict); dict->SetVR("US"); @@ -178,7 +178,7 @@ int TestDataEntry(int , char *[]) << " BinArea length is incorrect" << std::endl << " Found: " << entry->GetLength() << " - Must be: " << nbvalue*sizeof(uint16_t) << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } @@ -188,7 +188,7 @@ int TestDataEntry(int , char *[]) << " Content of string is incorrect" << std::endl << " Found: " << entry->GetString().c_str() << " - Must be: " << data << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } @@ -198,7 +198,7 @@ int TestDataEntry(int , char *[]) << " Number of content values is incorrect" << std::endl << " Found: " << entry->GetValueCount() << " - Must be: " << nbvalue << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } @@ -210,7 +210,7 @@ int TestDataEntry(int , char *[]) << " Content of entry's values is incorrect : id " << i << std::endl << " Found: " << entry->GetValue(i) << " - Must be: " << svalue[i] << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } @@ -228,7 +228,7 @@ int TestDataEntry(int , char *[]) << " Content of string is incorrect" << std::endl << " Found: " << entry->GetString().c_str() << " - Must be: " << data << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } @@ -238,7 +238,7 @@ int TestDataEntry(int , char *[]) << " Number of content values is incorrect" << std::endl << " Found: " << entry->GetValueCount() << " - Must be: " << nbvalue << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } @@ -250,18 +250,18 @@ int TestDataEntry(int , char *[]) << " Content of entry's values is incorrect : id " << i << std::endl << " Found: " << entry->GetValue(i) << " - Must be: " << svalue[i] << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } } std::cout << std::endl; - delete dict; + dict->Delete(); delete entry; //------------------------------------------------------------------ - dict = new gdcm::DictEntry(0x0000,0x0000); + dict = gdcm::DictEntry::New(0x0000,0x0000); entry = new gdcm::DataEntry(dict); dict->SetVR("UL"); @@ -277,7 +277,7 @@ int TestDataEntry(int , char *[]) << " BinArea length is incorrect" << std::endl << " Found: " << entry->GetLength() << " - Must be: " << nbvalue*sizeof(uint32_t) << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } @@ -287,7 +287,7 @@ int TestDataEntry(int , char *[]) << " Content of string is incorrect" << std::endl << " Found: " << entry->GetString().c_str() << " - Must be: " << data << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } @@ -297,7 +297,7 @@ int TestDataEntry(int , char *[]) << " Number of content values is incorrect" << std::endl << " Found: " << entry->GetValueCount() << " - Must be: " << nbvalue << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } @@ -309,7 +309,7 @@ int TestDataEntry(int , char *[]) << " Content of entry's values is incorrect : id " << i << std::endl << " Found: " << entry->GetValue(i) << " - Must be: " << lvalue[i] << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } @@ -327,7 +327,7 @@ int TestDataEntry(int , char *[]) << " Content of string is incorrect" << std::endl << " Found: " << entry->GetString().c_str() << " - Must be: " << data << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } @@ -337,7 +337,7 @@ int TestDataEntry(int , char *[]) << " Number of content values is incorrect" << std::endl << " Found: " << entry->GetValueCount() << " - Must be: " << nbvalue << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } @@ -349,18 +349,18 @@ int TestDataEntry(int , char *[]) << " Content of entry's values is incorrect : id " << i << std::endl << " Found: " << entry->GetValue(i) << " - Must be: " << lvalue[i] << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } } std::cout << std::endl; - delete dict; + dict->Delete(); delete entry; //------------------------------------------------------------------ - dict = new gdcm::DictEntry(0x0000,0x0000); + dict = gdcm::DictEntry::New(0x0000,0x0000); entry = new gdcm::DataEntry(dict); dict->SetVR("FL"); @@ -376,7 +376,7 @@ int TestDataEntry(int , char *[]) << " BinArea length is incorrect" << std::endl << " Found: " << entry->GetLength() << " - Must be: " << nbvalue*sizeof(float) << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } @@ -386,7 +386,7 @@ int TestDataEntry(int , char *[]) << " Content of string is incorrect" << std::endl << " Found: " << entry->GetString().c_str() << " - Must be: " << fdata << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } @@ -396,7 +396,7 @@ int TestDataEntry(int , char *[]) << " Number of content values is incorrect" << std::endl << " Found: " << entry->GetValueCount() << " - Must be: " << nbvalue << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } @@ -408,7 +408,7 @@ int TestDataEntry(int , char *[]) << " Content of entry's values is incorrect : id " << i << std::endl << " Found: " << entry->GetValue(i) << " - Must be: " << fvalue[i] << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } @@ -426,7 +426,7 @@ int TestDataEntry(int , char *[]) << " Content of string is incorrect" << std::endl << " Found: " << entry->GetString().c_str() << " - Must be: " << fdata << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } @@ -436,7 +436,7 @@ int TestDataEntry(int , char *[]) << " Number of content values is incorrect" << std::endl << " Found: " << entry->GetValueCount() << " - Must be: " << nbvalue << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } @@ -448,18 +448,18 @@ int TestDataEntry(int , char *[]) << " Content of entry's values is incorrect : id " << i << std::endl << " Found: " << entry->GetValue(i) << " - Must be: " << fvalue[i] << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } } std::cout << std::endl; - delete dict; + dict->Delete(); delete entry; //------------------------------------------------------------------ - dict = new gdcm::DictEntry(0x0000,0x0000); + dict = gdcm::DictEntry::New(0x0000,0x0000); entry = new gdcm::DataEntry(dict); dict->SetVR("FD"); @@ -475,7 +475,7 @@ int TestDataEntry(int , char *[]) << " BinArea length is incorrect" << std::endl << " Found: " << entry->GetLength() << " - Must be: " << nbvalue*sizeof(double) << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } @@ -485,7 +485,7 @@ int TestDataEntry(int , char *[]) << " Content of string is incorrect" << std::endl << " Found: " << entry->GetString().c_str() << " - Must be: " << fdata << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } @@ -495,7 +495,7 @@ int TestDataEntry(int , char *[]) << " Number of content values is incorrect" << std::endl << " Found: " << entry->GetValueCount() << " - Must be: " << nbvalue << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } @@ -507,7 +507,7 @@ int TestDataEntry(int , char *[]) << " Content of entry's values is incorrect : id " << i << std::endl << " Found: " << entry->GetValue(i) << " - Must be: " << dvalue[i] << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } @@ -525,7 +525,7 @@ int TestDataEntry(int , char *[]) << " Content of string is incorrect" << std::endl << " Found: " << entry->GetString().c_str() << " - Must be: " << fdata << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } @@ -535,7 +535,7 @@ int TestDataEntry(int , char *[]) << " Number of content values is incorrect" << std::endl << " Found: " << entry->GetValueCount() << " - Must be: " << nbvalue << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } @@ -547,14 +547,14 @@ int TestDataEntry(int , char *[]) << " Content of entry's values is incorrect : id " << i << std::endl << " Found: " << entry->GetValue(i) << " - Must be: " << dvalue[i] << std::endl; - delete dict; + dict->Delete(); delete entry; return(1); } } std::cout << std::endl; - delete dict; + dict->Delete(); delete entry; //------------------------------------------------------------------ diff --git a/Testing/TestDict.cxx b/Testing/TestDict.cxx index a883b477..232bbb01 100644 --- a/Testing/TestDict.cxx +++ b/Testing/TestDict.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: TestDict.cxx,v $ Language: C++ - Date: $Date: 2005/07/11 14:53:15 $ - Version: $Revision: 1.8 $ + Date: $Date: 2005/10/20 15:24:05 $ + Version: $Revision: 1.9 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -48,7 +48,7 @@ int TestDict(int , char *[]) std::cout<<"DictSet content :\n"; - gdcm::Dict *d = dicts->GetFirstEntry(); + gdcm::Dict *d = dicts->GetFirstDict(); if (!d) { std::cout << "Dictset is empty" << std::endl; @@ -63,7 +63,7 @@ int TestDict(int , char *[]) { std::cout << "------------- a Dict is found : ----------" << std::endl; d->Print(); - d = dicts->GetNextEntry(); + d = dicts->GetNextDict(); } // Print the Dict (public) diff --git a/gdcmPython/gdcm.i b/gdcmPython/gdcm.i index 1849a9a0..a3bdeda9 100644 --- a/gdcmPython/gdcm.i +++ b/gdcmPython/gdcm.i @@ -5,6 +5,7 @@ #include "gdcmCommon.h" #include "gdcmBase.h" +#include "gdcmRefCounter.h" #include "gdcmTagKey.h" #include "gdcmVRKey.h" #include "gdcmDict.h" @@ -242,6 +243,7 @@ typedef unsigned long long uint64_t; // Warning: Order matters ! %include "gdcmCommon.h" %include "gdcmBase.h" +%include "gdcmRefCounter.h" %include "gdcmTagKey.h" %include "gdcmVRKey.h" %include "gdcmDicomEntry.h" diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3b1710f7..51109483 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -58,6 +58,7 @@ SET(libgdcm_la_SOURCES gdcmOrientation.cxx gdcmPixelReadConvert.cxx gdcmPixelWriteConvert.cxx + gdcmRefCounter.cxx gdcmRLEFrame.cxx gdcmRLEFramesInfo.cxx gdcmSeqEntry.cxx diff --git a/src/gdcmBase.cxx b/src/gdcmBase.cxx index 3a94b08b..873e442f 100644 --- a/src/gdcmBase.cxx +++ b/src/gdcmBase.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmBase.cxx,v $ Language: C++ - Date: $Date: 2005/02/01 10:29:54 $ - Version: $Revision: 1.9 $ + Date: $Date: 2005/10/20 15:24:08 $ + Version: $Revision: 1.10 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -22,22 +22,6 @@ namespace gdcm { //------------------------------------------------------------------------- // Constructor / Destructor -/** - * \brief canonical constructor - */ -Base::Base( ) -{ - PrintLevel = 0; -} - -/** - * \brief canonical destructor - * \note If the Header was created by the File constructor, - * it is destroyed by the File - */ -Base::~Base() -{ -} //----------------------------------------------------------------------------- // Public @@ -50,14 +34,6 @@ Base::~Base() //----------------------------------------------------------------------------- // Print -/** - * \brief Print all the object - * @param os The output stream to be written to. - * @param indent Indentation string to be prepended during printing - */ -void Base::Print(std::ostream &, std::string const & ) -{ -} //----------------------------------------------------------------------------- } // end namespace gdcm diff --git a/src/gdcmBase.h b/src/gdcmBase.h index c8151f2d..3e6097d8 100644 --- a/src/gdcmBase.h +++ b/src/gdcmBase.h @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmBase.h,v $ Language: C++ - Date: $Date: 2005/09/02 07:10:03 $ - Version: $Revision: 1.7 $ + Date: $Date: 2005/10/20 15:24:08 $ + Version: $Revision: 1.8 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -34,11 +34,11 @@ namespace gdcm class GDCM_EXPORT Base { public: - Base( ); - virtual ~Base(); + Base( ) {PrintLevel = 0;} + virtual ~Base() {} virtual void Print(std::ostream &os = std::cout, - std::string const & indent = "" ); + std::string const & indent = "" ) {}; /// \brief Sets the print level for the Dicom Header Elements /// \note 0 for Light Print; 1 for 'medium' Print, 2 for Heavy Print diff --git a/src/gdcmCommon.h b/src/gdcmCommon.h index e7875ede..1361a754 100644 --- a/src/gdcmCommon.h +++ b/src/gdcmCommon.h @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmCommon.h,v $ Language: C++ - Date: $Date: 2005/10/19 13:17:04 $ - Version: $Revision: 1.99 $ + Date: $Date: 2005/10/20 15:24:08 $ + Version: $Revision: 1.100 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -21,6 +21,7 @@ #include "gdcmConfigure.h" #include "gdcmSystem.h" +#include "gdcmMacro.h" #include diff --git a/src/gdcmDefaultDicts.cxx.in b/src/gdcmDefaultDicts.cxx.in index 6418c31e..21d39605 100644 --- a/src/gdcmDefaultDicts.cxx.in +++ b/src/gdcmDefaultDicts.cxx.in @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmDefaultDicts.cxx.in,v $ Language: C++ - Date: $Date: 2005/10/19 12:01:50 $ - Version: $Revision: 1.10 $ + Date: $Date: 2005/10/20 15:24:08 $ + Version: $Revision: 1.11 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -44,12 +44,14 @@ static DICT_ENTRY datadir[] = { void FillDefaultDataDict(Dict *d) { + DictEntry *e; unsigned int i = 0; DICT_ENTRY n = datadir[i]; while( n.name != 0 ) { - const DictEntry e( n.group, n.element, n.vr, n.vm, n.name); + e = DictEntry::New( n.group, n.element, n.vr, n.vm, n.name); d->AddEntry( e ); + e->Delete(); n = datadir[++i]; } } diff --git a/src/gdcmDicomEntry.h b/src/gdcmDicomEntry.h index d74dca86..ddd9eaa7 100644 --- a/src/gdcmDicomEntry.h +++ b/src/gdcmDicomEntry.h @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmDicomEntry.h,v $ Language: C++ - Date: $Date: 2005/10/19 13:17:05 $ - Version: $Revision: 1.1 $ + Date: $Date: 2005/10/20 15:24:08 $ + Version: $Revision: 1.2 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -20,7 +20,7 @@ #define GDCMDICOMENTRY_H #include "gdcmCommon.h" -#include "gdcmBase.h" +#include "gdcmRefCounter.h" #include "gdcmVRKey.h" #include "gdcmTagKey.h" @@ -38,13 +38,12 @@ namespace gdcm * - the VM (Value Multplicity) * - the corresponding name in english */ -class GDCM_EXPORT DicomEntry : public Base +class GDCM_EXPORT DicomEntry : public RefCounter { public: - DicomEntry(const uint16_t &group,const uint16_t &elt, - const VRKey &vr = GDCM_VRUNKNOWN); - ~DicomEntry(); + gdcmTypeMacro(DicomEntry); +public: // Print void Print(std::ostream &os = std::cout, std::string const &indent = ""); @@ -71,6 +70,11 @@ public: // Key creation static TagKey TranslateToKey(uint16_t group, uint16_t elem); +protected: + DicomEntry(const uint16_t &group,const uint16_t &elt, + const VRKey &vr = GDCM_VRUNKNOWN); + ~DicomEntry(); + private: /// Dicom \ref TagKey. Contains DicomGroup number and DicomElement number TagKey Tag; diff --git a/src/gdcmDict.cxx b/src/gdcmDict.cxx index 321f168a..b01a84f6 100644 --- a/src/gdcmDict.cxx +++ b/src/gdcmDict.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmDict.cxx,v $ Language: C++ - Date: $Date: 2005/10/18 12:58:27 $ - Version: $Revision: 1.80 $ + Date: $Date: 2005/10/20 15:24:08 $ + Version: $Revision: 1.81 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -126,7 +126,7 @@ bool Dict::RemoveDict(std::string const &filename) // from >> std::ws; //remove white space std::getline(from, name); - RemoveEntry(DictEntry::TranslateToKey(group, elem)); + RemoveEntry(group,elem); } from.close(); return true; @@ -138,9 +138,9 @@ bool Dict::RemoveDict(std::string const &filename) * @param newEntry entry to add * @return false if Dicom Element already exists */ -bool Dict::AddEntry(DictEntry const &newEntry) +bool Dict::AddEntry(DictEntry *newEntry) { - const TagKey &key = newEntry.GetKey(); + const TagKey &key = newEntry->GetKey(); if ( KeyHt.count(key) == 1 ) { @@ -149,7 +149,8 @@ bool Dict::AddEntry(DictEntry const &newEntry) } else { - KeyHt.insert( TagKeyHT::value_type(newEntry.GetKey(), newEntry)); + newEntry->Register(); + KeyHt.insert( TagKeyHT::value_type(key, newEntry)); return true; } } @@ -159,12 +160,14 @@ bool Dict::AddEntry(DictEntry const &newEntry) * @param newEntry new entry (overwrites any previous one with same tag) * @return false if Dicom Element doesn't exist */ -bool Dict::ReplaceEntry(DictEntry const &newEntry) +bool Dict::ReplaceEntry(DictEntry *newEntry) { - if ( RemoveEntry(newEntry.GetKey()) ) + const TagKey &key = newEntry->GetKey(); + if ( RemoveEntry(key) ) { - KeyHt.insert( TagKeyHT::value_type(newEntry.GetKey(), newEntry)); - return true; + newEntry->Register(); + KeyHt.insert( TagKeyHT::value_type(key, newEntry)); + return true; } return false; } @@ -180,6 +183,7 @@ bool Dict::RemoveEntry(TagKey const &key) TagKeyHT::const_iterator it = KeyHt.find(key); if ( it != KeyHt.end() ) { + it->second->Unregister(); KeyHt.erase(key); return true; @@ -210,6 +214,9 @@ void Dict::ClearEntry() { // we assume all the pointed DictEntries are already cleaned-up // when we clean KeyHt. + TagKeyHT::const_iterator it; + for(it = KeyHt.begin();it!=KeyHt.end();++it) + it->second->Unregister(); KeyHt.clear(); } @@ -225,7 +232,7 @@ DictEntry *Dict::GetEntry(TagKey const &key) { return 0; } - return &(it->second); + return it->second; } DictEntry *Dict::GetEntry(uint16_t group, uint16_t elem) @@ -236,7 +243,7 @@ DictEntry *Dict::GetEntry(uint16_t group, uint16_t elem) { return 0; } - return &(it->second); + return it->second; } /** @@ -247,7 +254,7 @@ DictEntry *Dict::GetFirstEntry() { ItKeyHt = KeyHt.begin(); if ( ItKeyHt != KeyHt.end() ) - return &(ItKeyHt->second); + return ItKeyHt->second; return NULL; } @@ -262,7 +269,7 @@ DictEntry *Dict::GetNextEntry() ++ItKeyHt; if (ItKeyHt != KeyHt.end()) - return &(ItKeyHt->second); + return ItKeyHt->second; return NULL; } @@ -283,6 +290,7 @@ void Dict::DoTheLoadingJob(std::ifstream &from) TagName vm; TagName name; + DictEntry *newEntry; while (!from.eof() && from) { from >> std::hex; @@ -293,8 +301,9 @@ void Dict::DoTheLoadingJob(std::ifstream &from) from >> std::ws; //remove white space std::getline(from, name); - DictEntry newEntry(group, elem, vr, vm, name); + newEntry = DictEntry::New(group, elem, vr, vm, name); AddEntry(newEntry); + newEntry->Delete(); } from.close(); } @@ -314,12 +323,11 @@ void Dict::Print(std::ostream &os, std::string const & ) for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag) { s << "Entry : "; - s << "(" << std::hex << std::setw(4) << tag->second.GetGroup() << ','; - s << std::hex << std::setw(4) << tag->second.GetElement() << ") = " + s << "(" << tag->second->GetKey() << ") = " << std::dec; - s << tag->second.GetVR() << ", "; - s << tag->second.GetVM() << ", "; - s << tag->second.GetName() << "." << std::endl; + s << tag->second->GetVR() << ", "; + s << tag->second->GetVM() << ", "; + s << tag->second->GetName() << "." << std::endl; } os << s.str(); } diff --git a/src/gdcmDict.h b/src/gdcmDict.h index 06b78f3e..455afd0d 100644 --- a/src/gdcmDict.h +++ b/src/gdcmDict.h @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmDict.h,v $ Language: C++ - Date: $Date: 2005/10/20 08:58:18 $ - Version: $Revision: 1.42 $ + Date: $Date: 2005/10/20 15:24:08 $ + Version: $Revision: 1.43 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -32,7 +32,7 @@ namespace gdcm //----------------------------------------------------------------------------- typedef std::string DictKey; -typedef std::map TagKeyHT; +typedef std::map TagKeyHT; //----------------------------------------------------------------------------- /** @@ -57,8 +57,8 @@ public: void Print(std::ostream &os = std::cout, std::string const &indent = ""); // Entries - bool AddEntry(DictEntry const &newEntry); - bool ReplaceEntry(DictEntry const &newEntry); + bool AddEntry(DictEntry *newEntry); + bool ReplaceEntry(DictEntry *newEntry); bool RemoveEntry (TagKey const &key); bool RemoveEntry (uint16_t group, uint16_t elem); void ClearEntry(); diff --git a/src/gdcmDictEntry.cxx b/src/gdcmDictEntry.cxx index cfe420d8..4939bf63 100644 --- a/src/gdcmDictEntry.cxx +++ b/src/gdcmDictEntry.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmDictEntry.cxx,v $ Language: C++ - Date: $Date: 2005/10/19 13:17:04 $ - Version: $Revision: 1.53 $ + Date: $Date: 2005/10/20 15:24:08 $ + Version: $Revision: 1.54 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -36,7 +36,6 @@ namespace gdcm * @param vm Value Multiplicity * @param name description of the element */ - DictEntry::DictEntry(uint16_t group, uint16_t elem, VRKey const &vr, TagName const &vm, @@ -49,6 +48,22 @@ DictEntry::DictEntry(uint16_t group, uint16_t elem, //----------------------------------------------------------------------------- // Public +/** + * \brief Class allocator + * @param group DICOM-Group Number + * @param elem DICOM-Element Number + * @param vr Value Representation + * @param vm Value Multiplicity + * @param name description of the element +*/ +DictEntry *DictEntry::New(uint16_t group, uint16_t elem, + VRKey const &vr, + TagName const &vm, + TagName const &name) +{ + return new DictEntry(group,elem,vr,vm,name); +} + /** * \brief If-and only if-the V(alue) R(epresentation) * \ is unset then overwrite it. diff --git a/src/gdcmDictEntry.h b/src/gdcmDictEntry.h index 2be34877..9c0e679f 100644 --- a/src/gdcmDictEntry.h +++ b/src/gdcmDictEntry.h @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmDictEntry.h,v $ Language: C++ - Date: $Date: 2005/10/19 13:17:04 $ - Version: $Revision: 1.38 $ + Date: $Date: 2005/10/20 15:24:08 $ + Version: $Revision: 1.39 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -38,8 +38,10 @@ namespace gdcm class GDCM_EXPORT DictEntry : public DicomEntry { public: - DictEntry(uint16_t group, - uint16_t elem, + gdcmTypeMacro(DictEntry); + +public: + static DictEntry *New(uint16_t group, uint16_t elem, VRKey const &vr = GDCM_VRUNKNOWN, TagName const &vm = GDCM_UNKNOWN, TagName const &name = GDCM_UNKNOWN); @@ -62,7 +64,13 @@ public: /// e.g. "Patient Name" for Dicom Tag (0x0010, 0x0010) /// @return the Dicom Name const TagName &GetName() const { return Name; } - + +protected: + DictEntry(uint16_t group, uint16_t elem, + VRKey const &vr = GDCM_VRUNKNOWN, + TagName const &vm = GDCM_UNKNOWN, + TagName const &name = GDCM_UNKNOWN); + private: /// \brief Value Multiplicity (e.g. "1", "1-n", "6") TagName VM; diff --git a/src/gdcmDictSet.cxx b/src/gdcmDictSet.cxx index 2e7167d0..32a5023a 100644 --- a/src/gdcmDictSet.cxx +++ b/src/gdcmDictSet.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmDictSet.cxx,v $ Language: C++ - Date: $Date: 2005/10/20 09:23:24 $ - Version: $Revision: 1.70 $ + Date: $Date: 2005/10/20 15:24:08 $ + Version: $Revision: 1.71 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -55,9 +55,6 @@ DictSet::~DictSet() tag->second = NULL; } Dicts.clear(); - - // Remove virtual dictionary entries - VirtualEntries.clear(); } //----------------------------------------------------------------------------- @@ -96,51 +93,10 @@ Dict *DictSet::GetDict(DictKey const &dictName) } /** - * \brief Create a DictEntry which will be referenced in no dictionary - * @param group Group number of the Entry - * @param elem Element number of the Entry - * @param vr Value Representation of the Entry - * @param vm Value Multiplicity of the Entry - * @param name English name of the Entry - * @return virtual entry - */ -DictEntry *DictSet::NewVirtualDictEntry( uint16_t group, - uint16_t elem, - const VRKey &vr, - const TagName &vm, - const TagName &name) -{ - DictEntry *entry; - - // Let's follow 'Purify' advice - // const std::string tag = DictEntry::TranslateToKey(group,elem) - // + "#" + vr + "#" + vm + "#" + name; - ExtendedTagKey tag = DictEntry::TranslateToKey(group,elem).str(); - tag += "#" + vr.str() + "#" + vm + "#" + name; - - ExtendedTagKeyHT::iterator it; - - it = VirtualEntries.find(tag); - if ( it != VirtualEntries.end() ) - { - entry = &(it->second); - } - else - { - DictEntry ent(group, elem, vr, vm, name); - VirtualEntries.insert( - ExtendedTagKeyHT::value_type(tag, ent) ); - entry = &(VirtualEntries.find(tag)->second); - } - - return entry; -} - -/** - * \brief Get the first entry while visiting the DictSet + * \brief Get the first dictionary while visiting the DictSet * \return The first Dict if found, otherwhise NULL */ -Dict *DictSet::GetFirstEntry() +Dict *DictSet::GetFirstDict() { ItDictHt = Dicts.begin(); if ( ItDictHt != Dicts.end() ) @@ -149,11 +105,11 @@ Dict *DictSet::GetFirstEntry() } /** - * \brief Get the next entry while visiting the Hash table (DictSetHT) + * \brief Get the next dictionary while visiting the Hash table (DictSetHT) * \note : meaningfull only if GetFirstEntry already called * \return The next Dict if found, otherwhise NULL */ -Dict *DictSet::GetNextEntry() +Dict *DictSet::GetNextDict() { gdcmAssertMacro (ItDictHt != Dicts.end()); diff --git a/src/gdcmDictSet.h b/src/gdcmDictSet.h index c05e1b5c..237bbe80 100644 --- a/src/gdcmDictSet.h +++ b/src/gdcmDictSet.h @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmDictSet.h,v $ Language: C++ - Date: $Date: 2005/10/18 12:58:28 $ - Version: $Revision: 1.46 $ + Date: $Date: 2005/10/20 15:24:09 $ + Version: $Revision: 1.47 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -27,9 +27,8 @@ namespace gdcm { +//----------------------------------------------------------------------------- typedef std::map DictSetHT; -typedef std::string ExtendedTagKey; -typedef std::map ExtendedTagKeyHT; //----------------------------------------------------------------------------- /** @@ -63,13 +62,8 @@ public: // \ warning : not end user intended // Dict *GetVirtualDict() { return &VirtualEntries; } - DictEntry *NewVirtualDictEntry(uint16_t group, uint16_t elem, - const VRKey &vr = GDCM_VRUNKNOWN, - const TagName &vm = GDCM_UNKNOWN, - const TagName &name = GDCM_UNKNOWN); - - Dict *GetFirstEntry(); - Dict *GetNextEntry(); + Dict *GetFirstDict(); + Dict *GetNextDict(); static std::string BuildDictPath(); @@ -83,9 +77,6 @@ private: /// Directory path to dictionaries std::string DictPath; - - /// H table for the on the fly created DictEntries - ExtendedTagKeyHT VirtualEntries; }; } // end namespace gdcm diff --git a/src/gdcmDocEntry.cxx b/src/gdcmDocEntry.cxx index 18dfdf57..e54f0c98 100644 --- a/src/gdcmDocEntry.cxx +++ b/src/gdcmDocEntry.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmDocEntry.cxx,v $ Language: C++ - Date: $Date: 2005/10/20 07:29:43 $ - Version: $Revision: 1.74 $ + Date: $Date: 2005/10/20 15:24:09 $ + Version: $Revision: 1.75 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -44,6 +44,19 @@ DocEntry::DocEntry(DictEntry *in) // init some variables ReadLength = 0; Length = 0; + + gdcmAssertMacro(DicomDict); + DicomDict->Register(); +} + +/** + * \brief Destructor from a given DictEntry + */ +DocEntry::~DocEntry() +{ + gdcmAssertMacro(DicomDict); + + DicomDict->Unregister(); } //----------------------------------------------------------------------------- diff --git a/src/gdcmDocEntry.h b/src/gdcmDocEntry.h index cf12bdfa..acd1eb73 100644 --- a/src/gdcmDocEntry.h +++ b/src/gdcmDocEntry.h @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmDocEntry.h,v $ Language: C++ - Date: $Date: 2005/10/19 13:17:04 $ - Version: $Revision: 1.52 $ + Date: $Date: 2005/10/20 15:24:09 $ + Version: $Revision: 1.53 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -39,8 +39,7 @@ class GDCM_EXPORT DocEntry : public Base { public: DocEntry(DictEntry*); - /// \brief Canonical Destructor - virtual ~DocEntry() {} + virtual ~DocEntry(); virtual void Print (std::ostream &os = std::cout, std::string const &indent = ""); virtual void WriteContent(std::ofstream *fp, FileType filetype); diff --git a/src/gdcmDocEntrySet.cxx b/src/gdcmDocEntrySet.cxx index 0b4ff26d..8a622d92 100644 --- a/src/gdcmDocEntrySet.cxx +++ b/src/gdcmDocEntrySet.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmDocEntrySet.cxx,v $ Language: C++ - Date: $Date: 2005/10/18 12:58:28 $ - Version: $Revision: 1.61 $ + Date: $Date: 2005/10/20 15:24:09 $ + Version: $Revision: 1.62 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -448,9 +448,9 @@ DataEntry *DocEntrySet::NewDataEntry(uint16_t group,uint16_t elem, VRKey const &vr) { DictEntry *dictEntry = GetDictEntry(group, elem, vr); - gdcmAssertMacro(dictEntry); DataEntry *newEntry = new DataEntry(dictEntry); + dictEntry->Unregister(); // GetDictEntry register it if (!newEntry) { gdcmWarningMacro( "Failed to allocate DataEntry"); @@ -469,9 +469,9 @@ DataEntry *DocEntrySet::NewDataEntry(uint16_t group,uint16_t elem, SeqEntry* DocEntrySet::NewSeqEntry(uint16_t group, uint16_t elem) { DictEntry *dictEntry = GetDictEntry(group, elem, "SQ"); - gdcmAssertMacro(dictEntry); SeqEntry *newEntry = new SeqEntry( dictEntry ); + dictEntry->Unregister(); // GetDictEntry register it if (!newEntry) { gdcmWarningMacro( "Failed to allocate SeqEntry"); @@ -480,22 +480,6 @@ SeqEntry* DocEntrySet::NewSeqEntry(uint16_t group, uint16_t elem) return newEntry; } -/** - * \brief Request a new virtual dict entry to the dict set - * @param group Group number of the underlying DictEntry - * @param elem Element number of the underlying DictEntry - * @param vr V(alue) R(epresentation) of the underlying DictEntry - * @param vm V(alue) M(ultiplicity) of the underlying DictEntry - * @param name english name - */ -DictEntry* DocEntrySet::NewVirtualDictEntry( uint16_t group, uint16_t elem, - VRKey const &vr, - TagName const &vm, - TagName const &name ) -{ - return Global::GetDicts()->NewVirtualDictEntry(group,elem,vr,vm,name); -} - //----------------------------------------------------------------------------- // Protected /** @@ -506,6 +490,7 @@ DictEntry* DocEntrySet::NewVirtualDictEntry( uint16_t group, uint16_t elem, * @param group Group number of the searched DictEntry * @param elem Element number of the searched DictEntry * @return Corresponding DictEntry when it exists, NULL otherwise. + * \remarks The returned DictEntry is registered when existing */ DictEntry *DocEntrySet::GetDictEntry(uint16_t group,uint16_t elem) { @@ -517,7 +502,9 @@ DictEntry *DocEntrySet::GetDictEntry(uint16_t group,uint16_t elem) } else { - found = pubDict->GetEntry(group, elem); + found = pubDict->GetEntry(group, elem); + if( found ) + found->Register(); } return found; } @@ -530,6 +517,7 @@ DictEntry *DocEntrySet::GetDictEntry(uint16_t group,uint16_t elem) * @param elem element number of the searched DictEntry * @param vr V(alue) R(epresentation) to use, if necessary * @return Corresponding DictEntry when it exists, NULL otherwise. + * \remarks The returned DictEntry is registered */ DictEntry *DocEntrySet::GetDictEntry(uint16_t group, uint16_t elem, VRKey const &vr) @@ -538,7 +526,8 @@ DictEntry *DocEntrySet::GetDictEntry(uint16_t group, uint16_t elem, DictEntry *goodEntry = dictEntry; VRKey goodVR = vr; - if (elem == 0x0000) goodVR="UL"; + if (elem == 0x0000) + goodVR="UL"; if ( goodEntry ) { @@ -554,14 +543,18 @@ DictEntry *DocEntrySet::GetDictEntry(uint16_t group, uint16_t elem, { if (dictEntry) { - goodEntry = NewVirtualDictEntry(group, elem, goodVR, "FIXME", - dictEntry->GetName() ); + goodEntry = DictEntry::New(group, elem, goodVR, "FIXME", + dictEntry->GetName() ); } else { - goodEntry = NewVirtualDictEntry(group, elem, goodVR); + goodEntry = DictEntry::New(group, elem, goodVR); } } + else + { + goodEntry->Register(); + } return goodEntry; } diff --git a/src/gdcmDocEntrySet.h b/src/gdcmDocEntrySet.h index bcf22dd3..0173958a 100644 --- a/src/gdcmDocEntrySet.h +++ b/src/gdcmDocEntrySet.h @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmDocEntrySet.h,v $ Language: C++ - Date: $Date: 2005/10/19 13:17:05 $ - Version: $Revision: 1.59 $ + Date: $Date: 2005/10/20 15:24:09 $ + Version: $Revision: 1.60 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -115,12 +115,6 @@ public: VRKey const &vr = GDCM_VRUNKNOWN); SeqEntry *NewSeqEntry(uint16_t group, uint16_t elem); -// DictEntry related utilities - DictEntry *NewVirtualDictEntry(uint16_t group,uint16_t elem, - VRKey const &vr = GDCM_VRUNKNOWN, - TagName const &vm = GDCM_UNKNOWN, - TagName const &name = GDCM_UNKNOWN ); - protected: // DictEntry related utilities DictEntry *GetDictEntry(uint16_t group, uint16_t elem); diff --git a/src/gdcmDocument.cxx b/src/gdcmDocument.cxx index 5e33c4c1..4a82467d 100644 --- a/src/gdcmDocument.cxx +++ b/src/gdcmDocument.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmDocument.cxx,v $ Language: C++ - Date: $Date: 2005/10/20 13:55:05 $ - Version: $Revision: 1.300 $ + Date: $Date: 2005/10/20 15:24:09 $ + Version: $Revision: 1.301 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -1965,6 +1965,7 @@ DocEntry *Document::ReadNextDocEntry() if ( dictEntry ) { realVR = dictEntry->GetVR(); + dictEntry->Unregister(); } } } diff --git a/src/gdcmFile.cxx b/src/gdcmFile.cxx index 95a51268..88986f53 100644 --- a/src/gdcmFile.cxx +++ b/src/gdcmFile.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmFile.cxx,v $ Language: C++ - Date: $Date: 2005/10/19 19:36:54 $ - Version: $Revision: 1.283 $ + Date: $Date: 2005/10/20 15:24:09 $ + Version: $Revision: 1.284 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -230,10 +230,11 @@ bool File::DoTheLoadingJob( ) // Change only made if usefull if ( PixelVR != oldEntry->GetVR() ) { - DictEntry* newDict = NewVirtualDictEntry(GrPixel,NumPixel, - PixelVR,"1","Pixel Data"); + DictEntry* newDict = DictEntry::New(GrPixel,NumPixel, + PixelVR,"1","Pixel Data"); DataEntry *newEntry = new DataEntry(newDict); + newDict->Delete(); newEntry->Copy(entry); newEntry->SetBinArea(oldEntry->GetBinArea(),oldEntry->IsSelfArea()); oldEntry->SetSelfArea(false); diff --git a/src/gdcmMacro.h b/src/gdcmMacro.h new file mode 100644 index 00000000..0bd2b43b --- /dev/null +++ b/src/gdcmMacro.h @@ -0,0 +1,29 @@ +/*========================================================================= + + Program: gdcm + Module: $RCSfile: gdcmMacro.h,v $ + Language: C++ + Date: $Date: 2005/10/20 15:24:09 $ + Version: $Revision: 1.1 $ + + Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de + l'Image). All rights reserved. See Doc/License.txt or + http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details. + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ + +#ifndef GDCMMACRO_H +#define GDCMMACRO_H + +//----------------------------------------------------------------------------- +#define gdcmTypeMacro(type) \ + private : \ + type(type &); /* Not implemented */ \ + type &operator=(type &); /* Not implemented */ + +//----------------------------------------------------------------------------- +#endif diff --git a/src/gdcmRefCounter.cxx b/src/gdcmRefCounter.cxx new file mode 100644 index 00000000..0af71e96 --- /dev/null +++ b/src/gdcmRefCounter.cxx @@ -0,0 +1,40 @@ + /*========================================================================= + + Program: gdcm + Module: $RCSfile: gdcmRefCounter.cxx,v $ + Language: C++ + Date: $Date: 2005/10/20 15:24:10 $ + Version: $Revision: 1.1 $ + + Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de + l'Image). All rights reserved. See Doc/License.txt or + http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details. + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ + +#include "gdcmRefCounter.h" + +namespace gdcm +{ +//------------------------------------------------------------------------- +// Constructor / Destructor + +//----------------------------------------------------------------------------- +// Public + +//----------------------------------------------------------------------------- +// Protected + +//----------------------------------------------------------------------------- +// Private + +//----------------------------------------------------------------------------- +// Print + +//----------------------------------------------------------------------------- +} // end namespace gdcm + diff --git a/src/gdcmRefCounter.h b/src/gdcmRefCounter.h new file mode 100644 index 00000000..cb3471a9 --- /dev/null +++ b/src/gdcmRefCounter.h @@ -0,0 +1,83 @@ +/*========================================================================= + + Program: gdcm + Module: $RCSfile: gdcmRefCounter.h,v $ + Language: C++ + Date: $Date: 2005/10/20 15:24:10 $ + Version: $Revision: 1.1 $ + + Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de + l'Image). All rights reserved. See Doc/License.txt or + http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details. + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ + +#ifndef GDCMREFCOUNTER_H +#define GDCMREFCOUNTER_H + +#include "gdcmBase.h" +#include "gdcmDebug.h" + +namespace gdcm +{ +//----------------------------------------------------------------------------- +/** + * \brief Integration of reference counting with a destruction of the + * object only when the reference is to zero + */ +class GDCM_EXPORT RefCounter : public Base +{ +public: + gdcmTypeMacro(RefCounter); + +public: +// Allocator / Unallocator + /// Delete the object + /// \remarks The object is deleted only if its reference counting is to zero + inline virtual void Delete(void) + { + Unregister(); + } + +// Reference count + /// Register the object + /// \remarks It increments the reference counting + inline void Register(void) + { + RefCount++; + } + /// Unregister the object + /// \remarks It decrements the reference counting + inline void Unregister(void) + { + RefCount--; + if(RefCount==0) + { + delete this; + } + } + /// Get the reference counting + /// \return Reference count + inline const unsigned long &GetRefCount(void) const + { + return(RefCount); + } + +protected: + /// Constructor + RefCounter() { RefCount = 1; } + /// Destructor + virtual ~RefCounter() {} + +private: + /// \brief Reference count + unsigned long RefCount; +}; +} // end namespace gdcm + +//----------------------------------------------------------------------------- +#endif -- 2.45.1