欢迎访问WDPHP素材源码!今天是2024年04月29日 星期一,时间不早了,早点休息吧!
您好,游客 [ 马上登录 | 注册帐号 | 微信登录 | QQ登录]
当前位置:首页 > 教程 > 其他教程 > 

C/C++中的#define怎么使用
栏目分类:其他教程    发布日期:2023-11-10    浏览次数:218次     收藏

这篇文章主要介绍“C/C++中的#define怎么使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“C/C++中的#define怎么使用”文章能帮助大家解决问题。

1.数值类型输出易读的字符串形式

例如使用enum定义一些错误值,想要将数值类型的错误,输出易读的字符串形式

重要的一句代码

#define MAKE_PAIR(val) std::make_pair(val, #val)

可以看到 #val,宏定义中的传入参数名val 转换成字符串,就像用一对双引号包含起来的val

完整实现代码如下

#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.易记的简化调用

例如有两个函数

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;
}

这两个函数很长,函数名前缀相同

想要易记的简化调用

例如

CALL_MGR(A()); //实际调用ACameraManager_A()
CALL_MGR(B()); //实际调用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 后面的 表示下一行继续写宏定义。

两个#号 ## 表示连接操作符。 CALL_MGR(A());通过 ACameraManager_##func 变成了ACameraManager_A

实现完整代码如下

#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

相关热词: C++中的#define使用

源码 模板 特效 素材 资源 教程 站长