Hal Canary | 22be4c4 | 2018-06-12 12:37:31 -0400 | [diff] [blame] | 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 | /* |
| 11 | * Usage: SK_MACRO_CONCAT(a, b) to construct the symbol ab |
| 12 | * |
| 13 | * SK_MACRO_CONCAT_IMPL_PRIV just exists to make this work. Do not use directly |
| 14 | * |
| 15 | */ |
| 16 | #define SK_MACRO_CONCAT(X, Y) SK_MACRO_CONCAT_IMPL_PRIV(X, Y) |
| 17 | #define SK_MACRO_CONCAT_IMPL_PRIV(X, Y) X ## Y |
| 18 | |
| 19 | /* |
| 20 | * Usage: SK_MACRO_APPEND_LINE(foo) to make foo123, where 123 is the current |
| 21 | * line number. Easy way to construct |
| 22 | * unique names for local functions or |
| 23 | * variables. |
| 24 | */ |
| 25 | #define SK_MACRO_APPEND_LINE(name) SK_MACRO_CONCAT(name, __LINE__) |
| 26 | |
| 27 | /** |
| 28 | * For some classes, it's almost always an error to instantiate one without a name, e.g. |
| 29 | * { |
| 30 | * SkAutoMutexAcquire(&mutex); |
| 31 | * <some code> |
| 32 | * } |
| 33 | * In this case, the writer meant to hold mutex while the rest of the code in the block runs, |
| 34 | * but instead the mutex is acquired and then immediately released. The correct usage is |
| 35 | * { |
| 36 | * SkAutoMutexAcquire lock(&mutex); |
| 37 | * <some code> |
| 38 | * } |
| 39 | * |
| 40 | * To prevent callers from instantiating your class without a name, use SK_REQUIRE_LOCAL_VAR |
| 41 | * like this: |
| 42 | * class classname { |
| 43 | * <your class> |
| 44 | * }; |
| 45 | * #define classname(...) SK_REQUIRE_LOCAL_VAR(classname) |
| 46 | * |
| 47 | * This won't work with templates, and you must inline the class' constructors and destructors. |
| 48 | * Take a look at SkAutoFree and SkAutoMalloc in this file for examples. |
| 49 | */ |
| 50 | #define SK_REQUIRE_LOCAL_VAR(classname) \ |
| 51 | static_assert(false, "missing name for " #classname) |
| 52 | |
| 53 | #endif // SkMacros_DEFINED |