tesseract  5.0.0-alpha-619-ge9db
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 
43 namespace tesseract {
44 
45 class IcuErrorCode : public icu::ErrorCode {
46  public:
47  IcuErrorCode() {}
48  virtual ~IcuErrorCode();
49 
50  protected:
51  virtual void handleFailure() const {
52  tprintf("ICU ERROR: %s\n", errorName());
53  exit(errorCode);
54  }
55 
56  private:
57  // Disallow implicit copying of object.
58  IcuErrorCode(const IcuErrorCode&);
59  void operator=(const IcuErrorCode&);
60 };
61 
62 } // namespace tesseract
63 #endif // TESSERACT_CCUTIL_ICUERRORCODE_H_
tesseract::IcuErrorCode::IcuErrorCode
IcuErrorCode()
Definition: icuerrorcode.h:112
tesseract
Definition: baseapi.h:65
tprintf.h
tesseract::IcuErrorCode::~IcuErrorCode
virtual ~IcuErrorCode()
Definition: icuerrorcode.cpp:22
tprintf
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:34
tesseract::IcuErrorCode::handleFailure
virtual void handleFailure() const
Definition: icuerrorcode.h:116