FST  openfst-1.7.1
OpenFst Library
compat.h
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 //
13 // See www.openfst.org for extensive documentation on this weighted
14 // finite-state transducer library.
15 
16 #ifndef FST_LIB_COMPAT_H_
17 #define FST_LIB_COMPAT_H_
18 
19 #include <climits>
20 #include <cstdlib>
21 #include <cstring>
22 #include <iostream>
23 #include <string>
24 #include <vector>
25 
26 // Makes copy constructor and operator= private
27 // Deprecated: now just use =delete.
28 #define DISALLOW_COPY_AND_ASSIGN(type) \
29  type(const type&); \
30  void operator=(const type&)
31 
32 #if defined(__GNUC__) || defined(__clang__)
33 #define OPENFST_DEPRECATED(message) __attribute__((deprecated(message)))
34 #elif defined(_MSC_VER)
35 #define OPENFST_DEPRECATED(message) __declspec(deprecated(message))
36 #else
37 #define OPENFST_DEPRECATED(message)
38 #endif
39 
40 #include <fst/config.h>
41 #include <fst/types.h>
42 #include <fst/lock.h>
43 #include <fst/flags.h>
44 #include <fst/log.h>
45 #include <fst/icu.h>
46 
47 using std::string;
48 
49 void FailedNewHandler();
50 
51 namespace fst {
52 
53 // Downcasting.
54 template<typename To, typename From>
55 inline To down_cast(From* f) { return static_cast<To>(f); }
56 
57 // Bitcasting.
58 template <class Dest, class Source>
59 inline Dest bit_cast(const Source &source) {
60  static_assert(sizeof(Dest) == sizeof(Source),
61  "Bitcasting unsafe for specified types");
62  Dest dest;
63  memcpy(&dest, &source, sizeof(dest));
64  return dest;
65 }
66 
67 // Check sums
68 class CheckSummer {
69  public:
70  CheckSummer() : count_(0) {
71  check_sum_.resize(kCheckSumLength, '\0');
72  }
73 
74  void Reset() {
75  count_ = 0;
76  for (int i = 0; i < kCheckSumLength; ++i) check_sum_[i] = '\0';
77  }
78 
79  void Update(void const *data, int size) {
80  const char *p = reinterpret_cast<const char *>(data);
81  for (int i = 0; i < size; ++i) {
82  check_sum_[(count_++) % kCheckSumLength] ^= p[i];
83  }
84  }
85 
86  void Update(string const &data) {
87  for (int i = 0; i < data.size(); ++i) {
88  check_sum_[(count_++) % kCheckSumLength] ^= data[i];
89  }
90  }
91 
92  string Digest() { return check_sum_; }
93 
94  private:
95  static const int kCheckSumLength = 32;
96  int count_;
97  string check_sum_;
98 
99  CheckSummer(const CheckSummer &) = delete;
100  CheckSummer &operator=(const CheckSummer &) = delete;
101 };
102 
103 } // namespace fst
104 
105 #endif // FST_LIB_COMPAT_H_
Dest bit_cast(const Source &source)
Definition: compat.h:59
void Reset()
Definition: compat.h:74
To down_cast(From *f)
Definition: compat.h:55
void FailedNewHandler()
Definition: compat.cc:25
void Update(void const *data, int size)
Definition: compat.h:79
void Update(string const &data)
Definition: compat.h:86
string Digest()
Definition: compat.h:92