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
39 struct Property::Map::Impl
41 StringValueContainer mStringValueContainer;
42 IndexValueContainer mIndexValueContainer;
50 Property::Map::Map( const Property::Map& other )
53 mImpl->mStringValueContainer = other.mImpl->mStringValueContainer;
54 mImpl->mIndexValueContainer = other.mImpl->mIndexValueContainer;
62 Property::Map::SizeType Property::Map::Count() const
64 return mImpl->mStringValueContainer.size() + mImpl->mIndexValueContainer.size();
67 bool Property::Map::Empty() const
69 return mImpl->mStringValueContainer.empty() && mImpl->mIndexValueContainer.empty();
72 void Property::Map::Insert( const char* key, const Value& value )
74 mImpl->mStringValueContainer.push_back( std::make_pair( key, value ) );
77 void Property::Map::Insert( const std::string& key, const Value& value )
79 mImpl->mStringValueContainer.push_back( std::make_pair( key, value ) );
82 void Property::Map::Insert( Property::Index key, const Value& value )
84 mImpl->mIndexValueContainer.push_back( std::make_pair( key, value ) );
87 Property::Value& Property::Map::GetValue( SizeType position ) const
89 DALI_ASSERT_ALWAYS( position < mImpl->mStringValueContainer.size() && "position out-of-bounds" );
91 return mImpl->mStringValueContainer[ position ].second;
94 const std::string& Property::Map::GetKey( SizeType position ) const
96 DALI_LOG_WARNING_NOFN("DEPRECATION WARNING: GetKey() is deprecated and will be removed from next release.\n" );
98 DALI_ASSERT_ALWAYS( position < mImpl->mStringValueContainer.size() && "position out-of-bounds" );
100 return mImpl->mStringValueContainer[ position ].first;
103 StringValuePair& Property::Map::GetPair( SizeType position ) const
105 DALI_LOG_WARNING_NOFN("DEPRECATION WARNING: GetPair() is deprecated and will be removed from next release.\n" );
107 DALI_ASSERT_ALWAYS( position < mImpl->mStringValueContainer.size() && "position out-of-bounds" );
109 return mImpl->mStringValueContainer[ position ];
112 Property::Value* Property::Map::Find( const char* key ) const
114 for ( StringValueContainer::iterator iter = mImpl->mStringValueContainer.begin(), endIter = mImpl->mStringValueContainer.end(); iter != endIter; ++iter )
116 if ( iter->first == key )
118 return &iter->second;
121 return NULL; // Not found
124 Property::Value* Property::Map::Find( const std::string& key ) const
126 return Find( key.c_str() );
129 Property::Value* Property::Map::Find( Property::Index key ) const
131 for ( IndexValueContainer::iterator iter = mImpl->mIndexValueContainer.begin(), endIter = mImpl->mIndexValueContainer.end(); iter != endIter; ++iter )
133 if ( iter->first == key )
135 return &iter->second;
138 return NULL; // Not found
141 Property::Value* Property::Map::Find( Property::Index indexKey, const std::string& stringKey ) const
143 Property::Value* valuePtr = Find( indexKey );
146 valuePtr = Find( stringKey );
151 Property::Value* Property::Map::Find( const std::string& key, Property::Type type ) const
153 for ( StringValueContainer::iterator iter = mImpl->mStringValueContainer.begin(), endIter = mImpl->mStringValueContainer.end(); iter != endIter; ++iter )
155 if( (iter->second.GetType() == type) && (iter->first == key) )
157 return &iter->second;
160 return NULL; // Not found
163 Property::Value* Property::Map::Find( Property::Index key, Property::Type type ) const
165 for ( IndexValueContainer::iterator iter = mImpl->mIndexValueContainer.begin(), endIter = mImpl->mIndexValueContainer.end(); iter != endIter; ++iter )
167 if( (iter->second.GetType() == type) && (iter->first == key) )
169 return &iter->second;
172 return NULL; // Not found
175 void Property::Map::Clear()
177 mImpl->mStringValueContainer.clear();
178 mImpl->mIndexValueContainer.clear();
181 void Property::Map::Merge( const Property::Map& from )
183 // Ensure we're not attempting to merge with ourself
188 for ( StringValueContainer::const_iterator iter = from.mImpl->mStringValueContainer.begin(), endIter = from.mImpl->mStringValueContainer.end(); iter != endIter; ++iter )
190 (*this)[iter->first] = iter->second;
193 for ( IndexValueContainer::const_iterator iter = from.mImpl->mIndexValueContainer.begin(), endIter = from.mImpl->mIndexValueContainer.end(); iter != endIter; ++iter )
195 (*this)[iter->first] = iter->second;
200 // If we're empty, then just copy
206 const Property::Value& Property::Map::operator[]( const std::string& key ) const
208 for ( StringValueContainer::const_iterator iter = mImpl->mStringValueContainer.begin(), endIter = mImpl->mStringValueContainer.end(); iter != endIter; ++iter )
210 if ( iter->first == key )
216 DALI_ASSERT_ALWAYS( ! "Invalid Key" );
219 Property::Value& Property::Map::operator[]( const std::string& key )
221 for ( StringValueContainer::iterator iter = mImpl->mStringValueContainer.begin(), endIter = mImpl->mStringValueContainer.end(); iter != endIter; ++iter )
223 if ( iter->first == key )
229 // Create and return reference to new value
230 mImpl->mStringValueContainer.push_back( std::make_pair( key, Property::Value() ) );
231 return (mImpl->mStringValueContainer.end() - 1)->second;
234 const Property::Value& Property::Map::operator[]( Property::Index key ) const
236 for ( IndexValueContainer::const_iterator iter = mImpl->mIndexValueContainer.begin(), endIter = mImpl->mIndexValueContainer.end(); iter != endIter; ++iter )
238 if ( iter->first == key )
244 DALI_ASSERT_ALWAYS( ! "Invalid Key" );
247 Property::Value& Property::Map::operator[]( Property::Index key )
249 for ( IndexValueContainer::iterator iter = mImpl->mIndexValueContainer.begin(), endIter = mImpl->mIndexValueContainer.end(); iter != endIter; ++iter )
251 if ( iter->first == key )
257 // Create and return reference to new value
258 mImpl->mIndexValueContainer.push_back( std::make_pair( key, Property::Value() ) );
259 return (mImpl->mIndexValueContainer.end() - 1)->second;
262 Property::Map& Property::Map::operator=( const Property::Map& other )
268 mImpl->mStringValueContainer = other.mImpl->mStringValueContainer;
269 mImpl->mIndexValueContainer = other.mImpl->mIndexValueContainer;
274 std::ostream& operator<<( std::ostream& stream, const Property::Map& map )
276 stream << "Map(" << map.Count() << ") = {";
279 // Output the String-Value pairs
280 for ( StringValueContainer::iterator iter = map.mImpl->mStringValueContainer.begin(), endIter = map.mImpl->mStringValueContainer.end(); iter != endIter; ++iter )
286 stream<< iter->first << ":"<<iter->second;
289 // Output the Index-Value pairs
290 for ( IndexValueContainer::iterator iter = map.mImpl->mIndexValueContainer.begin(), endIter = map.mImpl->mIndexValueContainer.end(); iter != endIter; ++iter )
296 stream<< iter->first << ":"<<iter->second;