File: itkExceptionObject.cxx

package info (click to toggle)
insighttoolkit 3.20.1%2Bgit20120521-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 80,672 kB
  • ctags: 85,253
  • sloc: cpp: 458,133; ansic: 196,222; fortran: 28,000; python: 3,839; tcl: 1,811; sh: 1,184; java: 583; makefile: 428; csh: 220; perl: 193; xml: 20
file content (337 lines) | stat: -rw-r--r-- 9,800 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    itkExceptionObject.cxx
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) Insight Software Consortium. All rights reserved.
  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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 "itkExceptionObject.h"
#include "itkLightObject.h"
#include "itkIndent.h"

#include <string>

namespace itk
{

/** \class ExceptionObject::ExceptionData 
 * \brief Exception data, used to implement itk::ExceptionObject.
 *
 * Contains the location and description of the error, as well as
 * the text that should be returned by itk::ExceptionObject::what().
 */
class ExceptionObject::ExceptionData : public ReferenceCounterInterface
{
protected:
  // Constructor. Might throw an exception.
  ExceptionData(
    const std::string& file, unsigned int line,
    const std::string& description,
    const std::string& location)
  :
  m_Location(location),
  m_Description(description),
  m_File(file),
  m_Line(line)
    {
    OStringStream loc;
    loc << ":" << m_Line << ":\n";
    m_What = m_File;
    m_What += loc.str();
    m_What += m_Description;
    m_WhatPointer = m_What.c_str();
    }

private:
  void operator=(const ExceptionData&); //purposely not implemented

  friend class ExceptionObject;

  // The data members should never change after construction of the ExceptionData object,
  // to ensure the consistency of the exception data.
  const std::string       m_Location;
  const std::string       m_Description;
  const std::string       m_File;
  const unsigned int      m_Line;
  std::string             m_What;
  const char *            m_WhatPointer;
};


/** \class ExceptionObject::ReferenceCountedExceptionData 
 * \brief Reference counted exception data, used to implement itk::ExceptionObject.
 * 
 * Its first base class, ExceptionObject::ExceptionData, holds its data, while its second
 * base class, itk::LightObject, takes care of the reference counting.
 *
 * \note ExceptionData is constructed before LightObject, thereby it is ensured that
 * an exception within the constructor of ExceptionData won't trigger the destruction
 * of LightObject.
 */
class ExceptionObject::ReferenceCountedExceptionData : public ExceptionData, public LightObject
{
public:
  typedef ReferenceCountedExceptionData     Self;
  typedef SmartPointer<const Self>          ConstPointer;
  static ConstPointer ConstNew(
    const std::string& file, unsigned int line,
    const std::string& description,
    const std::string& location)
  {
    ConstPointer smartPtr;
    const Self *const rawPtr = new Self(file, line, description, location);
    smartPtr = rawPtr;
    rawPtr->LightObject::UnRegister();
    return smartPtr;
  }

  /** Increase the reference count (mark as used by another object).
    * Delegates the counting to its LightObject superclass  */
  virtual void Register() const
  {
  this->LightObject::Register();
  }

  /** Decrease the reference count (release by another object).
    * Delegates the counting to its LightObject superclass  */
  virtual void UnRegister() const
  {
  this->LightObject::UnRegister();
  }

private:
  // Constructor. Might throw an exception.
  ReferenceCountedExceptionData(
    const std::string& file, unsigned int line,
    const std::string& description,
    const std::string& location)
  :
  ExceptionData(file, line, description, location),
  LightObject()
  {
  }

  // Destructor. Only invoked via LightObject::UnRegister(), when its reference count drops to zero.
  ~ReferenceCountedExceptionData()
  {
  }

