当前位置: 首页 > 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代码中

http://www.lryc.cn/news/16190.html

相关文章:

  • Linux文件系统操作与磁盘管理
  • 【Python】批量采集原神表情包~
  • C语言基本语法注释类型关键字
  • 【C ++】C++入门知识(二)
  • qt qchart学习
  • 手工布署 java 项目
  • 《设计模式》观察者模式
  • 基于SpringBoot的外卖项目(详细开发过程)
  • ChatGPT 研发传言席卷互联网公司,这会是一门好生意吗?
  • 获取servlet转发和响应重定向的方式是什么?
  • jvm知识点
  • MoveIT Noetic控制真实机械臂
  • 如何快速入门编程
  • java的反射Reflect
  • 常用设计模式总结
  • 【算法基础】一维前缀和 + 二维前缀和
  • Kafka消费分组和分区分配策略
  • 犹太教、基督教、伊斯兰教的区别与联系
  • 华为OD机试 - 打印文件(Python) | 机试题+算法思路+考点+代码解析 【2023】
  • 网络工程师必备知识点
  • 数据在内存中的存储【下篇】
  • 前端开发项目规范写法介绍
  • 百万医疗险是什么
  • 矩阵中的路径 AcWing (JAVA)
  • 使用终端工具给你的电脑发送弹窗提醒
  • SpringCloud Alibaba 之Nacos集群部署-高可用保证
  • Scala集合详解(第七章:集合、数组、列表、set集合、map集合、元组、队列、并行)(尚硅谷笔记)
  • 定了:Python3.7,今年停止更新~
  • C# 业务单据号生成器(定义规则、获取编号、流水号)
  • Java的dump文件分析及JProfiler使用