Merge "Clean up the code to build successfully on macOS" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / update / common / uniform-map.cpp
1 /*
2  * Copyright (c) 2018 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 // CLASS HEADER
18 #include <dali/internal/update/common/uniform-map.h>
19
20 namespace Dali
21 {
22 namespace Internal
23 {
24 namespace SceneGraph
25 {
26 UniformMap::UniformMap() = default;
27
28 UniformMap::~UniformMap()
29 {
30   // Nothing to do - let the owner container delete the maps
31 }
32
33 void UniformMap::AddObserver( Observer& observer )
34 {
35   bool foundObserver = false;
36   for( ObserversIter iter = mObservers.Begin(); iter != mObservers.End(); ++iter )
37   {
38     if( *iter == &observer )
39     {
40       foundObserver = true;
41       break;
42     }
43   }
44   if( !foundObserver )
45   {
46     mObservers.PushBack( &observer );
47   }
48 }
49
50 void UniformMap::RemoveObserver( Observer& observer )
51 {
52   for( ObserversIter iter = mObservers.Begin(); iter != mObservers.End(); ++iter )
53   {
54     if( *iter == &observer )
55     {
56       mObservers.Erase(iter);
57       return;
58     }
59   }
60 }
61
62 void UniformMap::MappingChanged()
63 {
64   for( ObserversIter iter = mObservers.Begin(); iter != mObservers.End(); ++iter )
65   {
66     Observer* observer = (*iter);
67     observer->UniformMappingsChanged( *this );
68   }
69 }
70
71 void UniformMap::Add( UniformPropertyMapping* newMap )
72 {
73   UniformPropertyMapping::Hash nameHash = CalculateHash( newMap->uniformName );
74
75   bool found = false;
76
77   for( UniformMapIter iter = mUniformMaps.Begin() ;
78        iter != mUniformMaps.End() ;
79        ++iter )
80   {
81     UniformPropertyMapping* map = *iter;
82     if( map->uniformNameHash == nameHash )
83     {
84       if( map->uniformName == newMap->uniformName )
85       {
86         found = true;
87         // Mapping already exists - update it.
88         map->propertyPtr = newMap->propertyPtr;
89         break;
90       }
91     }
92   }
93
94   if( found == false )
95   {
96     // Take ownership of the new map
97     mUniformMaps.PushBack(newMap);
98   }
99
100   MappingChanged();
101 }
102
103 void UniformMap::Remove( const std::string& uniformName )
104 {
105   UniformPropertyMapping::Hash nameHash = CalculateHash( uniformName );
106
107   bool found=false;
108
109   for( UniformMapIter iter = mUniformMaps.Begin() ;
110        iter != mUniformMaps.End() ;
111        ++iter )
112   {
113     UniformPropertyMapping* map = *iter;
114     if( map->uniformNameHash == nameHash )
115     {
116       if( map->uniformName == uniformName )
117       {
118         mUniformMaps.Erase( iter );
119         found = true;
120         break;
121       }
122     }
123   }
124
125   if( found )
126   {
127     MappingChanged();
128   }
129 }
130
131 const PropertyInputImpl* UniformMap::Find( const std::string& uniformName )
132 {
133   UniformPropertyMapping::Hash nameHash = CalculateHash( uniformName );
134
135   for( UniformMapIter iter = mUniformMaps.Begin() ;
136        iter != mUniformMaps.End() ;
137        ++iter )
138   {
139     UniformPropertyMapping* map = *iter;
140     if( map->uniformNameHash == nameHash )
141     {
142       if( map->uniformName == uniformName )
143       {
144         return map->propertyPtr;
145       }
146     }
147   }
148   return nullptr;
149 }
150
151 UniformMap::SizeType UniformMap::Count() const
152 {
153   return static_cast<UniformMap::SizeType>( mUniformMaps.Count() );
154 }
155
156 const UniformPropertyMapping& UniformMap::operator[]( UniformMap::SizeType index ) const
157 {
158   return *mUniformMaps[index];
159 }
160
161 } // SceneGraph
162 } // Internal
163 } // Dali