Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/common/unicode/errorcode.h
38827 views
/*1*******************************************************************************2*3* Copyright (C) 2009-2011, International Business Machines4* Corporation and others. All Rights Reserved.5*6*******************************************************************************7* file name: errorcode.h8* encoding: US-ASCII9* tab size: 8 (not used)10* indentation:411*12* created on: 2009mar1013* created by: Markus W. Scherer14*/1516#ifndef __ERRORCODE_H__17#define __ERRORCODE_H__1819/**20* \file21* \brief C++ API: ErrorCode class intended to make it easier to use22* ICU C and C++ APIs from C++ user code.23*/2425#include "unicode/utypes.h"26#include "unicode/uobject.h"2728U_NAMESPACE_BEGIN2930/**31* Wrapper class for UErrorCode, with conversion operators for direct use32* in ICU C and C++ APIs.33* Intended to be used as a base class, where a subclass overrides34* the handleFailure() function so that it throws an exception,35* does an assert(), logs an error, etc.36* This is not an abstract base class. This class can be used and instantiated37* by itself, although it will be more useful when subclassed.38*39* Features:40* - The constructor initializes the internal UErrorCode to U_ZERO_ERROR,41* removing one common source of errors.42* - Same use in C APIs taking a UErrorCode * (pointer)43* and C++ taking UErrorCode & (reference) via conversion operators.44* - Possible automatic checking for success when it goes out of scope.45*46* Note: For automatic checking for success in the destructor, a subclass47* must implement such logic in its own destructor because the base class48* destructor cannot call a subclass function (like handleFailure()).49* The ErrorCode base class destructor does nothing.50*51* Note also: While it is possible for a destructor to throw an exception,52* it is generally unsafe to do so. This means that in a subclass the destructor53* and the handleFailure() function may need to take different actions.54*55* Sample code:56* \code57* class IcuErrorCode: public icu::ErrorCode {58* public:59* virtual ~IcuErrorCode() { // should be defined in .cpp as "key function"60* // Safe because our handleFailure() does not throw exceptions.61* if(isFailure()) { handleFailure(); }62* }63* protected:64* virtual void handleFailure() const {65* log_failure(u_errorName(errorCode));66* exit(errorCode);67* }68* };69* IcuErrorCode error_code;70* UConverter *cnv = ucnv_open("Shift-JIS", error_code);71* length = ucnv_fromUChars(dest, capacity, src, length, error_code);72* ucnv_close(cnv);73* // IcuErrorCode destructor checks for success.74* \endcode75*76* @stable ICU 4.277*/78class U_COMMON_API ErrorCode: public UMemory {79public:80/**81* Default constructor. Initializes its UErrorCode to U_ZERO_ERROR.82* @stable ICU 4.283*/84ErrorCode() : errorCode(U_ZERO_ERROR) {}85/** Destructor, does nothing. See class documentation for details. @stable ICU 4.2 */86virtual ~ErrorCode();87/** Conversion operator, returns a reference. @stable ICU 4.2 */88operator UErrorCode & () { return errorCode; }89/** Conversion operator, returns a pointer. @stable ICU 4.2 */90operator UErrorCode * () { return &errorCode; }91/** Tests for U_SUCCESS(). @stable ICU 4.2 */92UBool isSuccess() const { return U_SUCCESS(errorCode); }93/** Tests for U_FAILURE(). @stable ICU 4.2 */94UBool isFailure() const { return U_FAILURE(errorCode); }95/** Returns the UErrorCode value. @stable ICU 4.2 */96UErrorCode get() const { return errorCode; }97/** Sets the UErrorCode value. @stable ICU 4.2 */98void set(UErrorCode value) { errorCode=value; }99/** Returns the UErrorCode value and resets it to U_ZERO_ERROR. @stable ICU 4.2 */100UErrorCode reset();101/**102* Asserts isSuccess().103* In other words, this method checks for a failure code,104* and the base class handles it like this:105* \code106* if(isFailure()) { handleFailure(); }107* \endcode108* @stable ICU 4.4109*/110void assertSuccess() const;111/**112* Return a string for the UErrorCode value.113* The string will be the same as the name of the error code constant114* in the UErrorCode enum.115* @stable ICU 4.4116*/117const char* errorName() const;118119protected:120/**121* Internal UErrorCode, accessible to subclasses.122* @stable ICU 4.2123*/124UErrorCode errorCode;125/**126* Called by assertSuccess() if isFailure() is true.127* A subclass should override this function to deal with a failure code:128* Throw an exception, log an error, terminate the program, or similar.129* @stable ICU 4.2130*/131virtual void handleFailure() const {}132};133134U_NAMESPACE_END135136#endif // __ERRORCODE_H__137138139