skia2/include/private/SkMacros.h
Ben Wagner 6c630f2ce6 Make SkScalerContext enums scoped.
This was motivated by SkScalerContextFlags putting `kNone` into the
global namespace. Instead of adding a suffix, change these internal
enums to be scoped and update the users.

Because SkScalerContextFlags is used like a bitfield,
`SK_MAKE_BITFIELD_OPS` is used to provide the appropriate operators. The
macro implementation is updated to handle scoped enums, remove the very
dangerous, unwanted, and unused templated operators, and add operator~
since that is needed by one existing user.

Also, for some reason this allows the compiler to find an unused field
which is removed.

Change-Id: Id31c80b381bffaf9b623f6a8ec7d86751d3d99f0
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/517616
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Ben Wagner <bungeman@google.com>
2022-03-08 16:24:47 +00:00

80 lines
2.5 KiB
C++

/*
* Copyright 2018 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkMacros_DEFINED
#define SkMacros_DEFINED
#include <type_traits>
/*
* Usage: SK_MACRO_CONCAT(a, b) to construct the symbol ab
*
* SK_MACRO_CONCAT_IMPL_PRIV just exists to make this work. Do not use directly
*
*/
#define SK_MACRO_CONCAT(X, Y) SK_MACRO_CONCAT_IMPL_PRIV(X, Y)
#define SK_MACRO_CONCAT_IMPL_PRIV(X, Y) X ## Y
/*
* Usage: SK_MACRO_APPEND_LINE(foo) to make foo123, where 123 is the current
* line number. Easy way to construct
* unique names for local functions or
* variables.
*/
#define SK_MACRO_APPEND_LINE(name) SK_MACRO_CONCAT(name, __LINE__)
#define SK_MACRO_APPEND_COUNTER(name) SK_MACRO_CONCAT(name, __COUNTER__)
////////////////////////////////////////////////////////////////////////////////
// Can be used to bracket data types that must be dense, e.g. hash keys.
#if defined(__clang__) // This should work on GCC too, but GCC diagnostic pop didn't seem to work!
#define SK_BEGIN_REQUIRE_DENSE _Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic error \"-Wpadded\"")
#define SK_END_REQUIRE_DENSE _Pragma("GCC diagnostic pop")
#else
#define SK_BEGIN_REQUIRE_DENSE
#define SK_END_REQUIRE_DENSE
#endif
#define SK_INIT_TO_AVOID_WARNING = 0
////////////////////////////////////////////////////////////////////////////////
/**
* Defines overloaded bitwise operators to make it easier to use an enum as a
* bitfield.
*/
#define SK_MAKE_BITFIELD_OPS(X) \
inline X operator ~(X a) { \
using U = std::underlying_type_t<X>; \
return (X) (~static_cast<U>(a)); \
} \
inline X operator |(X a, X b) { \
using U = std::underlying_type_t<X>; \
return (X) (static_cast<U>(a) | static_cast<U>(b)); \
} \
inline X& operator |=(X& a, X b) { \
return (a = a | b); \
} \
inline X operator &(X a, X b) { \
using U = std::underlying_type_t<X>; \
return (X) (static_cast<U>(a) & static_cast<U>(b)); \
} \
inline X& operator &=(X& a, X b) { \
return (a = a & b); \
}
#define SK_DECL_BITFIELD_OPS_FRIENDS(X) \
friend X operator ~(X a); \
friend X operator |(X a, X b); \
friend X& operator |=(X& a, X b); \
\
friend X operator &(X a, X b); \
friend X& operator &=(X& a, X b);
#endif // SkMacros_DEFINED