Update copyright year to 2015 for public api: core
[platform/core/uifw/dali-core.git] / dali / public-api / object / property-map.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/public-api/object/property-map.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/common/vector-wrapper.h>
23
24 namespace Dali
25 {
26
27 namespace
28 {
29 typedef std::vector< StringValuePair > Container;
30 }; // unnamed namespace
31
32 struct Property::Map::Impl
33 {
34   Container mContainer;
35 };
36
37 Property::Map::Map()
38 : mImpl( new Impl )
39 {
40 }
41
42 Property::Map::Map( const Property::Map& other )
43 : mImpl( new Impl )
44 {
45   mImpl->mContainer = other.mImpl->mContainer;
46 }
47
48 Property::Map::~Map()
49 {
50   delete mImpl;
51 }
52
53 unsigned int Property::Map::Count() const
54 {
55   return mImpl->mContainer.size();
56 }
57
58 bool Property::Map::Empty() const
59 {
60   return mImpl->mContainer.empty();
61 }
62
63 Property::Value& Property::Map::GetValue( unsigned int position ) const
64 {
65   DALI_ASSERT_ALWAYS( position < Count() && "position out-of-bounds" );
66
67   return mImpl->mContainer[ position ].second;
68 }
69
70 const std::string& Property::Map::GetKey( unsigned int position ) const
71 {
72   DALI_ASSERT_ALWAYS( position < Count() && "position out-of-bounds" );
73
74   return mImpl->mContainer[ position ].first;
75 }
76
77 StringValuePair& Property::Map::GetPair( unsigned int position ) const
78 {
79   DALI_ASSERT_ALWAYS( position < Count() && "position out-of-bounds" );
80
81   return mImpl->mContainer[ position ];
82 }
83
84 Property::Value* Property::Map::Find( const std::string& key ) const
85 {
86   for ( Container::iterator iter = mImpl->mContainer.begin(), endIter = mImpl->mContainer.end(); iter != endIter; ++iter )
87   {
88     if ( iter->first == key )
89     {
90       return &iter->second;
91     }
92   }
93   return NULL; // Not found
94 }
95
96 void Property::Map::Clear()
97 {
98   mImpl->mContainer.clear();
99 }
100
101 void Property::Map::Merge( const Property::Map& from )
102 {
103   // Ensure we're not attempting to merge with ourself
104   if ( this != &from )
105   {
106     if ( Count() )
107     {
108       for ( unsigned int i = 0, count = from.Count(); i < count; ++i )
109       {
110         StringValuePair& pair( from.GetPair( i ) );
111         (*this)[ pair.first ] = pair.second;
112       }
113     }
114     else
115     {
116       // If we're empty, then just copy
117       *this = from;
118     }
119   }
120 }
121
122 const Property::Value& Property::Map::operator[]( const std::string& key ) const
123 {
124   for ( Container::const_iterator iter = mImpl->mContainer.begin(), endIter = mImpl->mContainer.end(); iter != endIter; ++iter )
125   {
126     if ( iter->first == key )
127     {
128       return iter->second;
129     }
130   }
131
132   DALI_ASSERT_ALWAYS( ! "Invalid Key" );
133 }
134
135 Property::Value& Property::Map::operator[]( const std::string& key )
136 {
137   for ( Container::iterator iter = mImpl->mContainer.begin(), endIter = mImpl->mContainer.end(); iter != endIter; ++iter )
138   {
139     if ( iter->first == key )
140     {
141       return iter->second;
142     }
143   }
144
145   // Create and return reference to new value
146   mImpl->mContainer.push_back( StringValuePair( key, Value() ) );
147   return (mImpl->mContainer.end() - 1)->second;
148 }
149
150 Property::Map& Property::Map::operator=( const Property::Map& other )
151 {
152   if( this != &other )
153   {
154     delete mImpl;
155     mImpl = new Impl;
156     mImpl->mContainer = other.mImpl->mContainer;
157   }
158   return *this;
159 }
160
161 } // namespace Dali