example: just renamed the svg file.
[platform/core/graphics/tizenvg.git] / src / lib / tvgArray.h
1 /*
2  * Copyright (c) 2020 - 2022 Samsung Electronics Co., Ltd. All rights reserved.
3
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 #ifndef _TVG_ARRAY_H_
23 #define _TVG_ARRAY_H_
24
25 #include <memory.h>
26
27 namespace tvg
28 {
29
30 template<class T>
31 struct Array
32 {
33     T* data = nullptr;
34     uint32_t count = 0;
35     uint32_t reserved = 0;
36
37     void push(T element)
38     {
39         if (count + 1 > reserved) {
40             reserved = (count + 1) * 2;
41             auto p  = data;
42             data = static_cast<T*>(realloc(data, sizeof(T) * reserved));
43             if (!data) {
44                 data = p;
45                 return;
46             }
47         }
48         data[count++] = element;
49     }
50
51     bool reserve(uint32_t size)
52     {
53         if (size > reserved) {
54             reserved = size;
55             auto p = data;
56             data = static_cast<T*>(realloc(data, sizeof(T) * reserved));
57             if (!data) {
58                 data = p;
59                 return false;
60             }
61         }
62         return true;
63     }
64
65     bool grow(uint32_t size)
66     {
67         return reserve(count + size);
68     }
69
70     T* ptr()
71     {
72         return data + count;
73     }
74
75     void pop()
76     {
77         if (count > 0) --count;
78     }
79
80     void reset()
81     {
82         if (data) {
83             free(data);
84             data = nullptr;
85         }
86         count = reserved = 0;
87     }
88
89     void clear()
90     {
91         count = 0;
92     }
93
94     void operator=(const Array& rhs)
95     {
96         reserve(rhs.count);
97         if (rhs.count > 0) memcpy(data, rhs.data, sizeof(T) * reserved);
98         count = rhs.count;
99     }
100
101     ~Array()
102     {
103         if (data) free(data);
104     }
105 };
106
107 }
108
109 #endif //_TVG_ARRAY_H_