common: code refactoring
[platform/core/graphics/tizenvg.git] / src / lib / tvgFill.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
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 #ifndef _TVG_FILL_CPP_
18 #define _TVG_FILL_CPP_
19
20 #include "tvgCommon.h"
21
22
23 /************************************************************************/
24 /* Internal Class Implementation                                        */
25 /************************************************************************/
26
27 struct Fill::Impl
28 {
29     ColorStop* colorStops = nullptr;
30     uint32_t cnt = 0;
31     FillSpread spread;
32
33     ~Impl()
34     {
35         if (colorStops) free(colorStops);
36     }
37 };
38
39
40 /************************************************************************/
41 /* External Class Implementation                                        */
42 /************************************************************************/
43
44 Fill::Fill():pImpl(make_unique<Impl>())
45 {
46 }
47
48
49 Fill::~Fill()
50 {
51 }
52
53
54 Result Fill::colorStops(const ColorStop* colorStops, uint32_t cnt) noexcept
55 {
56     auto impl = pImpl.get();
57
58     if (cnt == 0) {
59         if (impl->colorStops) {
60             free(impl->colorStops);
61             impl->colorStops = nullptr;
62             impl->cnt = cnt;
63         }
64         return Result::Success;
65     }
66
67     if (impl->cnt != cnt) {
68         impl->colorStops = static_cast<ColorStop*>(realloc(impl->colorStops, cnt * sizeof(ColorStop)));
69     }
70
71     impl->cnt = cnt;
72     memcpy(impl->colorStops, colorStops, cnt * sizeof(ColorStop));
73
74     return Result::Success;
75 }
76
77
78 uint32_t Fill::colorStops(const ColorStop** colorStops) const noexcept
79 {
80     if (colorStops) *colorStops = IMPL->colorStops;
81
82     return IMPL->cnt;
83 }
84
85
86 Result Fill::spread(FillSpread s) noexcept
87 {
88     IMPL->spread = s;
89
90     return Result::Success;
91 }
92
93
94 FillSpread Fill::spread() const noexcept
95 {
96     return IMPL->spread;
97 }
98
99 #endif /* _TVG_FILL_CPP_ */