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@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 #include <fst/icu.h>
43 
44 using std::cin;
45 using std::cout;
46 using std::cerr;
47 using std::endl;
48 using std::string;
49 
50 void FailedNewHandler();
51 
52 namespace fst {
53 
54 using namespace std;
55 
56 // Downcasting
57 template<typename To, typename From>
down_cast(From * f)58 inline To down_cast(From* f) {
59   return static_cast<To>(f);
60 }
61 
62 // Bitcasting
63 template <class Dest, class Source>
bit_cast(const Source & source)64 inline Dest bit_cast(const Source& source) {
65   // Compile time assertion: sizeof(Dest) == sizeof(Source)
66   // A compile error here means your Dest and Source have different sizes.
67   typedef char VerifySizesAreEqual [sizeof(Dest) == sizeof(Source) ? 1 :
68                                     -1];
69   Dest dest;
70   memcpy(&dest, &source, sizeof(dest));
71   return dest;
72 }
73 
74 // Check sums
75 class CheckSummer {
76  public:
CheckSummer()77   CheckSummer() : count_(0) {
78     check_sum_.resize(kCheckSumLength, '\0');
79   }
80 
Reset()81   void Reset() {
82     count_ = 0;
83     for (int i = 0; i < kCheckSumLength; ++i)
84       check_sum_[i] = '\0';
85   }
86 
Update(void const * data,int size)87   void Update(void const *data, int size) {
88     const char *p = reinterpret_cast<const char *>(data);
89     for (int i = 0; i < size; ++i)
90       check_sum_[(count_++) % kCheckSumLength] ^= p[i];
91   }
92 
Update(string const & data)93   void Update(string const &data) {
94     for (int i = 0; i < data.size(); ++i)
95       check_sum_[(count_++) % kCheckSumLength] ^= data[i];
96   }
97 
Digest()98   string Digest() {
99     return check_sum_;
100   }
101 
102  private:
103   static const int kCheckSumLength = 32;
104   int count_;
105   string check_sum_;
106 
107   DISALLOW_COPY_AND_ASSIGN(CheckSummer);
108 };
109 
110 }  // namespace fst
111 
112 #endif  // FST_LIB_COMPAT_H__
113