当前位置: 首页 > wzjs >正文

电子商务网站建设课件seo竞价培训

电子商务网站建设课件,seo竞价培训,兼职网站哪个靠谱,化妆品网站建设经济可行性分析C/C 中#define 的妙用,让代码更美一些 flyfish 1 数值类型输出易读的字符串形式 例如使用enum定义一些错误值,想要将数值类型的错误,输出易读的字符串形式 重要的一句代码 #define MAKE_PAIR(val) std::make_pair(val, #val)可以看到 #va…

C/C++ 中#define 的妙用,让代码更美一些

flyfish

1 数值类型输出易读的字符串形式

例如使用enum定义一些错误值,想要将数值类型的错误,输出易读的字符串形式
重要的一句代码

#define MAKE_PAIR(val) std::make_pair(val, #val)

可以看到 #val,宏定义中的传入参数名val 转换成字符串,就像用一对双引号包含起来的val

完整实现代码如下

#include <iostream>
#include <cinttypes>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
using namespace std;typedef enum {ACAMERA_OK = 0,ACAMERA_ERROR_BASE                  = -10000,ACAMERA_ERROR_UNKNOWN               = ACAMERA_ERROR_BASE,ACAMERA_ERROR_INVALID_PARAMETER     = ACAMERA_ERROR_BASE - 1,ACAMERA_ERROR_CAMERA_DISCONNECTED   = ACAMERA_ERROR_BASE - 2,} camera_status_t;#define UKNOWN_TAG "UNKNOW_TAG"
#define MAKE_PAIR(val) std::make_pair(val, #val)
template <typename T>
const char* GetPairStr(T key, std::vector<std::pair<T, const char*>>& store) {typedef typename std::vector<std::pair<T, const char*>>::iterator iterator;for (iterator it = store.begin(); it != store.end(); ++it) {if (it->first == key) {return it->second;}}//LOGW("(%#08x) : UNKNOWN_TAG for %s", key, typeid(store[0].first).name());return UKNOWN_TAG;
}
using ERROR_PAIR = std::pair<camera_status_t, const char*>;
static std::vector<ERROR_PAIR> errorInfo{MAKE_PAIR(ACAMERA_OK),MAKE_PAIR(ACAMERA_ERROR_UNKNOWN),MAKE_PAIR(ACAMERA_ERROR_INVALID_PARAMETER),MAKE_PAIR(ACAMERA_ERROR_CAMERA_DISCONNECTED),
};
const char* GetErrorStr(camera_status_t err) {return GetPairStr<camera_status_t>(err, errorInfo);
}int main()
{std::cout<<GetErrorStr(ACAMERA_ERROR_INVALID_PARAMETER)<<std::endl;return 0;
}

输出

ACAMERA_ERROR_INVALID_PARAMETER

2 易记的简化调用

例如有两个函数

camera_status_t ACameraManager_A()
{std::cout<<"A"<<std::endl;return ACAMERA_OK;
}camera_status_t ACameraManager_B()
{std::cout<<"B"<<std::endl;return ACAMERA_OK;
}

这两个函数很长,函数名前缀相同
想要易记的简化调用
例如

CALL_MGR(A()); //实际调用ACameraManager_A()
CALL_MGR(B()); //实际调用ACameraManager_B()
#define CALL_CAMERA(func)                                             \{                                                                   \camera_status_t status = func;                                    \std::cout<<GetErrorStr(status)<<std::endl;                        \}
#define CALL_MGR(func) CALL_CAMERA(ACameraManager_##func)

#define 后面的 \ 表示下一行继续写宏定义。
两个#号 ## 表示连接操作符。 CALL_MGR(A());通过 ACameraManager_##func 变成了ACameraManager_A
实现完整代码如下

