All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 "tprintf.h"
40 #include "unicode/errorcode.h" // From libicu
41 
42 namespace tesseract {
43 
44 class IcuErrorCode : public icu::ErrorCode {
45  public:
47  virtual ~IcuErrorCode() {
48  if (isFailure()) {
49  handleFailure();
50  }
51  }
52 
53  protected:
54  virtual void handleFailure() const {
55  tprintf("ICU ERROR: %s", errorName());
56  exit(errorCode);
57  }
58 
59  private:
60  // Disallow implicit copying of object.
61  IcuErrorCode(const IcuErrorCode&);
62  void operator=(const IcuErrorCode&);
63 };
64 
65 } // namespace tesseract
66 #endif // TESSERACT_CCUTIL_ICUERRORCODE_H_
#define tprintf(...)
Definition: tprintf.h:31
virtual void handleFailure() const
Definition: icuerrorcode.h:54