網(wǎng)站首頁 編程語言 正文
1.數(shù)值類型輸出易讀的字符串形式
例如使用enum定義一些錯誤值,想要將數(shù)值類型的錯誤,輸出易讀的字符串形式
重要的一句代碼
#define MAKE_PAIR(val) std::make_pair(val, #val)
可以看到 #val,宏定義中的傳入?yún)?shù)名val 轉(zhuǎn)換成字符串,就像用一對雙引號包含起來的val
完整實現(xiàn)代碼如下
#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.易記的簡化調(diào)用
例如有兩個函數(shù)
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;
}
這兩個函數(shù)很長,函數(shù)名前綴相同
想要易記的簡化調(diào)用
例如
CALL_MGR(A()); //實際調(diào)用ACameraManager_A()
CALL_MGR(B()); //實際調(diào)用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 后面的 \ 表示下一行繼續(xù)寫宏定義。
兩個#號 ## 表示連接操作符。 CALL_MGR(A());通過 ACameraManager_##func 變成了ACameraManager_A
實現(xiàn)完整代碼如下
#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
以上代碼應(yīng)用在google的ndk camera代碼中
原文鏈接:https://blog.csdn.net/flyfish1986/article/details/129163301
- 上一篇:沒有了
- 下一篇:沒有了
相關(guān)推薦
- 2022-05-15 Python?中的集合和字典_python
- 2022-06-02 Go語言流程控制詳情_Golang
- 2022-05-27 利用Matlab繪制好看的旋轉(zhuǎn)九邊形_C 語言
- 2023-05-23 深入了解React中的合成事件_React
- 2022-07-31 python實現(xiàn)學(xué)員管理系統(tǒng)(面向?qū)ο蟀?_python
- 2022-08-20 windows系統(tǒng)安裝配置nginx環(huán)境_nginx
- 2021-12-26 WebStorm?發(fā)布2021.3重大更新新功能介紹_其它綜合
- 2022-03-29 在pyqt5中展示pyecharts生成的圖像問題_python
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支