BPHash
General object hashing library for C++
memory.hpp
Go to the documentation of this file.
1 /*! \file
2  * \brief Hashing of std::unique_ptr and std::shared_ptr
3  */
4 
5 /* Copyright (c) 2016 Benjamin Pritchard <ben@bennyp.org>
6  * This file is part of the BPHash project, which is released
7  * under the BSD 3-clause license. See the LICENSE file for details
8  */
9 
10 #pragma once
11 
12 #include "bphash/Hasher.hpp"
13 #include <memory>
14 
15 
16 namespace bphash {
17 
18 /*! \brief Create a wrapper around a unique_ptr
19  *
20  * \param [in] ptr The shared pointer to wrap
21  * \param [in] len The number of elements that \p ptr points to
22  * \return The wrapped pointer that is ready for hashing
23  */
24 template<typename T, typename Deleter>
25 PointerWrapper<T> hash_pointer(const std::unique_ptr<T, Deleter> & ptr, size_t len = 1)
26 {
27  return PointerWrapper<T> {ptr.get(), len};
28 }
29 
30 /*! \brief Create a wrapper around a unique_ptr
31  *
32  * \note Since this unique_ptr uses the array syntax, you must specify the length
33  *
34  * \param [in] ptr The shared pointer to wrap
35  * \param [in] len The number of elements that \p ptr points to
36  * \return The wrapped pointer that is ready for hashing
37  */
38 template<typename T, typename Deleter>
39 PointerWrapper<T> hash_pointer(const std::unique_ptr<T[], Deleter> & ptr, size_t len)
40 {
41  return PointerWrapper<T> {ptr.get(), len};
42 }
43 
44 /*! \brief Create a wrapper around a shared_ptr
45  *
46  * \param [in] ptr The shared pointer to wrap
47  * \param [in] len The number of elements that \p ptr points to
48  * \return The wrapped pointer that is ready for hashing
49  */
50 template<typename T>
51 PointerWrapper<T> hash_pointer(const std::shared_ptr<T> & ptr, size_t len = 1)
52 {
53  return PointerWrapper<T> {ptr.get(), len};
54 }
55 
56 
57 /*
58 template<typename T>
59 PointerWrapper<T> hash_pointer(const std::shared_ptr<T[]> & ptr, size_t len)
60 {
61  return PointerWrapper<T>{typeid(std::shared_ptr<T[]>).name(),
62  ptr.get(), len};
63 }
64 */
65 
66 
67 
68 /*! \brief Hashing of std::unique_ptr
69  *
70  * It is assumed that the pointer points to a single element.
71  * If not, you must wrap the pointer with hash_pointer.
72  */
73 template<typename T, typename Deleter>
74 typename std::enable_if<is_hashable<T>::value, void>::type
75 hash_object(const std::unique_ptr<T, Deleter> & p, Hasher & h)
76 {
77  h(hash_pointer(p));
78 }
79 
80 
81 /*! \brief Hashing of std::shared_ptr
82  *
83  * It is assumed that the pointer points to a single element.
84  * If not, you must wrap the pointer with hash_pointer.
85  */
86 template<typename T>
87 typename std::enable_if<is_hashable<T>::value, void>::type
88 hash_object(const std::shared_ptr<T> & p, Hasher & h)
89 {
90  h(hash_pointer(p));
91 }
92 
93 
94 } // close namespace bphash
95 
std::enable_if< is_hashable< T >::value, void >::type hash_object(const std::array< T, N > &a, Hasher &h)
Hashing of std::array.
Definition: array.hpp:20
A class that hashes objects (header)
Wrapper for pointers and arrays.
Definition: Hasher.hpp:49
Class that is used to hash objects.
Definition: Hasher.hpp:78
PointerWrapper< T > hash_pointer(const T *ptr, size_t len=1)
Wrap a raw pointer so that it can be hashed.
Definition: Hasher.hpp:67