axmol/core/platform/PlatformMacros.h

453 lines
18 KiB
C
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2017 Chukong Technologies
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
2021-10-06 18:45:58 +08:00
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
2022-07-16 10:43:05 +08:00
#ifndef __AX_PLATFORM_MACROS_H__
#define __AX_PLATFORM_MACROS_H__
2019-11-23 20:27:39 +08:00
/**
* Define some platform specific macros.
*/
#include "base/Config.h"
#include "base/hlookup.h"
#include "platform/PlatformConfig.h"
#include "platform/PlatformDefine.h"
2019-11-23 20:27:39 +08:00
/** @def CREATE_FUNC(__TYPE__)
* Define a create function for a specific type, such as Layer.
*
* @param __TYPE__ class type to add create(), such as Layer.
*/
2021-12-25 10:04:45 +08:00
#define CREATE_FUNC(__TYPE__) \
static __TYPE__* create() \
{ \
__TYPE__* pRet = new __TYPE__(); \
if (pRet->init()) \
{ \
pRet->autorelease(); \
return pRet; \
} \
else \
{ \
delete pRet; \
pRet = nullptr; \
return nullptr; \
} \
}
2019-11-23 20:27:39 +08:00
/** @def NODE_FUNC(__TYPE__)
* Define a node function for a specific type, such as Layer.
*
* @param __TYPE__ class type to add node(), such as Layer.
* @deprecated This interface will be deprecated sooner or later.
*/
2021-12-25 10:04:45 +08:00
#define NODE_FUNC(__TYPE__) \
2022-07-16 10:43:05 +08:00
AX_DEPRECATED_ATTRIBUTE static __TYPE__* node() \
2021-12-25 10:04:45 +08:00
{ \
__TYPE__* pRet = new __TYPE__(); \
if (pRet->init()) \
{ \
pRet->autorelease(); \
return pRet; \
} \
else \
{ \
delete pRet; \
pRet = NULL; \
return NULL; \
} \
}
2019-11-23 20:27:39 +08:00
2022-07-16 10:43:05 +08:00
/** @def AX_ENABLE_CACHE_TEXTURE_DATA
2019-11-23 20:27:39 +08:00
* Enable it if you want to cache the texture data.
* Not enabling for Emscripten any more -- doesn't seem necessary and don't want
* to be different from other platforms unless there's a good reason.
2021-12-25 10:04:45 +08:00
*
2019-11-23 20:27:39 +08:00
* @since v0.99.5
*/
2022-07-16 10:43:05 +08:00
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
# define AX_ENABLE_CACHE_TEXTURE_DATA 1
2019-11-23 20:27:39 +08:00
#else
2022-07-16 10:43:05 +08:00
# define AX_ENABLE_CACHE_TEXTURE_DATA 0
2019-11-23 20:27:39 +08:00
#endif
/** @def AX_ENABLE_RESTART_APPLICATION_ON_CONTEXT_LOST
* Enable this if application should be restarted after the OpenGL context is lost
*
*/
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID && !AX_ENABLE_CACHE_TEXTURE_DATA)
# define AX_ENABLE_RESTART_APPLICATION_ON_CONTEXT_LOST 1
#else
# define AX_ENABLE_RESTART_APPLICATION_ON_CONTEXT_LOST 0
#endif
2022-07-16 10:43:05 +08:00
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID) || (AX_TARGET_PLATFORM == AX_PLATFORM_WIN32)
2021-12-25 10:04:45 +08:00
/** Application will crash in glDrawElements function on some win32 computers and some android devices.
* Indices should be bound again while drawing to avoid this bug.
*/
2022-07-16 10:43:05 +08:00
# define AX_REBIND_INDICES_BUFFER 1
2019-11-23 20:27:39 +08:00
#else
2022-07-16 10:43:05 +08:00
# define AX_REBIND_INDICES_BUFFER 0
2019-11-23 20:27:39 +08:00
#endif
// Generic macros
2022-07-16 10:43:05 +08:00
/** @def AX_PROPERTY_READONLY
2019-11-23 20:27:39 +08:00
* It is used to declare a protected variable. We can use getter to read the variable.
2021-12-25 10:04:45 +08:00
*
2019-11-23 20:27:39 +08:00
* @param varType the type of variable.
* @param varName variable name.
* @param funName "get + funName" will be the name of the getter.
* @warning The getter is a public virtual function, you should rewrite it first.
2022-07-16 10:43:05 +08:00
* The variables and methods declared after AX_PROPERTY_READONLY are all public.
2019-11-23 20:27:39 +08:00
* If you need protected or private, please declare.
*/
2022-07-16 10:43:05 +08:00
#define AX_PROPERTY_READONLY(varType, varName, funName) \
2021-12-25 10:04:45 +08:00
protected: \
varType varName; \
\
public: \
virtual varType get##funName() const;
2022-07-16 10:43:05 +08:00
#define AX_PROPERTY_READONLY_PASS_BY_REF(varType, varName, funName) \
2021-12-25 10:04:45 +08:00
protected: \
varType varName; \
\
public: \
virtual const varType& get##funName() const;
2022-07-16 10:43:05 +08:00
/** @def AX_PROPERTY
2019-11-23 20:27:39 +08:00
* It is used to declare a protected variable.
* We can use getter to read the variable, and use the setter to change the variable.
*
* @param varType The type of variable.
* @param varName Variable name.
* @param funName "get + funName" will be the name of the getter.
* "set + funName" will be the name of the setter.
* @warning The getter and setter are public virtual functions, you should rewrite them first.
2022-07-16 10:43:05 +08:00
* The variables and methods declared after AX_PROPERTY are all public.
2019-11-23 20:27:39 +08:00
* If you need protected or private, please declare.
*/
2022-07-16 10:43:05 +08:00
#define AX_PROPERTY(varType, varName, funName) \
2021-12-25 10:04:45 +08:00
protected: \
varType varName; \
\
public: \
virtual varType get##funName() const; \
virtual void set##funName(varType var);
2022-07-16 10:43:05 +08:00
#define AX_PROPERTY_PASS_BY_REF(varType, varName, funName) \
2021-12-25 10:04:45 +08:00
protected: \
varType varName; \
\
public: \
virtual const varType& get##funName() const; \
virtual void set##funName(const varType& var);
2022-07-16 10:43:05 +08:00
/** @def AX_SYNTHESIZE_READONLY
2019-11-23 20:27:39 +08:00
* It is used to declare a protected variable. We can use getter to read the variable.
*
* @param varType The type of variable.
* @param varName Variable name.
* @param funName "get + funName" will be the name of the getter.
* @warning The getter is a public inline function.
2022-07-16 10:43:05 +08:00
* The variables and methods declared after AX_SYNTHESIZE_READONLY are all public.
2019-11-23 20:27:39 +08:00
* If you need protected or private, please declare.
*/
2022-07-16 10:43:05 +08:00
#define AX_SYNTHESIZE_READONLY(varType, varName, funName) \
2021-12-25 10:04:45 +08:00
protected: \
varType varName; \
\
public: \
virtual inline varType get##funName() const { return varName; }
2022-07-16 10:43:05 +08:00
#define AX_SYNTHESIZE_READONLY_PASS_BY_REF(varType, varName, funName) \
2021-12-25 10:04:45 +08:00
protected: \
varType varName; \
\
public: \
virtual inline const varType& get##funName() const { return varName; }
2022-07-16 10:43:05 +08:00
/** @def AX_SYNTHESIZE
2019-11-23 20:27:39 +08:00
* It is used to declare a protected variable.
* We can use getter to read the variable, and use the setter to change the variable.
*
* @param varType The type of variable.
* @param varName Variable name.
* @param funName "get + funName" will be the name of the getter.
* "set + funName" will be the name of the setter.
* @warning The getter and setter are public inline functions.
2022-07-16 10:43:05 +08:00
* The variables and methods declared after AX_SYNTHESIZE are all public.
2019-11-23 20:27:39 +08:00
* If you need protected or private, please declare.
*/
2022-07-16 10:43:05 +08:00
#define AX_SYNTHESIZE(varType, varName, funName) \
2021-12-25 10:04:45 +08:00
protected: \
varType varName; \
\
public: \
virtual inline varType get##funName() const { return varName; } \
virtual inline void set##funName(varType var) { varName = var; }
2022-07-16 10:43:05 +08:00
#define AX_SYNTHESIZE_PASS_BY_REF(varType, varName, funName) \
2021-12-25 10:04:45 +08:00
protected: \
varType varName; \
\
public: \
virtual inline const varType& get##funName() const { return varName; } \
virtual inline void set##funName(const varType& var) { varName = var; }
2022-07-16 10:43:05 +08:00
#define AX_SYNTHESIZE_RETAIN(varType, varName, funName) \
2021-12-25 10:04:45 +08:00
private: \
varType varName; \
\
public: \
virtual inline varType get##funName() const { return varName; } \
virtual inline void set##funName(varType var) \
{ \
if (varName != var) \
{ \
2022-07-16 10:43:05 +08:00
AX_SAFE_RETAIN(var); \
AX_SAFE_RELEASE(varName); \
2021-12-25 10:04:45 +08:00
varName = var; \
} \
}
2022-07-16 10:43:05 +08:00
#define AX_SAFE_DELETE(p) \
2021-12-25 10:04:45 +08:00
do \
{ \
delete (p); \
(p) = nullptr; \
} while (0)
2022-07-16 10:43:05 +08:00
#define AX_SAFE_DELETE_ARRAY(p) \
2021-12-25 10:04:45 +08:00
do \
{ \
if (p) \
{ \
delete[](p); \
(p) = nullptr; \
} \
} while (0)
2022-07-16 10:43:05 +08:00
#define AX_SAFE_FREE(p) \
2021-12-25 10:04:45 +08:00
do \
{ \
if (p) \
{ \
free(p); \
(p) = nullptr; \
} \
} while (0)
2022-07-16 10:43:05 +08:00
#define AX_SAFE_RELEASE(p) \
2021-12-25 10:04:45 +08:00
do \
{ \
if (p) \
{ \
(p)->release(); \
} \
} while (0)
2022-07-16 10:43:05 +08:00
#define AX_SAFE_RELEASE_NULL(p) \
2021-12-25 10:04:45 +08:00
do \
{ \
if (p) \
{ \
(p)->release(); \
(p) = nullptr; \
} \
} while (0)
2022-07-16 10:43:05 +08:00
#define AX_SAFE_RETAIN(p) \
2021-12-25 10:04:45 +08:00
do \
{ \
if (p) \
{ \
(p)->retain(); \
} \
} while (0)
2022-07-16 10:43:05 +08:00
#define AX_BREAK_IF(cond) \
2021-12-25 10:04:45 +08:00
if (cond) \
break
2019-11-23 20:27:39 +08:00
2022-07-16 10:43:05 +08:00
#define __AXLOGWITHFUNCTION(s, ...) \
ax::print("%s : %s", __FUNCTION__, ax::StringUtils::format(s, ##__VA_ARGS__).c_str())
2019-11-23 20:27:39 +08:00
/// @name Cocos2d debug
/// @{
2022-08-08 18:02:17 +08:00
#if !defined(_AX_DEBUG) || _AX_DEBUG == 0
2022-07-16 10:43:05 +08:00
# define AXLOG(...) \
2021-12-25 10:04:45 +08:00
do \
{ \
} while (0)
2022-07-16 10:43:05 +08:00
# define AXLOGINFO(...) \
2021-12-25 10:04:45 +08:00
do \
{ \
} while (0)
2022-07-16 10:43:05 +08:00
# define AXLOGERROR(...) \
2021-12-25 10:04:45 +08:00
do \
{ \
} while (0)
2022-07-16 10:43:05 +08:00
# define AXLOGWARN(...) \
2021-12-25 10:04:45 +08:00
do \
{ \
} while (0)
2019-11-23 20:27:39 +08:00
2022-08-08 18:02:17 +08:00
#elif _AX_DEBUG == 1
# define AXLOG(format, ...) ax::print(format, ##__VA_ARGS__)
# define AXLOGERROR(format, ...) ax::print(format, ##__VA_ARGS__)
2022-07-16 10:43:05 +08:00
# define AXLOGINFO(format, ...) \
2021-12-25 10:04:45 +08:00
do \
{ \
} while (0)
2022-07-16 10:43:05 +08:00
# define AXLOGWARN(...) __AXLOGWITHFUNCTION(__VA_ARGS__)
2019-11-23 20:27:39 +08:00
2022-08-08 18:02:17 +08:00
#elif _AX_DEBUG > 1
# define AXLOG(format, ...) ax::print(format, ##__VA_ARGS__)
# define AXLOGERROR(format, ...) ax::print(format, ##__VA_ARGS__)
# define AXLOGINFO(format, ...) ax::print(format, ##__VA_ARGS__)
2022-07-16 10:43:05 +08:00
# define AXLOGWARN(...) __AXLOGWITHFUNCTION(__VA_ARGS__)
2022-08-08 18:02:17 +08:00
#endif // _AX_DEBUG
2019-11-23 20:27:39 +08:00
/** Lua engine debug */
2022-08-08 18:02:17 +08:00
#if !defined(_AX_DEBUG) || _AX_DEBUG == 0 || AX_LUA_ENGINE_DEBUG == 0
2021-12-25 10:04:45 +08:00
# define LUALOG(...)
2019-11-23 20:27:39 +08:00
#else
# define LUALOG(format, ...) ax::print(format, ##__VA_ARGS__)
2021-12-25 10:04:45 +08:00
#endif // Lua engine debug
2019-11-23 20:27:39 +08:00
// end of debug group
/// @}
2022-07-16 10:43:05 +08:00
/** @def AX_DISALLOW_COPY_AND_ASSIGN(TypeName)
2019-11-23 20:27:39 +08:00
* A macro to disallow the copy constructor and operator= functions.
* This should be used in the private: declarations for a class
*/
2021-12-25 10:04:45 +08:00
#if defined(__GNUC__) && ((__GNUC__ >= 5) || ((__GNUG__ == 4) && (__GNUC_MINOR__ >= 4))) || \
(defined(__clang__) && (__clang_major__ >= 3)) || (_MSC_VER >= 1800)
2022-07-16 10:43:05 +08:00
# define AX_DISALLOW_COPY_AND_ASSIGN(TypeName) \
2021-12-25 10:04:45 +08:00
TypeName(const TypeName&) = delete; \
TypeName& operator=(const TypeName&) = delete;
2019-11-23 20:27:39 +08:00
#else
2022-07-16 10:43:05 +08:00
# define AX_DISALLOW_COPY_AND_ASSIGN(TypeName) \
2021-12-25 10:04:45 +08:00
TypeName(const TypeName&); \
TypeName& operator=(const TypeName&);
2019-11-23 20:27:39 +08:00
#endif
2022-07-16 10:43:05 +08:00
/** @def AX_DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName)
2019-11-23 20:27:39 +08:00
* A macro to disallow all the implicit constructors, namely the
* default constructor, copy constructor and operator= functions.
*
* This should be used in the private: declarations for a class
* that wants to prevent anyone from instantiating it. This is
2021-12-25 10:04:45 +08:00
* especially useful for classes containing only static methods.
2019-11-23 20:27:39 +08:00
*/
2022-07-16 10:43:05 +08:00
#define AX_DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
2021-12-25 10:04:45 +08:00
TypeName(); \
2022-07-16 10:43:05 +08:00
AX_DISALLOW_COPY_AND_ASSIGN(TypeName)
2019-11-23 20:27:39 +08:00
2022-07-16 10:43:05 +08:00
/** @def AX_DEPRECATED_ATTRIBUTE
2019-11-23 20:27:39 +08:00
* Only certain compilers support __attribute__((deprecated)).
*/
#if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
2022-07-16 10:43:05 +08:00
# define AX_DEPRECATED_ATTRIBUTE __attribute__((deprecated))
2021-12-25 10:04:45 +08:00
#elif _MSC_VER >= 1400 // vs 2005 or higher
2022-07-16 10:43:05 +08:00
# define AX_DEPRECATED_ATTRIBUTE __declspec(deprecated)
2019-11-23 20:27:39 +08:00
#else
2022-07-16 10:43:05 +08:00
# define AX_DEPRECATED_ATTRIBUTE
2021-12-25 10:04:45 +08:00
#endif
2019-11-23 20:27:39 +08:00
2022-07-16 10:43:05 +08:00
/** @def AX_DEPRECATED(...)
2019-11-23 20:27:39 +08:00
* Macro to mark things deprecated as of a particular version
* can be used with arbitrary parameters which are thrown away.
2022-07-16 10:43:05 +08:00
* e.g. AX_DEPRECATED(4.0) or AX_DEPRECATED(4.0, "not going to need this anymore") etc.
2019-11-23 20:27:39 +08:00
*/
2022-07-16 10:43:05 +08:00
#define AX_DEPRECATED(...) AX_DEPRECATED_ATTRIBUTE
2019-11-23 20:27:39 +08:00
2022-07-16 10:43:05 +08:00
/** @def AX_FORMAT_PRINTF(formatPos, argPos)
2019-11-23 20:27:39 +08:00
* Only certain compiler support __attribute__((format))
*
* @param formatPos 1-based position of format string argument.
* @param argPos 1-based position of first format-dependent argument.
*/
#if defined(__GNUC__) && (__GNUC__ >= 4)
2022-07-16 10:43:05 +08:00
# define AX_FORMAT_PRINTF(formatPos, argPos) __attribute__((__format__(printf, formatPos, argPos)))
2019-11-23 20:27:39 +08:00
#elif defined(__has_attribute)
2021-12-25 10:04:45 +08:00
# if __has_attribute(format)
2022-07-16 10:43:05 +08:00
# define AX_FORMAT_PRINTF(formatPos, argPos) __attribute__((__format__(printf, formatPos, argPos)))
2021-12-25 10:04:45 +08:00
# else
2022-07-16 10:43:05 +08:00
# define AX_FORMAT_PRINTF(formatPos, argPos)
2021-12-25 10:04:45 +08:00
# endif // __has_attribute(format)
2019-11-23 20:27:39 +08:00
#else
2022-07-16 10:43:05 +08:00
# define AX_FORMAT_PRINTF(formatPos, argPos)
2019-11-23 20:27:39 +08:00
#endif
#if defined(_MSC_VER)
2022-07-16 10:43:05 +08:00
# define AX_FORMAT_PRINTF_SIZE_T "%08lX"
2019-11-23 20:27:39 +08:00
#else
2022-07-16 10:43:05 +08:00
# define AX_FORMAT_PRINTF_SIZE_T "%08zX"
2019-11-23 20:27:39 +08:00
#endif
#ifdef __GNUC__
2022-07-16 10:43:05 +08:00
# define AX_UNUSED __attribute__((unused))
2019-11-23 20:27:39 +08:00
#else
2022-07-16 10:43:05 +08:00
# define AX_UNUSED
2019-11-23 20:27:39 +08:00
#endif
2022-07-16 10:43:05 +08:00
/** @def AX_REQUIRES_NULL_TERMINATION
2021-12-25 10:04:45 +08:00
*
2019-11-23 20:27:39 +08:00
*/
2022-07-16 10:43:05 +08:00
#if !defined(AX_REQUIRES_NULL_TERMINATION)
# if defined(__APPLE_AX__) && (__APPLE_AX__ >= 5549)
# define AX_REQUIRES_NULL_TERMINATION __attribute__((sentinel(0, 1)))
2021-12-25 10:04:45 +08:00
# elif defined(__GNUC__)
2022-07-16 10:43:05 +08:00
# define AX_REQUIRES_NULL_TERMINATION __attribute__((sentinel))
2021-12-25 10:04:45 +08:00
# else
2022-07-16 10:43:05 +08:00
# define AX_REQUIRES_NULL_TERMINATION
2021-12-25 10:04:45 +08:00
# endif
2019-11-23 20:27:39 +08:00
#endif
2021-10-06 18:45:58 +08:00
// compatibility with non-clang compilers...
#ifndef __has_attribute
# define __has_attribute(x) 0
#endif
#ifndef __has_builtin
# define __has_builtin(x) 0
#endif
/*
* helps the compiler's optimizer predicting branches
*/
#if __has_builtin(__builtin_expect)
# ifdef __cplusplus
# define UTILS_LIKELY(exp) (__builtin_expect(!!(exp), true))
# define UTILS_UNLIKELY(exp) (__builtin_expect(!!(exp), false))
# else
# define UTILS_LIKELY(exp) (__builtin_expect(!!(exp), 1))
# define UTILS_UNLIKELY(exp) (__builtin_expect(!!(exp), 0))
# endif
#else
# define UTILS_LIKELY(exp) (!!(exp))
# define UTILS_UNLIKELY(exp) (!!(exp))
#endif
2022-07-16 10:43:05 +08:00
#endif // __AX_PLATFORM_MACROS_H__