tesseract
3.03
|
00001 /********************************************************************** 00002 * File: icuerrorcode.h 00003 * Description: Wrapper class for UErrorCode, with conversion operators for 00004 * direct use in ICU C and C++ APIs. 00005 * Author: Fredrik Roubert 00006 * Created: Thu July 4 2013 00007 * 00008 * Features: 00009 * - The constructor initializes the internal UErrorCode to U_ZERO_ERROR, 00010 * removing one common source of errors. 00011 * - Same use in C APIs taking a UErrorCode* (pointer) and C++ taking 00012 * UErrorCode& (reference), via conversion operators. 00013 * - Automatic checking for success when it goes out of scope. On failure, 00014 * the destructor will log an error message and exit. 00015 * 00016 * Most of ICU will handle errors gracefully and provide sensible fallbacks. 00017 * Using IcuErrorCode, it is therefore possible to write very compact code 00018 * that does sensible things on failure and provides logging for debugging. 00019 * 00020 * Example: 00021 * IcuErrorCode icuerrorcode; 00022 * return collator.compareUTF8(a, b, icuerrorcode) == UCOL_EQUAL; 00023 * 00024 * (C) Copyright 2013, Google Inc. 00025 * Licensed under the Apache License, Version 2.0 (the "License"); 00026 * you may not use this file except in compliance with the License. 00027 * You may obtain a copy of the License at 00028 * http://www.apache.org/licenses/LICENSE-2.0 00029 * Unless required by applicable law or agreed to in writing, software 00030 * distributed under the License is distributed on an "AS IS" BASIS, 00031 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00032 * See the License for the specific language governing permissions and 00033 * limitations under the License. 00034 * 00035 **********************************************************************/ 00036 #ifndef TESSERACT_CCUTIL_ICUERRORCODE_H_ 00037 #define TESSERACT_CCUTIL_ICUERRORCODE_H_ 00038 00039 #include "tprintf.h" 00040 #include "unicode/errorcode.h" // From libicu 00041 00042 namespace tesseract { 00043 00044 class IcuErrorCode : public icu::ErrorCode { 00045 public: 00046 IcuErrorCode() {} 00047 virtual ~IcuErrorCode() { 00048 if (isFailure()) { 00049 handleFailure(); 00050 } 00051 } 00052 00053 protected: 00054 virtual void handleFailure() const { 00055 tprintf("ICU ERROR: %s", errorName()); 00056 exit(errorCode); 00057 } 00058 00059 private: 00060 // Disallow implicit copying of object. 00061 IcuErrorCode(const IcuErrorCode&); 00062 void operator=(const IcuErrorCode&); 00063 }; 00064 00065 } // namespace tesseract 00066 #endif // TESSERACT_CCUTIL_ICUERRORCODE_H_