2009-03-11 05:27:11 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
|
|
|
*
|
2016-05-26 22:32:17 +00:00
|
|
|
* Copyright (C) 2016 and later: Unicode, Inc. and others.
|
|
|
|
* License & terms of use: http://www.unicode.org/copyright.html
|
2009-03-11 05:27:11 +00:00
|
|
|
*
|
|
|
|
*******************************************************************************
|
|
|
|
* file name: errorcode.h
|
|
|
|
* encoding: US-ASCII
|
|
|
|
* tab size: 8 (not used)
|
|
|
|
* indentation:4
|
|
|
|
*
|
|
|
|
* created on: 2009mar10
|
|
|
|
* created by: Markus W. Scherer
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __ERRORCODE_H__
|
|
|
|
#define __ERRORCODE_H__
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* \brief C++ API: ErrorCode class intended to make it easier to use
|
|
|
|
* ICU C and C++ APIs from C++ user code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
#include "unicode/uobject.h"
|
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper class for UErrorCode, with conversion operators for direct use
|
|
|
|
* in ICU C and C++ APIs.
|
|
|
|
* Intended to be used as a base class, where a subclass overrides
|
|
|
|
* the handleFailure() function so that it throws an exception,
|
|
|
|
* does an assert(), logs an error, etc.
|
|
|
|
* This is not an abstract base class. This class can be used and instantiated
|
|
|
|
* by itself, although it will be more useful when subclassed.
|
|
|
|
*
|
|
|
|
* Features:
|
|
|
|
* - The constructor initializes the internal UErrorCode to U_ZERO_ERROR,
|
|
|
|
* removing one common source of errors.
|
|
|
|
* - Same use in C APIs taking a UErrorCode * (pointer)
|
|
|
|
* and C++ taking UErrorCode & (reference) via conversion operators.
|
2009-03-11 17:57:24 +00:00
|
|
|
* - Possible automatic checking for success when it goes out of scope.
|
2009-03-11 05:27:11 +00:00
|
|
|
*
|
2009-03-11 17:57:24 +00:00
|
|
|
* Note: For automatic checking for success in the destructor, a subclass
|
|
|
|
* must implement such logic in its own destructor because the base class
|
|
|
|
* destructor cannot call a subclass function (like handleFailure()).
|
|
|
|
* The ErrorCode base class destructor does nothing.
|
|
|
|
*
|
|
|
|
* Note also: While it is possible for a destructor to throw an exception,
|
|
|
|
* it is generally unsafe to do so. This means that in a subclass the destructor
|
|
|
|
* and the handleFailure() function may need to take different actions.
|
|
|
|
*
|
|
|
|
* Sample code:
|
2009-03-11 05:27:11 +00:00
|
|
|
* \code
|
2009-03-11 17:57:24 +00:00
|
|
|
* class IcuErrorCode: public icu::ErrorCode {
|
|
|
|
* public:
|
2011-07-26 05:32:25 +00:00
|
|
|
* virtual ~IcuErrorCode() { // should be defined in .cpp as "key function"
|
2009-03-11 17:57:24 +00:00
|
|
|
* // Safe because our handleFailure() does not throw exceptions.
|
|
|
|
* if(isFailure()) { handleFailure(); }
|
|
|
|
* }
|
|
|
|
* protected:
|
2009-11-13 19:25:21 +00:00
|
|
|
* virtual void handleFailure() const {
|
2009-03-11 17:57:24 +00:00
|
|
|
* log_failure(u_errorName(errorCode));
|
|
|
|
* exit(errorCode);
|
|
|
|
* }
|
|
|
|
* };
|
2009-03-11 05:27:11 +00:00
|
|
|
* IcuErrorCode error_code;
|
|
|
|
* UConverter *cnv = ucnv_open("Shift-JIS", error_code);
|
|
|
|
* length = ucnv_fromUChars(dest, capacity, src, length, error_code);
|
|
|
|
* ucnv_close(cnv);
|
|
|
|
* // IcuErrorCode destructor checks for success.
|
|
|
|
* \endcode
|
|
|
|
*
|
2010-01-28 21:09:53 +00:00
|
|
|
* @stable ICU 4.2
|
2009-03-11 05:27:11 +00:00
|
|
|
*/
|
|
|
|
class U_COMMON_API ErrorCode: public UMemory {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Default constructor. Initializes its UErrorCode to U_ZERO_ERROR.
|
2010-01-28 21:09:53 +00:00
|
|
|
* @stable ICU 4.2
|
2009-03-11 05:27:11 +00:00
|
|
|
*/
|
|
|
|
ErrorCode() : errorCode(U_ZERO_ERROR) {}
|
2010-01-28 21:09:53 +00:00
|
|
|
/** Destructor, does nothing. See class documentation for details. @stable ICU 4.2 */
|
2011-07-26 05:32:25 +00:00
|
|
|
virtual ~ErrorCode();
|
2010-01-28 21:09:53 +00:00
|
|
|
/** Conversion operator, returns a reference. @stable ICU 4.2 */
|
2009-03-11 05:27:11 +00:00
|
|
|
operator UErrorCode & () { return errorCode; }
|
2010-01-28 21:09:53 +00:00
|
|
|
/** Conversion operator, returns a pointer. @stable ICU 4.2 */
|
2009-03-11 05:27:11 +00:00
|
|
|
operator UErrorCode * () { return &errorCode; }
|
2010-01-28 21:09:53 +00:00
|
|
|
/** Tests for U_SUCCESS(). @stable ICU 4.2 */
|
2009-03-11 05:27:11 +00:00
|
|
|
UBool isSuccess() const { return U_SUCCESS(errorCode); }
|
2010-01-28 21:09:53 +00:00
|
|
|
/** Tests for U_FAILURE(). @stable ICU 4.2 */
|
2009-03-11 05:27:11 +00:00
|
|
|
UBool isFailure() const { return U_FAILURE(errorCode); }
|
2010-01-28 21:09:53 +00:00
|
|
|
/** Returns the UErrorCode value. @stable ICU 4.2 */
|
2009-03-11 05:27:11 +00:00
|
|
|
UErrorCode get() const { return errorCode; }
|
2010-01-28 21:09:53 +00:00
|
|
|
/** Sets the UErrorCode value. @stable ICU 4.2 */
|
2009-03-11 05:27:11 +00:00
|
|
|
void set(UErrorCode value) { errorCode=value; }
|
2010-01-28 21:09:53 +00:00
|
|
|
/** Returns the UErrorCode value and resets it to U_ZERO_ERROR. @stable ICU 4.2 */
|
2009-03-11 05:27:11 +00:00
|
|
|
UErrorCode reset();
|
|
|
|
/**
|
2009-05-27 22:31:13 +00:00
|
|
|
* Asserts isSuccess().
|
|
|
|
* In other words, this method checks for a failure code,
|
|
|
|
* and the base class handles it like this:
|
2009-03-11 17:57:24 +00:00
|
|
|
* \code
|
|
|
|
* if(isFailure()) { handleFailure(); }
|
|
|
|
* \endcode
|
2010-11-12 06:03:28 +00:00
|
|
|
* @stable ICU 4.4
|
2009-03-11 05:27:11 +00:00
|
|
|
*/
|
2009-05-27 22:31:13 +00:00
|
|
|
void assertSuccess() const;
|
2009-11-09 15:40:57 +00:00
|
|
|
/**
|
|
|
|
* Return a string for the UErrorCode value.
|
|
|
|
* The string will be the same as the name of the error code constant
|
|
|
|
* in the UErrorCode enum.
|
2010-11-12 06:03:28 +00:00
|
|
|
* @stable ICU 4.4
|
2009-11-09 15:40:57 +00:00
|
|
|
*/
|
|
|
|
const char* errorName() const;
|
2009-03-11 05:27:11 +00:00
|
|
|
|
|
|
|
protected:
|
2009-03-11 17:57:24 +00:00
|
|
|
/**
|
|
|
|
* Internal UErrorCode, accessible to subclasses.
|
2010-01-28 21:09:53 +00:00
|
|
|
* @stable ICU 4.2
|
2009-03-11 17:57:24 +00:00
|
|
|
*/
|
2009-03-11 05:27:11 +00:00
|
|
|
UErrorCode errorCode;
|
2009-03-11 17:57:24 +00:00
|
|
|
/**
|
2009-05-27 22:31:13 +00:00
|
|
|
* Called by assertSuccess() if isFailure() is true.
|
2009-03-11 17:57:24 +00:00
|
|
|
* A subclass should override this function to deal with a failure code:
|
|
|
|
* Throw an exception, log an error, terminate the program, or similar.
|
2010-01-28 21:09:53 +00:00
|
|
|
* @stable ICU 4.2
|
2009-03-11 17:57:24 +00:00
|
|
|
*/
|
|
|
|
virtual void handleFailure() const {}
|
2009-03-11 05:27:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif // __ERRORCODE_H__
|