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