• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 ////////////////////////////////////////////////////////////////////////////////
54 
55 // Can be used to bracket data types that must be dense, e.g. hash keys.
56 #if defined(__clang__)  // This should work on GCC too, but GCC diagnostic pop didn't seem to work!
57     #define SK_BEGIN_REQUIRE_DENSE _Pragma("GCC diagnostic push") \
58                                    _Pragma("GCC diagnostic error \"-Wpadded\"")
59     #define SK_END_REQUIRE_DENSE   _Pragma("GCC diagnostic pop")
60 #else
61     #define SK_BEGIN_REQUIRE_DENSE
62     #define SK_END_REQUIRE_DENSE
63 #endif
64 
65 #define SK_INIT_TO_AVOID_WARNING    = 0
66 
67 #endif  // SkMacros_DEFINED
68