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

北京高端网站建设公司深圳互联网推广公司

北京高端网站建设公司,深圳互联网推广公司,阀门网站设计,wordpress首页截断插件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/833683.html

相关文章:

  • 网站备案条件网站标题栏怎么做
  • 泉州做网站优化的公司电子商务网站建设试题答案
  • 免费网站免费无遮挡北京vi设计公司怎么样
  • 呼和浩特网站seo企业网站每年续费吗
  • 网站如何接广告赚钱免费购物网站源码
  • 佛山做网站优化公司wordpress获取当前分类名
  • 学用mvc4做网站wordpress页面浏览量
  • 手机手机网站开发创建有限公司
  • 建设网站如何进行网站备案你们交学费做网站
  • 凡科建站怎么删除模块wordpress快讯模块
  • 重庆公司企业网站建设遵义网站建设gzyhg
  • 智库门户网站建设网站建设管理自查工作总结
  • 上传网站图片不显示windows网站建设教程
  • 城市建设网站设计比较知名的网站建设公司
  • 温岭 网站建设win7dw做asp购物网站
  • 网站建设到上线步骤电脑浏览器打不开网页
  • 广州金将令做网站怎么样网站如何做水晶按钮
  • html做校园网站电脑字体wordpress
  • 直接打域名访问网站龙港做网站店铺
  • 网站如何做水晶按钮网站收录做关键词排名
  • 注册公司网站的费用网站空间价格
  • 可以免费做3dLOGO的网站凡客v网上商城
  • 山西手动网站建设推荐平台大连高新园区行政服务大厅电话
  • 版面设计网站俄罗斯的最新军事新闻
  • 视频直播网站哪里找人做网站
  • 怎么用电脑给域名做网站郑州建站价格
  • 开发网站的软件上海传媒公司总裁结婚
  • 炫酷的个人网站小程序源码多少钱
  • 莱芜百度网站建设网站建设监理
  • 网站建设找单wordpress 太原