溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C/C++中的#define怎么使用

發(fā)布時間:2023-02-23 10:02:24 來源:億速云 閱讀:109 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“C/C++中的#define怎么使用”的相關(guān)知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強(qiáng),希望這篇“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

關(guān)于“C/C++中的#define怎么使用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

c++ c
AI