| 1 |
/* |
| 2 |
* Copyright 2018 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
#ifndef SkMacros_DEFINED |
| 8 |
#define SkMacros_DEFINED |
| 9 |
|
| 10 |
#include "include/private/base/SkTo.h" // IWYU pragma: keep |
| 11 |
|
| 12 |
/* |
| 13 |
* Usage: SK_MACRO_CONCAT(a, b) to construct the symbol ab |
| 14 |
* |
| 15 |
* SK_MACRO_CONCAT_IMPL_PRIV just exists to make this work. Do not use directly |
| 16 |
* |
| 17 |
*/ |
| 18 |
#define SK_MACRO_CONCAT(X, Y) SK_MACRO_CONCAT_IMPL_PRIV(X, Y) |
| 19 |
#define SK_MACRO_CONCAT_IMPL_PRIV(X, Y) X ## Y |
| 20 |
|
| 21 |
/* |
| 22 |
* Usage: SK_MACRO_APPEND_LINE(foo) to make foo123, where 123 is the current |
| 23 |
* line number. Easy way to construct |
| 24 |
* unique names for local functions or |
| 25 |
* variables. |
| 26 |
*/ |
| 27 |
#define SK_MACRO_APPEND_LINE(name) SK_MACRO_CONCAT(name, __LINE__) |
| 28 |
|
| 29 |
#define SK_MACRO_APPEND_COUNTER(name) SK_MACRO_CONCAT(name, __COUNTER__) |
| 30 |
|
| 31 |
/* |
| 32 |
* Makes a string of X, where X is expanded before conversion to a string if X itself contains |
| 33 |
* macros. |
| 34 |
* |
| 35 |
* Usage: SK_MACRO_STRINGIFY(__LINE__) to make "123", where 123 is the current line number. |
| 36 |
*/ |
| 37 |
#define SK_MACRO_STRINGIFY(X) SK_MACRO_STRINGIFY_IMPL_PRIV(X) |
| 38 |
#define SK_MACRO_STRINGIFY_IMPL_PRIV(X) #X |
| 39 |
|
| 40 |
//////////////////////////////////////////////////////////////////////////////// |
| 41 |
|
| 42 |
// Can be used to bracket data types that must be dense/packed, e.g. hash keys. |
| 43 |
#if defined(__clang__) // This should work on GCC too, but GCC diagnostic pop didn't seem to work! |
| 44 |
#define SK_BEGIN_REQUIRE_DENSE _Pragma("GCC diagnostic push") \ |
| 45 |
_Pragma("GCC diagnostic error \"-Wpadded\"") |
| 46 |
#define SK_END_REQUIRE_DENSE _Pragma("GCC diagnostic pop") |
| 47 |
#else |
| 48 |
#define SK_BEGIN_REQUIRE_DENSE |
| 49 |
#define SK_END_REQUIRE_DENSE |
| 50 |
#endif |
| 51 |
|
| 52 |
#if defined(__clang__) && defined(__has_feature) |
| 53 |
// Some compilers have a preprocessor that does not appear to do short-circuit |
| 54 |
// evaluation as expected |
| 55 |
#if __has_feature(leak_sanitizer) || __has_feature(address_sanitizer) |
| 56 |
// Chrome had issues if we tried to include lsan_interface.h ourselves. |
| 57 |
// https://github.com/llvm/llvm-project/blob/10a35632d55bb05004fe3d0c2d4432bb74897ee7/compiler-rt/include/sanitizer/lsan_interface.h#L26 |
| 58 |
extern "C" { |
| 59 |
void __lsan_ignore_object(const void *p); |
| 60 |
} |
| 61 |
#define SK_INTENTIONALLY_LEAKED(X) __lsan_ignore_object(X) |
| 62 |
#else |
| 63 |
#define SK_INTENTIONALLY_LEAKED(X) ((void)0) |
| 64 |
#endif |
| 65 |
#else |
| 66 |
#define SK_INTENTIONALLY_LEAKED(X) ((void)0) |
| 67 |
#endif |
| 68 |
|
| 69 |
#define SK_INIT_TO_AVOID_WARNING = 0 |
| 70 |
|
| 71 |
//////////////////////////////////////////////////////////////////////////////// |
| 72 |
|
| 73 |
/** |
| 74 |
* Defines overloaded bitwise operators to make it easier to use an enum as a |
| 75 |
* bitfield. |
| 76 |
*/ |
| 77 |
#define SK_MAKE_BITFIELD_OPS(X) \ |
| 78 |
inline X operator ~(X a) { \ |
| 79 |
using U = std::underlying_type_t<X>; \ |
| 80 |
return (X) (~static_cast<U>(a)); \ |
| 81 |
} \ |
| 82 |
inline X operator |(X a, X b) { \ |
| 83 |
using U = std::underlying_type_t<X>; \ |
| 84 |
return (X) (static_cast<U>(a) | static_cast<U>(b)); \ |
| 85 |
} \ |
| 86 |
inline X& operator |=(X& a, X b) { \ |
| 87 |
return (a = a | b); \ |
| 88 |
} \ |
| 89 |
inline X operator &(X a, X b) { \ |
| 90 |
using U = std::underlying_type_t<X>; \ |
| 91 |
return (X) (static_cast<U>(a) & static_cast<U>(b)); \ |
| 92 |
} \ |
| 93 |
inline X& operator &=(X& a, X b) { \ |
| 94 |
return (a = a & b); \ |
| 95 |
} |
| 96 |
|
| 97 |
#define SK_DECL_BITFIELD_OPS_FRIENDS(X) \ |
| 98 |
friend X operator ~(X a); \ |
| 99 |
friend X operator |(X a, X b); \ |
| 100 |
friend X& operator |=(X& a, X b); \ |
| 101 |
\ |
| 102 |
friend X operator &(X a, X b); \ |
| 103 |
friend X& operator &=(X& a, X b); |
| 104 |
|
| 105 |
/** |
| 106 |
* Wraps a C++11 enum that we use as a bitfield, and enables a limited amount of |
| 107 |
* masking with type safety. Instantiated with the ~ operator. |
| 108 |
*/ |
| 109 |
template<typename TFlags> class SkTFlagsMask { |
| 110 |
public: |
| 111 |
constexpr explicit SkTFlagsMask(TFlags value) : SkTFlagsMask(static_cast<int>(value)) {} |
| 112 |
constexpr explicit SkTFlagsMask(int value) : fValue(value) {} |
| 113 |
constexpr int value() const { return fValue; } |
| 114 |
private: |
| 115 |
const int fValue; |
| 116 |
}; |
| 117 |
|
| 118 |
/** |
| 119 |
* Defines bitwise operators that make it possible to use an enum class as a |
| 120 |
* basic bitfield. |
| 121 |
*/ |
| 122 |
#define SK_MAKE_BITFIELD_CLASS_OPS(X) \ |
| 123 |
[[maybe_unused]] constexpr SkTFlagsMask<X> operator~(X a) { \ |
| 124 |
return SkTFlagsMask<X>(~static_cast<int>(a)); \ |
| 125 |
} \ |
| 126 |
[[maybe_unused]] constexpr X operator|(X a, X b) { \ |
| 127 |
return static_cast<X>(static_cast<int>(a) | static_cast<int>(b)); \ |
| 128 |
} \ |
| 129 |
[[maybe_unused]] inline X& operator|=(X& a, X b) { \ |
| 130 |
return (a = a | b); \ |
| 131 |
} \ |
| 132 |
[[maybe_unused]] constexpr bool operator&(X a, X b) { \ |
| 133 |
return SkToBool(static_cast<int>(a) & static_cast<int>(b)); \ |
| 134 |
} \ |
| 135 |
[[maybe_unused]] constexpr SkTFlagsMask<X> operator|(SkTFlagsMask<X> a, SkTFlagsMask<X> b) { \ |
| 136 |
return SkTFlagsMask<X>(a.value() | b.value()); \ |
| 137 |
} \ |
| 138 |
[[maybe_unused]] constexpr SkTFlagsMask<X> operator|(SkTFlagsMask<X> a, X b) { \ |
| 139 |
return SkTFlagsMask<X>(a.value() | static_cast<int>(b)); \ |
| 140 |
} \ |
| 141 |
[[maybe_unused]] constexpr SkTFlagsMask<X> operator|(X a, SkTFlagsMask<X> b) { \ |
| 142 |
return SkTFlagsMask<X>(static_cast<int>(a) | b.value()); \ |
| 143 |
} \ |
| 144 |
[[maybe_unused]] constexpr X operator&(SkTFlagsMask<X> a, SkTFlagsMask<X> b) { \ |
| 145 |
return static_cast<X>(a.value() & b.value()); \ |
| 146 |
} \ |
| 147 |
[[maybe_unused]] constexpr X operator&(SkTFlagsMask<X> a, X b) { \ |
| 148 |
return static_cast<X>(a.value() & static_cast<int>(b)); \ |
| 149 |
} \ |
| 150 |
[[maybe_unused]] constexpr X operator&(X a, SkTFlagsMask<X> b) { \ |
| 151 |
return static_cast<X>(static_cast<int>(a) & b.value()); \ |
| 152 |
} \ |
| 153 |
[[maybe_unused]] inline X& operator&=(X& a, SkTFlagsMask<X> b) { \ |
| 154 |
return (a = a & b); \ |
| 155 |
} \ |
| 156 |
|
| 157 |
#define SK_DECL_BITFIELD_CLASS_OPS_FRIENDS(X) \ |
| 158 |
friend constexpr SkTFlagsMask<X> operator ~(X); \ |
| 159 |
friend constexpr X operator |(X, X); \ |
| 160 |
friend X& operator |=(X&, X); \ |
| 161 |
friend constexpr bool operator &(X, X); \ |
| 162 |
friend constexpr SkTFlagsMask<X> operator|(SkTFlagsMask<X>, SkTFlagsMask<X>); \ |
| 163 |
friend constexpr SkTFlagsMask<X> operator|(SkTFlagsMask<X>, X); \ |
| 164 |
friend constexpr SkTFlagsMask<X> operator|(X, SkTFlagsMask<X>); \ |
| 165 |
friend constexpr X operator&(SkTFlagsMask<X>, SkTFlagsMask<X>); \ |
| 166 |
friend constexpr X operator&(SkTFlagsMask<X>, X); \ |
| 167 |
friend constexpr X operator&(X, SkTFlagsMask<X>); \ |
| 168 |
friend X& operator &=(X&, SkTFlagsMask<X>) |
| 169 |
|
| 170 |
#endif // SkMacros_DEFINED |