Path: blob/jdk8u272-b10-aarch32-20201026/jdk/src/share/native/common/unicode/errorcode.h
48785 views
// © 2016 and later: Unicode, Inc. and others.1// License & terms of use: http://www.unicode.org/copyright.html2/*3*******************************************************************************4*5* Copyright (C) 2009-2011, International Business Machines6* Corporation and others. All Rights Reserved.7*8*******************************************************************************9* file name: errorcode.h10* encoding: UTF-811* tab size: 8 (not used)12* indentation:413*14* created on: 2009mar1015* created by: Markus W. Scherer16*/1718#ifndef __ERRORCODE_H__19#define __ERRORCODE_H__2021/**22* \file23* \brief C++ API: ErrorCode class intended to make it easier to use24* ICU C and C++ APIs from C++ user code.25*/2627#include "unicode/utypes.h"28#include "unicode/uobject.h"2930U_NAMESPACE_BEGIN3132/**33* Wrapper class for UErrorCode, with conversion operators for direct use34* in ICU C and C++ APIs.35* Intended to be used as a base class, where a subclass overrides36* the handleFailure() function so that it throws an exception,37* does an assert(), logs an error, etc.38* This is not an abstract base class. This class can be used and instantiated39* by itself, although it will be more useful when subclassed.40*41* Features:42* - The constructor initializes the internal UErrorCode to U_ZERO_ERROR,43* removing one common source of errors.44* - Same use in C APIs taking a UErrorCode * (pointer)45* and C++ taking UErrorCode & (reference) via conversion operators.46* - Possible automatic checking for success when it goes out of scope.47*48* Note: For automatic checking for success in the destructor, a subclass49* must implement such logic in its own destructor because the base class50* destructor cannot call a subclass function (like handleFailure()).51* The ErrorCode base class destructor does nothing.52*53* Note also: While it is possible for a destructor to throw an exception,54* it is generally unsafe to do so. This means that in a subclass the destructor55* and the handleFailure() function may need to take different actions.56*57* Sample code:58* \code59* class IcuErrorCode: public icu::ErrorCode {60* public:61* virtual ~IcuErrorCode() { // should be defined in .cpp as "key function"62* // Safe because our handleFailure() does not throw exceptions.63* if(isFailure()) { handleFailure(); }64* }65* protected:66* virtual void handleFailure() const {67* log_failure(u_errorName(errorCode));68* exit(errorCode);69* }70* };71* IcuErrorCode error_code;72* UConverter *cnv = ucnv_open("Shift-JIS", error_code);73* length = ucnv_fromUChars(dest, capacity, src, length, error_code);74* ucnv_close(cnv);75* // IcuErrorCode destructor checks for success.76* \endcode77*78* @stable ICU 4.279*/80class U_COMMON_API ErrorCode: public UMemory {81public:82/**83* Default constructor. Initializes its UErrorCode to U_ZERO_ERROR.84* @stable ICU 4.285*/86ErrorCode() : errorCode(U_ZERO_ERROR) {}87/** Destructor, does nothing. See class documentation for details. @stable ICU 4.2 */88virtual ~ErrorCode();89/** Conversion operator, returns a reference. @stable ICU 4.2 */90operator UErrorCode & () { return errorCode; }91/** Conversion operator, returns a pointer. @stable ICU 4.2 */92operator UErrorCode * () { return &errorCode; }93/** Tests for U_SUCCESS(). @stable ICU 4.2 */94UBool isSuccess() const { return U_SUCCESS(errorCode); }95/** Tests for U_FAILURE(). @stable ICU 4.2 */96UBool isFailure() const { return U_FAILURE(errorCode); }97/** Returns the UErrorCode value. @stable ICU 4.2 */98UErrorCode get() const { return errorCode; }99/** Sets the UErrorCode value. @stable ICU 4.2 */100void set(UErrorCode value) { errorCode=value; }101/** Returns the UErrorCode value and resets it to U_ZERO_ERROR. @stable ICU 4.2 */102UErrorCode reset();103/**104* Asserts isSuccess().105* In other words, this method checks for a failure code,106* and the base class handles it like this:107* \code108* if(isFailure()) { handleFailure(); }109* \endcode110* @stable ICU 4.4111*/112void assertSuccess() const;113/**114* Return a string for the UErrorCode value.115* The string will be the same as the name of the error code constant116* in the UErrorCode enum.117* @stable ICU 4.4118*/119const char* errorName() const;120121protected:122/**123* Internal UErrorCode, accessible to subclasses.124* @stable ICU 4.2125*/126UErrorCode errorCode;127/**128* Called by assertSuccess() if isFailure() is true.129* A subclass should override this function to deal with a failure code:130* Throw an exception, log an error, terminate the program, or similar.131* @stable ICU 4.2132*/133virtual void handleFailure() const {}134};135136U_NAMESPACE_END137138#endif // __ERRORCODE_H__139140141