CCPlatformMacros.h 13 KB

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