GDCM  2.2.0
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 
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  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  assert( ReferenceCount > 0 );
75  }
76  void UnRegister() {
77  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

Generated on Wed Jun 13 2012 20:40:37 for GDCM by doxygen 1.8.1
SourceForge.net Logo