errorcode.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. *******************************************************************************
  3. *
  4. * Copyright (C) 2009-2011, International Business Machines
  5. * Corporation and others. All Rights Reserved.
  6. *
  7. *******************************************************************************
  8. * file name: errorcode.h
  9. * encoding: US-ASCII
  10. * tab size: 8 (not used)
  11. * indentation:4
  12. *
  13. * created on: 2009mar10
  14. * created by: Markus W. Scherer
  15. */
  16. #ifndef __ERRORCODE_H__
  17. #define __ERRORCODE_H__
  18. /**
  19. * \file
  20. * \brief C++ API: ErrorCode class intended to make it easier to use
  21. * ICU C and C++ APIs from C++ user code.
  22. */
  23. #include "unicode/utypes.h"
  24. #include "unicode/uobject.h"
  25. U_NAMESPACE_BEGIN
  26. /**
  27. * Wrapper class for UErrorCode, with conversion operators for direct use
  28. * in ICU C and C++ APIs.
  29. * Intended to be used as a base class, where a subclass overrides
  30. * the handleFailure() function so that it throws an exception,
  31. * does an assert(), logs an error, etc.
  32. * This is not an abstract base class. This class can be used and instantiated
  33. * by itself, although it will be more useful when subclassed.
  34. *
  35. * Features:
  36. * - The constructor initializes the internal UErrorCode to U_ZERO_ERROR,
  37. * removing one common source of errors.
  38. * - Same use in C APIs taking a UErrorCode * (pointer)
  39. * and C++ taking UErrorCode & (reference) via conversion operators.
  40. * - Possible automatic checking for success when it goes out of scope.
  41. *
  42. * Note: For automatic checking for success in the destructor, a subclass
  43. * must implement such logic in its own destructor because the base class
  44. * destructor cannot call a subclass function (like handleFailure()).
  45. * The ErrorCode base class destructor does nothing.
  46. *
  47. * Note also: While it is possible for a destructor to throw an exception,
  48. * it is generally unsafe to do so. This means that in a subclass the destructor
  49. * and the handleFailure() function may need to take different actions.
  50. *
  51. * Sample code:
  52. * \code
  53. * class IcuErrorCode: public icu::ErrorCode {
  54. * public:
  55. * virtual ~IcuErrorCode() { // should be defined in .cpp as "key function"
  56. * // Safe because our handleFailure() does not throw exceptions.
  57. * if(isFailure()) { handleFailure(); }
  58. * }
  59. * protected:
  60. * virtual void handleFailure() const {
  61. * log_failure(u_errorName(errorCode));
  62. * exit(errorCode);
  63. * }
  64. * };
  65. * IcuErrorCode error_code;
  66. * UConverter *cnv = ucnv_open("Shift-JIS", error_code);
  67. * length = ucnv_fromUChars(dest, capacity, src, length, error_code);
  68. * ucnv_close(cnv);
  69. * // IcuErrorCode destructor checks for success.
  70. * \endcode
  71. *
  72. * @stable ICU 4.2
  73. */
  74. class U_COMMON_API ErrorCode: public UMemory {
  75. public:
  76. /**
  77. * Default constructor. Initializes its UErrorCode to U_ZERO_ERROR.
  78. * @stable ICU 4.2
  79. */
  80. ErrorCode() : errorCode(U_ZERO_ERROR) {}
  81. /** Destructor, does nothing. See class documentation for details. @stable ICU 4.2 */
  82. virtual ~ErrorCode();
  83. /** Conversion operator, returns a reference. @stable ICU 4.2 */
  84. operator UErrorCode & () { return errorCode; }
  85. /** Conversion operator, returns a pointer. @stable ICU 4.2 */
  86. operator UErrorCode * () { return &errorCode; }
  87. /** Tests for U_SUCCESS(). @stable ICU 4.2 */
  88. UBool isSuccess() const { return U_SUCCESS(errorCode); }
  89. /** Tests for U_FAILURE(). @stable ICU 4.2 */
  90. UBool isFailure() const { return U_FAILURE(errorCode); }
  91. /** Returns the UErrorCode value. @stable ICU 4.2 */
  92. UErrorCode get() const { return errorCode; }
  93. /** Sets the UErrorCode value. @stable ICU 4.2 */
  94. void set(UErrorCode value) { errorCode=value; }
  95. /** Returns the UErrorCode value and resets it to U_ZERO_ERROR. @stable ICU 4.2 */
  96. UErrorCode reset();
  97. /**
  98. * Asserts isSuccess().
  99. * In other words, this method checks for a failure code,
  100. * and the base class handles it like this:
  101. * \code
  102. * if(isFailure()) { handleFailure(); }
  103. * \endcode
  104. * @stable ICU 4.4
  105. */
  106. void assertSuccess() const;
  107. /**
  108. * Return a string for the UErrorCode value.
  109. * The string will be the same as the name of the error code constant
  110. * in the UErrorCode enum.
  111. * @stable ICU 4.4
  112. */
  113. const char* errorName() const;
  114. protected:
  115. /**
  116. * Internal UErrorCode, accessible to subclasses.
  117. * @stable ICU 4.2
  118. */
  119. UErrorCode errorCode;
  120. /**
  121. * Called by assertSuccess() if isFailure() is true.
  122. * A subclass should override this function to deal with a failure code:
  123. * Throw an exception, log an error, terminate the program, or similar.
  124. * @stable ICU 4.2
  125. */
  126. virtual void handleFailure() const {}
  127. };
  128. U_NAMESPACE_END
  129. #endif // __ERRORCODE_H__