| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2.1 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 |
| 19 | #ifndef VGLOBAL_H |
| 20 | #define VGLOBAL_H |
| 21 |
| 22 | #include <cmath> |
| 23 | #include <cstdint> |
| 24 | #include <iostream> |
| 25 | #include <type_traits> |
| 26 | #include <utility> |
| 27 |
| 28 | typedef uint32_t uint; |
| 29 | typedef uint16_t ushort; |
| 30 | typedef uint8_t uchar; |
| 31 |
| 32 | #if !defined(V_NAMESPACE) |
| 33 |
| 34 | #define V_USE_NAMESPACE |
| 35 | #define V_BEGIN_NAMESPACE |
| 36 | #define V_END_NAMESPACE |
| 37 |
| 38 | #else /* user namespace */ |
| 39 |
| 40 | #define V_USE_NAMESPACE using namespace ::V_NAMESPACE; |
| 41 | #define V_BEGIN_NAMESPACE namespace V_NAMESPACE { |
| 42 | #define V_END_NAMESPACE } |
| 43 |
| 44 | #endif |
| 45 |
| 46 | #ifndef __has_attribute |
| 47 | # define __has_attribute(x) 0 |
| 48 | #endif /* !__has_attribute */ |
| 49 |
| 50 | #if __has_attribute(unused) |
| 51 | # define V_UNUSED __attribute__((__unused__)) |
| 52 | #else |
| 53 | # define V_UNUSED |
| 54 | #endif /* V_UNUSED */ |
| 55 |
| 56 | #if __has_attribute(warn_unused_result) |
| 57 | # define V_REQUIRED_RESULT __attribute__((__warn_unused_result__)) |
| 58 | #else |
| 59 | # define V_REQUIRED_RESULT |
| 60 | #endif /* V_REQUIRED_RESULT */ |
| 61 |
| 62 | #define V_CONSTEXPR constexpr |
| 63 | #define V_NOTHROW noexcept |
| 64 |
| 65 | #include "vdebug.h" |
| 66 |
| 67 | #if __GNUC__ >= 7 |
| 68 | #define VECTOR_FALLTHROUGH __attribute__ ((fallthrough)); |
| 69 | #else |
| 70 | #define VECTOR_FALLTHROUGH |
| 71 | #endif |
| 72 |
| 73 | #include <atomic> |
| 74 | class RefCount { |
| 75 | public: |
| 76 | inline RefCount(int i) : atomic(i) {} |
| 77 | inline bool ref() |
| 78 | { |
| 79 | int count = atomic.load(); |
| 80 | if (count == 0) // !isSharable |
| 81 | return false; |
| 82 | if (count != -1) // !isStatic |
| 83 | atomic.fetch_add(1); |
| 84 | return true; |
| 85 | } |
| 86 | inline bool deref() |
| 87 | { |
| 88 | int count = atomic.load(); |
| 89 | if (count == 0) // !isSharable |
| 90 | return false; |
| 91 | if (count == -1) // isStatic |
| 92 | return true; |
| 93 | atomic.fetch_sub(1); |
| 94 | return --count; |
| 95 | } |
| 96 | bool isShared() const |
| 97 | { |
| 98 | int count = atomic.load(); |
| 99 | return (count != 1) && (count != 0); |
| 100 | } |
| 101 | bool isStatic() const |
| 102 | { |
| 103 | // Persistent object, never deleted |
| 104 | int count = atomic.load(); |
| 105 | return count == -1; |
| 106 | } |
| 107 | inline int count() const { return atomic; } |
| 108 | void setOwned() { atomic.store(1); } |
| 109 |
| 110 | private: |
| 111 | std::atomic<int> atomic; |
| 112 | }; |
| 113 |
| 114 | template <typename T> |
| 115 | V_CONSTEXPR inline const T &vMin(const T &a, const T &b) |
| 116 | { |
| 117 | return (a < b) ? a : b; |
| 118 | } |
| 119 | template <typename T> |
| 120 | V_CONSTEXPR inline const T &vMax(const T &a, const T &b) |
| 121 | { |
| 122 | return (a < b) ? b : a; |
| 123 | } |
| 124 |
| 125 | static const double EPSILON_DOUBLE = 0.000000000001f; |
| 126 | static const float EPSILON_FLOAT = 0.000001f; |
| 127 |
| 128 | static inline bool vCompare(float p1, float p2) |
| 129 | { |
| 130 | return (std::abs(p1 - p2) < EPSILON_FLOAT); |
| 131 | } |
| 132 |
| 133 | static inline bool vIsZero(float f) |
| 134 | { |
| 135 | return (std::abs(f) <= EPSILON_FLOAT); |
| 136 | } |
| 137 |
| 138 | static inline bool vIsZero(double f) |
| 139 | { |
| 140 | return (std::abs(f) <= EPSILON_DOUBLE); |
| 141 | } |
| 142 |
| 143 | class vFlagHelper { |
| 144 | int i; |
| 145 |
| 146 | public: |
| 147 | constexpr inline vFlagHelper(int ai) noexcept : i(ai) {} |
| 148 | constexpr inline operator int() const noexcept { return i; } |
| 149 |
| 150 | constexpr inline vFlagHelper(uint ai) noexcept : i(int(ai)) {} |
| 151 | constexpr inline vFlagHelper(short ai) noexcept : i(int(ai)) {} |
| 152 | constexpr inline vFlagHelper(ushort ai) noexcept : i(int(uint(ai))) {} |
| 153 | constexpr inline operator uint() const noexcept { return uint(i); } |
| 154 | }; |
| 155 |
| 156 | template <typename Enum> |
| 157 | class vFlag { |
| 158 | public: |
| 159 | static_assert( |
| 160 | (sizeof(Enum) <= sizeof(int)), |
| 161 | "vFlag only supports int as storage so bigger type will overflow"); |
| 162 | static_assert((std::is_enum<Enum>::value), |
| 163 | "vFlag is only usable on enumeration types."); |
| 164 |
| 165 | typedef typename std::conditional< |
| 166 | std::is_unsigned<typename std::underlying_type<Enum>::type>::value, |
| 167 | unsigned int, signed int>::type Int; |
| 168 |
| 169 | typedef Enum enum_type; |
| 170 | // compiler-generated copy/move ctor/assignment operators are fine! |
| 171 |
| 172 | constexpr inline vFlag(Enum f) noexcept : i(Int(f)) {} |
| 173 | constexpr inline vFlag() noexcept : i(0) {} |
| 174 | constexpr inline vFlag(vFlagHelper f) noexcept : i(f) {} |
| 175 |
| 176 | inline vFlag &operator&=(int mask) noexcept |
| 177 | { |
| 178 | i &= mask; |
| 179 | return *this; |
| 180 | } |
| 181 | inline vFlag &operator&=(uint mask) noexcept |
| 182 | { |
| 183 | i &= mask; |
| 184 | return *this; |
| 185 | } |
| 186 | inline vFlag &operator&=(Enum mask) noexcept |
| 187 | { |
| 188 | i &= Int(mask); |
| 189 | return *this; |
| 190 | } |
| 191 | inline vFlag &operator|=(vFlag f) noexcept |
| 192 | { |
| 193 | i |= f.i; |
| 194 | return *this; |
| 195 | } |
| 196 | inline vFlag &operator|=(Enum f) noexcept |
| 197 | { |
| 198 | i |= Int(f); |
| 199 | return *this; |
| 200 | } |
| 201 | inline vFlag &operator^=(vFlag f) noexcept |
| 202 | { |
| 203 | i ^= f.i; |
| 204 | return *this; |
| 205 | } |
| 206 | inline vFlag &operator^=(Enum f) noexcept |
| 207 | { |
| 208 | i ^= Int(f); |
| 209 | return *this; |
| 210 | } |
| 211 |
| 212 | constexpr inline operator Int() const noexcept { return i; } |
| 213 |
| 214 | constexpr inline vFlag operator|(vFlag f) const |
| 215 | { |
| 216 | return vFlag(vFlagHelper(i | f.i)); |
| 217 | } |
| 218 | constexpr inline vFlag operator|(Enum f) const noexcept |
| 219 | { |
| 220 | return vFlag(vFlagHelper(i | Int(f))); |
| 221 | } |
| 222 | constexpr inline vFlag operator^(vFlag f) const noexcept |
| 223 | { |
| 224 | return vFlag(vFlagHelper(i ^ f.i)); |
| 225 | } |
| 226 | constexpr inline vFlag operator^(Enum f) const noexcept |
| 227 | { |
| 228 | return vFlag(vFlagHelper(i ^ Int(f))); |
| 229 | } |
| 230 | constexpr inline vFlag operator&(int mask) const noexcept |
| 231 | { |
| 232 | return vFlag(vFlagHelper(i & mask)); |
| 233 | } |
| 234 | constexpr inline vFlag operator&(uint mask) const noexcept |
| 235 | { |
| 236 | return vFlag(vFlagHelper(i & mask)); |
| 237 | } |
| 238 | constexpr inline vFlag operator&(Enum f) const noexcept |
| 239 | { |
| 240 | return vFlag(vFlagHelper(i & Int(f))); |
| 241 | } |
| 242 | constexpr inline vFlag operator~() const noexcept |
| 243 | { |
| 244 | return vFlag(vFlagHelper(~i)); |
| 245 | } |
| 246 |
| 247 | constexpr inline bool operator!() const noexcept { return !i; } |
| 248 |
| 249 | constexpr inline bool testFlag(Enum f) const noexcept |
| 250 | { |
| 251 | return (i & Int(f)) == Int(f) && (Int(f) != 0 || i == Int(f)); |
| 252 | } |
| 253 | inline vFlag &setFlag(Enum f, bool on = true) noexcept |
| 254 | { |
| 255 | return on ? (*this |= f) : (*this &= ~f); |
| 256 | } |
| 257 |
| 258 | Int i; |
| 259 | }; |
| 260 |
| 261 | class VColor { |
| 262 | public: |
| 263 | inline VColor() noexcept { a = r = g = b = 0; } |
| 264 | inline VColor(int red, int green, int blue, int alpha = 255) noexcept |
| 265 | { |
| 266 | r = red; |
| 267 | g = green; |
| 268 | b = blue; |
| 269 | a = alpha; |
| 270 | } |
| 271 | inline int red() const noexcept { return r; } |
| 272 | inline int green() const noexcept { return g; } |
| 273 | inline int blue() const noexcept { return b; } |
| 274 | inline int alpha() const noexcept { return a; } |
| 275 | inline void setRed(int red) noexcept { r = red; } |
| 276 | inline void setGreen(int green) noexcept { g = green; } |
| 277 | inline void setBlue(int blue) noexcept { b = blue; } |
| 278 | inline void setAlpha(int alpha) noexcept { a = alpha; } |
| 279 | inline bool isOpaque() const { return a == 255; } |
| 280 | inline bool operator==(const VColor &o) const |
| 281 | { |
| 282 | return ((a == o.a) && (r == o.r) && (g == o.g) && (b == o.b)); |
| 283 | } |
| 284 | uint premulARGB() const |
| 285 | { |
| 286 | int pr = (r * a) / 255; |
| 287 | int pg = (g * a) / 255; |
| 288 | int pb = (b * a) / 255; |
| 289 | return uint((a << 24) | (pr << 16) | (pg << 8) | (pb)); |
| 290 | } |
| 291 |
| 292 | uint premulARGB(float opacity) const |
| 293 | { |
| 294 | int alpha = a * opacity; |
| 295 | int pr = (r * alpha) / 255; |
| 296 | int pg = (g * alpha) / 255; |
| 297 | int pb = (b * alpha) / 255; |
| 298 | return uint((alpha << 24) | (pr << 16) | (pg << 8) | (pb)); |
| 299 | } |
| 300 |
| 301 | public: |
| 302 | uchar a; |
| 303 | uchar r; |
| 304 | uchar g; |
| 305 | uchar b; |
| 306 | }; |
| 307 |
| 308 | enum class FillRule: unsigned char { EvenOdd, Winding }; |
| 309 | enum class JoinStyle: unsigned char { Miter, Bevel, Round }; |
| 310 | enum class CapStyle: unsigned char { Flat, Square, Round }; |
| 311 |
| 312 | #ifndef V_CONSTRUCTOR_FUNCTION |
| 313 | #define V_CONSTRUCTOR_FUNCTION0(AFUNC) \ |
| 314 | namespace { \ |
| 315 | static const struct AFUNC##_ctor_class_ { \ |
| 316 | inline AFUNC##_ctor_class_() { AFUNC(); } \ |
| 317 | } AFUNC##_ctor_instance_; \ |
| 318 | } |
| 319 |
| 320 | #define V_CONSTRUCTOR_FUNCTION(AFUNC) V_CONSTRUCTOR_FUNCTION0(AFUNC) |
| 321 | #endif |
| 322 |
| 323 | #endif // VGLOBAL_H |
| 324 |