[dali_2.1.32] 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) 2022 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 #include <cstdint>
21 #include <cstring>
22 #include <string>
23
24 namespace Dali
25 {
26 namespace Scene3D
27 {
28 namespace Loader
29 {
30 /**
31  * @brief Rudimentary hash generator that follows a builder pattern.
32  */
33 class Hash
34 {
35 public:
36   static constexpr uint64_t DEFAULT_SEED = 61081;
37
38   explicit Hash(uint64_t initial = DEFAULT_SEED);
39
40   /**
41    * @brief Applies a boolean to the hash.
42    * @return Its updated self.
43    */
44   Hash& Add(bool b);
45
46   /**
47    * @brief Applies a signed 32-bit integer to the hash.
48    * @return Its updated self.
49    */
50   Hash& Add(int32_t i);
51
52   /**
53    * @brief Applies an unsigned 32-bit integer to the hash.
54    * @return Its updated self.
55    */
56   Hash& Add(uint32_t i);
57
58   /**
59    * @brief Applies an unsigned 64-bit integer to the hash.
60    * @return Its updated self.
61    */
62   Hash& Add(uint64_t i);
63
64   /**
65    * @brief Applies a float to the hash.
66    * @return Its updated self.
67    */
68   Hash& Add(float f);
69
70   /**
71    * @brief Applies a c-string to the hash.
72    * @return Its updated self.
73    */
74   Hash& Add(const char* cStr);
75
76   /**
77    * @brief Applies a c-string @a cStr of @a len characters, to the hash.
78    * @return Its updated self.
79    */
80   Hash& Add(const char* cStr, size_t len);
81
82   /**
83    * @brief Applies a string to the hash.
84    * @return Its updated self.
85    */
86   Hash& Add(const std::string& str);
87
88   /**
89    * @brief Applies a series of bytes between @a i0 and @a i1 to the hash.
90    * @return Its updated self.
91    */
92   Hash& AddBytes(const uint8_t* i0, const uint8_t* i1);
93
94   /**
95    * @brief Applies the bytes of an object @a value, to the hash.
96    * @return Its updated self.
97    */
98   template<typename T>
99   Hash& AddObjectBytes(const T& value);
100
101   operator uint64_t() const;
102
103 private:
104   uint64_t mValue;
105
106   uint64_t Concatenate(uint64_t value);
107 };
108
109 template<typename T>
110 Hash& Hash::AddObjectBytes(const T& value)
111 {
112   auto i0 = reinterpret_cast<const uint8_t*>(&value);
113   auto i1 = i0 + sizeof(T);
114   while(i0 != i1)
115   {
116     mValue = Concatenate(*i0);
117     ++i0;
118   }
119   return *this;
120 }
121
122 } // namespace Loader
123 } // namespace Scene3D
124 } // namespace Dali
125
126 #endif // DALI_SCENE3D_LOADER_HASH_H_