表格线识别通用库文档
载入中...
搜索中...
未找到
complex_printer.hpp
浏览该文件的文档.
1/*
2 * @Description: 复杂打印器类 头文件及其实现
3 * @Version:
4 * @Autor: dreamy-xay
5 * @date: 2024-01-23
6 * @LastEditors: dreamy-xay
7 * @LastEditTime: 2024-03-04
8 */
9
10#ifndef COMMON_COMPLEX_PRINTER_HPP
11#define COMMON_COMPLEX_PRINTER_HPP
12
13#include <cstring>
14#include <iostream>
15
18#include "common/enum.h"
19#include "common/macro.h"
20#include "common/type.h"
22
23namespace cm {
24
32class ComplexPrinter : public Printer {
33 private:
34 std::string param_name_output_color; // 默认参数名输出的颜色
35 std::string param_name_output_color_cache; // 默认参数名输出的颜色缓存
36
37 std::vector<std::string> param_names; // 参数名列表
38 int current_param_index; // 当前打印的参数名索引
39
40 public:
41 ComplexPrinter(const std::string& default_split_string = "\n", const std::string& output_color = Color::BLUE, const std::string& param_name_output_color = Color::GREEN + Color::FONT_BOLD);
43
45
46 template <typename... ARGS>
47 std::ostream& operator()(const std::string& param_names_str, ARGS... args);
48
49 ComplexPrinter& SetParamNameOutputColor(const std::string& param_name_output_color);
50
51 private:
52 std::string GetPrintPrefix(bool is_last_arg) override;
53};
54
66inline ComplexPrinter::ComplexPrinter(const std::string& split_string, const std::string& output_color, const std::string& param_name_output_color) : Printer(split_string, output_color), param_name_output_color(param_name_output_color), param_names(), current_param_index(-1) {
67}
68
77
94 param_names.clear(); // 清空参数名字列表
95 param_names_str.push_back(','); // 向最后添加逗号
96
97 int is_not_split = 0; // 是否支持分隔符
98 size_t len = param_names_str.length(); // 参数名子列表字符串长度
99 size_t prev_pos = 0; // 存储上一个逗号的位置的后一个索引
100 std::string token; // 存储每个参数名
101
102 // 获取参数名列表(通过逗号分割)
103 for (size_t i = 0; i < len; ++i)
104 if (param_names_str[i] == '(')
105 ++is_not_split;
106 else if (param_names_str[i] == ')')
107 --is_not_split;
108 else if (param_names_str[i] == ',' && !is_not_split) {
110 prev_pos = i + 1;
111
112 size_t first = token.find_first_not_of(" \t\n\r"); // 删除参数名首尾空格和回车
113 size_t last = token.find_last_not_of(" \t\n\r");
114
115 if (first == std::string::npos) // 如果字符串由回车或空格组成,则不动源字符串
116 param_names.emplace_back("<" + token + ">");
117 else
118 param_names.emplace_back("<" + token.substr(first, last - first + 1) + ">"); // 删除参数名首尾空格和回车
119 }
120
121 current_param_index = 0; // 初始化当前打印的参数索引
122
123 return *this;
124}
125
155template <typename... ARGS>
156inline std::ostream& ComplexPrinter::operator()(const std::string& param_names_str, ARGS... args) {
157 this->ParseParamNames(param_names_str); // 解析所有参数名
158 this->Print(args...); // 打印所有参数
159
160 return std::cout;
161}
162
174inline ComplexPrinter& ComplexPrinter::SetParamNameOutputColor(const std::string& param_name_output_color) {
175 this->param_name_output_color = param_name_output_color;
176
177 return *this;
178}
179
193inline std::string ComplexPrinter::GetPrintPrefix(bool is_last_arg) {
194 Cm_Assert(current_param_index >= 0 && param_names.size() > current_param_index, "Parameter name list string data error!!!");
195
196 // 如果撤销了颜色打印,则特殊处理
197 if (is_cancel_color) {
198 param_name_output_color_cache = param_name_output_color;
199 param_name_output_color = "";
200 }
201
202 // 新的前缀字符串
203 std::string prefix;
204
205 prefix += param_name_output_color;
206 prefix += param_names[current_param_index++];
207 prefix += ": ";
208 prefix += Color::RESET;
209
210 // 如果是最后一个参数
211 if (is_last_arg) {
212 current_param_index = -1;
213
214 // 如果撤销了颜色打印,则恢复颜色打印
215 if (is_cancel_color)
216 param_name_output_color = param_name_output_color_cache;
217 }
218
219 return std::move(prefix);
220}
221
222} // namespace cm
223
224#endif
static std::string RESET
结束符,重置所有颜色设置
Definition color.hpp:48
static std::string BLUE
蓝色
Definition color.hpp:34
static std::string FONT_BOLD
加粗
Definition color.hpp:47
static std::string GREEN
绿色
Definition color.hpp:32
复杂打印器类
~ComplexPrinter()
复杂打印器类的析构函数
std::ostream & operator()(const std::string &param_names_str, ARGS... args)
复杂打印器参数打印
ComplexPrinter & SetParamNameOutputColor(const std::string &param_name_output_color)
设置默认输出内容的颜色
ComplexPrinter & ParseParamNames(std::string param_names_str)
复杂打印器参数名列表解析
ComplexPrinter(const std::string &default_split_string="\n", const std::string &output_color=Color::BLUE, const std::string &param_name_output_color=Color::GREEN+Color::FONT_BOLD)
复杂打印器类的带参构造函数
点类
Definition point.hpp:52
打印器类
Definition printer.hpp:32
Printer & Print(T arg)
实现单个参数的打印
Definition printer.hpp:186
#define Cm_Assert(expr, message)
断言宏
Definition macro.h:109