2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/public-api/object/property-map.h>
22 #include <dali/integration-api/debug.h>
25 #include <dali/public-api/common/vector-wrapper.h>
32 typedef std::vector< StringValuePair > StringValueContainer;
34 typedef std::pair< Property::Index, Property::Value > IndexValuePair;
35 typedef std::vector< IndexValuePair > IndexValueContainer;
37 }; // unnamed namespace
40 struct Property::Map::Impl
42 StringValueContainer mStringValueContainer;
43 IndexValueContainer mIndexValueContainer;
51 Property::Map::Map( const Property::Map& other )
54 mImpl->mStringValueContainer = other.mImpl->mStringValueContainer;
55 mImpl->mIndexValueContainer = other.mImpl->mIndexValueContainer;
63 Property::Map::SizeType Property::Map::Count() const
65 return mImpl->mStringValueContainer.size() + mImpl->mIndexValueContainer.size();
68 bool Property::Map::Empty() const
70 return mImpl->mStringValueContainer.empty() && mImpl->mIndexValueContainer.empty();
73 void Property::Map::Insert( const char* key, const Value& value )
75 mImpl->mStringValueContainer.push_back( std::make_pair( key, value ) );
78 void Property::Map::Insert( const std::string& key, const Value& value )
80 mImpl->mStringValueContainer.push_back( std::make_pair( key, value ) );
83 void Property::Map::Insert( Property::Index key, const Value& value )
85 mImpl->mIndexValueContainer.push_back( std::make_pair( key, value ) );
88 Property::Value& Property::Map::GetValue( SizeType position ) const
90 SizeType numStringKeys = mImpl->mStringValueContainer.size();
91 SizeType numIndexKeys = mImpl->mIndexValueContainer.size();
92 DALI_ASSERT_ALWAYS( position < ( numStringKeys + numIndexKeys ) && "position out-of-bounds" );
94 if( position < numStringKeys )
96 return mImpl->mStringValueContainer[ position ].second;
100 return mImpl->mIndexValueContainer[ position-numStringKeys ].second;
104 const std::string& Property::Map::GetKey( SizeType position ) const
106 DALI_LOG_WARNING_NOFN("DEPRECATION WARNING: GetKey() is deprecated and will be removed from next release.\n" );
108 SizeType numStringKeys = mImpl->mStringValueContainer.size();
109 DALI_ASSERT_ALWAYS( position < numStringKeys && "position out-of-bounds" );
111 return mImpl->mStringValueContainer[ position ].first;
114 Property::Key Property::Map::GetKeyAt( SizeType position ) const
116 SizeType numStringKeys = mImpl->mStringValueContainer.size();
117 SizeType numIndexKeys = mImpl->mIndexValueContainer.size();
118 DALI_ASSERT_ALWAYS( position < ( numStringKeys + numIndexKeys ) && "position out-of-bounds" );
120 if( position < numStringKeys )
122 Key key(mImpl->mStringValueContainer[ position ].first);
127 Key key( mImpl->mIndexValueContainer[ position-numStringKeys ].first );
132 StringValuePair& Property::Map::GetPair( SizeType position ) const
134 DALI_LOG_WARNING_NOFN("DEPRECATION WARNING: GetPair() is deprecated and will be removed from next release.\n" );
136 SizeType numStringKeys = mImpl->mStringValueContainer.size();
138 DALI_ASSERT_ALWAYS( position < ( numStringKeys ) && "position out-of-bounds" );
140 return mImpl->mStringValueContainer[ position ];
143 KeyValuePair Property::Map::GetKeyValue( SizeType position ) const
145 SizeType numStringKeys = mImpl->mStringValueContainer.size();
146 SizeType numIndexKeys = mImpl->mIndexValueContainer.size();
148 DALI_ASSERT_ALWAYS( position < ( numStringKeys + numIndexKeys ) && "position out-of-bounds" );
150 if( position < numStringKeys )
152 Key key(mImpl->mStringValueContainer[ position ].first);
153 KeyValuePair keyValue(key, mImpl->mStringValueContainer[ position ].second );
158 Key key( mImpl->mIndexValueContainer[ position-numStringKeys ].first );
159 KeyValuePair keyValue(key, mImpl->mIndexValueContainer[ position-numStringKeys ].second );
164 Property::Value* Property::Map::Find( const char* key ) const
166 for ( StringValueContainer::iterator iter = mImpl->mStringValueContainer.begin(), endIter = mImpl->mStringValueContainer.end(); iter != endIter; ++iter )
168 if ( iter->first == key )
170 return &iter->second;
173 return NULL; // Not found
176 Property::Value* Property::Map::Find( const std::string& key ) const
178 return Find( key.c_str() );
181 Property::Value* Property::Map::Find( Property::Index key ) const
183 for ( IndexValueContainer::iterator iter = mImpl->mIndexValueContainer.begin(), endIter = mImpl->mIndexValueContainer.end(); iter != endIter; ++iter )
185 if ( iter->first == key )
187 return &iter->second;
190 return NULL; // Not found
193 Property::Value* Property::Map::Find( Property::Index indexKey, const std::string& stringKey ) const
195 Property::Value* valuePtr = Find( indexKey );
198 valuePtr = Find( stringKey );
203 Property::Value* Property::Map::Find( const std::string& key, Property::Type type ) const
205 for ( StringValueContainer::iterator iter = mImpl->mStringValueContainer.begin(), endIter = mImpl->mStringValueContainer.end(); iter != endIter; ++iter )
207 if( (iter->second.GetType() == type) && (iter->first == key) )
209 return &iter->second;
212 return NULL; // Not found
215 Property::Value* Property::Map::Find( Property::Index key, Property::Type type ) const
217 for ( IndexValueContainer::iterator iter = mImpl->mIndexValueContainer.begin(), endIter = mImpl->mIndexValueContainer.end(); iter != endIter; ++iter )
219 if( (iter->second.GetType() == type) && (iter->first == key) )
221 return &iter->second;
224 return NULL; // Not found
227 void Property::Map::Clear()
229 mImpl->mStringValueContainer.clear();
230 mImpl->mIndexValueContainer.clear();
233 void Property::Map::Merge( const Property::Map& from )
235 // Ensure we're not attempting to merge with ourself
240 for ( StringValueContainer::const_iterator iter = from.mImpl->mStringValueContainer.begin(), endIter = from.mImpl->mStringValueContainer.end(); iter != endIter; ++iter )
242 (*this)[iter->first] = iter->second;
245 for ( IndexValueContainer::const_iterator iter = from.mImpl->mIndexValueContainer.begin(), endIter = from.mImpl->mIndexValueContainer.end(); iter != endIter; ++iter )
247 (*this)[iter->first] = iter->second;
252 // If we're empty, then just copy
258 const Property::Value& Property::Map::operator[]( const std::string& key ) const
260 for ( StringValueContainer::const_iterator iter = mImpl->mStringValueContainer.begin(), endIter = mImpl->mStringValueContainer.end(); iter != endIter; ++iter )
262 if ( iter->first == key )
268 DALI_ASSERT_ALWAYS( ! "Invalid Key" );
271 Property::Value& Property::Map::operator[]( const std::string& key )
273 for ( StringValueContainer::iterator iter = mImpl->mStringValueContainer.begin(), endIter = mImpl->mStringValueContainer.end(); iter != endIter; ++iter )
275 if ( iter->first == key )
281 // Create and return reference to new value
282 mImpl->mStringValueContainer.push_back( std::make_pair( key, Property::Value() ) );
283 return (mImpl->mStringValueContainer.end() - 1)->second;
286 const Property::Value& Property::Map::operator[]( Property::Index key ) const
288 for ( IndexValueContainer::const_iterator iter = mImpl->mIndexValueContainer.begin(), endIter = mImpl->mIndexValueContainer.end(); iter != endIter; ++iter )
290 if ( iter->first == key )
296 DALI_ASSERT_ALWAYS( ! "Invalid Key" );
299 Property::Value& Property::Map::operator[]( Property::Index key )
301 for ( IndexValueContainer::iterator iter = mImpl->mIndexValueContainer.begin(), endIter = mImpl->mIndexValueContainer.end(); iter != endIter; ++iter )
303 if ( iter->first == key )
309 // Create and return reference to new value
310 mImpl->mIndexValueContainer.push_back( std::make_pair( key, Property::Value() ) );
311 return (mImpl->mIndexValueContainer.end() - 1)->second;
314 Property::Map& Property::Map::operator=( const Property::Map& other )
320 mImpl->mStringValueContainer = other.mImpl->mStringValueContainer;
321 mImpl->mIndexValueContainer = other.mImpl->mIndexValueContainer;
326 std::ostream& operator<<( std::ostream& stream, const Property::Map& map )
328 stream << "Map(" << map.Count() << ") = {";
331 // Output the String-Value pairs
332 for ( StringValueContainer::iterator iter = map.mImpl->mStringValueContainer.begin(), endIter = map.mImpl->mStringValueContainer.end(); iter != endIter; ++iter )
338 stream<< iter->first << ":"<<iter->second;
341 // Output the Index-Value pairs
342 for ( IndexValueContainer::iterator iter = map.mImpl->mIndexValueContainer.begin(), endIter = map.mImpl->mIndexValueContainer.end(); iter != endIter; ++iter )
348 stream<< iter->first << ":"<<iter->second;