C ++模板名称漂亮的打印
为了进行调试,我需要打印缩进的模板名称。 例如,而不是单行,我想缩进这样的名字:
boost::phoenix::actor<
boost::phoenix::composite<
boost::phoenix::less_eval,
boost::fusion::vector<
boost::phoenix::argument<0>,
boost::phoenix::argument<1>,
我开始写我自己的,但变得越来越复杂。 有没有现有的解决方案?
如果没有,你能帮我完成我的实施吗? 如果是这样,我会发布它。
谢谢
这是typeid.name的样子,
boost::phoenix::actor<boost::phoenix::composite<boost::phoenix::less_eval,
boost::fusion::vector<boost::phoenix::argument<0>,
boost::phoenix::composite<boost::phoenix::multiplies_eval,
boost::fusion::vector<boost::phoenix::argument<1>, boost::phoenix::argument<2>,
boost::fusion::void_, boost::fusion::void_, boost::fusion::void_,
boost::fusion::void_, boost::fusion::void_, boost::fusion::void_,
boost::fusion::void_, boost::fusion::void >, boost::fusion::void_,
boost::fusion::void_, boost::fusion::void_, boost::fusion::void_,
boost::fusion::void_, boost::fusion::void_, boost::fusion::void_,
boost::fusion::void_> > >
这是我的目标
6 boost::phoenix::actor<
7 boost::phoenix::composite<
8 boost::phoenix::less_eval,
9 boost::fusion::vector<
10 boost::phoenix::argument<0>,
11 boost::phoenix::composite<
12 boost::phoenix::multiplies_eval,
13 boost::fusion::vector<
14 boost::phoenix::argument<1>,
15 boost::phoenix::argument<2>,
16 boost::fusion::void_,
17 boost::fusion::void_,
18 boost::fusion::void_,
19 boost::fusion::void_,
20 boost::fusion::void_,
21 boost::fusion::void_,
22 boost::fusion::void_,
23 boost::fusion::void >, // indentation messed up
24 boost::fusion::void_,
25 boost::fusion::void_,
26 boost::fusion::void_,
27 boost::fusion::void_,
28 boost::fusion::void_,
29 boost::fusion::void_,
30 boost::fusion::void_,
31 boost::fusion::void_
32 >
33 >
34 >
这样我才能真正阅读宣言
当然不是最优雅的作品,但是这应该让你关注结束标签:
std::string indent(std::string str, const std::string &indent = " ") {
std::string indent_ = std::string("n");
size_t token = 0;
while ((token = str.find_first_of("<>,", token)) != std::string::npos) {
switch(str[token]) {
case '<': indent_.append(indent);
case ',': str.insert(token + 1, indent_);
break;
case '>': indent_.erase(indent_.size() - indent.size());
str.insert(token, indent_);
}
token += indent_.size() + 1;
const size_t nw = str.find_first_not_of(" ", token);
if(nw != std::string::npos) {
str.erase(token, nw-token);
}
}
return str;
}
gf程序的小调整,主要不是拆分短模板
#ifndef PRETTY_NAME_HPP
#define PRETTY_NAME_HPP
#include <typeinfo>
#include <string>
#include <iostream>
#include <cxxabi.h>
#define TYPENAME(TYPE) typeid_name(typeid(TYPE).name())
std::string indent(std::string str, const std::string &indent = " ") {
std::string indent_ = std::string("n");
size_t token = 0;
bool one_line = false;
while ((token = str.find_first_of("<>,", token)) != std::string::npos) {
size_t size = str.size();
size_t close, open, comma;
switch(str[token]) {
case '<':
close = str.find(">", token+1);
open = str.find("<", token+1);
comma = str.find(",", token+1);
one_line = !(close > open) && !(comma < close);
if (one_line) break;
indent_.append(indent);
case ',':
str.insert(token + 1, indent_);
break;
case '>':
if (!one_line) {
indent_.erase(indent_.size() - indent.size());
str.insert(token, indent_);
}
one_line = false;
}
token += 1 + str.size() - size;
const size_t nw = str.find_first_not_of(" ", token);
if(nw != std::string::npos) {
str.erase(token, nw-token);
}
}
return str;
}
std::string typeid_name(const char* name) {
// #ifdef HAVE_CXA_DEMANGLE
size_t size;
int status;
char *buf = abi::__cxa_demangle(name, NULL, &size, &status);
if (status != 0) throw status;
std::string string(buf);
free(buf);
return indent(string);
// #else
// return name;
// #endif
}
#endif /* PRETTY_NAME_HPP */
如何,然后复制到剪贴板
$ xclip -o | clang-format
例如,这将采用OP的模板
boost::phoenix::actor <
boost::phoenix::composite<
boost::phoenix::less_eval,
boost::fusion::vector<
boost::phoenix::argument<0>,
boost::phoenix::composite<
boost::phoenix::multiplies_eval,
boost::fusion::vector<
boost::phoenix::argument<1>, boost::phoenix::argument<2>,
boost::fusion::void_, boost::fusion::void_,
boost::fusion::void_, boost::fusion::void_,
boost::fusion::void_, boost::fusion::void_,
boost::fusion::void_, boost::fusion::void>,
boost::fusion::void_, boost::fusion::void_,
boost::fusion::void_, boost::fusion::void_,
boost::fusion::void_, boost::fusion::void_,
boost::fusion::void_, boost::fusion::void_> > >
不理想,因为它的某处存在错误。 但是它使得找到错误变得相当容易(在中间的void
应该移动到最后之后,extra >
)。 如果我们修复它,我们会得到
boost::phoenix::actor<boost::phoenix::composite<
boost::phoenix::less_eval,
boost::fusion::vector<
boost::phoenix::argument<0>,
boost::phoenix::composite<
boost::phoenix::multiplies_eval,
boost::fusion::vector<
boost::phoenix::argument<1>, boost::phoenix::argument<2>,
boost::fusion::void_, boost::fusion::void_,
boost::fusion::void_, boost::fusion::void_,
boost::fusion::void_, boost::fusion::void_,
boost::fusion::void_, boost::fusion::void, boost::fusion::void_,
boost::fusion::void_, boost::fusion::void_,
boost::fusion::void_, boost::fusion::void_,
boost::fusion::void_, boost::fusion::void_,
boost::fusion::void_>>>>>
链接地址: http://www.djcxy.com/p/957.html
上一篇: C++ template name pretty print
下一篇: Hibernate criteria query to match against all child collection elements