errorcode.h 4.8 KB

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