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