| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkLightObject.cxx.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:40 $ |
| 7 |
|
Version: $Revision: 1.4 $ |
| 8 |
|
|
| 9 |
|
Copyright (c) Insight Software Consortium. All rights reserved. |
| 10 |
|
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. |
| 11 |
|
|
| 12 |
|
This software is distributed WITHOUT ANY WARRANTY; without even |
| 13 |
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 |
|
PURPOSE. See the above copyright notices for more information. |
| 15 |
|
|
| 16 |
DEF |
=========================================================================*/ |
| 17 |
|
#include "itkLightObject.h" |
| 18 |
|
#include "itkObjectFactory.h" |
| 19 |
|
#include "itkFastMutexLock.h" |
| 20 |
|
#include <list> |
| 21 |
|
#include <memory> |
| 22 |
|
|
| 23 |
|
// Better name demanging for gcc |
| 24 |
|
#if __GNUC__ > 3 || ( __GNUC__ == 3 && __GNUC_MINOR__ > 0 ) |
| 25 |
|
#define GCC_USEDEMANGLE |
| 26 |
|
#endif |
| 27 |
|
|
| 28 |
|
#ifdef GCC_USEDEMANGLE |
| 29 |
|
#include <cstdlib> |
| 30 |
|
#include <cxxabi.h> |
| 31 |
|
#endif |
| 32 |
|
|
| 33 |
|
namespace itk |
| 34 |
|
{ |
| 35 |
|
|
| 36 |
|
LightObject::Pointer |
| 37 |
|
LightObject::New() |
| 38 |
|
{ |
| 39 |
|
Pointer smartPtr; |
| 40 |
|
LightObject *rawPtr = ::itk::ObjectFactory<LightObject>::Create(); |
| 41 |
|
if(rawPtr == NULL) |
| 42 |
|
{ |
| 43 |
|
rawPtr = new LightObject; |
| 44 |
|
} |
| 45 |
|
smartPtr = rawPtr; |
| 46 |
|
rawPtr->UnRegister(); |
| 47 |
|
return smartPtr; |
| 48 |
|
} |
| 49 |
|
|
| 50 |
|
LightObject::Pointer |
| 51 |
|
LightObject::CreateAnother() const |
| 52 |
|
{ |
| 53 |
|
return LightObject::New(); |
| 54 |
|
} |
| 55 |
|
|
| 56 |
|
/** |
| 57 |
|
* Delete a itk object. This method should always be used to delete an object |
| 58 |
|
* when the new operator was used to create it. Using the C++ delete method |
| 59 |
|
* will not work with reference counting. |
| 60 |
|
*/ |
| 61 |
|
void |
| 62 |
|
LightObject |
| 63 |
|
::Delete() |
| 64 |
|
{ |
| 65 |
|
this->UnRegister(); |
| 66 |
|
} |
| 67 |
|
|
| 68 |
|
|
| 69 |
|
/** |
| 70 |
|
* Avoid DLL boundary problems. |
| 71 |
|
*/ |
| 72 |
|
#ifdef _WIN32 |
| 73 |
|
void* |
| 74 |
|
LightObject |
| 75 |
|
::operator new(size_t n) |
| 76 |
|
{ |
| 77 |
|
return new char[n]; |
| 78 |
|
} |
| 79 |
|
|
| 80 |
|
void* |
| 81 |
|
LightObject |
| 82 |
|
::operator new[](size_t n) |
| 83 |
|
{ |
| 84 |
|
return new char[n]; |
| 85 |
|
} |
| 86 |
|
|
| 87 |
|
void |
| 88 |
|
LightObject |
| 89 |
|
::operator delete(void* m) |
| 90 |
|
{ |
| 91 |
|
delete [] (char*)m; |
| 92 |
|
} |
| 93 |
|
|
| 94 |
|
void |
| 95 |
|
LightObject |
| 96 |
|
::operator delete[](void* m, size_t) |
| 97 |
|
{ |
| 98 |
|
delete [] (char*)m; |
| 99 |
|
} |
| 100 |
|
#endif |
| 101 |
|
|
| 102 |
|
|
| 103 |
|
/** |
| 104 |
|
* This function will be common to all itk objects. It just calls the |
| 105 |
|
* header/self/trailer virtual print methods, which can be overriden by |
| 106 |
|
* subclasses (any itk object). |
| 107 |
|
*/ |
| 108 |
|
void |
| 109 |
|
LightObject |
| 110 |
|
::Print(std::ostream& os, Indent indent) const |
| 111 |
|
{ |
| 112 |
|
this->PrintHeader(os, indent); |
| 113 |
|
this->PrintSelf(os, indent.GetNextIndent()); |
| 114 |
|
this->PrintTrailer(os, indent); |
| 115 |
|
} |
| 116 |
|
|
| 117 |
|
|
| 118 |
|
/** |
| 119 |
|
* This method is called when itkExceptionMacro executes. It allows |
| 120 |
|
* the debugger to break on error. |
| 121 |
|
*/ |
| 122 |
|
void |
| 123 |
|
LightObject |
| 124 |
|
::BreakOnError() |
| 125 |
|
{ |
| 126 |
SEM,SEM |
; |
| 127 |
|
} |
| 128 |
|
|
| 129 |
|
|
| 130 |
|
/** |
| 131 |
|
* Increase the reference count (mark as used by another object). |
| 132 |
|
*/ |
| 133 |
|
void |
| 134 |
|
LightObject |
| 135 |
|
::Register() const |
| 136 |
|
{ |
| 137 |
|
m_ReferenceCountLock.Lock(); |
| 138 |
|
m_ReferenceCount++; |
| 139 |
|
m_ReferenceCountLock.Unlock(); |
| 140 |
|
} |
| 141 |
|
|
| 142 |
|
|
| 143 |
|
/** |
| 144 |
|
* Decrease the reference count (release by another object). |
| 145 |
|
*/ |
| 146 |
|
void |
| 147 |
|
LightObject |
| 148 |
|
::UnRegister() const |
| 149 |
|
{ |
| 150 |
|
m_ReferenceCountLock.Lock(); |
| 151 |
|
m_ReferenceCount--; |
| 152 |
|
m_ReferenceCountLock.Unlock(); |
| 153 |
|
|
| 154 |
|
// ReferenceCount in now unlocked. We may have a race condition |
| 155 |
|
// to delete the object. |
| 156 |
|
if ( m_ReferenceCount <= 0) |
| 157 |
|
{ |
| 158 |
|
delete this; |
| 159 |
|
} |
| 160 |
|
} |
| 161 |
|
|
| 162 |
|
|
| 163 |
|
/** |
| 164 |
|
* Sets the reference count (use with care) |
| 165 |
|
*/ |
| 166 |
|
void |
| 167 |
|
LightObject |
| 168 |
|
::SetReferenceCount(int ref) |
| 169 |
|
{ |
| 170 |
|
m_ReferenceCountLock.Lock(); |
| 171 |
|
m_ReferenceCount = ref; |
| 172 |
|
m_ReferenceCountLock.Unlock(); |
| 173 |
|
|
| 174 |
|
if ( m_ReferenceCount <= 0) |
| 175 |
|
{ |
| 176 |
|
delete this; |
| 177 |
|
} |
| 178 |
|
} |
| 179 |
|
|
| 180 |
|
LightObject |
| 181 |
|
::~LightObject() |
| 182 |
|
{ |
| 183 |
|
/** |
| 184 |
|
* warn user if reference counting is on and the object is being referenced |
| 185 |
|
* by another object |
| 186 |
|
*/ |
| 187 |
|
if ( m_ReferenceCount > 0) |
| 188 |
|
{ |
| 189 |
LEN |
itkExceptionMacro(<< "Trying to delete object with non-zero reference count."); |
| 190 |
|
} |
| 191 |
|
} |
| 192 |
|
|
| 193 |
|
|
| 194 |
|
/** |
| 195 |
|
* Chaining method to print an object's instance variables, as well as |
| 196 |
|
* its superclasses. |
| 197 |
|
*/ |
| 198 |
|
void |
| 199 |
|
LightObject |
| 200 |
|
::PrintSelf(std::ostream& os, Indent indent) const |
| 201 |
|
{ |
| 202 |
|
#ifdef GCC_USEDEMANGLE |
| 203 |
|
char const * mangledName = typeid(*this).name(); |
| 204 |
|
int status; |
| 205 |
|
char* unmangled = abi::__cxa_demangle(mangledName, 0, 0, &status); |
| 206 |
|
|
| 207 |
|
os << indent << "RTTI typeinfo: "; |
| 208 |
|
|
| 209 |
|
if(status == 0) |
| 210 |
|
{ |
| 211 |
|
os << unmangled; |
| 212 |
|
free(unmangled); |
| 213 |
|
} |
| 214 |
|
else |
| 215 |
|
{ |
| 216 |
|
os << mangledName; |
| 217 |
|
} |
| 218 |
|
|
| 219 |
|
os << std::endl; |
| 220 |
|
#else |
| 221 |
|
os << indent << "RTTI typeinfo: " << typeid( *this ).name() << std::endl; |
| 222 |
|
#endif |
| 223 |
|
os << indent << "Reference Count: " << m_ReferenceCount << std::endl; |
| 224 |
|
} |
| 225 |
|
|
| 226 |
|
|
| 227 |
|
/** |
| 228 |
|
* Define a default print header for all objects. |
| 229 |
|
*/ |
| 230 |
|
void |
| 231 |
|
LightObject |
| 232 |
|
::PrintHeader(std::ostream& os, Indent indent) const |
| 233 |
|
{ |
| 234 |
|
os << indent << this->GetNameOfClass() << " (" << this << ")\n"; |
| 235 |
|
} |
| 236 |
|
|
| 237 |
|
|
| 238 |
|
/** |
| 239 |
|
* Define a default print trailer for all objects. |
| 240 |
|
*/ |
| 241 |
|
void |
| 242 |
|
LightObject |
| 243 |
|
::PrintTrailer(std::ostream& itkNotUsed(os), Indent itkNotUsed(indent)) const |
| 244 |
|
{ |
| 245 |
|
} |
| 246 |
|
|
| 247 |
|
|
| 248 |
|
/** |
| 249 |
|
* This operator allows all subclasses of LightObject to be printed via <<. |
| 250 |
|
* It in turn invokes the Print method, which in turn will invoke the |
| 251 |
|
* PrintSelf method that all objects should define, if they have anything |
| 252 |
|
* interesting to print out. |
| 253 |
|
*/ |
| 254 |
|
std::ostream& |
| 255 |
|
operator<<(std::ostream& os, const LightObject& o) |
| 256 |
|
{ |
| 257 |
|
o.Print(os); |
| 258 |
|
return os; |
| 259 |
|
} |
| 260 |
|
|
| 261 |
|
|
| 262 |
|
} // end namespace itk |
| 263 |
|
|