#include <iostream>
#include <cinttypes>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
#include <assert.h>
using namespace std;typedef enum {ACAMERA_OK = 0,ACAMERA_ERROR_BASE                  = -10000,ACAMERA_ERROR_UNKNOWN               = ACAMERA_ERROR_BASE,ACAMERA_ERROR_INVALID_PARAMETER     = ACAMERA_ERROR_BASE - 1,ACAMERA_ERROR_CAMERA_DISCONNECTED   = ACAMERA_ERROR_BASE - 2,} camera_status_t;#define UKNOWN_TAG "UNKNOW_TAG"
#define MAKE_PAIR(val) std::make_pair(val, #val)
template <typename T>
const char* GetPairStr(T key, std::vector<std::pair<T, const char*>>& store) {typedef typename std::vector<std::pair<T, const char*>>::iterator iterator;for (iterator it = store.begin(); it != store.end(); ++it) {if (it->first == key) {return it->second;}}//LOGW("(%#08x) : UNKNOWN_TAG for %s", key, typeid(store[0].first).name());return UKNOWN_TAG;
}
using ERROR_PAIR = std::pair<camera_status_t, const char*>;
static std::vector<ERROR_PAIR> errorInfo{MAKE_PAIR(ACAMERA_OK),MAKE_PAIR(ACAMERA_ERROR_UNKNOWN),MAKE_PAIR(ACAMERA_ERROR_INVALID_PARAMETER),MAKE_PAIR(ACAMERA_ERROR_CAMERA_DISCONNECTED),
};
const char* GetErrorStr(camera_status_t err) {return GetPairStr<camera_status_t>(err, errorInfo);
}camera_status_t ACameraManager_A()
{std::cout<<"A"<<std::endl;return ACAMERA_OK;
}
camera_status_t ACameraManager_B()
{std::cout<<"B"<<std::endl;return ACAMERA_OK;
}
#define CALL_CAMERA(func)                                             \{                                                                   \camera_status_t status = func;                                    \std::cout<<GetErrorStr(status)<<std::endl;                        \}
#define CALL_MGR(func) CALL_CAMERA(ACameraManager_##func)
int main()
{CALL_MGR(A());CALL_MGR(B());return 0;
}

输出

A
ACAMERA_OK
B
ACAMERA_OK

以上代码应用在google的ndk camera代码中

http://www.dtcms.com/wzjs/215202.html

相关文章:

  • 潍坊企业自助建站百度网址大全电脑版
  • 网站开发好吗win优化大师
  • 女生学网站建设好学吗网络公司排名
  • 茶酒行业网站建设网站排名优化快速
  • 宁夏吴忠市红寺堡建设局网站石家庄seo顾问
  • 做淘宝代销哪个网站好网上接单平台
  • 三亚网站定制开发公司seo搜索优化软件
  • 网站建设阶段自己网站怎么推广
  • 国外做袜靴的网站网络营销推广seo
  • 怎么在vk网站上做推广爱网站查询
  • 阜城县网站建设抖音优化排名
  • 哈尔滨建站在线咨询广告营销
  • 延安网站建设电话注册一个网站
  • 自适应网站建设方案爱站关键词搜索
  • 绍兴做网站的优化网址
  • php手机网站开发山东网站seo推广优化价格
  • 公司网站建设要求书网络营销和网络推广有什么区别
  • 十堰的网站建设torrentkitty磁力搜索引擎
  • 做网站优化的弊端海外市场推广策略
  • 鼎成中考开卷通有关网站做的电子书最近的新闻热点
  • 延安做网站电话网络营销与管理专业是干什么的
  • 西安做网站的公司有mac蜜桃923色号
  • 做一个动态网站多少钱网络推广是网络营销的基础
  • wordpress怎么做的公司网站优化
  • 徐州提供网站建设要多少钱数据指数
  • 郑州建网站价格宁波seo搜索引擎优化公司
  • 购买网站空间后怎么做专业郑州企业网站建设
  • wordpress如何制作网站深圳互联网公司排行榜
  • 用php做的网站有如何让自己的网站快速被百度收录
  • 房产中介网站开发百度小说搜索热度排行榜