GDCM 3.2.1

gdcmObject.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: GDCM (Grassroots DICOM). A DICOM library
4
5 Copyright (c) 2006-2011 Mathieu Malaterre
6 All rights reserved.
7 See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
8
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the above copyright notice for more information.
12
13=========================================================================*/
14#ifndef GDCMOBJECT_H
15#define GDCMOBJECT_H
16
17#include "gdcmTypes.h"
18
19#include <assert.h>
20#include <iostream> // grrrr
21
22//namespace std { class ostream; }
23 namespace gdcm
24{
25
26 template<class ObjectType> class SmartPointer;
27
36 class GDCM_EXPORT Object
37{
38 template <class ObjectType> friend class SmartPointer;
39 friend std::ostream& operator<<(std::ostream &os, const Object &obj);
40
41 public:
42 Object():ReferenceCount(0) {}
43
44 // Implementation note:
45 // If I move ~Object in the protected section I can prevent people
46 // from writing:
47 // SmartPointer<Object> p = new Object;
48 // delete p; // due to SmartPointer::operator ObjectType * () const
49 // but on the other hand one could not define an Object on the stack
50 // Object obj;
51 // Furthermore it would not prevent anyone from doing:
52 // class MyObject : public Object {};
53 // SmartPointer<MyObject> o = new MyObject;
54 // delete o; // grrrrrr
55 virtual ~Object() {
56 // If your debugger reach here it means you are doing something silly
57 // like using SmartPointer on object allocated on the stack (vs heap)
58 gdcm_forced_assert(ReferenceCount == 0);
59 }
60
61 // http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.24
62 // Set the ref count to 0
63 // Do NOT copy the reference count !
65 Object(const Object&):ReferenceCount(0){}
66 void operator=(const Object&){}
67
68 //static Object* New() { return new Object; }
69
70 protected:
71 // For the purpose of the invasive SmartPointer implementation
72 void Register() {
73 ReferenceCount++;
74 gdcm_assert( ReferenceCount > 0 );
75 }
76 void UnRegister() {
77 gdcm_assert( ReferenceCount > 0 );
78 ReferenceCount--;
79 if(!ReferenceCount)
80 {
81 delete this;
82 }
83 }
84
85 public:
86 // For dealing with printing of object and polymorphism
87 virtual void Print(std::ostream &) const {}
88
89 private:
90 long ReferenceCount;
91};
92
93//----------------------------------------------------------------------------
94// function do not carry vtable. Thus define in the base class the operator
95// and use the member function ->Print() to call the appropriate function
96// NOTE: All subclass of Object needs to implement the Print function
97 inline std::ostream& operator<<(std::ostream &os, const Object &obj)
98{
99 obj.Print(os);
100 return os;
101}
102
103} // end namespace gdcm
104
105#endif //GDCMOBJECT_H
gdcm::Object::UnRegister
void UnRegister()
Definition gdcmObject.h:76
gdcm::Object::Register
void Register()
Definition gdcmObject.h:72
gdcm::Object::Object
Object()
Definition gdcmObject.h:42
gdcm::Object::operator=
void operator=(const Object &)
Definition gdcmObject.h:66
gdcm::Object::Object
Object(const Object &)
Special requirement for copy/cstor, assignment operator.
Definition gdcmObject.h:65
gdcm::Object::SmartPointer
friend class SmartPointer
Definition gdcmObject.h:38
gdcm::Object::operator<<
friend std::ostream & operator<<(std::ostream &os, const Object &obj)
Definition gdcmObject.h:97
gdcm::Object::~Object
virtual ~Object()
Definition gdcmObject.h:55
gdcm::Object::Print
virtual void Print(std::ostream &) const
Definition gdcmObject.h:87
gdcm::SmartPointer
Class for Smart Pointer.
Definition gdcmSmartPointer.h:40
gdcm_assert
#define gdcm_assert(cond)
Definition gdcmException.h:104
gdcm_forced_assert
#define gdcm_forced_assert(cond)
Definition gdcmException.h:34
gdcmTypes.h
GDCM_EXPORT
#define GDCM_EXPORT
Definition gdcmWin32.h:34
gdcm
Definition gdcmASN1.h:21
gdcm::operator<<
std::ostream & operator<<(std::ostream &os, const Directory &d)
Definition gdcmDirectory.h:88