All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
hashfn.h
Go to the documentation of this file.
1 /**********************************************************************
2  * File: hashfn.h (Formerly hash.h)
3  * Description: Portability hacks for hash_map, hash_set and unique_ptr.
4  * Author: Ray Smith
5  * Created: Wed Jan 08 14:08:25 PST 2014
6  *
7  * (C) Copyright 2014, Google Inc.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  *
18  **********************************************************************/
19 
20 #ifndef HASHFN_H
21 #define HASHFN_H
22 
23 #ifdef USE_STD_NAMESPACE
24 #if (__cplusplus >= 201103L) || defined(_MSC_VER) // Visual Studio
25 #include <unordered_map>
26 #include <unordered_set>
27 #define hash_map std::unordered_map
28 #if (_MSC_VER >= 1500 && _MSC_VER < 1600) // Visual Studio 2008
29 using namespace std::tr1;
30 #else // _MSC_VER
31 using std::unordered_map;
32 using std::unordered_set;
33 #include <memory>
34 #define SmartPtr std::unique_ptr
35 #define HAVE_UNIQUE_PTR
36 #endif // _MSC_VER
37 #elif (defined(__GNUC__) && (((__GNUC__ == 3) && (__GNUC_MINOR__ > 0)) || \
38  __GNUC__ >= 4)) // gcc
39 // hash_set is deprecated in gcc
40 #include <ext/hash_map>
41 #include <ext/hash_set>
42 using __gnu_cxx::hash_map;
43 using __gnu_cxx::hash_set;
44 #define unordered_map hash_map
45 #define unordered_set hash_set
46 #else
47 #include <hash_map>
48 #include <hash_set>
49 #endif // gcc
50 #else // USE_STD_NAMESPACE
51 #include <hash_map>
52 #include <hash_set>
53 #define unordered_map hash_map
54 #define unordered_set hash_set
55 #endif // USE_STD_NAMESPACE
56 
57 #ifndef HAVE_UNIQUE_PTR
58 // Trivial smart ptr. Expand to add features of std::unique_ptr as required.
59 template<class T> class SmartPtr {
60  public:
61  SmartPtr() : ptr_(NULL) {}
62  explicit SmartPtr(T* ptr) : ptr_(ptr) {}
64  delete ptr_;
65  }
66 
67  T* get() const {
68  return ptr_;
69  }
70  void reset(T* ptr) {
71  if (ptr_ != NULL) delete ptr_;
72  ptr_ = ptr;
73  }
74  bool operator==(const T* ptr) const {
75  return ptr_ == ptr;
76  }
77  T* operator->() const {
78  return ptr_;
79  }
80  private:
81  T* ptr_;
82 };
83 #endif // HAVE_UNIQUE_PTR
84 
85 #endif // HASHFN_H
bool operator==(const T *ptr) const
Definition: hashfn.h:74
~SmartPtr()
Definition: hashfn.h:63
#define unordered_set
Definition: hashfn.h:54
void reset(T *ptr)
Definition: hashfn.h:70
#define unordered_map
Definition: hashfn.h:53
T * operator->() const
Definition: hashfn.h:77
#define NULL
Definition: host.h:144
SmartPtr(T *ptr)
Definition: hashfn.h:62
SmartPtr()
Definition: hashfn.h:61