C/C++中#define的妙用分享
1.數(shù)值類型輸出易讀的字符串形式
例如使用enum定義一些錯(cuò)誤值,想要將數(shù)值類型的錯(cuò)誤,輸出易讀的字符串形式
重要的一句代碼
#define MAKE_PAIR(val) std::make_pair(val, #val)
可以看到 #val,宏定義中的傳入?yún)?shù)名val 轉(zhuǎn)換成字符串,就像用一對(duì)雙引號(hào)包含起來的val
完整實(shí)現(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.易記的簡(jiǎn)化調(diào)用
例如有兩個(gè)函數(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; }
這兩個(gè)函數(shù)很長(zhǎng),函數(shù)名前綴相同
想要易記的簡(jiǎn)化調(diào)用
例如
CALL_MGR(A()); //實(shí)際調(diào)用ACameraManager_A() CALL_MGR(B()); //實(shí)際調(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ù)寫宏定義。
兩個(gè)#號(hào) ## 表示連接操作符。 CALL_MGR(A());通過 ACameraManager_##func 變成了ACameraManager_A
實(shí)現(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++設(shè)計(jì)模式編程之Flyweight享元模式結(jié)構(gòu)詳解
這篇文章主要介紹了C++設(shè)計(jì)模式編程的Flyweight享元模式結(jié)構(gòu),享元模式在實(shí)現(xiàn)過程中主要是要為共享對(duì)象提供一個(gè)存放的"倉庫"(對(duì)象池),需要的朋友可以參考下2016-03-03C語言實(shí)現(xiàn)日期和時(shí)間處理的常用函數(shù)總結(jié)
在C語言中,時(shí)間和日期處理是一項(xiàng)非?;A(chǔ)的技能,也是開發(fā)實(shí)際應(yīng)用程序時(shí)經(jīng)常會(huì)用到的功能,本文為大家總結(jié)了C語言中一些常用的時(shí)間庫函數(shù),希望對(duì)大家有所幫助2023-06-06淺談返回函數(shù)內(nèi)部new分配的內(nèi)存的引用
下面小編就為大家?guī)硪黄獪\談返回函數(shù)內(nèi)部new分配的內(nèi)存的引用。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12C++面試題之進(jìn)制轉(zhuǎn)換的實(shí)例
這篇文章主要介紹了C++面試題之進(jìn)制轉(zhuǎn)換的實(shí)例的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這樣的知識(shí),需要的朋友可以參考下2017-10-10