1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /**
17  * @addtogroup GraphicGeometry
18  * @{
19  *
20  * @brief Defines Arc.
21  *
22  * @since 1.0
23  * @version 1.0
24  */
25 #ifndef GRAPHIC_LTE_GEOMETRY_PLAINDATA_ARRAY_H
26 #define GRAPHIC_LTE_GEOMETRY_PLAINDATA_ARRAY_H
27 
28 #include "gfx_utils/diagram/common/common_basics.h"
29 #include "gfx_utils/graphic_log.h"
30 #include "gfx_utils/heap_base.h"
31 #include "securec.h"
32 namespace OHOS {
33 /**
34  * @file geometry_plaindata_array.h
35  *
36  * @brief Defines GeometryPlainDataArray,Variable capacity.
37  *
38  * @since 1.0
39  * @version 1.0
40  */
41 template <class T>
42 class GeometryPlainDataArray : public HeapBase {
43 public:
44     using SelfType = GeometryPlainDataArray<T>;
45 
~GeometryPlainDataArray()46     ~GeometryPlainDataArray()
47     {
48         GeometryArrayAllocator<T>::Deallocate(data_, size_);
49     }
50 
GeometryPlainDataArray()51     GeometryPlainDataArray() : data_(0), size_(0) {}
52     /**
53      *
54      * @brief Construct definitions podarray array.
55      * @param size Initial capacity.
56      * @since 1.0
57      * @version 1.0
58      */
GeometryPlainDataArray(uint32_t size)59     GeometryPlainDataArray(uint32_t size)
60         : data_(GeometryArrayAllocator<T>::Allocate(size)), size_(size) {}
61 
GeometryPlainDataArray(const SelfType & podArray)62     GeometryPlainDataArray(const SelfType& podArray)
63         : data_(GeometryArrayAllocator<T>::Allocate(podArray.size_)), size_(podArray.size_)
64     {
65         if (memcpy_s(data_, sizeof(T) * size_, podArray.data_, sizeof(T) * size_) != EOK) {
66             GRAPHIC_LOGE("GeometryPlainDataArray fail");
67         }
68     }
69 
70     const SelfType& operator=(const SelfType& podArray)
71     {
72         Resize(podArray.GetSize());
73         if (memcpy_s(data_, sizeof(T) * size_, podArray.data_, sizeof(T) * size_) != EOK) {
74             GRAPHIC_LOGE("GeometryPlainDataArray fail");
75         }
76         return *this;
77     }
78     /**
79      * @brief Gets the element of the specified index.
80      *
81      * @since 1.0
82      * @version 1.0
83      */
84     const T& operator[](uint32_t index) const
85     {
86         return data_[index];
87     }
88     /**
89      * @brief Gets the element of the specified index.
90      *
91      * @since 1.0
92      * @version 1.0
93      */
94     T& operator[](uint32_t index)
95     {
96         if (index > size_) {
97             return data_[size_ - 1];
98         }
99 
100         return data_[index];
101     }
102     /**
103      * @brief Gets the element of the specified index.
104      *
105      * @since 1.0
106      * @version 1.0
107      */
ValueAt(uint32_t index)108     T ValueAt(uint32_t index) const
109     {
110         return data_[index];
111     }
112     /**
113      * @brief Gets the element of the specified index.
114      *
115      * @since 1.0
116      * @version 1.0
117      */
IndexAt(uint32_t index)118     const T& IndexAt(uint32_t index) const
119     {
120         return data_[index];
121     }
122     /**
123      * @brief Gets the element of the specified index.
124      *
125      * @since 1.0
126      * @version 1.0
127      */
IndexAt(uint32_t index)128     T& IndexAt(uint32_t index)
129     {
130         return data_[index];
131     }
132     /**
133      * @brief Get element header address.
134      *
135      * @since 1.0
136      * @version 1.0
137      */
Data()138     const T* Data() const
139     {
140         return data_;
141     }
142     /**
143      * @brief Get element header address.
144      *
145      * @since 1.0
146      * @version 1.0
147      */
Data()148     T* Data()
149     {
150         return data_;
151     }
152 
153     /**
154      *
155      * @brief Modify the capacity of the definitions podarray array.
156      * @param size capacity
157      * @since 1.0
158      * @version 1.0
159      */
Resize(uint32_t size)160     void Resize(uint32_t size)
161     {
162         if (size != size_) {
163             GeometryArrayAllocator<T>::Deallocate(data_, size_);
164             data_ = GeometryArrayAllocator<T>::Allocate(size_ = size);
165         }
166     }
167     /**
168      * @brief Get the number of elements.
169      *
170      * @since 1.0
171      * @version 1.0
172      */
GetSize()173     uint32_t GetSize() const
174     {
175         return size_;
176     }
177 
178 private:
179     T* data_;
180     uint32_t size_;
181 };
182 } // namespace OHOS
183 #endif
184