C/C++中#define的妙用分享
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代碼中
到此這篇關(guān)于C/C++中#define的妙用分享的文章就介紹到這了,更多相關(guān)C++ #define內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
探討:將兩個鏈表非降序合并為一個鏈表并依然有序的實現(xiàn)方法
本篇文章是對將兩個鏈表非降序合并為一個鏈表并依然有序的實現(xiàn)方法進行了詳細的分析介紹,需要的朋友參考下2013-05-05
C語言實現(xiàn)輸入ascii碼,輸出對應(yīng)的字符方式
這篇文章主要介紹了C語言實現(xiàn)輸入ascii碼,輸出對應(yīng)的字符方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
QT使用QComBox和QLineEdit實現(xiàn)模糊查詢功能
模糊查詢是指根據(jù)用戶輸入的文本,在下拉框的選項中進行模糊匹配,并動態(tài)地顯示匹配的選項,本文將使用QComBox和QLineEdit實現(xiàn)模糊查詢功能,需要的可以參考下2023-11-11

