SkMacros.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. * Usage: SK_MACRO_CONCAT(a, b) to construct the symbol ab
  11. *
  12. * SK_MACRO_CONCAT_IMPL_PRIV just exists to make this work. Do not use directly
  13. *
  14. */
  15. #define SK_MACRO_CONCAT(X, Y) SK_MACRO_CONCAT_IMPL_PRIV(X, Y)
  16. #define SK_MACRO_CONCAT_IMPL_PRIV(X, Y) X ## Y
  17. /*
  18. * Usage: SK_MACRO_APPEND_LINE(foo) to make foo123, where 123 is the current
  19. * line number. Easy way to construct
  20. * unique names for local functions or
  21. * variables.
  22. */
  23. #define SK_MACRO_APPEND_LINE(name) SK_MACRO_CONCAT(name, __LINE__)
  24. /**
  25. * For some classes, it's almost always an error to instantiate one without a name, e.g.
  26. * {
  27. * SkAutoMutexAcquire(&mutex);
  28. * <some code>
  29. * }
  30. * In this case, the writer meant to hold mutex while the rest of the code in the block runs,
  31. * but instead the mutex is acquired and then immediately released. The correct usage is
  32. * {
  33. * SkAutoMutexAcquire lock(&mutex);
  34. * <some code>
  35. * }
  36. *
  37. * To prevent callers from instantiating your class without a name, use SK_REQUIRE_LOCAL_VAR
  38. * like this:
  39. * class classname {
  40. * <your class>
  41. * };
  42. * #define classname(...) SK_REQUIRE_LOCAL_VAR(classname)
  43. *
  44. * This won't work with templates, and you must inline the class' constructors and destructors.
  45. * Take a look at SkAutoFree and SkAutoMalloc in this file for examples.
  46. */
  47. #define SK_REQUIRE_LOCAL_VAR(classname) \
  48. static_assert(false, "missing name for " #classname)
  49. ////////////////////////////////////////////////////////////////////////////////
  50. // Can be used to bracket data types that must be dense, e.g. hash keys.
  51. #if defined(__clang__) // This should work on GCC too, but GCC diagnostic pop didn't seem to work!
  52. #define SK_BEGIN_REQUIRE_DENSE _Pragma("GCC diagnostic push") \
  53. _Pragma("GCC diagnostic error \"-Wpadded\"")
  54. #define SK_END_REQUIRE_DENSE _Pragma("GCC diagnostic pop")
  55. #else
  56. #define SK_BEGIN_REQUIRE_DENSE
  57. #define SK_END_REQUIRE_DENSE
  58. #endif
  59. #define SK_INIT_TO_AVOID_WARNING = 0
  60. #endif // SkMacros_DEFINED