Renamed ConnectionObservers class, un-consted objects
[platform/core/uifw/dali-core.git] / dali / internal / update / common / uniform-map.cpp
1 /*
2  * Copyright (c) 2015 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 "uniform-map.h"
18
19 namespace Dali
20 {
21 namespace Internal
22 {
23 namespace SceneGraph
24 {
25
26 UniformMap::UniformMap()
27 {
28 }
29
30 UniformMap::~UniformMap()
31 {
32   // Nothing to do - let the owner container delete the maps
33 }
34
35 void UniformMap::AddObserver( Observer& observer )
36 {
37   bool foundObserver = false;
38   for( ObserversIter iter = mObservers.Begin(); iter != mObservers.End(); ++iter )
39   {
40     if( *iter == &observer )
41     {
42       foundObserver = true;
43       break;
44     }
45   }
46   if( !foundObserver )
47   {
48     mObservers.PushBack( &observer );
49   }
50 }
51
52 void UniformMap::RemoveObserver( Observer& observer )
53 {
54   for( ObserversIter iter = mObservers.Begin(); iter != mObservers.End(); ++iter )
55   {
56     if( *iter == &observer )
57     {
58       mObservers.Erase(iter);
59       return;
60     }
61   }
62 }
63
64 void UniformMap::MappingChanged()
65 {
66   for( ObserversIter iter = mObservers.Begin(); iter != mObservers.End(); ++iter )
67   {
68     Observer* observer = (*iter);
69     observer->UniformMappingsChanged( *this );
70   }
71 }
72
73 // @todo MESH_REWORK Benchmark and test
74 // The uniform map can never grow beyond the limits of GL - so really, the
75 // map size is likely to be small; if retaining an unsorted map proves to be
76 // slow, then it should be changed to perform an insertion sort.
77 void UniformMap::Add( UniformPropertyMapping* newMap )
78 {
79   UniformPropertyMapping::Hash nameHash = CalculateHash( newMap->uniformName );
80
81   bool found = false;
82
83   for( UniformMapIter iter = mUniformMaps.Begin() ;
84        iter != mUniformMaps.End() ;
85        ++iter )
86   {
87     UniformPropertyMapping* map = *iter;
88     if( map->uniformNameHash == nameHash )
89     {
90       if( map->uniformName == newMap->uniformName )
91       {
92         found = true;
93         // Mapping already exists - update it.
94         map->propertyPtr = newMap->propertyPtr;
95         break;
96       }
97     }
98   }
99
100   if( found == false )
101   {
102     // Take ownership of the new map
103     mUniformMaps.PushBack(newMap);
104   }
105
106   MappingChanged();
107 }
108
109 void UniformMap::Remove( const std::string& uniformName )
110 {
111   UniformPropertyMapping::Hash nameHash = CalculateHash( uniformName );
112
113   bool found=false;
114
115   for( UniformMapIter iter = mUniformMaps.Begin() ;
116        iter != mUniformMaps.End() ;
117        ++iter )
118   {
119     UniformPropertyMapping* map = *iter;
120     if( map->uniformNameHash == nameHash )
121     {
122       if( map->uniformName == uniformName )
123       {
124         mUniformMaps.Erase( iter );
125         found = true;
126         break;
127       }
128     }
129   }
130
131   if( found )
132   {
133     MappingChanged();
134   }
135 }
136
137 const PropertyInputImpl* UniformMap::Find( const std::string& uniformName )
138 {
139   UniformPropertyMapping::Hash nameHash = CalculateHash( uniformName );
140
141   for( UniformMapIter iter = mUniformMaps.Begin() ;
142        iter != mUniformMaps.End() ;
143        ++iter )
144   {
145     UniformPropertyMapping* map = *iter;
146     if( map->uniformNameHash == nameHash )
147     {
148       if( map->uniformName == uniformName )
149       {
150         return map->propertyPtr;
151       }
152     }
153   }
154   return NULL;
155 }
156
157 unsigned int UniformMap::Count() const
158 {
159   return mUniformMaps.Count();
160 }
161
162 const UniformPropertyMapping& UniformMap::operator[]( unsigned int index ) const
163 {
164   return *mUniformMaps[index];
165 }
166
167 } // SceneGraph
168 } // Internal
169 } // Dali