10 typedef uint32_t uint;
11 typedef uint16_t ushort;
12 typedef uint8_t uchar;
14 #if !defined(V_NAMESPACE)
16 #define V_USE_NAMESPACE
17 #define V_BEGIN_NAMESPACE
18 #define V_END_NAMESPACE
20 #else /* user namespace */
22 #define V_USE_NAMESPACE using namespace ::V_NAMESPACE;
23 #define V_BEGIN_NAMESPACE namespace V_NAMESPACE {
24 #define V_END_NAMESPACE }
28 #define V_UNUSED __attribute__((__unused__))
29 #define V_REQUIRED_RESULT __attribute__((__warn_unused_result__))
31 #define V_CONSTEXPR constexpr
32 #define V_NOTHROW noexcept
36 #define VECTOR_FALLTHROUGH
41 inline RefCount(int i) : atomic(i) {}
44 int count = atomic.load();
45 if (count == 0) // !isSharable
47 if (count != -1) // !isStatic
53 int count = atomic.load();
54 if (count == 0) // !isSharable
56 if (count == -1) // isStatic
63 int count = atomic.load();
64 return (count != 1) && (count != 0);
68 // Persistent object, never deleted
69 int count = atomic.load();
72 inline int count() const { return atomic; }
73 void setOwned() { atomic.store(1); }
76 std::atomic<int> atomic;
80 V_CONSTEXPR inline const T &vMin(const T &a, const T &b)
82 return (a < b) ? a : b;
85 V_CONSTEXPR inline const T &vMax(const T &a, const T &b)
87 return (a < b) ? b : a;
90 static const double EPSILON_DOUBLE = 0.000000000001f;
91 static const float EPSILON_FLOAT = 0.000001f;
93 static inline bool vCompare(double p1, double p2)
95 return (std::abs(p1 - p2) < EPSILON_DOUBLE);
98 static inline bool vCompare(float p1, float p2)
100 return (std::abs(p1 - p2) < EPSILON_FLOAT);
103 static inline bool vIsZero(float f)
105 return (std::abs(f) <= EPSILON_FLOAT);
108 static inline bool vIsZero(double f)
110 return (std::abs(f) <= EPSILON_DOUBLE);
117 constexpr inline vFlagHelper(int ai) noexcept : i(ai) {}
118 constexpr inline operator int() const noexcept { return i; }
120 constexpr inline vFlagHelper(uint ai) noexcept : i(int(ai)) {}
121 constexpr inline vFlagHelper(short ai) noexcept : i(int(ai)) {}
122 constexpr inline vFlagHelper(ushort ai) noexcept : i(int(uint(ai))) {}
123 constexpr inline operator uint() const noexcept { return uint(i); }
126 template <typename Enum>
130 (sizeof(Enum) <= sizeof(int)),
131 "vFlag only supports int as storage so bigger type will overflow");
132 static_assert((std::is_enum<Enum>::value),
133 "vFlag is only usable on enumeration types.");
135 typedef typename std::conditional<
136 std::is_unsigned<typename std::underlying_type<Enum>::type>::value,
137 unsigned int, signed int>::type Int;
139 typedef Enum enum_type;
140 // compiler-generated copy/move ctor/assignment operators are fine!
142 constexpr inline vFlag(Enum f) noexcept : i(Int(f)) {}
143 constexpr inline vFlag() noexcept : i(0) {}
144 constexpr inline vFlag(vFlagHelper f) noexcept : i(f) {}
146 inline vFlag &operator&=(int mask) noexcept
151 inline vFlag &operator&=(uint mask) noexcept
156 inline vFlag &operator&=(Enum mask) noexcept
161 inline vFlag &operator|=(vFlag f) noexcept
166 inline vFlag &operator|=(Enum f) noexcept
171 inline vFlag &operator^=(vFlag f) noexcept
176 inline vFlag &operator^=(Enum f) noexcept
182 constexpr inline operator Int() const noexcept { return i; }
184 constexpr inline vFlag operator|(vFlag f) const
186 return vFlag(vFlagHelper(i | f.i));
188 constexpr inline vFlag operator|(Enum f) const noexcept
190 return vFlag(vFlagHelper(i | Int(f)));
192 constexpr inline vFlag operator^(vFlag f) const noexcept
194 return vFlag(vFlagHelper(i ^ f.i));
196 constexpr inline vFlag operator^(Enum f) const noexcept
198 return vFlag(vFlagHelper(i ^ Int(f)));
200 constexpr inline vFlag operator&(int mask) const noexcept
202 return vFlag(vFlagHelper(i & mask));
204 constexpr inline vFlag operator&(uint mask) const noexcept
206 return vFlag(vFlagHelper(i & mask));
208 constexpr inline vFlag operator&(Enum f) const noexcept
210 return vFlag(vFlagHelper(i & Int(f)));
212 constexpr inline vFlag operator~() const noexcept
214 return vFlag(vFlagHelper(~i));
217 constexpr inline bool operator!() const noexcept { return !i; }
219 constexpr inline bool testFlag(Enum f) const noexcept
221 return (i & Int(f)) == Int(f) && (Int(f) != 0 || i == Int(f));
223 inline vFlag &setFlag(Enum f, bool on = true) noexcept
225 return on ? (*this |= f) : (*this &= ~f);
233 inline VColor() noexcept { a = r = g = b = 0; }
234 inline VColor(int red, int green, int blue, int alpha = 255) noexcept
241 inline int red() const noexcept { return r; }
242 inline int green() const noexcept { return g; }
243 inline int blue() const noexcept { return b; }
244 inline int alpha() const noexcept { return a; }
245 inline void setRed(int red) noexcept { r = red; }
246 inline void setGreen(int green) noexcept { g = green; }
247 inline void setBlue(int blue) noexcept { b = blue; }
248 inline void setAlpha(int alpha) noexcept { a = alpha; }
249 inline bool isOpaque() const { return a == 255; }
250 inline bool operator==(const VColor &o) const
252 return ((a == o.a) && (r == o.r) && (g == o.g) && (b == o.b));
254 uint premulARGB() const
256 int pr = (r * a) / 255;
257 int pg = (g * a) / 255;
258 int pb = (b * a) / 255;
259 return uint((a << 24) | (pr << 16) | (pg << 8) | (pb));
269 enum class FillRule { EvenOdd, Winding };
271 enum class JoinStyle { Miter, Bevel, Round };
272 enum class CapStyle { Flat, Square, Round };
274 #ifndef V_CONSTRUCTOR_FUNCTION
275 #define V_CONSTRUCTOR_FUNCTION0(AFUNC) \
277 static const struct AFUNC##_ctor_class_ { \
278 inline AFUNC##_ctor_class_() { AFUNC(); } \
279 } AFUNC##_ctor_instance_; \
282 #define V_CONSTRUCTOR_FUNCTION(AFUNC) V_CONSTRUCTOR_FUNCTION0(AFUNC)