00001 //-------------------------------------------------------------------------- 00002 // File and Version Information: 00003 // $Id: TrkErrCode.h,v 1.1.1.1 2005/04/21 06:01:42 zhangy Exp $ 00004 // 00005 // Description: 00006 // Encapsulate error/success status of tracking operations. 00007 // Either failure() or success() will be non-zero, but not both. 00008 // Failure => no valid answer available. 00009 // Success => a valid answer has been 00010 // provided, even if it wasn't exactly what you asked for. The 00011 // value of failure() or success() distinguishes different 00012 // failure/success modes. A string describing the success/failure 00013 // mode can also be provided, and printed by the user. 00014 // 00015 // Note that if this string is provided by the called function, 00016 // it _must_ be a pointer to a statically stored string (which includes 00017 // string literals). E.g. 00018 // TrkErrCode err; 00019 // err.setFailure(10,"Forgot to tie my shoelaces."); 00020 // return err; 00021 // is valid. 00022 // 00023 // Several codes have predefined meanings and strings; strings 00024 // supplied for them will be ignored. Strings for codes >= 10 00025 // can be supplied by users. Predefined: 00026 // failure = 1 -- "Arithmetic error." 00027 // = 2 -- "Failed to converge." 00028 // = 3 -- "Failed because parallel." 00029 // = 4-9 -- reserved until I think of some more standard codes 00030 // success = 1 -- "Normal completion." 00031 // = 2 -- "Didn't converge." 00032 // = 3 -- "Parallel" 00033 // = 4-9 -- reserved 00034 // 00035 // Environment: 00036 // Software developed for the BaBar Detector at the SLAC B-Factory. 00037 // 00038 // Authors: (Steve Schaffner) -- initial implementation stolen from A. Snyder 00039 // 00040 //------------------------------------------------------------------------ 00041 #ifndef TRKERRCODE_HH 00042 #define TRKERRCODE_HH 00043 00044 #include <iosfwd> 00045 #include <string> 00046 00047 // Class interface // 00048 class TrkErrCode { 00049 public: 00050 00051 enum TrkSuccess {fail,succeed}; 00052 00053 TrkErrCode(TrkSuccess=succeed, int code=1, const char* str=0); 00054 ~TrkErrCode(); 00055 00056 // Copy constructors 00057 TrkErrCode(const TrkErrCode&); 00058 TrkErrCode& operator=(const TrkErrCode&); 00059 00060 // access 00061 int failure() const {return _failure;} 00062 int success() const {return _success;} 00063 const std::string& message() const 00064 { 00065 return (_string != 0) ? *_string : _nullStr; 00066 } 00067 void print(std::ostream& ostr) const; 00068 00069 // set 00070 void setMessage(const char* str = 0) { 00071 if (_string != 0) delete _string; 00072 if (str != 0) { 00073 _string= new std::string(str); 00074 } 00075 else { 00076 _string = 0; 00077 } 00078 } 00079 void setFailure(int i, const char* str = 0) 00080 { 00081 setMessage(str); 00082 _failure=(i==0?1:i); _success=0; 00083 } 00084 void setSuccess(int i, const char* str = 0) { 00085 setMessage(str); 00086 _success=(i==0?1:i); _failure=0; 00087 } 00088 00089 00090 private: 00091 // Data 00092 int _failure; 00093 int _success; 00094 std::string* _string; 00095 static std::string _nullStr; 00096 }; 00097 00098 std::ostream& operator<<(std::ostream& os, const TrkErrCode& trkerr); 00099 00100 #endif 00101