当前位置: 首页 > news >正文

C/C++ 中#define 的妙用,让代码更美一些

C/C++ 中#define 的妙用,让代码更美一些

flyfish

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

以上代码应用在google的ndk camera代码中

相关文章:

  • 缺少IT人员的服装行业该如何进行数字化转型?
  • Spring Cloud Nacos源码讲解(二)- Nacos客户端服务注册源码分析
  • 复旦发布国内首个类ChatGPT模型MOSS,和《流浪地球》有关?
  • MyBatis基于XML的详细使用——动态sql
  • Qt 开发使用VSCode 笔记2
  • 算法分析详解
  • 进程间通信(二)/共享内存
  • 边玩边学,13个 Python 小游戏真有趣啊(含源码)
  • 「TCG 规范解读」第7章 TPM工作组 TPM 总结
  • 七大排序经典排序算法
  • 带你一步步搭建Web自动化测试框架
  • 踩大坑:json格式存储wav二进制内容
  • ChatGPT 简介
  • 猜数字大小 II
  • Python3 pip
  • 【C语言】指针进阶
  • 两年外包生涯做完,感觉自己废了一半....
  • 加油站会员管理小程序实战开发教程10
  • 基于SpringBoot+vue的无偿献血后台管理系统
  • Redis底层原理(持久化+分布式锁)
  • 一周文化讲座|读书是通往世界的路
  • 何立峰会见美国英伟达公司总裁黄仁勋:欢迎美资企业深耕中国市场
  • 大外交|习近平时隔9年访柬,专家:中柬铁杆友谊的地区意义日渐凸显
  • 农业农村部原党组书记、部长唐仁健被提起公诉
  • 应急部:持续开展燃气、电动自行车等“一件事”全链条整治
  • 青海大学常务副校长(正厅级)任延明拟调任青海省直单位正职