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 unsigned int Property::Map::Count() const
55 return mImpl->mContainer.size();
58 bool Property::Map::Empty() const
60 return mImpl->mContainer.empty();
63 Property::Value& Property::Map::GetValue( unsigned int position ) const
65 DALI_ASSERT_ALWAYS( position < Count() && "position out-of-bounds" );
67 return mImpl->mContainer[ position ].second;
70 const std::string& Property::Map::GetKey( unsigned int position ) const
72 DALI_ASSERT_ALWAYS( position < Count() && "position out-of-bounds" );
74 return mImpl->mContainer[ position ].first;
77 StringValuePair& Property::Map::GetPair( unsigned int position ) const
79 DALI_ASSERT_ALWAYS( position < Count() && "position out-of-bounds" );
81 return mImpl->mContainer[ position ];
84 Property::Value* Property::Map::Find( const std::string& key ) const
86 for ( Container::iterator iter = mImpl->mContainer.begin(), endIter = mImpl->mContainer.end(); iter != endIter; ++iter )
88 if ( iter->first == key )
93 return NULL; // Not found
96 Property::Value* Property::Map::Find( const std::string& key, Property::Type type ) const
98 for ( Container::iterator iter = mImpl->mContainer.begin(), endIter = mImpl->mContainer.end(); iter != endIter; ++iter )
100 // test type first to shortcut eval (possibly reducing string compares)
101 if( (iter->second.GetType() == type) && (iter->first == key) )
103 return &iter->second;
106 return NULL; // Not found
109 void Property::Map::Clear()
111 mImpl->mContainer.clear();
114 void Property::Map::Merge( const Property::Map& from )
116 // Ensure we're not attempting to merge with ourself
121 for ( unsigned int i = 0, count = from.Count(); i < count; ++i )
123 StringValuePair& pair( from.GetPair( i ) );
124 (*this)[ pair.first ] = pair.second;
129 // If we're empty, then just copy
135 const Property::Value& Property::Map::operator[]( const std::string& key ) const
137 for ( Container::const_iterator iter = mImpl->mContainer.begin(), endIter = mImpl->mContainer.end(); iter != endIter; ++iter )
139 if ( iter->first == key )
145 DALI_ASSERT_ALWAYS( ! "Invalid Key" );
148 Property::Value& Property::Map::operator[]( const std::string& key )
150 for ( Container::iterator iter = mImpl->mContainer.begin(), endIter = mImpl->mContainer.end(); iter != endIter; ++iter )
152 if ( iter->first == key )
158 // Create and return reference to new value
159 mImpl->mContainer.push_back( StringValuePair( key, Value() ) );
160 return (mImpl->mContainer.end() - 1)->second;
163 Property::Map& Property::Map::operator=( const Property::Map& other )
169 mImpl->mContainer = other.mImpl->mContainer;