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 > Container;
30 }; // unnamed namespace
32 struct Property::Map::Impl
42 Property::Map::Map( const Property::Map& other )
45 mImpl->mContainer = other.mImpl->mContainer;
53 Property::Map::SizeType Property::Map::Count() const
55 return mImpl->mContainer.size();
58 bool Property::Map::Empty() const
60 return mImpl->mContainer.empty();
63 void Property::Map::Insert( const char* key, const Value& value )
65 mImpl->mContainer.push_back( std::make_pair( key, value ) );
68 void Property::Map::Insert( const std::string& key, const Value& value )
70 mImpl->mContainer.push_back( std::make_pair( key, value ) );
73 Property::Value& Property::Map::GetValue( SizeType position ) const
75 DALI_ASSERT_ALWAYS( position < Count() && "position out-of-bounds" );
77 return mImpl->mContainer[ position ].second;
80 const std::string& Property::Map::GetKey( SizeType position ) const
82 DALI_ASSERT_ALWAYS( position < Count() && "position out-of-bounds" );
84 return mImpl->mContainer[ position ].first;
87 StringValuePair& Property::Map::GetPair( SizeType position ) const
89 DALI_ASSERT_ALWAYS( position < Count() && "position out-of-bounds" );
91 return mImpl->mContainer[ position ];
94 Property::Value* Property::Map::Find( const char* key ) const
96 for ( Container::iterator iter = mImpl->mContainer.begin(), endIter = mImpl->mContainer.end(); iter != endIter; ++iter )
98 if ( iter->first == key )
100 return &iter->second;
103 return NULL; // Not found
106 Property::Value* Property::Map::Find( const std::string& key ) const
108 return Find( key.c_str() );
112 Property::Value* Property::Map::Find( const std::string& key, Property::Type type ) const
114 for ( Container::iterator iter = mImpl->mContainer.begin(), endIter = mImpl->mContainer.end(); iter != endIter; ++iter )
116 // test type first to shortcut eval (possibly reducing string compares)
117 if( (iter->second.GetType() == type) && (iter->first == key) )
119 return &iter->second;
122 return NULL; // Not found
125 void Property::Map::Clear()
127 mImpl->mContainer.clear();
130 void Property::Map::Merge( const Property::Map& from )
132 // Ensure we're not attempting to merge with ourself
137 for ( unsigned int i = 0, count = from.Count(); i < count; ++i )
139 StringValuePair& pair( from.GetPair( i ) );
140 (*this)[ pair.first ] = pair.second;
145 // If we're empty, then just copy
151 const Property::Value& Property::Map::operator[]( const std::string& key ) const
153 for ( Container::const_iterator iter = mImpl->mContainer.begin(), endIter = mImpl->mContainer.end(); iter != endIter; ++iter )
155 if ( iter->first == key )
161 DALI_ASSERT_ALWAYS( ! "Invalid Key" );
164 Property::Value& Property::Map::operator[]( const std::string& key )
166 for ( Container::iterator iter = mImpl->mContainer.begin(), endIter = mImpl->mContainer.end(); iter != endIter; ++iter )
168 if ( iter->first == key )
174 // Create and return reference to new value
175 mImpl->mContainer.push_back( std::make_pair( key, Property::Value() ) );
176 return (mImpl->mContainer.end() - 1)->second;
179 Property::Map& Property::Map::operator=( const Property::Map& other )
185 mImpl->mContainer = other.mImpl->mContainer;
190 std::ostream& operator<<( std::ostream& stream, const Property::Map& map )
192 stream << "Map(" << map.Count() << ") = {";
193 for( unsigned int i=0; i<map.Count(); ++i )
199 stream << map.GetKey(i) << ":" << map.GetValue( i );