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( const std::string& key, Property::Type type ) const
136 for ( StringValueContainer::iterator iter = mImpl->mStringValueContainer.begin(), endIter = mImpl->mStringValueContainer.end(); iter != endIter; ++iter )
138 if( (iter->second.GetType() == type) && (iter->first == key) )
140 return &iter->second;
143 return NULL; // Not found
146 Property::Value* Property::Map::Find( Property::Index key, Property::Type type ) const
148 for ( IndexValueContainer::iterator iter = mImpl->mIndexValueContainer.begin(), endIter = mImpl->mIndexValueContainer.end(); iter != endIter; ++iter )
150 if( (iter->second.GetType() == type) && (iter->first == key) )
152 return &iter->second;
155 return NULL; // Not found
158 void Property::Map::Clear()
160 mImpl->mStringValueContainer.clear();
161 mImpl->mIndexValueContainer.clear();
164 void Property::Map::Merge( const Property::Map& from )
166 // Ensure we're not attempting to merge with ourself
171 for ( StringValueContainer::const_iterator iter = from.mImpl->mStringValueContainer.begin(), endIter = from.mImpl->mStringValueContainer.end(); iter != endIter; ++iter )
173 (*this)[iter->first] = iter->second;
176 for ( IndexValueContainer::const_iterator iter = from.mImpl->mIndexValueContainer.begin(), endIter = from.mImpl->mIndexValueContainer.end(); iter != endIter; ++iter )
178 (*this)[iter->first] = iter->second;
183 // If we're empty, then just copy
189 const Property::Value& Property::Map::operator[]( const std::string& key ) const
191 for ( StringValueContainer::const_iterator iter = mImpl->mStringValueContainer.begin(), endIter = mImpl->mStringValueContainer.end(); iter != endIter; ++iter )
193 if ( iter->first == key )
199 DALI_ASSERT_ALWAYS( ! "Invalid Key" );
202 Property::Value& Property::Map::operator[]( const std::string& key )
204 for ( StringValueContainer::iterator iter = mImpl->mStringValueContainer.begin(), endIter = mImpl->mStringValueContainer.end(); iter != endIter; ++iter )
206 if ( iter->first == key )
212 // Create and return reference to new value
213 mImpl->mStringValueContainer.push_back( std::make_pair( key, Property::Value() ) );
214 return (mImpl->mStringValueContainer.end() - 1)->second;
217 const Property::Value& Property::Map::operator[]( Property::Index key ) const
219 for ( IndexValueContainer::const_iterator iter = mImpl->mIndexValueContainer.begin(), endIter = mImpl->mIndexValueContainer.end(); iter != endIter; ++iter )
221 if ( iter->first == key )
227 DALI_ASSERT_ALWAYS( ! "Invalid Key" );
230 Property::Value& Property::Map::operator[]( Property::Index key )
232 for ( IndexValueContainer::iterator iter = mImpl->mIndexValueContainer.begin(), endIter = mImpl->mIndexValueContainer.end(); iter != endIter; ++iter )
234 if ( iter->first == key )
240 // Create and return reference to new value
241 mImpl->mIndexValueContainer.push_back( std::make_pair( key, Property::Value() ) );
242 return (mImpl->mIndexValueContainer.end() - 1)->second;
245 Property::Map& Property::Map::operator=( const Property::Map& other )
251 mImpl->mStringValueContainer = other.mImpl->mStringValueContainer;
252 mImpl->mIndexValueContainer = other.mImpl->mIndexValueContainer;
257 std::ostream& operator<<( std::ostream& stream, const Property::Map& map )
259 stream << "Map(" << map.Count() << ") = {";
262 // Output the String-Value pairs
263 for ( StringValueContainer::iterator iter = map.mImpl->mStringValueContainer.begin(), endIter = map.mImpl->mStringValueContainer.end(); iter != endIter; ++iter )
269 stream<< iter->first << ":"<<iter->second;
272 // Output the Index-Value pairs
273 for ( IndexValueContainer::iterator iter = map.mImpl->mIndexValueContainer.begin(), endIter = map.mImpl->mIndexValueContainer.end(); iter != endIter; ++iter )
279 stream<< iter->first << ":"<<iter->second;