| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* GATE PROJECT LICENSE: | ||
| 2 | +----------------------------------------------------------------------------+ | ||
| 3 | | Copyright (c) 2018-2026, 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 | 54 | static gate_type_hash_generator_t get() | |
| 78 | { | ||
| 79 | 54 | return gate_hash_generator_of(TypeId<T>::value()); | |
| 80 | } | ||
| 81 | }; | ||
| 82 | |||
| 83 | template<> | ||
| 84 | struct TypeHashGenerator<uint8_t> | ||
| 85 | { | ||
| 86 | 1 | static gate_type_hash_generator_t get() | |
| 87 | { | ||
| 88 | 1 | return gate_hash_generator_of(GATE_TYPE_UI8); | |
| 89 | } | ||
| 90 | }; | ||
| 91 | |||
| 92 | template<> | ||
| 93 | struct TypeHashGenerator<bool_t> | ||
| 94 | { | ||
| 95 | 1 | static gate_type_hash_generator_t get() | |
| 96 | { | ||
| 97 | 1 | return gate_hash_generator_of(GATE_TYPE_BOOL); | |
| 98 | } | ||
| 99 | }; | ||
| 100 | |||
| 101 | /// @brief Type-based hash generator | ||
| 102 | /// @tparam T Target type to calculate hashes for | ||
| 103 | template<class T> | ||
| 104 | class TypeHash | ||
| 105 | { | ||
| 106 | public: | ||
| 107 | typedef TypeHash<T> self_t; | ||
| 108 | |||
| 109 | 60 | static gate_type_hash_generator_t c_generator() | |
| 110 | { | ||
| 111 | 60 | return TypeHashGenerator<T>::get(); | |
| 112 | } | ||
| 113 | |||
| 114 | static hash_code_t generate(T const& instance) | ||
| 115 | { | ||
| 116 | gate_type_hash_generator_t generator = self_t::c_generator(); | ||
| 117 | return generator(&instance); | ||
| 118 | } | ||
| 119 | |||
| 120 | private: | ||
| 121 | gate_type_hash_generator_t impl; | ||
| 122 | |||
| 123 | public: | ||
| 124 | 60 | TypeHash() | |
| 125 | 60 | : impl(self_t::c_generator()) | |
| 126 | { | ||
| 127 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
60 | if (NULL == this->impl) |
| 128 | { | ||
| 129 | ✗ | GATEXX_RAISE_ERROR(results::NotImplemented); | |
| 130 | } | ||
| 131 | 60 | } | |
| 132 | TypeHash(self_t const& src) noexcept | ||
| 133 | : impl(src.impl) | ||
| 134 | { | ||
| 135 | } | ||
| 136 | self_t& operator=(self_t const& src) noexcept | ||
| 137 | { | ||
| 138 | this->impl = src.impl; | ||
| 139 | return *this; | ||
| 140 | } | ||
| 141 | 60 | ~TypeHash() noexcept | |
| 142 | { | ||
| 143 | 60 | } | |
| 144 | |||
| 145 | 60 | hash_code_t operator()(T const& instance) noexcept | |
| 146 | { | ||
| 147 | 60 | return this->impl(&instance); | |
| 148 | } | ||
| 149 | |||
| 150 | }; | ||
| 151 | |||
| 152 | |||
| 153 | /// @brief | ||
| 154 | template<> struct TypeHashGenerator<String> | ||
| 155 | { | ||
| 156 | 1 | static gate_hash_code_t string_hash_generator(void const* datatype) | |
| 157 | { | ||
| 158 | 1 | String const* ptr_str = static_cast<String const*>(datatype); | |
| 159 | 1 | return gate_hash_generate_string(ptr_str->c_impl()); | |
| 160 | } | ||
| 161 | |||
| 162 | 1 | static gate_type_hash_generator_t get() | |
| 163 | { | ||
| 164 | 1 | return &string_hash_generator; | |
| 165 | } | ||
| 166 | }; | ||
| 167 | |||
| 168 | } // end of namespace gate | ||
| 169 | |||
| 170 | #endif | ||
| 171 |