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