[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / loader / hash.h
1 #ifndef DALI_SCENE3D_LOADER_HASH_H_
2 #define DALI_SCENE3D_LOADER_HASH_H_
3 /*
4  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 // EXTERNAL INCLUDES
21 #include <cstdint>
22 #include <cstring>
23 #include <string>
24
25 namespace Dali::Scene3D::Loader
26 {
27 /**
28  * @brief Rudimentary hash generator that follows a builder pattern.
29  */
30 class Hash
31 {
32 public:
33   static constexpr uint64_t DEFAULT_SEED = 61081;
34
35   explicit Hash(uint64_t initial = DEFAULT_SEED);
36
37   /**
38    * @brief Applies a boolean to the hash.
39    * @return Its updated self.
40    */
41   Hash& Add(bool b);
42
43   /**
44    * @brief Applies a signed 32-bit integer to the hash.
45    * @return Its updated self.
46    */
47   Hash& Add(int32_t i);
48
49   /**
50    * @brief Applies an unsigned 32-bit integer to the hash.
51    * @return Its updated self.
52    */
53   Hash& Add(uint32_t i);
54
55   /**
56    * @brief Applies an unsigned 64-bit integer to the hash.
57    * @return Its updated self.
58    */
59   Hash& Add(uint64_t i);
60
61   /**
62    * @brief Applies a float to the hash.
63    * @return Its updated self.
64    */
65   Hash& Add(float f);
66
67   /**
68    * @brief Applies a c-string to the hash.
69    * @return Its updated self.
70    */
71   Hash& Add(const char* cStr);
72
73   /**
74    * @brief Applies a c-string @a cStr of @a len characters, to the hash.
75    * @return Its updated self.
76    */
77   Hash& Add(const char* cStr, size_t len);
78
79   /**
80    * @brief Applies a string to the hash.
81    * @return Its updated self.
82    */
83   Hash& Add(const std::string& str);
84
85   /**
86    * @brief Applies a series of bytes between @a i0 and @a i1 to the hash.
87    * @return Its updated self.
88    */
89   Hash& AddBytes(const uint8_t* i0, const uint8_t* i1);
90
91   /**
92    * @brief Applies the bytes of an object @a value, to the hash.
93    * @return Its updated self.
94    */
95   template<typename T>
96   Hash& AddObjectBytes(const T& value);
97
98   operator uint64_t() const;
99
100 private:
101   uint64_t mValue;
102
103   uint64_t Concatenate(uint64_t value);
104 };
105
106 template<typename T>
107 Hash& Hash::AddObjectBytes(const T& value)
108 {
109   auto i0 = reinterpret_cast<const uint8_t*>(&value);
110   auto i1 = i0 + sizeof(T);
111   while(i0 != i1)
112   {
113     mValue = Concatenate(*i0);
114     ++i0;
115   }
116   return *this;
117 }
118
119 } // namespace Dali::Scene3D::Loader
120
121 #endif // DALI_SCENE3D_LOADER_HASH_H_