Add support to create Property::Map with initializer_list
[platform/core/uifw/dali-core.git] / dali / public-api / object / property-array.cpp
1 /*
2  * Copyright (c) 2019 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-array.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/common/vector-wrapper.h>
23
24 namespace Dali
25 {
26
27 namespace
28 {
29 }; // unnamed namespace
30
31 struct Property::Array::Impl
32 {
33   typedef std::vector<Value> Array;
34
35   Array mArray;
36 };
37
38 Property::Array::Array()
39 : mImpl( new Impl )
40 {
41 }
42
43 Property::Array::Array( const std::initializer_list< Property::Value >& values )
44 : Array()
45 {
46   for( auto&& value : values )
47   {
48     PushBack( value );
49   }
50 }
51
52 Property::Array::Array( const Property::Array& other )
53 : mImpl( new Impl )
54 {
55   mImpl->mArray = other.mImpl->mArray;
56 }
57
58 Property::Array::Array( Property::Array&& other )
59 : mImpl( other.mImpl )
60 {
61   other.mImpl = nullptr;
62 }
63
64 Property::Array::~Array()
65 {
66   delete mImpl;
67 }
68
69 Property::Array::SizeType Property::Array::Count() const
70 {
71   DALI_ASSERT_DEBUG( mImpl && "Cannot use an object previously used as an r-value" );
72   return mImpl->mArray.size();
73 }
74
75 void Property::Array::PushBack( const Value& value )
76 {
77   DALI_ASSERT_DEBUG( mImpl && "Cannot use an object previously used as an r-value" );
78   mImpl->mArray.push_back( value );
79 }
80
81 void Property::Array::Clear()
82 {
83   DALI_ASSERT_DEBUG( mImpl && "Cannot use an object previously used as an r-value" );
84   mImpl->mArray.clear();
85 }
86
87 void Property::Array::Reserve( SizeType size )
88 {
89   DALI_ASSERT_DEBUG( mImpl && "Cannot use an object previously used as an r-value" );
90   mImpl->mArray.reserve(size);
91 }
92
93 void Property::Array::Resize( SizeType size )
94 {
95   DALI_ASSERT_DEBUG( mImpl && "Cannot use an object previously used as an r-value" );
96   mImpl->mArray.resize(size);
97 }
98
99 Property::Array::SizeType Property::Array::Capacity()
100 {
101   DALI_ASSERT_DEBUG( mImpl && "Cannot use an object previously used as an r-value" );
102   return mImpl->mArray.capacity();
103 }
104
105 const Property::Value& Property::Array::operator[]( SizeType index ) const
106 {
107   DALI_ASSERT_DEBUG( mImpl && "Cannot use an object previously used as an r-value" );
108
109   // Note says no bounds checking is performed so we don't need to verify mImpl as Count() will return 0 anyway
110   return mImpl->mArray[ index ];
111 }
112
113 Property::Value& Property::Array::operator[]( SizeType index )
114 {
115   DALI_ASSERT_DEBUG( mImpl && "Cannot use an object previously used as an r-value" );
116
117   // Note says no bounds checking is performed so we don't need to verify mImpl as Count() will return 0 anyway
118   return mImpl->mArray[ index ];
119 }
120
121 Property::Array& Property::Array::operator=( const Property::Array& other )
122 {
123   DALI_ASSERT_DEBUG( mImpl && "Cannot use an object previously used as an r-value" );
124
125   if( this != &other )
126   {
127     mImpl->mArray = other.mImpl->mArray;
128   }
129   return *this;
130 }
131
132 Property::Array& Property::Array::operator=( Property::Array&& other )
133 {
134   if( this != &other )
135   {
136     delete mImpl;
137     mImpl = other.mImpl;
138     other.mImpl = nullptr;
139   }
140   return *this;
141 }
142
143 std::ostream& operator<<( std::ostream& stream, const Property::Array& array )
144 {
145   stream << "Array(" << array.Count() << ") = [";
146   for( Property::Array::SizeType i=0; i<array.Count(); ++i )
147   {
148     if( i>0 )
149     {
150       stream << ", ";
151     }
152     stream << array.GetElementAt(i);
153   }
154   stream << "]";
155
156   return stream;
157 }
158
159
160 } // namespace Dali