DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
HashHelper.h
Go to the documentation of this file.
1
5
6#pragma once
7#include "Common.h"
8#include "System/Object.h"
9#include "System/String.h"
10#include "System/SmartPointer.h"
11
12namespace DotNetDupe {
13 namespace System {
14
20 template <typename T>
21 struct HashHelper {
25 static int GetHashCode(const T& value) {
26 const char* p = reinterpret_cast<const char*>(&value);
27 int hash = 17;
28 for (size_t i = 0; i < sizeof(T); ++i) {
29 hash = hash * 31 + p[i];
30 }
31 return hash;
32 }
33 };
34
35 template <> struct HashHelper<int> { static int GetHashCode(const int& value) { return value; } };
36 template <> struct HashHelper<unsigned int> { static int GetHashCode(const unsigned int& value) { return (int)value; } };
37 template <> struct HashHelper<long long> { static int GetHashCode(const long long& value) { return (int)(value ^ (value >> 32)); } };
38 template <> struct HashHelper<unsigned long long> { static int GetHashCode(const unsigned long long& value) { return (int)(value ^ (value >> 32)); } };
39 template <> struct HashHelper<short> { static int GetHashCode(const short& value) { return value; } };
40 template <> struct HashHelper<unsigned short> { static int GetHashCode(const unsigned short& value) { return value; } };
41 template <> struct HashHelper<char> { static int GetHashCode(const char& value) { return value; } };
42 template <> struct HashHelper<unsigned char> { static int GetHashCode(const unsigned char& value) { return value; } };
43 template <> struct HashHelper<wchar_t> { static int GetHashCode(const wchar_t& value) { return value; } };
44 template <> struct HashHelper<bool> { static int GetHashCode(const bool& value) { return value ? 1 : 0; } };
45
46 template <> struct HashHelper<String> {
47 static int GetHashCode(const String& value) {
48 return value.GetHashCode();
49 }
50 };
51
52 template <typename T>
54 static int GetHashCode(const SmartPointer<T>& value) {
55 if (value.IsNull()) return 0;
56 return value->GetHashCode();
57 }
58 };
59
60 template <typename T>
61 struct HashHelper<T*> {
62 static int GetHashCode(T* const& value) {
63 if (!value) return 0;
64 unsigned long long ptr = reinterpret_cast<unsigned long long>(value);
65 return (int)(ptr ^ (ptr >> 32));
66 }
67 };
68
69 }
70}
Defines common cross-platform macros, export decorators, and fundamental types.
Base object class for DotNetDupe mirroring .NET System.Object.
Provides reference-counted and weak pointer memory management primitives ensuring zero raw ownership.
High-performance UTF-8 / UTF-16 string manipulation class mirroring .NET System.String.
A unified smart pointer that supports both unique and shared ownership semantics.
bool IsNull() const noexcept
Checks if the SmartPointer is null.
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
Definition String.h:74
static int GetHashCode(const SmartPointer< T > &value)
Definition HashHelper.h:54
static int GetHashCode(const String &value)
Definition HashHelper.h:47
static int GetHashCode(T *const &value)
Definition HashHelper.h:62
static int GetHashCode(const bool &value)
Definition HashHelper.h:44
static int GetHashCode(const char &value)
Definition HashHelper.h:41
static int GetHashCode(const int &value)
Definition HashHelper.h:35
static int GetHashCode(const long long &value)
Definition HashHelper.h:37
static int GetHashCode(const short &value)
Definition HashHelper.h:39
static int GetHashCode(const unsigned char &value)
Definition HashHelper.h:42
static int GetHashCode(const unsigned int &value)
Definition HashHelper.h:36
static int GetHashCode(const unsigned long long &value)
Definition HashHelper.h:38
static int GetHashCode(const unsigned short &value)
Definition HashHelper.h:40
static int GetHashCode(const wchar_t &value)
Definition HashHelper.h:43
Primary template for generating 32-bit hash codes from arbitrary types using byte-level folding.
Definition HashHelper.h:21
static int GetHashCode(const T &value)
Computes a 32-bit hash code for the specified value.
Definition HashHelper.h:25