]> Creatis software - gdcm.git/blob - src/gdcmException.cxx
doxygenation
[gdcm.git] / src / gdcmException.cxx
1 // gdcmException.cxx
2 //-----------------------------------------------------------------------------
3 #include "gdcmException.h"
4
5 #include <typeinfo>
6 #include <stdio.h>
7
8 //-----------------------------------------------------------------------------
9 // gdcmException
10
11 /*
12  * \ingroup gdcmException
13  * \brief 
14  * @param   
15  */
16 gdcmException::gdcmException(const std::string &f, const std::string& msg) throw()
17 #ifdef __GNUC__
18   try
19 #endif
20   : from(f), error(msg) {
21   }
22 #ifdef __GNUC__
23 catch(...) {
24   fatal("gdcmException::gdcmException(const std::string&, const std::string&, const std::string&)");
25 }
26 #endif
27
28
29 /*
30  * \ingroup gdcmException
31  * \brief 
32  * @param   
33  */
34  void gdcmException::fatal(const char *from) throw() {
35   try {
36     std::cerr << "Fatal: exception received in " << from 
37          << " while handling exception." << std::endl;
38     exit(-1);
39   }
40   catch(...) {
41     try {
42       std::cerr << "Fatal: exception received in Exception::fatal while handling exception."
43            << std::endl;
44       exit(-1);
45     }
46     catch(...) {
47       exit(-1);
48     }
49   }  
50 }
51
52 /*
53  * \ingroup gdcmException
54  * \brief 
55  * @param   
56  */
57  std::string gdcmException::getName() const throw() {
58   try {
59 #ifdef __GNUC__   // GNU C++ compiler class name demangling
60       unsigned int nested = 1, i, nb, offset;
61       std::string one;
62
63       std::string name;
64       std::string iname = typeid(*this).name();
65       if(iname[0] == 'Q') {
66         nested = iname[1] - '0';
67         iname = std::string(iname, 2, std::string::npos);
68       }
69       for(i = 0; i < nested; i++) {
70         ::sscanf(iname.c_str(), "%u%n", &nb, &offset);
71         iname = std::string(iname, offset, std::string::npos);
72         name += std::string(iname, 0, nb);
73         if(i + 1 < nested) name += "::";
74         iname = std::string(iname, nb, std::string::npos);
75       }
76       return name;
77 #else             // no class name demangling
78       //name = typeid(*this).name();
79       return "Exception";
80 #endif
81   }
82   catch(...) {
83     fatal("Exception::getName(std::string &)");
84     return "";
85   }
86 }
87
88 /*
89  * \ingroup gdcmException
90  * \brief 
91  * @param   
92  */
93  gdcmException::operator const char *() const throw() {
94   return getName().c_str();
95 }
96
97 //-----------------------------------------------------------------------------
98 /*
99  * \ingroup gdcmException
100  * \brief 
101  * @param   
102  */
103  std::ostream& operator<<(std::ostream &os, const gdcmException &e) {
104   try {  
105     os << "Exception " << e.getName() << " thrown: " << e.getError() << std::endl;
106   }
107   catch(...) {
108     gdcmException::fatal("operator<<(std::ostream &, const gdcmException&)");
109   }
110   return os;
111 }
112
113 //-----------------------------------------------------------------------------