  ReferenceCountedExceptionData(const Self&); //purposely not implemented
  void operator=(const Self&); //purposely not implemented
};


ExceptionObject::ExceptionObject()
{
  // The default construction never throws an exception.
}

ExceptionObject::ExceptionObject(
  const char *file,
  unsigned int lineNumber,
  const char *desc,
  const char *loc)
:
m_ExceptionData( ReferenceCountedExceptionData::ConstNew(file == 0 ? "" : file, lineNumber, desc == 0 ? "" : desc, loc == 0 ? "" : loc) )
{
}

ExceptionObject::ExceptionObject(
  const std::string& file,
  unsigned int lineNumber,
  const std::string& desc,
  const std::string& loc)
:
m_ExceptionData( ReferenceCountedExceptionData::ConstNew(file, lineNumber, desc, loc) )
{
}

ExceptionObject::ExceptionObject( const ExceptionObject &orig )
:
Superclass(orig),
m_ExceptionData(orig.m_ExceptionData)
{
  // This copy construction never throws, because it just copies the smart pointer.
}


ExceptionObject::~ExceptionObject() throw()
{
  // During destruction, the reference count of the ReferenceCountedExceptionData will be decreased
  // automatically, by the destructor of the smart pointer.
}

const ExceptionObject::ExceptionData *
ExceptionObject::GetExceptionData() const
{
  // Note: dynamic_cast does a runtime check if the m_ExceptionData pointer is indeed
  // pointing to an ExceptionData object. In this case, a static_cast could have been
  // used instead, which only does compile time checking. But we expect the
  // runtime overhead of this particular dynamic_cast to be insignificant.
  const ExceptionData * thisData = 
    dynamic_cast< const ExceptionData *>( this->m_ExceptionData.GetPointer() );
  return thisData;
}
 
ExceptionObject &
ExceptionObject::operator= ( const ExceptionObject &orig )
{
  // Note: there is no superclass assignment here, because std::exception::operator= 
  // appears have a bug on some platforms, including MSVC 2003. The MSVC 2003 bug is
  // described at the Usenet newsgroup microsoft.public.vc.language, June 2, 2008,
  // subject "VC7.1 std::exception assignment operator bug (crash) a known issue?"
  // http://groups.google.com/group/microsoft.public.vc.language/msg/15b927c8c1130e88

  // Assigns its smart pointer:
  m_ExceptionData = orig.m_ExceptionData;
  return *this;
}

bool
ExceptionObject::operator==( const ExceptionObject &orig )
{
  // operator== is reimplemented, but it still behaves like the previous version, from ITK 3.6.0.
  const ExceptionData *const thisData = this->GetExceptionData();
  const ExceptionData *const origData = orig.GetExceptionData();

  if ( thisData == origData )
    {
    return true;
    }
  else
    {
    return (thisData != 0) && (origData != 0) &&
      thisData->m_Location == origData->m_Location &&
      thisData->m_Description == origData->m_Description &&
      thisData->m_File == origData->m_File &&
      thisData->m_Line == origData->m_Line; 
    }
}

void
ExceptionObject::SetLocation(const std::string& s)
{
  const bool IsNull = m_ExceptionData.IsNull();
  m_ExceptionData = ReferenceCountedExceptionData::ConstNew(
    IsNull ? "" : this->GetExceptionData()->m_File.c_str(),
    IsNull ? 0 : this->GetExceptionData()->m_Line,
    IsNull ? "" : this->GetExceptionData()->m_Description.c_str(),
    s);
}

void
ExceptionObject::SetDescription(const std::string& s) 
{
  const bool IsNull = m_ExceptionData.IsNull();
  m_ExceptionData = ReferenceCountedExceptionData::ConstNew(
    IsNull ? "" : this->GetExceptionData()->m_File.c_str(),
    IsNull ? 0 : this->GetExceptionData()->m_Line,
    s,
    IsNull ? "" : this->GetExceptionData()->m_Location.c_str());
}

void
ExceptionObject::SetLocation(const char * s)
{
  std::string location;
  if( s ) 
    {
    location = s;
    }
  ExceptionObject::SetLocation( location );
}

void
ExceptionObject::SetDescription(const char *s)
{
  std::string description;
  if( s ) 
    {
    description = s;
    }
  ExceptionObject::SetDescription( description );
}

const char *
ExceptionObject::GetLocation() const 
{
  // Note: std::string::c_str() might throw an exception.
  return m_ExceptionData.IsNull() ? "" : this->GetExceptionData()->m_Location.c_str();
}

const char *
ExceptionObject::GetDescription() const
{
  // Note: std::string::c_str() might throw an exception.
  return m_ExceptionData.IsNull() ? "" : this->GetExceptionData()->m_Description.c_str();
}

const char *
ExceptionObject::GetFile() const 
{
  // Note: std::string::c_str() might throw an exception.
  return m_ExceptionData.IsNull() ? "" : this->GetExceptionData()->m_File.c_str();
}

unsigned int
ExceptionObject::GetLine() const 
{
  return m_ExceptionData.IsNull() ? 0 : this->GetExceptionData()->m_Line;
}

const char *
ExceptionObject::what() const throw()
{ 
  const ExceptionData * const thisData = this->GetExceptionData();

  // Note: m_What.c_str() wouldn't be safe, because c_str() might throw an exception.
  return thisData ? thisData->m_WhatPointer : "ExceptionObject";
}

void
ExceptionObject
::Print(std::ostream& os) const
{
  Indent indent;

  // Print header
  os << std::endl;
  os << indent << "itk::" << this->GetNameOfClass() << " (" << this << ")\n";

  // Print self
  indent.GetNextIndent();

  if (m_ExceptionData.IsNotNull())
    {
    const ExceptionData & data = *(this->GetExceptionData());

    if (! data.m_Location.empty()) 
      {
      os << indent << "Location: \"" << data.m_Location << "\" " << std::endl;
      }
    
    if (! data.m_File.empty()) 
      {
      os << indent << "File: " << data.m_File << std::endl;
      os << indent << "Line: " << data.m_Line << std::endl;
      }
    
    if (! data.m_Description.empty()) 
      {
      os << indent << "Description: " << data.m_Description << std::endl;  
      }
    }
  // Print trailer
  os << indent << std::endl;
}  

} // end namespace itk