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)文章
C++設(shè)計模式編程之Flyweight享元模式結(jié)構(gòu)詳解
這篇文章主要介紹了C++設(shè)計模式編程的Flyweight享元模式結(jié)構(gòu),享元模式在實現(xiàn)過程中主要是要為共享對象提供一個存放的"倉庫"(對象池),需要的朋友可以參考下2016-03-03
C語言實現(xiàn)日期和時間處理的常用函數(shù)總結(jié)
在C語言中,時間和日期處理是一項非?;A(chǔ)的技能,也是開發(fā)實際應(yīng)用程序時經(jīng)常會用到的功能,本文為大家總結(jié)了C語言中一些常用的時間庫函數(shù),希望對大家有所幫助2023-06-06
淺談返回函數(shù)內(nèi)部new分配的內(nèi)存的引用
下面小編就為大家?guī)硪黄獪\談返回函數(shù)內(nèi)部new分配的內(nèi)存的引用。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12

