00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "TrkBase/TrkErrCode.h"
00017 #include <iostream>
00018 #include <algorithm>
00019 using std::ostream;
00020
00021 std::string TrkErrCode::_nullStr("");
00022
00023 TrkErrCode::TrkErrCode(TrkSuccess succ, int code, const char* str)
00024 : _string(0)
00025 {
00026 setMessage(str);
00027 if (succ) {
00028 _failure = 0; _success = code;
00029 } else {
00030 _success = 0; _failure = code;
00031 }
00032 }
00033
00034
00035 TrkErrCode::TrkErrCode(const TrkErrCode& theCode)
00036 : _failure(theCode._failure)
00037 , _success(theCode._success)
00038 {
00039 if (theCode._string != 0) {
00040 _string = new std::string(*theCode._string);
00041 }
00042 else {
00043 _string = 0;
00044 }
00045 }
00046
00047
00048 TrkErrCode::~TrkErrCode() {
00049 if (_string != 0) {
00050 delete _string;
00051 _string = 0;
00052 }
00053 }
00054
00055 TrkErrCode&
00056 TrkErrCode::operator =(const TrkErrCode& theCode)
00057 {
00058 _failure = theCode._failure;
00059 _success = theCode._success;
00060
00061 if (theCode._string != 0) {
00062 if (_string != 0) {
00063 *_string = *theCode._string;
00064 }
00065 else {
00066 _string = new std::string(*theCode._string);
00067 }
00068 }
00069 else {
00070 if (_string != 0) delete _string;
00071 _string = 0;
00072 }
00073
00074 return *this;
00075 }
00076
00077
00078 void
00079 TrkErrCode::print(ostream& ostr) const
00080 {
00081 const char* pstatus = 0;
00082 int code;
00083 if (success()) {
00084 pstatus = "succeeded";
00085 code = success();
00086 } else {
00087 pstatus = "failed";
00088 code = failure();
00089 }
00090 std::string pstring;
00091 static const std::string failed[4] = { "Arithmetic error.",
00092 "Failed to converge.",
00093 "Failed because parallel.",
00094 "Undefined error." };
00095
00096 static const std::string succeeded[4] = { "Normal completion.",
00097 "Didn't converge.",
00098 "Parallel.",
00099 "Undefined success state."};
00100
00101 if (code > 0 && code < 10) {
00102 if (failure()) {
00103 pstring = failed[std::min(code-1,3) ];
00104 } else if (success()) {
00105 pstring = succeeded[std::min(code-1,3) ];
00106 }
00107 } else if (_string == 0 ) {
00108 pstring = "Unknown error.";
00109 } else {
00110 pstring = *_string;
00111 }
00112
00113 ostr << "TrkErrCode: " << pstatus << ", code " << code
00114 << ". Status: " << pstring.c_str();
00115 }
00116
00117 ostream&
00118 operator<<(ostream& os, const TrkErrCode& trkerr)
00119 {
00120 trkerr.print(os);
00121 return os;
00122 }