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