|
tesseract 3.04.01
|
00001 /********************************************************************** 00002 * File: hashfn.h (Formerly hash.h) 00003 * Description: Portability hacks for hash_map, hash_set and unique_ptr. 00004 * Author: Ray Smith 00005 * Created: Wed Jan 08 14:08:25 PST 2014 00006 * 00007 * (C) Copyright 2014, Google Inc. 00008 ** Licensed under the Apache License, Version 2.0 (the "License"); 00009 ** you may not use this file except in compliance with the License. 00010 ** You may obtain a copy of the License at 00011 ** http://www.apache.org/licenses/LICENSE-2.0 00012 ** Unless required by applicable law or agreed to in writing, software 00013 ** distributed under the License is distributed on an "AS IS" BASIS, 00014 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 ** See the License for the specific language governing permissions and 00016 ** limitations under the License. 00017 * 00018 **********************************************************************/ 00019 00020 #ifndef HASHFN_H 00021 #define HASHFN_H 00022 00023 #ifdef USE_STD_NAMESPACE 00024 #if (__cplusplus >= 201103L) || defined(_MSC_VER) // Visual Studio 00025 #include <unordered_map> 00026 #include <unordered_set> 00027 #define hash_map std::unordered_map 00028 #if (_MSC_VER >= 1500 && _MSC_VER < 1600) // Visual Studio 2008 00029 using namespace std::tr1; 00030 #else // _MSC_VER 00031 using std::unordered_map; 00032 using std::unordered_set; 00033 #include <memory> 00034 #define SmartPtr std::unique_ptr 00035 #define HAVE_UNIQUE_PTR 00036 #endif // _MSC_VER 00037 #elif (defined(__GNUC__) && (((__GNUC__ == 3) && (__GNUC_MINOR__ > 0)) || \ 00038 __GNUC__ >= 4)) // gcc 00039 // hash_set is deprecated in gcc 00040 #include <ext/hash_map> 00041 #include <ext/hash_set> 00042 using __gnu_cxx::hash_map; 00043 using __gnu_cxx::hash_set; 00044 #define unordered_map hash_map 00045 #define unordered_set hash_set 00046 #else 00047 #include <hash_map> 00048 #include <hash_set> 00049 #endif // gcc 00050 #elif (__clang__) 00051 #include <unordered_map> 00052 #include <unordered_set> 00053 #define hash_map std::unordered_map 00054 #define unordered_set std::unordered_set 00055 #else // USE_STD_NAMESPACE 00056 #include <hash_map> 00057 #include <hash_set> 00058 #define unordered_map hash_map 00059 #define unordered_set hash_set 00060 #endif // USE_STD_NAMESPACE 00061 00062 #ifndef HAVE_UNIQUE_PTR 00063 // Trivial smart ptr. Expand to add features of std::unique_ptr as required. 00064 template<class T> class SmartPtr { 00065 public: 00066 SmartPtr() : ptr_(NULL) {} 00067 explicit SmartPtr(T* ptr) : ptr_(ptr) {} 00068 ~SmartPtr() { 00069 delete ptr_; 00070 } 00071 00072 T* get() const { 00073 return ptr_; 00074 } 00075 void reset(T* ptr) { 00076 if (ptr_ != NULL) delete ptr_; 00077 ptr_ = ptr; 00078 } 00079 bool operator==(const T* ptr) const { 00080 return ptr_ == ptr; 00081 } 00082 T* operator->() const { 00083 return ptr_; 00084 } 00085 private: 00086 T* ptr_; 00087 }; 00088 #endif // HAVE_UNIQUE_PTR 00089 00090 #endif // HASHFN_H