tesseract v5.3.3.20231005
icuerrorcode.h
Go to the documentation of this file.
1/**********************************************************************
2 * File: icuerrorcode.h
3 * Description: Wrapper class for UErrorCode, with conversion operators for
4 * direct use in ICU C and C++ APIs.
5 * Author: Fredrik Roubert
6 * Created: Thu July 4 2013
7 *
8 * Features:
9 * - The constructor initializes the internal UErrorCode to U_ZERO_ERROR,
10 * removing one common source of errors.
11 * - Same use in C APIs taking a UErrorCode* (pointer) and C++ taking
12 * UErrorCode& (reference), via conversion operators.
13 * - Automatic checking for success when it goes out of scope. On failure,
14 * the destructor will log an error message and exit.
15 *
16 * Most of ICU will handle errors gracefully and provide sensible fallbacks.
17 * Using IcuErrorCode, it is therefore possible to write very compact code
18 * that does sensible things on failure and provides logging for debugging.
19 *
20 * Example:
21 * IcuErrorCode icuerrorcode;
22 * return collator.compareUTF8(a, b, icuerrorcode) == UCOL_EQUAL;
23 *
24 * (C) Copyright 2013, Google Inc.
25 * Licensed under the Apache License, Version 2.0 (the "License");
26 * you may not use this file except in compliance with the License.
27 * You may obtain a copy of the License at
28 * http://www.apache.org/licenses/LICENSE-2.0
29 * Unless required by applicable law or agreed to in writing, software
30 * distributed under the License is distributed on an "AS IS" BASIS,
31 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32 * See the License for the specific language governing permissions and
33 * limitations under the License.
34 *
35 **********************************************************************/
36#ifndef TESSERACT_CCUTIL_ICUERRORCODE_H_
37#define TESSERACT_CCUTIL_ICUERRORCODE_H_
38
39#include <cstdlib> // for exit
40#include "tprintf.h"
41#include "unicode/errorcode.h" // From libicu
42
43namespace tesseract {
44
45class IcuErrorCode : public icu::ErrorCode {
46public:
47 IcuErrorCode() = default;
48 ~IcuErrorCode() override;
49
50protected:
51 void handleFailure() const override {
52 tprintf("ICU ERROR: %s\n", errorName());
53 exit(errorCode);
54 }
55
56private:
57 // Disallow implicit copying of object.
58 IcuErrorCode(const IcuErrorCode &) = delete;
59 void operator=(const IcuErrorCode &) = delete;
60};
61
62} // namespace tesseract
63#endif // TESSERACT_CCUTIL_ICUERRORCODE_H_
void tprintf(const char *format,...)
Definition: tprintf.cpp:41
void handleFailure() const override
Definition: icuerrorcode.h:51