1 // compat.h 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // Author: riley (at) google.com (Michael Riley) 16 // 17 // \file 18 // Google compatibility declarations and inline definitions. 19 20 #ifndef FST_LIB_COMPAT_H__ 21 #define FST_LIB_COMPAT_H__ 22 23 #include <dlfcn.h> 24 25 #include <climits> 26 #include <cstdlib> 27 #include <cstring> 28 #include <iostream> 29 #include <string> 30 #include <vector> 31 32 // Makes copy constructor and operator= private 33 #define DISALLOW_COPY_AND_ASSIGN(type) \ 34 type(const type&); \ 35 void operator=(const type&) 36 37 #include <fst/config.h> 38 #include <fst/types.h> 39 #include <fst/lock.h> 40 #include <fst/flags.h> 41 #include <fst/log.h> 42 43 #ifdef HAVE_ICU 44 #include <fst/icu.h> 45 #endif 46 47 using std::cin; 48 using std::cout; 49 using std::cerr; 50 using std::endl; 51 using std::string; 52 53 void FailedNewHandler(); 54 55 namespace fst { 56 57 using namespace std; 58 59 void SplitToVector(char *line, const char *delim, 60 std::vector<char *> *vec, bool omit_empty_strings); 61 62 // Downcasting 63 template<typename To, typename From> 64 inline To down_cast(From* f) { 65 return static_cast<To>(f); 66 } 67 68 // Bitcasting 69 template <class Dest, class Source> 70 inline Dest bit_cast(const Source& source) { 71 // Compile time assertion: sizeof(Dest) == sizeof(Source) 72 // A compile error here means your Dest and Source have different sizes. 73 typedef char VerifySizesAreEqual [sizeof(Dest) == sizeof(Source) ? 1 : 74 -1]; 75 Dest dest; 76 memcpy(&dest, &source, sizeof(dest)); 77 return dest; 78 } 79 80 // Check sums 81 class CheckSummer { 82 public: 83 CheckSummer() : count_(0) { 84 check_sum_.resize(kCheckSumLength, '\0'); 85 } 86 87 void Reset() { 88 count_ = 0; 89 for (int i = 0; i < kCheckSumLength; ++i) 90 check_sum_[0] = '\0'; 91 } 92 93 void Update(void const *data, int size) { 94 const char *p = reinterpret_cast<const char *>(data); 95 for (int i = 0; i < size; ++i) 96 check_sum_[(count_++) % kCheckSumLength] ^= p[i]; 97 } 98 99 void Update(string const &data) { 100 for (int i = 0; i < data.size(); ++i) 101 check_sum_[(count_++) % kCheckSumLength] ^= data[i]; 102 } 103 104 string Digest() { 105 return check_sum_; 106 } 107 108 private: 109 static const int kCheckSumLength = 32; 110 int count_; 111 string check_sum_; 112 113 DISALLOW_COPY_AND_ASSIGN(CheckSummer); 114 }; 115 116 // Define the UTF8 string conversion function to throw an error 117 // when the ICU Library is missing or disabled. 118 #ifndef HAVE_ICU 119 120 template <class Label> 121 bool UTF8StringToLabels(const string&, std::vector<Label>*) { 122 LOG(ERROR) << "UTF8StringToLabels: ICU Library required for UTF8 handling"; 123 return false; 124 } 125 126 template <class Label> 127 bool LabelsToUTF8String(const std::vector<Label>&, string*) { 128 LOG(ERROR) << "LabelsToUTF8String: ICU Library required for UTF8 handling"; 129 return false; 130 } 131 132 #endif // HAVE_ICU 133 134 } // namespace fst 135 136 137 // Define missing hash functions if needed 138 #ifndef HAVE_STD__TR1__HASH_LONG_LONG_UNSIGNED_ 139 namespace std { 140 namespace tr1 { 141 142 template <class T> class hash; 143 144 template<> struct hash<uint64> { 145 size_t operator()(uint64 x) const { return x; } 146 }; 147 148 } 149 } 150 #endif // HAVE_STD__TR1__HASH_LONG_LONG_UNSIGNED_ 151 152 #endif // FST_LIB_COMPAT_H__ 153