|
15 | 15 | #ifndef COMMON_H_ |
16 | 16 | #define COMMON_H_ |
17 | 17 |
|
18 | | -#include <stdint.h> |
19 | | -#include <stdlib.h> |
20 | | -#include <string.h> |
21 | | - |
| 18 | +#include <cstdint> |
22 | 19 | #include <iostream> |
23 | | -#include <memory> |
24 | | -#include <string> |
25 | | -#include <utility> |
26 | | -#include <vector> |
27 | 20 |
|
28 | | -#include "config.h" |
| 21 | +#include "third_party/absl/log/check.h" |
| 22 | +#include "third_party/absl/log/globals.h" |
| 23 | +#include "third_party/absl/log/log.h" |
29 | 24 | #include "third_party/absl/strings/string_view.h" |
30 | 25 |
|
31 | 26 | #if defined(_WIN32) && !defined(__CYGWIN__) |
|
41 | 36 | #include <windows.h> |
42 | 37 | #endif |
43 | 38 |
|
44 | | -typedef uint32_t char32; |
| 39 | +using char32 = uint32_t; |
45 | 40 |
|
46 | 41 | static constexpr uint32_t kUnicodeError = 0xFFFD; |
47 | 42 |
|
48 | | -#if defined(_FREEBSD) |
49 | | -#include <sys/endian.h> |
50 | | -#endif |
51 | | -#if !defined(__APPLE__) && !defined(_WIN32) && !defined(_FREEBSD) && \ |
52 | | - !defined(_AIX) |
53 | | -#include <endian.h> |
54 | | -#if BYTE_ORDER == __BIG_ENDIAN |
55 | | -#define IS_BIG_ENDIAN |
56 | | -#endif |
57 | | -#endif |
58 | | - |
59 | | -#if defined(_AIX) && BYTE_ORDER == BIG_ENDIAN |
60 | | -#define IS_BIG_ENDIAN |
61 | | -#endif |
62 | | - |
63 | | -namespace sentencepiece { |
64 | | -namespace util { |
65 | | - |
66 | | -inline uint32_t Swap32(uint32_t x) { |
67 | | -#ifdef OS_WIN |
68 | | - return _byteswap_ulong(x); |
69 | | -#else // OS_WIN |
70 | | - return __builtin_bswap32(x); |
71 | | -#endif // OS_WIN |
72 | | -} |
73 | | - |
74 | | -} // namespace util |
75 | | - |
76 | | -constexpr bool is_bigendian() { |
77 | | -#ifdef IS_BIG_ENDIAN |
78 | | - return true; |
79 | | -#else // IS_BIG_ENDIAN |
80 | | - return false; |
81 | | -#endif // IS_BIG_ENDIAN |
82 | | -} |
83 | | - |
84 | | -namespace error { |
85 | | - |
86 | | -void Abort(); |
87 | | -void Exit(int code); |
88 | | - |
89 | | -class Die { |
90 | | - public: |
91 | | - explicit Die(bool die) : die_(die) {} |
92 | | - ~Die() { |
93 | | - std::cerr << std::endl; |
94 | | - if (die_) { |
95 | | - Abort(); |
96 | | - } |
97 | | - } |
98 | | - int operator&(std::ostream &) { return 0; } |
99 | | - |
100 | | - private: |
101 | | - bool die_; |
102 | | -}; |
103 | | -} // namespace error |
104 | | - |
105 | | -namespace logging { |
106 | | -enum LogSeverity { |
107 | | - LOG_INFO = 0, |
108 | | - LOG_WARNING = 1, |
109 | | - LOG_ERROR = 2, |
110 | | - LOG_FATAL = 3, |
111 | | - LOG_SEVERITY_SIZE = 4, |
112 | | -}; |
113 | | - |
114 | | -int GetMinLogLevel(); |
115 | | -void SetMinLogLevel(int v); |
116 | | - |
117 | | -inline absl::string_view BaseName(absl::string_view path) { |
118 | | -#ifdef OS_WIN |
119 | | - const size_t pos = path.find_last_of('\\'); |
120 | | -#else |
121 | | - const size_t pos = path.find_last_of('/'); |
122 | | -#endif |
123 | | - return pos == absl::string_view::npos ? path : path.substr(pos + 1); |
124 | | -} |
125 | | - |
126 | | -} // namespace logging |
127 | | -} // namespace sentencepiece |
128 | | - |
129 | | -#define LOG(severity) \ |
130 | | - (::sentencepiece::logging::GetMinLogLevel() > \ |
131 | | - ::sentencepiece::logging::LOG_##severity) \ |
132 | | - ? 0 \ |
133 | | - : ::sentencepiece::error::Die( \ |
134 | | - ::sentencepiece::logging::LOG_##severity >= \ |
135 | | - ::sentencepiece::logging::LOG_FATAL) & \ |
136 | | - std::cerr << ::sentencepiece::logging::BaseName(__FILE__) << "(" \ |
137 | | - << __LINE__ << ") " \ |
138 | | - << "LOG(" << #severity << ") " |
| 43 | +#define FRIEND_TEST(a, b) friend class a##_Test_##b; |
139 | 44 |
|
140 | | -#define CHECK(condition) \ |
141 | | - (condition) ? 0 \ |
142 | | - : ::sentencepiece::error::Die(true) & \ |
143 | | - std::cerr << ::sentencepiece::logging::BaseName(__FILE__) \ |
144 | | - << "(" << __LINE__ << ") [" << #condition \ |
145 | | - << "] " |
| 45 | +#define RETURN_IF_ERROR(expr) \ |
| 46 | + do { \ |
| 47 | + const auto _status = expr; \ |
| 48 | + if (!_status.ok()) return _status; \ |
| 49 | + } while (0) |
146 | 50 |
|
147 | | -#define CHECK_EQ(a, b) CHECK((a) == (b)) |
148 | | -#define CHECK_NE(a, b) CHECK((a) != (b)) |
149 | | -#define CHECK_GE(a, b) CHECK((a) >= (b)) |
150 | | -#define CHECK_LE(a, b) CHECK((a) <= (b)) |
151 | | -#define CHECK_GT(a, b) CHECK((a) > (b)) |
152 | | -#define CHECK_LT(a, b) CHECK((a) < (b)) |
| 51 | +// CHECK_OK must work on util::Status, not absl::Status. |
| 52 | +#if defined CHECK_OK |
| 53 | +#undef CHECK_OK |
| 54 | +#endif // CHECK_OK |
153 | 55 |
|
154 | | -#define FRIEND_TEST(a, b) friend class a##_Test_##b; |
| 56 | +#if defined QCHECK_OK |
| 57 | +#undef QCHECK_OK |
| 58 | +#endif // QCHECK_OK |
155 | 59 |
|
156 | 60 | #define CHECK_OK(expr) \ |
157 | 61 | do { \ |
158 | 62 | const auto _status = expr; \ |
159 | 63 | CHECK(_status.ok()) << _status.ToString(); \ |
160 | 64 | } while (0) |
161 | 65 |
|
162 | | -#define CHECK_NOT_OK(expr) \ |
163 | | - do { \ |
164 | | - const auto _status = expr; \ |
165 | | - CHECK(!_status.ok()) << _status.ToString(); \ |
166 | | - } while (0) |
167 | | - |
168 | | -#define RETURN_IF_ERROR(expr) \ |
169 | | - do { \ |
170 | | - const auto _status = expr; \ |
171 | | - if (!_status.ok()) return _status; \ |
172 | | - } while (0) |
| 66 | +#define QCHECK_OK CHECK_OK |
173 | 67 |
|
174 | 68 | #endif // COMMON_H_ |
0 commit comments