Home | History | Annotate | Download | only in fst
      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 #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 void SplitToVector(char *line, const char *delim,
     57                    std::vector<char *> *vec, bool omit_empty_strings);
     58 
     59 // Downcasting
     60 template<typename To, typename From>
     61 inline To down_cast(From* f) {
     62   return static_cast<To>(f);
     63 }
     64 
     65 // Bitcasting
     66 template <class Dest, class Source>
     67 inline Dest bit_cast(const Source& source) {
     68   // Compile time assertion: sizeof(Dest) == sizeof(Source)
     69   // A compile error here means your Dest and Source have different sizes.
     70   typedef char VerifySizesAreEqual [sizeof(Dest) == sizeof(Source) ? 1 :
     71                                     -1];
     72   Dest dest;
     73   memcpy(&dest, &source, sizeof(dest));
     74   return dest;
     75 }
     76 
     77 // Check sums
     78 class CheckSummer {
     79  public:
     80   CheckSummer() : count_(0) {
     81     check_sum_.resize(kCheckSumLength, '\0');
     82   }
     83 
     84   void Reset() {
     85     count_ = 0;
     86     for (int i = 0; i < kCheckSumLength; ++i)
     87       check_sum_[i] = '\0';
     88   }
     89 
     90   void Update(void const *data, int size) {
     91     const char *p = reinterpret_cast<const char *>(data);
     92     for (int i = 0; i < size; ++i)
     93       check_sum_[(count_++) % kCheckSumLength] ^= p[i];
     94   }
     95 
     96   void Update(string const &data) {
     97     for (int i = 0; i < data.size(); ++i)
     98       check_sum_[(count_++) % kCheckSumLength] ^= data[i];
     99   }
    100 
    101   string Digest() {
    102     return check_sum_;
    103   }
    104 
    105  private:
    106   static const int kCheckSumLength = 32;
    107   int count_;
    108   string check_sum_;
    109 
    110   DISALLOW_COPY_AND_ASSIGN(CheckSummer);
    111 };
    112 
    113 }  // namespace fst
    114 
    115 
    116 // Define missing hash functions if needed
    117 #ifndef HAVE_STD__TR1__HASH_LONG_LONG_UNSIGNED_
    118 namespace std {
    119 namespace tr1 {
    120 
    121 template <class T> class hash;
    122 
    123 template<> struct hash<uint64> {
    124   size_t operator()(uint64 x) const { return x; }
    125 };
    126 
    127 }
    128 }
    129 #endif  // HAVE_STD__TR1__HASH_LONG_LONG_UNSIGNED_
    130 
    131 #endif  // FST_LIB_COMPAT_H__
    132