CCPlatformMacros.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2017 Chukong Technologies
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #ifndef __CC_PLATFORM_MACROS_H__
  23. #define __CC_PLATFORM_MACROS_H__
  24. /**
  25. * Define some platform specific macros.
  26. */
  27. #include "base/ccConfig.h"
  28. #include "platform/CCPlatformConfig.h"
  29. #include "platform/CCPlatformDefine.h"
  30. /** @def CREATE_FUNC(__TYPE__)
  31. * Define a create function for a specific type, such as Layer.
  32. *
  33. * @param __TYPE__ class type to add create(), such as Layer.
  34. */
  35. #define CREATE_FUNC(__TYPE__) \
  36. static __TYPE__* create() \
  37. { \
  38. __TYPE__ *pRet = new(std::nothrow) __TYPE__(); \
  39. if (pRet && pRet->init()) \
  40. { \
  41. pRet->autorelease(); \
  42. return pRet; \
  43. } \
  44. else \
  45. { \
  46. delete pRet; \
  47. pRet = nullptr; \
  48. return nullptr; \
  49. } \
  50. }
  51. /** @def NODE_FUNC(__TYPE__)
  52. * Define a node function for a specific type, such as Layer.
  53. *
  54. * @param __TYPE__ class type to add node(), such as Layer.
  55. * @deprecated This interface will be deprecated sooner or later.
  56. */
  57. #define NODE_FUNC(__TYPE__) \
  58. CC_DEPRECATED_ATTRIBUTE static __TYPE__* node() \
  59. { \
  60. __TYPE__ *pRet = new(std::nothrow) __TYPE__(); \
  61. if (pRet && pRet->init()) \
  62. { \
  63. pRet->autorelease(); \
  64. return pRet; \
  65. } \
  66. else \
  67. { \
  68. delete pRet; \
  69. pRet = NULL; \
  70. return NULL; \
  71. } \
  72. }
  73. /** @def CC_ENABLE_CACHE_TEXTURE_DATA
  74. * Enable it if you want to cache the texture data.
  75. * Not enabling for Emscripten any more -- doesn't seem necessary and don't want
  76. * to be different from other platforms unless there's a good reason.
  77. *
  78. * @since v0.99.5
  79. */
  80. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  81. #define CC_ENABLE_CACHE_TEXTURE_DATA 1
  82. #else
  83. #define CC_ENABLE_CACHE_TEXTURE_DATA 0
  84. #endif
  85. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_EMSCRIPTEN)
  86. /** Application will crash in glDrawElements function on some win32 computers and some android devices.
  87. * Indices should be bound again while drawing to avoid this bug.
  88. */
  89. #define CC_REBIND_INDICES_BUFFER 1
  90. #else
  91. #define CC_REBIND_INDICES_BUFFER 0
  92. #endif
  93. // Generic macros
  94. /// @name namespace cocos2d
  95. /// @{
  96. #ifdef __cplusplus
  97. #define NS_CC_BEGIN namespace cocos2d {
  98. #define NS_CC_END }
  99. #define USING_NS_CC using namespace cocos2d
  100. #define NS_CC ::cocos2d
  101. #else
  102. #define NS_CC_BEGIN
  103. #define NS_CC_END
  104. #define USING_NS_CC
  105. #define NS_CC
  106. #endif
  107. // end of namespace group
  108. /// @}
  109. /** @def CC_PROPERTY_READONLY
  110. * It is used to declare a protected variable. We can use getter to read the variable.
  111. *
  112. * @param varType the type of variable.
  113. * @param varName variable name.
  114. * @param funName "get + funName" will be the name of the getter.
  115. * @warning The getter is a public virtual function, you should rewrite it first.
  116. * The variables and methods declared after CC_PROPERTY_READONLY are all public.
  117. * If you need protected or private, please declare.
  118. */
  119. #define CC_PROPERTY_READONLY(varType, varName, funName)\
  120. protected: varType varName; public: virtual varType get##funName(void) const;
  121. #define CC_PROPERTY_READONLY_PASS_BY_REF(varType, varName, funName)\
  122. protected: varType varName; public: virtual const varType& get##funName(void) const;
  123. /** @def CC_PROPERTY
  124. * It is used to declare a protected variable.
  125. * We can use getter to read the variable, and use the setter to change the variable.
  126. *
  127. * @param varType The type of variable.
  128. * @param varName Variable name.
  129. * @param funName "get + funName" will be the name of the getter.
  130. * "set + funName" will be the name of the setter.
  131. * @warning The getter and setter are public virtual functions, you should rewrite them first.
  132. * The variables and methods declared after CC_PROPERTY are all public.
  133. * If you need protected or private, please declare.
  134. */
  135. #define CC_PROPERTY(varType, varName, funName)\
  136. protected: varType varName; public: virtual varType get##funName(void) const; virtual void set##funName(varType var);
  137. #define CC_PROPERTY_PASS_BY_REF(varType, varName, funName)\
  138. protected: varType varName; public: virtual const varType& get##funName(void) const; virtual void set##funName(const varType& var);
  139. /** @def CC_SYNTHESIZE_READONLY
  140. * It is used to declare a protected variable. We can use getter to read the variable.
  141. *
  142. * @param varType The type of variable.
  143. * @param varName Variable name.
  144. * @param funName "get + funName" will be the name of the getter.
  145. * @warning The getter is a public inline function.
  146. * The variables and methods declared after CC_SYNTHESIZE_READONLY are all public.
  147. * If you need protected or private, please declare.
  148. */
  149. #define CC_SYNTHESIZE_READONLY(varType, varName, funName)\
  150. protected: varType varName; public: virtual inline varType get##funName(void) const { return varName; }
  151. #define CC_SYNTHESIZE_READONLY_PASS_BY_REF(varType, varName, funName)\
  152. protected: varType varName; public: virtual inline const varType& get##funName(void) const { return varName; }
  153. /** @def CC_SYNTHESIZE
  154. * It is used to declare a protected variable.
  155. * We can use getter to read the variable, and use the setter to change the variable.
  156. *
  157. * @param varType The type of variable.
  158. * @param varName Variable name.
  159. * @param funName "get + funName" will be the name of the getter.
  160. * "set + funName" will be the name of the setter.
  161. * @warning The getter and setter are public inline functions.
  162. * The variables and methods declared after CC_SYNTHESIZE are all public.
  163. * If you need protected or private, please declare.
  164. */
  165. #define CC_SYNTHESIZE(varType, varName, funName)\
  166. protected: varType varName; public: virtual inline varType get##funName(void) const { return varName; } virtual inline void set##funName(varType var){ varName = var; }
  167. #define CC_SYNTHESIZE_PASS_BY_REF(varType, varName, funName)\
  168. protected: varType varName; public: virtual inline const varType& get##funName(void) const { return varName; } virtual inline void set##funName(const varType& var){ varName = var; }
  169. #define CC_SYNTHESIZE_RETAIN(varType, varName, funName) \
  170. private: varType varName; public: virtual inline varType get##funName(void) const { return varName; } virtual inline void set##funName(varType var) \
  171. { \
  172. if (varName != var) \
  173. { \
  174. CC_SAFE_RETAIN(var); \
  175. CC_SAFE_RELEASE(varName); \
  176. varName = var; \
  177. } \
  178. }
  179. #define CC_SAFE_DELETE(p) do { delete (p); (p) = nullptr; } while(0)
  180. #define CC_SAFE_DELETE_ARRAY(p) do { if(p) { delete[] (p); (p) = nullptr; } } while(0)
  181. #define CC_SAFE_FREE(p) do { if(p) { free(p); (p) = nullptr; } } while(0)
  182. #define CC_SAFE_RELEASE(p) do { if(p) { (p)->release(); } } while(0)
  183. #define CC_SAFE_RELEASE_NULL(p) do { if(p) { (p)->release(); (p) = nullptr; } } while(0)
  184. #define CC_SAFE_RETAIN(p) do { if(p) { (p)->retain(); } } while(0)
  185. #define CC_BREAK_IF(cond) if(cond) break
  186. #define __CCLOGWITHFUNCTION(s, ...) \
  187. cocos2d::log("%s : %s",__FUNCTION__, cocos2d::StringUtils::format(s, ##__VA_ARGS__).c_str())
  188. /// @name Cocos2d debug
  189. /// @{
  190. #if !defined(COCOS2D_DEBUG) || COCOS2D_DEBUG == 0
  191. #define CCLOG(...) do {} while (0)
  192. #define CCLOGINFO(...) do {} while (0)
  193. #define CCLOGERROR(...) do {} while (0)
  194. #define CCLOGWARN(...) do {} while (0)
  195. #elif COCOS2D_DEBUG == 1
  196. #define CCLOG(format, ...) cocos2d::log(format, ##__VA_ARGS__)
  197. #define CCLOGERROR(format,...) cocos2d::log(format, ##__VA_ARGS__)
  198. #define CCLOGINFO(format,...) do {} while (0)
  199. #define CCLOGWARN(...) __CCLOGWITHFUNCTION(__VA_ARGS__)
  200. #elif COCOS2D_DEBUG > 1
  201. #define CCLOG(format, ...) cocos2d::log(format, ##__VA_ARGS__)
  202. #define CCLOGERROR(format,...) cocos2d::log(format, ##__VA_ARGS__)
  203. #define CCLOGINFO(format,...) cocos2d::log(format, ##__VA_ARGS__)
  204. #define CCLOGWARN(...) __CCLOGWITHFUNCTION(__VA_ARGS__)
  205. #endif // COCOS2D_DEBUG
  206. /** Lua engine debug */
  207. #if !defined(COCOS2D_DEBUG) || COCOS2D_DEBUG == 0 || CC_LUA_ENGINE_DEBUG == 0
  208. #define LUALOG(...)
  209. #else
  210. #define LUALOG(format, ...) cocos2d::log(format, ##__VA_ARGS__)
  211. #endif // Lua engine debug
  212. // end of debug group
  213. /// @}
  214. /** @def CC_DISALLOW_COPY_AND_ASSIGN(TypeName)
  215. * A macro to disallow the copy constructor and operator= functions.
  216. * This should be used in the private: declarations for a class
  217. */
  218. #if defined(__GNUC__) && ((__GNUC__ >= 5) || ((__GNUG__ == 4) && (__GNUC_MINOR__ >= 4))) \
  219. || (defined(__clang__) && (__clang_major__ >= 3)) || (_MSC_VER >= 1800)
  220. #define CC_DISALLOW_COPY_AND_ASSIGN(TypeName) \
  221. TypeName(const TypeName &) = delete; \
  222. TypeName &operator =(const TypeName &) = delete;
  223. #else
  224. #define CC_DISALLOW_COPY_AND_ASSIGN(TypeName) \
  225. TypeName(const TypeName &); \
  226. TypeName &operator =(const TypeName &);
  227. #endif
  228. /** @def CC_DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName)
  229. * A macro to disallow all the implicit constructors, namely the
  230. * default constructor, copy constructor and operator= functions.
  231. *
  232. * This should be used in the private: declarations for a class
  233. * that wants to prevent anyone from instantiating it. This is
  234. * especially useful for classes containing only static methods.
  235. */
  236. #define CC_DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
  237. TypeName(); \
  238. CC_DISALLOW_COPY_AND_ASSIGN(TypeName)
  239. /** @def CC_DEPRECATED_ATTRIBUTE
  240. * Only certain compilers support __attribute__((deprecated)).
  241. */
  242. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  243. #define CC_DEPRECATED_ATTRIBUTE __attribute__((deprecated))
  244. #elif _MSC_VER >= 1400 //vs 2005 or higher
  245. #define CC_DEPRECATED_ATTRIBUTE __declspec(deprecated)
  246. #else
  247. #define CC_DEPRECATED_ATTRIBUTE
  248. #endif
  249. /** @def CC_DEPRECATED(...)
  250. * Macro to mark things deprecated as of a particular version
  251. * can be used with arbitrary parameters which are thrown away.
  252. * e.g. CC_DEPRECATED(4.0) or CC_DEPRECATED(4.0, "not going to need this anymore") etc.
  253. */
  254. #define CC_DEPRECATED(...) CC_DEPRECATED_ATTRIBUTE
  255. /** @def CC_FORMAT_PRINTF(formatPos, argPos)
  256. * Only certain compiler support __attribute__((format))
  257. *
  258. * @param formatPos 1-based position of format string argument.
  259. * @param argPos 1-based position of first format-dependent argument.
  260. */
  261. #if defined(__GNUC__) && (__GNUC__ >= 4)
  262. #define CC_FORMAT_PRINTF(formatPos, argPos) __attribute__((__format__(printf, formatPos, argPos)))
  263. #elif defined(__has_attribute)
  264. #if __has_attribute(format)
  265. #define CC_FORMAT_PRINTF(formatPos, argPos) __attribute__((__format__(printf, formatPos, argPos)))
  266. #endif // __has_attribute(format)
  267. #else
  268. #define CC_FORMAT_PRINTF(formatPos, argPos)
  269. #endif
  270. #if defined(_MSC_VER)
  271. #define CC_FORMAT_PRINTF_SIZE_T "%08lX"
  272. #else
  273. #define CC_FORMAT_PRINTF_SIZE_T "%08zX"
  274. #endif
  275. #ifdef __GNUC__
  276. #define CC_UNUSED __attribute__ ((unused))
  277. #else
  278. #define CC_UNUSED
  279. #endif
  280. /** @def CC_REQUIRES_NULL_TERMINATION
  281. *
  282. */
  283. #if !defined(CC_REQUIRES_NULL_TERMINATION)
  284. #if defined(__APPLE_CC__) && (__APPLE_CC__ >= 5549)
  285. #define CC_REQUIRES_NULL_TERMINATION __attribute__((sentinel(0,1)))
  286. #elif defined(__GNUC__)
  287. #define CC_REQUIRES_NULL_TERMINATION __attribute__((sentinel))
  288. #else
  289. #define CC_REQUIRES_NULL_TERMINATION
  290. #endif
  291. #endif
  292. #endif // __CC_PLATFORM_MACROS_H__