Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The macro definition


May 21, 2021 CrossApp



Macro name: CALLBACK_BIND_1

Macro definition: #define CALLBACK_BIND_1 (__selector__,__target__,...) std:: bind (samp;__selector__,__target__, std::p laceholders: : s1, s#__VA_ARGS__)

Explanation: This is the callback macro definition, __selector__ represents the callback method, __target__ represents the target object, CALLBACK_BIND_0 means that the callback method has no parameters, CALLBACK_BIND_1 means that the callback method has a parameter, and so on

Example code:

// CALLBACK_BIND_2对应的tableViewHeightForRowAtIndexPath方法有两个参数
m_pTableView->onCellHeightAtIndexPath(CALLBACK_BIND_2(TableViewTest::tableViewHeightForRowAtIndexPath, this));
 
// tableViewHeightForRowAtIndexPath的实现 两个参数分别为section和row
unsigned int TableViewTest::tableViewHeightForRowAtIndexPath(unsigned int section, unsigned int row)
{
    return 130;
}
 
// CALLBACK_BIND_1对应的tableViewHeightForHeaderInSection方法有一个参数
m_pTableView->onHeightForHeaderInSection(CALLBACK_BIND_1(TableViewTest::tableViewHeightForHeaderInSection, this));
 
// tableViewHeightForHeaderInSection的实现 一个参数是section
unsigned int TableViewTest::tableViewHeightForHeaderInSection(unsigned int section)
{
    return 50;
}

Macro name: CC_LISTENING_FUNCTION

Macro definition:

#define CC_LISTENING_FUNCTION(FUNCTION, VARNAME)\

protected: std::function<FUNCTION> m_ob##VARNAME{nullptr};\

public: void on##VARNAME(const std::function<FUNCTION>& var){ m_ob##VARNAME = var; }

Explanation: the main alternative to delegate, the previous need to set up the agent, and then implement the method in the agent, you can now directly implement the method, convenient and concise.

FUNCTION represents the method of callback, and VARNAME stands for method in the class.

Example:

CC_LISTENING_FUNCTION(unsigned int(unsigned int section), HeightForHeaderInSection);
// 解释:HeightForHeaderInSection为类CATableView的方法名,用到时前面需要加on,unsigned int section为回调方法的参数,
// unsigned int为回调方法的返回值类型。
 
// 用法
m_pTableView->onHeightForHeaderInSection(CALLBACK_BIND_1(TableViewTest::tableViewHeightForHeaderInSection, this));
 
// 实现tableViewHeightForHeaderInSection回调方法
unsigned int TableViewTest::tableViewHeightForHeaderInSection(unsigned int section)
{
    return 50;
}

To judge the macros of the platform:

#define CC_PLATFORM_UNKNOWN 0 // Unknown platform

#define CC_PLATFORM_IOS 1 // Apple phone

#define CC_PLATFORM_ANDROID 2 // Android phone

#define CC_PLATFORM_WIN32 3 // Windows system

#define CC_PLATFORM_LINUX 5 // LINUX system

#define CC_PLATFORM_BADA 6 // Samsung smartphone operating system

#define CC_PLATFORM_MAC 8 // Apple's Mac system

#define CC_PLATFORM_EMSCRIPTEN 10 // EMSCRIPTEN system

#define CC_PLATFORM_WINRT 12 // windows rt

#define CC_PLATFORM_WP8 13 // Windows Phone 8 system

CC_TARGET_PLATFORM used to judge the platform.

Here's an example of opening a URL by judging a platform:

void openUrl(const std::string &url)
{
// 如果当前系统是MAC系统
#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
    [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithUTF8String:url.c_str()]]];
     
// 如果当前系统是IOS系统
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithUTF8String:url.c_str()]]];
         
#endif
}