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