NSVD Reader  0.0.1
refhash.hpp
Go to the documentation of this file.
1 
5 /*
6  * These codes are licensed under CC0.
7  * http://creativecommons.org/publicdomain/zero/1.0/
8  */
9 
10 #ifndef __NODAMUSHI_REFHASH_HPP__
11 #define __NODAMUSHI_REFHASH_HPP__
12 # include <functional>
13 
14 namespace nodamushi{
15 
16 
20 template<typename T>struct ref
21 {
22  const T* t;
23  ref():t(nullptr){};
24  ref(const T& v):t(&v){};
25  ref(const ref<T>& r)=default;
26  ref(ref<T>&& r)=default;
27 
28  bool operator==(const ref<T>& r)const
29  {
30  if(t == r.t)return true;
31  if(!t || !r.t)return false;
32  return *t == *r.t;
33  }
34  bool operator==(const T& r)const
35  {
36  if(t == &r)return true;
37  if(!t)return false;
38  return *t == r;
39  }
40  bool operator==(T&& r)const
41  {
42  if(t == &r)return true;
43  if(!t)return false;
44  return *t == r;
45  }
46  bool operator==(const T* r)const
47  {
48  if(t == r)return true;
49  if(!t || !r)return false;
50  return *t == *r;
51  }
52  explicit operator bool()const{return t;}
53  const T& operator*()const{return *t;}
54 };
55 
56 template<typename T>
57 ref<T> ref_of(const T& t){return {t};}
58 
59 
63 template<typename T>struct refhash
64 {
65  size_t operator()(const ref<T>& key) const{
66  return key?std::hash<T>()(*key)+1:0;
67  }
68  size_t operator()(const T& key) const{
69  return std::hash<T>()(key)+1;
70  }
71 };
72 
73 
74 }// end namespace nodamushi
75 
76 #endif // __NODAMUSHI_REFHASH_HPP__
bool operator==(const T *r) const
Definition: refhash.hpp:46
size_t operator()(const ref< T > &key) const
Definition: refhash.hpp:65
const T & operator *() const
Definition: refhash.hpp:53
ref< T > ref_of(const T &t)
Definition: refhash.hpp:57
bool operator==(const T &r) const
Definition: refhash.hpp:34
size_t operator()(const T &key) const
Definition: refhash.hpp:68
bool operator==(T &&r) const
Definition: refhash.hpp:40
raw pointer container.(not managed)
Definition: refhash.hpp:20
ref(const T &v)
Definition: refhash.hpp:24
const T * t
Definition: refhash.hpp:22
bool operator==(const ref< T > &r) const
Definition: refhash.hpp:28
hash of ref<T>
Definition: refhash.hpp:63