ASBaseDefines.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // ASBaseDefines.h
  3. // AsyncDisplayKit
  4. //
  5. // Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  6. // This source code is licensed under the BSD-style license found in the
  7. // LICENSE file in the root directory of this source tree. An additional grant
  8. // of patent rights can be found in the PATENTS file in the same directory.
  9. //
  10. #pragma once
  11. #import "ASLog.h"
  12. // The C++ compiler mangles C function names. extern "C" { /* your C functions */ } prevents this.
  13. // You should wrap all C function prototypes declared in headers with ASDISPLAYNODE_EXTERN_C_BEGIN/END, even if
  14. // they are included only from .m (Objective-C) files. It's common for .m files to start using C++
  15. // features and become .mm (Objective-C++) files. Always wrapping the prototypes with
  16. // ASDISPLAYNODE_EXTERN_C_BEGIN/END will save someone a headache once they need to do this. You do not need to
  17. // wrap constants, only C functions. See StackOverflow for more details:
  18. // http://stackoverflow.com/questions/1041866/in-c-source-what-is-the-effect-of-extern-c
  19. #ifdef __cplusplus
  20. # define ASDISPLAYNODE_EXTERN_C_BEGIN extern "C" {
  21. # define ASDISPLAYNODE_EXTERN_C_END }
  22. #else
  23. # define ASDISPLAYNODE_EXTERN_C_BEGIN
  24. # define ASDISPLAYNODE_EXTERN_C_END
  25. #endif
  26. #ifdef __GNUC__
  27. # define ASDISPLAYNODE_GNUC(major, minor) \
  28. (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
  29. #else
  30. # define ASDISPLAYNODE_GNUC(major, minor) 0
  31. #endif
  32. #ifndef ASDISPLAYNODE_INLINE
  33. # if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
  34. # define ASDISPLAYNODE_INLINE static inline
  35. # elif defined (__MWERKS__) || defined (__cplusplus)
  36. # define ASDISPLAYNODE_INLINE static inline
  37. # elif ASDISPLAYNODE_GNUC (3, 0)
  38. # define ASDISPLAYNODE_INLINE static __inline__ __attribute__ ((always_inline))
  39. # else
  40. # define ASDISPLAYNODE_INLINE static
  41. # endif
  42. #endif
  43. #ifndef ASDISPLAYNODE_HIDDEN
  44. # if ASDISPLAYNODE_GNUC (4,0)
  45. # define ASDISPLAYNODE_HIDDEN __attribute__ ((visibility ("hidden")))
  46. # else
  47. # define ASDISPLAYNODE_HIDDEN /* no hidden */
  48. # endif
  49. #endif
  50. #ifndef ASDISPLAYNODE_PURE
  51. # if ASDISPLAYNODE_GNUC (3, 0)
  52. # define ASDISPLAYNODE_PURE __attribute__ ((pure))
  53. # else
  54. # define ASDISPLAYNODE_PURE /* no pure */
  55. # endif
  56. #endif
  57. #ifndef ASDISPLAYNODE_WARN_UNUSED
  58. # if ASDISPLAYNODE_GNUC (3, 4)
  59. # define ASDISPLAYNODE_WARN_UNUSED __attribute__ ((warn_unused_result))
  60. # else
  61. # define ASDISPLAYNODE_WARN_UNUSED /* no warn_unused */
  62. # endif
  63. #endif
  64. #ifndef ASDISPLAYNODE_WARN_DEPRECATED
  65. # define ASDISPLAYNODE_WARN_DEPRECATED 1
  66. #endif
  67. #ifndef ASDISPLAYNODE_DEPRECATED
  68. # if ASDISPLAYNODE_GNUC (3, 0) && ASDISPLAYNODE_WARN_DEPRECATED
  69. # define ASDISPLAYNODE_DEPRECATED __attribute__ ((deprecated))
  70. # else
  71. # define ASDISPLAYNODE_DEPRECATED
  72. # endif
  73. #endif
  74. #ifndef ASDISPLAYNODE_DEPRECATED_MSG
  75. # if ASDISPLAYNODE_GNUC (3, 0) && ASDISPLAYNODE_WARN_DEPRECATED
  76. # define ASDISPLAYNODE_DEPRECATED_MSG(msg) __deprecated_msg(msg)
  77. # else
  78. # define ASDISPLAYNODE_DEPRECATED_MSG(msg)
  79. # endif
  80. #endif
  81. #if defined (__cplusplus) && defined (__GNUC__)
  82. # define ASDISPLAYNODE_NOTHROW __attribute__ ((nothrow))
  83. #else
  84. # define ASDISPLAYNODE_NOTHROW
  85. #endif
  86. /**
  87. * The event backtraces take a static 2KB of memory
  88. * and retain all objects present in all the registers
  89. * of the stack frames. The memory consumption impact
  90. * is too significant even to be enabled during general
  91. * development.
  92. */
  93. #ifndef AS_SAVE_EVENT_BACKTRACES
  94. # define AS_SAVE_EVENT_BACKTRACES 0
  95. #endif
  96. #define ARRAY_COUNT(x) sizeof(x) / sizeof(x[0])
  97. #ifndef __has_feature // Optional.
  98. #define __has_feature(x) 0 // Compatibility with non-clang compilers.
  99. #endif
  100. #ifndef __has_attribute // Optional.
  101. #define __has_attribute(x) 0 // Compatibility with non-clang compilers.
  102. #endif
  103. #ifndef NS_CONSUMED
  104. #if __has_feature(attribute_ns_consumed)
  105. #define NS_CONSUMED __attribute__((ns_consumed))
  106. #else
  107. #define NS_CONSUMED
  108. #endif
  109. #endif
  110. #ifndef NS_RETURNS_RETAINED
  111. #if __has_feature(attribute_ns_returns_retained)
  112. #define NS_RETURNS_RETAINED __attribute__((ns_returns_retained))
  113. #else
  114. #define NS_RETURNS_RETAINED
  115. #endif
  116. #endif
  117. #ifndef CF_RETURNS_RETAINED
  118. #if __has_feature(attribute_cf_returns_retained)
  119. #define CF_RETURNS_RETAINED __attribute__((cf_returns_retained))
  120. #else
  121. #define CF_RETURNS_RETAINED
  122. #endif
  123. #endif
  124. #ifndef ASDISPLAYNODE_NOT_DESIGNATED_INITIALIZER
  125. #define ASDISPLAYNODE_NOT_DESIGNATED_INITIALIZER() \
  126. do { \
  127. NSAssert2(NO, @"%@ is not the designated initializer for instances of %@.", NSStringFromSelector(_cmd), NSStringFromClass([self class])); \
  128. return nil; \
  129. } while (0)
  130. #endif // ASDISPLAYNODE_NOT_DESIGNATED_INITIALIZER
  131. // It's hard to pass quoted strings via xcodebuild preprocessor define arguments, so we'll convert
  132. // the preprocessor values to strings here.
  133. //
  134. // It takes two steps to do this in gcc as per
  135. // http://gcc.gnu.org/onlinedocs/cpp/Stringification.html
  136. #define ASDISPLAYNODE_TO_STRING(str) #str
  137. #define ASDISPLAYNODE_TO_UNICODE_STRING(str) @ASDISPLAYNODE_TO_STRING(str)
  138. #ifndef ASDISPLAYNODE_REQUIRES_SUPER
  139. #if __has_attribute(objc_requires_super)
  140. #define ASDISPLAYNODE_REQUIRES_SUPER __attribute__((objc_requires_super))
  141. #else
  142. #define ASDISPLAYNODE_REQUIRES_SUPER
  143. #endif
  144. #endif
  145. #ifndef AS_UNAVAILABLE
  146. #if __has_attribute(unavailable)
  147. #define AS_UNAVAILABLE(message) __attribute__((unavailable(message)))
  148. #else
  149. #define AS_UNAVAILABLE(message)
  150. #endif
  151. #endif
  152. #ifndef AS_WARN_UNUSED_RESULT
  153. #if __has_attribute(warn_unused_result)
  154. #define AS_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
  155. #else
  156. #define AS_WARN_UNUSED_RESULT
  157. #endif
  158. #endif
  159. #define ASOVERLOADABLE __attribute__((overloadable))
  160. #if __has_attribute(noescape)
  161. #define AS_NOESCAPE __attribute__((noescape))
  162. #else
  163. #define AS_NOESCAPE
  164. #endif
  165. #if __has_attribute(objc_subclassing_restricted)
  166. #define AS_SUBCLASSING_RESTRICTED __attribute__((objc_subclassing_restricted))
  167. #else
  168. #define AS_SUBCLASSING_RESTRICTED
  169. #endif
  170. /// Ensure that class is of certain kind
  171. #define ASDynamicCast(x, c) ({ \
  172. id __val = x;\
  173. ((c *) ([__val isKindOfClass:[c class]] ? __val : nil));\
  174. })