errorcode.h 4.8 KB

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