X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=src%2FKM_error.h;h=8270cc0a5c7581399b9f421265dbdbfb9df264c7;hb=260692554632887d4e978775001081add386b909;hp=c17fe76ecf57e1c9a5b16ad8e209b5d8c0c4c962;hpb=eba0b92c6ce46a626f46c0843a88ab33d6b281af;p=asdcplib.git diff --git a/src/KM_error.h b/src/KM_error.h index c17fe76..8270cc0 100755 --- a/src/KM_error.h +++ b/src/KM_error.h @@ -1,5 +1,5 @@ /* -Copyright (c) 2004-2009, John Hurst +Copyright (c) 2004-2015, John Hurst All rights reserved. Redistribution and use in source and binary forms, with or without @@ -34,6 +34,10 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef _KM_ERROR_H_ #define _KM_ERROR_H_ +#include + +#define KM_DECLARE_RESULT(sym, i, l) const Result_t RESULT_##sym = Result_t(i, #sym, l); + namespace Kumu { // Result code container. Both a signed integer and a text string are stored in the object. @@ -44,50 +48,72 @@ namespace Kumu class Result_t { int value; - const char* label; + std::string label, symbol, message; Result_t(); public: - static const Result_t& Find(int); - static Result_t Delete(int); - - Result_t(int v, const char* l); + // Return registered Result_t for the given "value" code. + static const Result_t& Find(int value); + + // Unregister the Result_t matching the given "value" code. Returns + // RESULT_FALSE if "value" does not match a registered Result_t. + // Returns RESULT_FAIL if ( value < -99 || value > 99 ) (Kumu core + // codes may not be deleted). + static Result_t Delete(int value); + + // Iteration through registered result codes, not thread safe. + // Get accepts contiguous values from 0 to End() - 1. + static unsigned int End(); + static const Result_t& Get(unsigned int); + + Result_t(int v, const std::string& s, const std::string& l); + Result_t(const Result_t& rhs); + const Result_t& operator=(const Result_t& rhs); ~Result_t(); + const Result_t operator()(const std::string& message) const; + const Result_t operator()(const int& line, const char* filename) const; + const Result_t operator()(const std::string& message, const int& line, const char* filename) const; + inline bool operator==(const Result_t& rhs) const { return value == rhs.value; } inline bool operator!=(const Result_t& rhs) const { return value != rhs.value; } - inline bool Success() const { return ( value >= 0 ); } + inline bool Success() const { return ! ( value < 0 ); } inline bool Failure() const { return ( value < 0 ); } inline int Value() const { return value; } inline operator int() const { return value; } - - inline const char* Label() const { return label; } - inline operator const char*() const { return label; } + inline const char* Label() const { return label.c_str(); } + inline operator const char*() const { return label.c_str(); } + inline const char* Symbol() const { return symbol.c_str(); } + inline const char* Message() const { return message.c_str(); } }; - const Result_t RESULT_FALSE ( 1, "Successful but not true."); - const Result_t RESULT_OK ( 0, "Success."); - const Result_t RESULT_FAIL (-1, "An undefined error was detected."); - const Result_t RESULT_PTR (-2, "An unexpected NULL pointer was given."); - const Result_t RESULT_NULL_STR (-3, "An unexpected empty string was given."); - const Result_t RESULT_ALLOC (-4, "Error allocating memory."); - const Result_t RESULT_PARAM (-5, "Invalid parameter."); - const Result_t RESULT_NOTIMPL (-6, "Unimplemented Feature."); - const Result_t RESULT_SMALLBUF (-7, "The given buffer is too small."); - const Result_t RESULT_INIT (-8, "The object is not yet initialized."); - const Result_t RESULT_NOT_FOUND (-9, "The requested file does not exist on the system."); - const Result_t RESULT_NO_PERM (-10, "Insufficient privilege exists to perform the operation."); - const Result_t RESULT_STATE (-11, "Object state error."); - const Result_t RESULT_CONFIG (-12, "Invalid configuration option detected."); - const Result_t RESULT_FILEOPEN (-13, "File open failure."); - const Result_t RESULT_BADSEEK (-14, "An invalid file location was requested."); - const Result_t RESULT_READFAIL (-15, "File read error."); - const Result_t RESULT_WRITEFAIL (-16, "File write error."); - const Result_t RESULT_ENDOFFILE (-17, "Attempt to read past end of file."); - const Result_t RESULT_FILEEXISTS (-18, "Filename already exists."); - const Result_t RESULT_NOTAFILE (-19, "Filename not found."); - const Result_t RESULT_UNKNOWN (-20, "Unknown result code."); + KM_DECLARE_RESULT(FALSE, 1, "Successful but not true."); + KM_DECLARE_RESULT(OK, 0, "Success."); + KM_DECLARE_RESULT(FAIL, -1, "An undefined error was detected."); + KM_DECLARE_RESULT(PTR, -2, "An unexpected NULL pointer was given."); + KM_DECLARE_RESULT(NULL_STR, -3, "An unexpected empty string was given."); + KM_DECLARE_RESULT(ALLOC, -4, "Error allocating memory."); + KM_DECLARE_RESULT(PARAM, -5, "Invalid parameter."); + KM_DECLARE_RESULT(NOTIMPL, -6, "Unimplemented Feature."); + KM_DECLARE_RESULT(SMALLBUF, -7, "The given buffer is too small."); + KM_DECLARE_RESULT(INIT, -8, "The object is not yet initialized."); + KM_DECLARE_RESULT(NOT_FOUND, -9, "The requested file does not exist on the system."); + KM_DECLARE_RESULT(NO_PERM, -10, "Insufficient privilege exists to perform the operation."); + KM_DECLARE_RESULT(STATE, -11, "Object state error."); + KM_DECLARE_RESULT(CONFIG, -12, "Invalid configuration option detected."); + KM_DECLARE_RESULT(FILEOPEN, -13, "File open failure."); + KM_DECLARE_RESULT(BADSEEK, -14, "An invalid file location was requested."); + KM_DECLARE_RESULT(READFAIL, -15, "File read error."); + KM_DECLARE_RESULT(WRITEFAIL, -16, "File write error."); + KM_DECLARE_RESULT(ENDOFFILE, -17, "Attempt to read past end of file."); + KM_DECLARE_RESULT(FILEEXISTS, -18, "Filename already exists."); + KM_DECLARE_RESULT(NOTAFILE, -19, "Filename not found."); + KM_DECLARE_RESULT(UNKNOWN, -20, "Unknown result code."); + KM_DECLARE_RESULT(DIR_CREATE, -21, "Unable to create directory."); + KM_DECLARE_RESULT(NOT_EMPTY, -22, "Unable to delete non-empty directory."); + // 23-100 are reserved + } // namespace Kumu //-------------------------------------------------------------------------------- @@ -102,7 +128,7 @@ namespace Kumu // See Result_t above for an explanation of RESULT_* symbols. # define KM_TEST_NULL(p) \ if ( (p) == 0 ) { \ - return Kumu::RESULT_PTR; \ + return Kumu::RESULT_PTR(__LINE__, __FILE__); \ } // Returns RESULT_PTR if the given argument is NULL. See Result_t @@ -113,9 +139,25 @@ namespace Kumu # define KM_TEST_NULL_STR(p) \ KM_TEST_NULL(p); \ if ( (p)[0] == '\0' ) { \ - return Kumu::RESULT_NULL_STR; \ + return Kumu::RESULT_NULL_STR(__LINE__, __FILE__); \ + } + +// RESULT_STATE is ambiguous. Use these everywhere it is assigned to provide some context +#define KM_RESULT_STATE_TEST_IMPLICIT() \ + if ( result == Kumu::RESULT_STATE ) { \ + Kumu::DefaultLogSink().Error("RESULT_STATE RETURNED at %s (%d)\n", __FILE__, __LINE__); \ + } + +#define KM_RESULT_STATE_TEST_THIS(_this__r_) \ + if ( _this__r_ == Kumu::RESULT_STATE ) { \ + Kumu::DefaultLogSink().Error("RESULT_STATE RETURNED at %s (%d)\n", __FILE__, __LINE__); \ } +#define KM_RESULT_STATE_HERE() \ + Kumu::DefaultLogSink().Error("RESULT_STATE RETURNED at %s (%d)\n", __FILE__, __LINE__); + + + namespace Kumu { // simple tracing mechanism