CCPlatformMacros.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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;\
  120. public: virtual varType get##funName(void) const;
  121. #define CC_PROPERTY_READONLY_PASS_BY_REF(varType, varName, funName)\
  122. protected: varType varName;\
  123. public: virtual const varType& get##funName(void) const;
  124. /** @def CC_PROPERTY
  125. * It is used to declare a protected variable.
  126. * We can use getter to read the variable, and use the setter to change the variable.
  127. *
  128. * @param varType The type of variable.
  129. * @param varName Variable name.
  130. * @param funName "get + funName" will be the name of the getter.
  131. * "set + funName" will be the name of the setter.
  132. * @warning The getter and setter are public virtual functions, you should rewrite them first.
  133. * The variables and methods declared after CC_PROPERTY are all public.
  134. * If you need protected or private, please declare.
  135. */
  136. #define CC_PROPERTY(varType, varName, funName)\
  137. protected: varType varName;\
  138. public: virtual varType get##funName(void) const;\
  139. public: virtual void set##funName(varType var);
  140. #define CC_PROPERTY_PASS_BY_REF(varType, varName, funName)\
  141. protected: varType varName;\
  142. public: virtual const varType& get##funName(void) const;\
  143. public: virtual void set##funName(const varType& var);
  144. /** @def CC_SYNTHESIZE_READONLY
  145. * It is used to declare a protected variable. We can use getter to read the variable.
  146. *
  147. * @param varType The type of variable.
  148. * @param varName Variable name.
  149. * @param funName "get + funName" will be the name of the getter.
  150. * @warning The getter is a public inline function.
  151. * The variables and methods declared after CC_SYNTHESIZE_READONLY are all public.
  152. * If you need protected or private, please declare.
  153. */
  154. #define CC_SYNTHESIZE_READONLY(varType, varName, funName)\
  155. protected: varType varName;\
  156. public: virtual varType get##funName(void) const { return varName; }
  157. #define CC_SYNTHESIZE_READONLY_PASS_BY_REF(varType, varName, funName)\
  158. protected: varType varName;\
  159. public: virtual const varType& get##funName(void) const { return varName; }
  160. /** @def CC_SYNTHESIZE
  161. * It is used to declare a protected variable.
  162. * We can use getter to read the variable, and use the setter to change the variable.
  163. *
  164. * @param varType The type of variable.
  165. * @param varName Variable name.
  166. * @param funName "get + funName" will be the name of the getter.
  167. * "set + funName" will be the name of the setter.
  168. * @warning The getter and setter are public inline functions.
  169. * The variables and methods declared after CC_SYNTHESIZE are all public.
  170. * If you need protected or private, please declare.
  171. */
  172. #define CC_SYNTHESIZE(varType, varName, funName)\
  173. protected: varType varName;\
  174. public: virtual varType get##funName(void) const { return varName; }\
  175. public: virtual void set##funName(varType var){ varName = var; }
  176. #define CC_SYNTHESIZE_PASS_BY_REF(varType, varName, funName)\
  177. protected: varType varName;\
  178. public: virtual const varType& get##funName(void) const { return varName; }\
  179. public: virtual void set##funName(const varType& var){ varName = var; }
  180. #define CC_SYNTHESIZE_RETAIN(varType, varName, funName) \
  181. private: varType varName; \
  182. public: virtual varType get##funName(void) const { return varName; } \
  183. public: virtual void set##funName(varType var) \
  184. { \
  185. if (varName != var) \
  186. { \
  187. CC_SAFE_RETAIN(var); \
  188. CC_SAFE_RELEASE(varName); \
  189. varName = var; \
  190. } \
  191. }
  192. #define CC_SAFE_DELETE(p) do { delete (p); (p) = nullptr; } while(0)
  193. #define CC_SAFE_DELETE_ARRAY(p) do { if(p) { delete[] (p); (p) = nullptr; } } while(0)
  194. #define CC_SAFE_FREE(p) do { if(p) { free(p); (p) = nullptr; } } while(0)
  195. #define CC_SAFE_RELEASE(p) do { if(p) { (p)->release(); } } while(0)
  196. #define CC_SAFE_RELEASE_NULL(p) do { if(p) { (p)->release(); (p) = nullptr; } } while(0)
  197. #define CC_SAFE_RETAIN(p) do { if(p) { (p)->retain(); } } while(0)
  198. #define CC_BREAK_IF(cond) if(cond) break
  199. #define __CCLOGWITHFUNCTION(s, ...) \
  200. cocos2d::log("%s : %s",__FUNCTION__, cocos2d::StringUtils::format(s, ##__VA_ARGS__).c_str())
  201. /// @name Cocos2d debug
  202. /// @{
  203. #if !defined(COCOS2D_DEBUG) || COCOS2D_DEBUG == 0
  204. #define CCLOG(...) do {} while (0)
  205. #define CCLOGINFO(...) do {} while (0)
  206. #define CCLOGERROR(...) do {} while (0)
  207. #define CCLOGWARN(...) do {} while (0)
  208. #elif COCOS2D_DEBUG == 1
  209. #define CCLOG(format, ...) cocos2d::log(format, ##__VA_ARGS__)
  210. #define CCLOGERROR(format,...) cocos2d::log(format, ##__VA_ARGS__)
  211. #define CCLOGINFO(format,...) do {} while (0)
  212. #define CCLOGWARN(...) __CCLOGWITHFUNCTION(__VA_ARGS__)
  213. #elif COCOS2D_DEBUG > 1
  214. #define CCLOG(format, ...) cocos2d::log(format, ##__VA_ARGS__)
  215. #define CCLOGERROR(format,...) cocos2d::log(format, ##__VA_ARGS__)
  216. #define CCLOGINFO(format,...) cocos2d::log(format, ##__VA_ARGS__)
  217. #define CCLOGWARN(...) __CCLOGWITHFUNCTION(__VA_ARGS__)
  218. #endif // COCOS2D_DEBUG
  219. /** Lua engine debug */
  220. #if !defined(COCOS2D_DEBUG) || COCOS2D_DEBUG == 0 || CC_LUA_ENGINE_DEBUG == 0
  221. #define LUALOG(...)
  222. #else
  223. #define LUALOG(format, ...) cocos2d::log(format, ##__VA_ARGS__)
  224. #endif // Lua engine debug
  225. // end of debug group
  226. /// @}
  227. /** @def CC_DISALLOW_COPY_AND_ASSIGN(TypeName)
  228. * A macro to disallow the copy constructor and operator= functions.
  229. * This should be used in the private: declarations for a class
  230. */
  231. #if defined(__GNUC__) && ((__GNUC__ >= 5) || ((__GNUG__ == 4) && (__GNUC_MINOR__ >= 4))) \
  232. || (defined(__clang__) && (__clang_major__ >= 3)) || (_MSC_VER >= 1800)
  233. #define CC_DISALLOW_COPY_AND_ASSIGN(TypeName) \
  234. TypeName(const TypeName &) = delete; \
  235. TypeName &operator =(const TypeName &) = delete;
  236. #else
  237. #define CC_DISALLOW_COPY_AND_ASSIGN(TypeName) \
  238. TypeName(const TypeName &); \
  239. TypeName &operator =(const TypeName &);
  240. #endif
  241. /** @def CC_DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName)
  242. * A macro to disallow all the implicit constructors, namely the
  243. * default constructor, copy constructor and operator= functions.
  244. *
  245. * This should be used in the private: declarations for a class
  246. * that wants to prevent anyone from instantiating it. This is
  247. * especially useful for classes containing only static methods.
  248. */
  249. #define CC_DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
  250. TypeName(); \
  251. CC_DISALLOW_COPY_AND_ASSIGN(TypeName)
  252. /** @def CC_DEPRECATED_ATTRIBUTE
  253. * Only certain compilers support __attribute__((deprecated)).
  254. */
  255. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  256. #define CC_DEPRECATED_ATTRIBUTE __attribute__((deprecated))
  257. #elif _MSC_VER >= 1400 //vs 2005 or higher
  258. #define CC_DEPRECATED_ATTRIBUTE __declspec(deprecated)
  259. #else
  260. #define CC_DEPRECATED_ATTRIBUTE
  261. #endif
  262. /** @def CC_DEPRECATED(...)
  263. * Macro to mark things deprecated as of a particular version
  264. * can be used with arbitrary parameters which are thrown away.
  265. * e.g. CC_DEPRECATED(4.0) or CC_DEPRECATED(4.0, "not going to need this anymore") etc.
  266. */
  267. #define CC_DEPRECATED(...) CC_DEPRECATED_ATTRIBUTE
  268. /** @def CC_FORMAT_PRINTF(formatPos, argPos)
  269. * Only certain compiler support __attribute__((format))
  270. *
  271. * @param formatPos 1-based position of format string argument.
  272. * @param argPos 1-based position of first format-dependent argument.
  273. */
  274. #if defined(__GNUC__) && (__GNUC__ >= 4)
  275. #define CC_FORMAT_PRINTF(formatPos, argPos) __attribute__((__format__(printf, formatPos, argPos)))
  276. #elif defined(__has_attribute)
  277. #if __has_attribute(format)
  278. #define CC_FORMAT_PRINTF(formatPos, argPos) __attribute__((__format__(printf, formatPos, argPos)))
  279. #endif // __has_attribute(format)
  280. #else
  281. #define CC_FORMAT_PRINTF(formatPos, argPos)
  282. #endif
  283. #if defined(_MSC_VER)
  284. #define CC_FORMAT_PRINTF_SIZE_T "%08lX"
  285. #else
  286. #define CC_FORMAT_PRINTF_SIZE_T "%08zX"
  287. #endif
  288. #ifdef __GNUC__
  289. #define CC_UNUSED __attribute__ ((unused))
  290. #else
  291. #define CC_UNUSED
  292. #endif
  293. /** @def CC_REQUIRES_NULL_TERMINATION
  294. *
  295. */
  296. #if !defined(CC_REQUIRES_NULL_TERMINATION)
  297. #if defined(__APPLE_CC__) && (__APPLE_CC__ >= 5549)
  298. #define CC_REQUIRES_NULL_TERMINATION __attribute__((sentinel(0,1)))
  299. #elif defined(__GNUC__)
  300. #define CC_REQUIRES_NULL_TERMINATION __attribute__((sentinel))
  301. #else
  302. #define CC_REQUIRES_NULL_TERMINATION
  303. #endif
  304. #endif
  305. #endif // __CC_PLATFORM_MACROS_H__