[dali_2.1.31] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / loader / hash.cpp
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 #include "dali-scene3d/internal/loader/hash.h"
18
19 namespace Dali
20 {
21 namespace Scene3D
22 {
23 namespace Loader
24 {
25 Hash::Hash(uint64_t initial)
26 : mValue(initial)
27 {
28 }
29
30 Hash& Hash::Add(bool b)
31 {
32   mValue = Concatenate(b ? 0 : 1);
33   return *this;
34 }
35
36 Hash& Hash::Add(int32_t i)
37 {
38   mValue = Concatenate(i);
39   return *this;
40 }
41
42 Hash& Hash::Add(uint32_t i)
43 {
44   mValue = Concatenate(i);
45   return *this;
46 }
47
48 Hash& Hash::Add(uint64_t i)
49 {
50   mValue = Concatenate(i);
51   return *this;
52 }
53
54 Hash& Hash::Add(float f)
55 {
56   return AddObjectBytes(f);
57 }
58
59 Hash& Hash::Add(const char* cStr)
60 {
61   return Add(cStr, strlen(cStr));
62 }
63
64 Hash& Hash::Add(const char* cStr, size_t len)
65 {
66   auto i0 = reinterpret_cast<const uint8_t*>(cStr);
67   return AddBytes(i0, i0 + len);
68 }
69
70 Hash& Hash::Add(const std::string& str)
71 {
72   auto i0 = reinterpret_cast<const uint8_t*>(str.c_str());
73   return AddBytes(i0, i0 + str.size());
74 }
75
76 Hash& Hash::AddBytes(const uint8_t* i0, const uint8_t* i1)
77 {
78   while(i0 != i1)
79   {
80     mValue = Concatenate(*i0);
81     ++i0;
82   }
83   return *this;
84 }
85
86 Hash::operator uint64_t() const
87 {
88   return mValue;
89 }
90
91 uint64_t Hash::Concatenate(uint64_t value)
92 {
93   return mValue * 31 + value;
94 }
95
96 } // namespace Loader
97 } // namespace Scene3D
98 } // namespace Dali