BPHash
General object hashing library for C++
|
BPHash has a few different types of hashes. See the documentation of bphash::HashType for the different types.
The easiest way to obtain the hash of an object would be to use the make_hash() function.
A bphash::HashValue object can be converted to its string representation with the hash_to_string() function.
Hashes can also be truncated or converted to a C++ integral type. Truncating to a size that is greater than the current hash size results in a hash that is padded with zero.
If needed, a bphash::Hasher object could be used instead of the make_hash() convenience function. This function uses the same format as when passing data to the make_hash() function (ie, some pointers must be wrapped etc. See later sections).
Enumerations are supported as well
BPHash supports STL containers. To support hashing of a container, include the appropriate header from bphash/types
(which mirrors the include file from the STL).
Arrays and pointers can be hashed also. If the pointer points to only one element, it can simply be hashed as-is. If it points to multiple elements, or is an array type (ie, int[]
) it must be wrapped in a helper structure via the hash_pointer() function.
C-style strings are hashed as strings, with the length determined via strlen
.
BPHash can be extended to custom classes as well. This can be done via a class member function or via a free function.
A hash function can be added to the class to make it hashable. The signature is void hash(bphash::Hasher & h) const
. It must be a const
function. Note that The bphash::Hasher function is declared in the bphash/Hasher_fwd.hpp
header file so that is all that is needed to declare this function. The full bphash/Hasher.hpp
header is needed when defining the function.
The hash function can be made private
if desired. If so, you must use the BPHASH_DECLARE_HASHING_FRIENDS
macro inside the class.
A free function with the form void hash_object(const ObjectType &, bphash::Hasher &)
can also be used instead of the member functions.
It's up to you how to handle inheritence (in particular, how to handle hashing through a base class via virtual functions). One idea would be to have each class have its own hashing function similar to the hash()
member function but with a different name (say, hash_()
). Then each class is responsible for hashing its own data and then calling the custom hash_()
function of its base class. Then, either the original hash()
function could call a virtual hash_()
function, or the hash()
function itself is virtual and calls the hash_()
function of its own class, starting the chain.
Hashing ability can be detected at compile time with the bphash::is_hashable
trait class.
BPHash includes an equivalent of std::hash
for use in some containers. This is included in the bphash/StdHash.hpp
header.