最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C/C++中#define的妙用分享

 更新時(shí)間:2023年02月23日 08:43:52   作者:西西弗Sisyphus  
本文主要介紹了C++/C關(guān)于#define的一些妙用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

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)包含起來(lái)的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)文章

最新評(píng)論

滨州市| 荣成市| 通许县| 涿州市| 府谷县| 东乌珠穆沁旗| 云安县| 梁平县| 万源市| 永州市| 通道| 丹凤县| 巴彦县| 渭南市| 普格县| 丁青县| 团风县| 合阳县| 新密市| 靖宇县| 白水县| 保德县| 防城港市| 怀宁县| 朝阳区| 阜南县| 镇巴县| 呼玛县| 商水县| 改则县| 抚宁县| 绥芬河市| 称多县| 平原县| 江油市| 涿鹿县| 屯昌县| 开远市| 务川| 湘阴县| 襄城县|