2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
4 * Licensed under the Flora License, Version 1.1 (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
8 * http://floralicense.org/license/
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.
23 #include <type_traits>
26 typedef uint32_t uint;
27 typedef uint16_t ushort;
28 typedef uint8_t uchar;
30 #if !defined(V_NAMESPACE)
32 #define V_USE_NAMESPACE
33 #define V_BEGIN_NAMESPACE
34 #define V_END_NAMESPACE
36 #else /* user namespace */
38 #define V_USE_NAMESPACE using namespace ::V_NAMESPACE;
39 #define V_BEGIN_NAMESPACE namespace V_NAMESPACE {
40 #define V_END_NAMESPACE }
44 #define V_UNUSED __attribute__((__unused__))
45 #define V_REQUIRED_RESULT __attribute__((__warn_unused_result__))
47 #define V_CONSTEXPR constexpr
48 #define V_NOTHROW noexcept
53 #define VECTOR_FALLTHROUGH __attribute__ ((fallthrough));
55 #define VECTOR_FALLTHROUGH
61 inline RefCount(int i) : atomic(i) {}
64 int count = atomic.load();
65 if (count == 0) // !isSharable
67 if (count != -1) // !isStatic
73 int count = atomic.load();
74 if (count == 0) // !isSharable
76 if (count == -1) // isStatic
83 int count = atomic.load();
84 return (count != 1) && (count != 0);
88 // Persistent object, never deleted
89 int count = atomic.load();
92 inline int count() const { return atomic; }
93 void setOwned() { atomic.store(1); }
96 std::atomic<int> atomic;
100 V_CONSTEXPR inline const T &vMin(const T &a, const T &b)
102 return (a < b) ? a : b;
104 template <typename T>
105 V_CONSTEXPR inline const T &vMax(const T &a, const T &b)
107 return (a < b) ? b : a;
110 static const double EPSILON_DOUBLE = 0.000000000001f;
111 static const float EPSILON_FLOAT = 0.000001f;
113 static inline bool vCompare(double p1, double p2)
115 return (std::abs(p1 - p2) < EPSILON_DOUBLE);
118 static inline bool vCompare(float p1, float p2)
120 return (std::abs(p1 - p2) < EPSILON_FLOAT);
123 static inline bool vIsZero(float f)
125 return (std::abs(f) <= EPSILON_FLOAT);
128 static inline bool vIsZero(double f)
130 return (std::abs(f) <= EPSILON_DOUBLE);
137 constexpr inline vFlagHelper(int ai) noexcept : i(ai) {}
138 constexpr inline operator int() const noexcept { return i; }
140 constexpr inline vFlagHelper(uint ai) noexcept : i(int(ai)) {}
141 constexpr inline vFlagHelper(short ai) noexcept : i(int(ai)) {}
142 constexpr inline vFlagHelper(ushort ai) noexcept : i(int(uint(ai))) {}
143 constexpr inline operator uint() const noexcept { return uint(i); }
146 template <typename Enum>
150 (sizeof(Enum) <= sizeof(int)),
151 "vFlag only supports int as storage so bigger type will overflow");
152 static_assert((std::is_enum<Enum>::value),
153 "vFlag is only usable on enumeration types.");
155 typedef typename std::conditional<
156 std::is_unsigned<typename std::underlying_type<Enum>::type>::value,
157 unsigned int, signed int>::type Int;
159 typedef Enum enum_type;
160 // compiler-generated copy/move ctor/assignment operators are fine!
162 constexpr inline vFlag(Enum f) noexcept : i(Int(f)) {}
163 constexpr inline vFlag() noexcept : i(0) {}
164 constexpr inline vFlag(vFlagHelper f) noexcept : i(f) {}
166 inline vFlag &operator&=(int mask) noexcept
171 inline vFlag &operator&=(uint mask) noexcept
176 inline vFlag &operator&=(Enum mask) noexcept
181 inline vFlag &operator|=(vFlag f) noexcept
186 inline vFlag &operator|=(Enum f) noexcept
191 inline vFlag &operator^=(vFlag f) noexcept
196 inline vFlag &operator^=(Enum f) noexcept
202 constexpr inline operator Int() const noexcept { return i; }
204 constexpr inline vFlag operator|(vFlag f) const
206 return vFlag(vFlagHelper(i | f.i));
208 constexpr inline vFlag operator|(Enum f) const noexcept
210 return vFlag(vFlagHelper(i | Int(f)));
212 constexpr inline vFlag operator^(vFlag f) const noexcept
214 return vFlag(vFlagHelper(i ^ f.i));
216 constexpr inline vFlag operator^(Enum f) const noexcept
218 return vFlag(vFlagHelper(i ^ Int(f)));
220 constexpr inline vFlag operator&(int mask) const noexcept
222 return vFlag(vFlagHelper(i & mask));
224 constexpr inline vFlag operator&(uint mask) const noexcept
226 return vFlag(vFlagHelper(i & mask));
228 constexpr inline vFlag operator&(Enum f) const noexcept
230 return vFlag(vFlagHelper(i & Int(f)));
232 constexpr inline vFlag operator~() const noexcept
234 return vFlag(vFlagHelper(~i));
237 constexpr inline bool operator!() const noexcept { return !i; }
239 constexpr inline bool testFlag(Enum f) const noexcept
241 return (i & Int(f)) == Int(f) && (Int(f) != 0 || i == Int(f));
243 inline vFlag &setFlag(Enum f, bool on = true) noexcept
245 return on ? (*this |= f) : (*this &= ~f);
253 inline VColor() noexcept { a = r = g = b = 0; }
254 inline VColor(int red, int green, int blue, int alpha = 255) noexcept
261 inline int red() const noexcept { return r; }
262 inline int green() const noexcept { return g; }
263 inline int blue() const noexcept { return b; }
264 inline int alpha() const noexcept { return a; }
265 inline void setRed(int red) noexcept { r = red; }
266 inline void setGreen(int green) noexcept { g = green; }
267 inline void setBlue(int blue) noexcept { b = blue; }
268 inline void setAlpha(int alpha) noexcept { a = alpha; }
269 inline bool isOpaque() const { return a == 255; }
270 inline bool operator==(const VColor &o) const
272 return ((a == o.a) && (r == o.r) && (g == o.g) && (b == o.b));
274 uint premulARGB() const
276 int pr = (r * a) / 255;
277 int pg = (g * a) / 255;
278 int pb = (b * a) / 255;
279 return uint((a << 24) | (pr << 16) | (pg << 8) | (pb));
282 uint premulARGB(float opacity) const
284 int alpha = a * opacity;
285 int pr = (r * alpha) / 255;
286 int pg = (g * alpha) / 255;
287 int pb = (b * alpha) / 255;
288 return uint((alpha << 24) | (pr << 16) | (pg << 8) | (pb));
298 enum class FillRule: unsigned char { EvenOdd, Winding };
299 enum class JoinStyle: unsigned char { Miter, Bevel, Round };
300 enum class CapStyle: unsigned char { Flat, Square, Round };
302 #ifndef V_CONSTRUCTOR_FUNCTION
303 #define V_CONSTRUCTOR_FUNCTION0(AFUNC) \
305 static const struct AFUNC##_ctor_class_ { \
306 inline AFUNC##_ctor_class_() { AFUNC(); } \
307 } AFUNC##_ctor_instance_; \
310 #define V_CONSTRUCTOR_FUNCTION(AFUNC) V_CONSTRUCTOR_FUNCTION0(AFUNC)