GCC Code Coverage Report


Directory: src/gate/
File: src/gate/hashes.hpp
Date: 2025-12-12 23:40:09
Exec Total Coverage
Lines: 12 13 92.3%
Functions: 56 56 100.0%
Branches: 1 2 50.0%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> |
4 | All rights reserved. |
5 | |
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met:|
8 | |
9 | 1. Redistributions of source code must retain the above copyright notice, |
10 | this list of conditions and the following disclaimer. |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
25 | THE POSSIBILITY OF SUCH DAMAGE. |
26 +----------------------------------------------------------------------------+
27 */
28
29 /** @file
30 * @brief Defines a standard to generate hashcodes for generic and typed data
31 * @ingroup gatecore_cpp
32 */
33
34 #ifndef GATE_HASHES_HPP_INCLUDED
35 #define GATE_HASHES_HPP_INCLUDED
36
37 #include "gate/gate_core_api.hpp"
38 #include "gate/gatetypes.hpp"
39 #include "gate/typeids.hpp"
40 #include "gate/exceptions.hpp"
41 #include "gate/strings.hpp"
42 #include "gate/hashes.h"
43
44 namespace gate
45 {
46
47 /// @brief Generates a hash code from all added data blocks
48 class GATE_CORE_CPP_API HashGenerator : private NonCopyable
49 {
50 public:
51 HashGenerator();
52 ~HashGenerator() noexcept;
53
54 /// @brief Adds a data block to hash generator
55 /// @param[in] data Pointer to data block
56 /// @param[in] datalength Length @ref data in bytes
57 void update(void const* data, size_t datalength) noexcept;
58
59
60 /// @brief Completes hash generation and returns result
61 /// @return Hash code of all data blocks previously added
62 hash_code_t finish() noexcept;
63
64 private:
65 gate_hash_generator_context_t impl;
66 };
67
68 /// @brief Returns a generic hash code generator function for a specific data type and size
69 /// @tparam T Target data type to generate hash code for
70 template<class T>
71 struct TypeHashGenerator
72 {
73
74 /// @brief Return hash generator function
75 /// @return Function pointer to generator function that takes a pointer to the
76 /// specified type instance and returns an appropriate hash code
77 28 static gate_type_hash_generator_t get()
78 {
79 28 return gate_hash_generator_of(TypeId<T>::value());
80 }
81 };
82
83
84 /// @brief Type-based hash generator
85 /// @tparam T Target type to calculate hashes for
86 template<class T>
87 class TypeHash
88 {
89 public:
90 typedef TypeHash<T> self_t;
91
92 28 static gate_type_hash_generator_t c_generator()
93 {
94 28 return TypeHashGenerator<T>::get();
95 }
96
97 static hash_code_t generate(T const& instance)
98 {
99 gate_type_hash_generator_t generator = self_t::c_generator();
100 return generator(&instance);
101 }
102
103 private:
104 gate_type_hash_generator_t impl;
105
106 public:
107 28 TypeHash()
108 28 : impl(self_t::c_generator())
109 {
110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
28 if (NULL == this->impl)
111 {
112 GATEXX_RAISE_ERROR(results::NotImplemented);
113 }
114 28 }
115 TypeHash(self_t const& src) noexcept
116 : impl(src.impl)
117 {
118 }
119 self_t& operator=(self_t const& src) noexcept
120 {
121 this->impl = src.impl;
122 return *this;
123 }
124 28 ~TypeHash() noexcept
125 {
126 28 }
127
128 14 hash_code_t operator()(T const& instance) noexcept
129 {
130 14 return this->impl(&instance);
131 }
132
133 };
134
135
136 /// @brief
137 template<> struct TypeHashGenerator<String>
138 {
139 static gate_hash_code_t string_hash_generator(void const* datatype)
140 {
141 String const* ptr_str = static_cast<String const*>(datatype);
142 return gate_hash_generate_string(ptr_str->c_impl());
143 }
144
145 static gate_type_hash_generator_t get()
146 {
147 return &string_hash_generator;
148 }
149 };
150
151 } // end of namespace gate
152
153 #endif
154