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/public-api/common/vector-wrapper.h>
29 typedef std::vector< StringValuePair > StringValueContainer;
31 typedef std::pair< Property::Index, Property::Value > IndexValuePair;
32 typedef std::vector< IndexValuePair > IndexValueContainer;
34 }; // unnamed namespace
36 struct Property::Map::Impl
38 StringValueContainer mStringValueContainer;
39 IndexValueContainer mIndexValueContainer;
47 Property::Map::Map( const Property::Map& other )
50 mImpl->mStringValueContainer = other.mImpl->mStringValueContainer;
51 mImpl->mIndexValueContainer = other.mImpl->mIndexValueContainer;
59 Property::Map::SizeType Property::Map::Count() const
61 return mImpl->mStringValueContainer.size() + mImpl->mIndexValueContainer.size();
64 bool Property::Map::Empty() const
66 return mImpl->mStringValueContainer.empty() && mImpl->mIndexValueContainer.empty();
69 void Property::Map::Insert( const char* key, const Value& value )
71 mImpl->mStringValueContainer.push_back( std::make_pair( key, value ) );
74 void Property::Map::Insert( const std::string& key, const Value& value )
76 mImpl->mStringValueContainer.push_back( std::make_pair( key, value ) );
79 void Property::Map::Insert( Property::Index key, const Value& value )
81 mImpl->mIndexValueContainer.push_back( std::make_pair( key, value ) );
84 Property::Value& Property::Map::GetValue( SizeType position ) const
86 DALI_ASSERT_ALWAYS( position < mImpl->mStringValueContainer.size() && "position out-of-bounds" );
88 return mImpl->mStringValueContainer[ position ].second;
91 const std::string& Property::Map::GetKey( SizeType position ) const
93 DALI_ASSERT_ALWAYS( position < mImpl->mStringValueContainer.size() && "position out-of-bounds" );
95 return mImpl->mStringValueContainer[ position ].first;
98 StringValuePair& Property::Map::GetPair( SizeType position ) const
100 DALI_ASSERT_ALWAYS( position < mImpl->mStringValueContainer.size() && "position out-of-bounds" );
102 return mImpl->mStringValueContainer[ position ];
105 Property::Value* Property::Map::Find( const char* key ) const
107 for ( StringValueContainer::iterator iter = mImpl->mStringValueContainer.begin(), endIter = mImpl->mStringValueContainer.end(); iter != endIter; ++iter )
109 if ( iter->first == key )
111 return &iter->second;
114 return NULL; // Not found
117 Property::Value* Property::Map::Find( const std::string& key ) const
119 return Find( key.c_str() );
122 Property::Value* Property::Map::Find( Property::Index key ) const
124 for ( IndexValueContainer::iterator iter = mImpl->mIndexValueContainer.begin(), endIter = mImpl->mIndexValueContainer.end(); iter != endIter; ++iter )
126 if ( iter->first == key )
128 return &iter->second;
131 return NULL; // Not found
134 Property::Value* Property::Map::Find( Property::Index indexKey, const std::string& stringKey ) const
136 Property::Value* valuePtr = Find( indexKey );
139 valuePtr = Find( stringKey );
144 Property::Value* Property::Map::Find( const std::string& key, Property::Type type ) const
146 for ( StringValueContainer::iterator iter = mImpl->mStringValueContainer.begin(), endIter = mImpl->mStringValueContainer.end(); iter != endIter; ++iter )
148 if( (iter->second.GetType() == type) && (iter->first == key) )
150 return &iter->second;
153 return NULL; // Not found
156 Property::Value* Property::Map::Find( Property::Index key, Property::Type type ) const
158 for ( IndexValueContainer::iterator iter = mImpl->mIndexValueContainer.begin(), endIter = mImpl->mIndexValueContainer.end(); iter != endIter; ++iter )
160 if( (iter->second.GetType() == type) && (iter->first == key) )
162 return &iter->second;
165 return NULL; // Not found
168 void Property::Map::Clear()
170 mImpl->mStringValueContainer.clear();
171 mImpl->mIndexValueContainer.clear();
174 void Property::Map::Merge( const Property::Map& from )
176 // Ensure we're not attempting to merge with ourself
181 for ( StringValueContainer::const_iterator iter = from.mImpl->mStringValueContainer.begin(), endIter = from.mImpl->mStringValueContainer.end(); iter != endIter; ++iter )
183 (*this)[iter->first] = iter->second;
186 for ( IndexValueContainer::const_iterator iter = from.mImpl->mIndexValueContainer.begin(), endIter = from.mImpl->mIndexValueContainer.end(); iter != endIter; ++iter )
188 (*this)[iter->first] = iter->second;
193 // If we're empty, then just copy
199 const Property::Value& Property::Map::operator[]( const std::string& key ) const
201 for ( StringValueContainer::const_iterator iter = mImpl->mStringValueContainer.begin(), endIter = mImpl->mStringValueContainer.end(); iter != endIter; ++iter )
203 if ( iter->first == key )
209 DALI_ASSERT_ALWAYS( ! "Invalid Key" );
212 Property::Value& Property::Map::operator[]( const std::string& key )
214 for ( StringValueContainer::iterator iter = mImpl->mStringValueContainer.begin(), endIter = mImpl->mStringValueContainer.end(); iter != endIter; ++iter )
216 if ( iter->first == key )
222 // Create and return reference to new value
223 mImpl->mStringValueContainer.push_back( std::make_pair( key, Property::Value() ) );
224 return (mImpl->mStringValueContainer.end() - 1)->second;
227 const Property::Value& Property::Map::operator[]( Property::Index key ) const
229 for ( IndexValueContainer::const_iterator iter = mImpl->mIndexValueContainer.begin(), endIter = mImpl->mIndexValueContainer.end(); iter != endIter; ++iter )
231 if ( iter->first == key )
237 DALI_ASSERT_ALWAYS( ! "Invalid Key" );
240 Property::Value& Property::Map::operator[]( Property::Index key )
242 for ( IndexValueContainer::iterator iter = mImpl->mIndexValueContainer.begin(), endIter = mImpl->mIndexValueContainer.end(); iter != endIter; ++iter )
244 if ( iter->first == key )
250 // Create and return reference to new value
251 mImpl->mIndexValueContainer.push_back( std::make_pair( key, Property::Value() ) );
252 return (mImpl->mIndexValueContainer.end() - 1)->second;
255 Property::Map& Property::Map::operator=( const Property::Map& other )
261 mImpl->mStringValueContainer = other.mImpl->mStringValueContainer;
262 mImpl->mIndexValueContainer = other.mImpl->mIndexValueContainer;
267 std::ostream& operator<<( std::ostream& stream, const Property::Map& map )
269 stream << "Map(" << map.Count() << ") = {";
272 // Output the String-Value pairs
273 for ( StringValueContainer::iterator iter = map.mImpl->mStringValueContainer.begin(), endIter = map.mImpl->mStringValueContainer.end(); iter != endIter; ++iter )
279 stream<< iter->first << ":"<<iter->second;
282 // Output the Index-Value pairs
283 for ( IndexValueContainer::iterator iter = map.mImpl->mIndexValueContainer.begin(), endIter = map.mImpl->mIndexValueContainer.end(); iter != endIter; ++iter )
289 stream<< iter->first << ":"<<iter->second;