[dali_2.0.7] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-scene-loader / internal / hash.cpp
1 /*
2  * Copyright (c) 2020 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-scene-loader/internal/hash.h"
18
19 namespace Dali
20 {
21 namespace SceneLoader
22 {
23
24 Hash::Hash(uint64_t initial)
25 : mValue(initial)
26 {}
27
28 Hash& Hash::Add(bool b)
29 {
30   mValue = Concatenate(b ? 0 : 1);
31   return *this;
32 }
33
34 Hash& Hash::Add(int32_t i)
35 {
36   mValue = Concatenate(i);
37   return *this;
38 }
39
40 Hash& Hash::Add(uint32_t i)
41 {
42   mValue = Concatenate(i);
43   return *this;
44 }
45
46 Hash& Hash::Add(uint64_t i)
47 {
48   mValue = Concatenate(i);
49   return *this;
50 }
51
52 Hash& Hash::Add(float f)
53 {
54   return AddObjectBytes(f);
55 }
56
57 Hash& Hash::Add(const char * cStr)
58 {
59   return Add(cStr, strlen(cStr));
60 }
61
62 Hash& Hash::Add(const char * cStr, size_t len)
63 {
64   auto i0 = reinterpret_cast<const uint8_t*>(cStr);
65   return AddBytes(i0, i0 + len);
66 }
67
68 Hash& Hash::Add(const std::string & str)
69 {
70   auto i0 = reinterpret_cast<const uint8_t*>(str.c_str());
71   return AddBytes(i0, i0 + str.size());
72 }
73
74 Hash& Hash::AddBytes(const uint8_t * i0, const uint8_t * i1)
75 {
76   while (i0 != i1)
77   {
78     mValue = Concatenate(*i0);
79     ++i0;
80   }
81   return *this;
82 }
83
84 Hash::operator uint64_t() const
85 {
86   return mValue;
87 }
88
89 uint64_t Hash::Concatenate(uint64_t value)
90 {
91   return mValue * 31 + value;
92 }
93
94 }
95 }