用于从一个基派生的类的GDB漂亮的打印机
GDB pretty printer for classes derived from one base
我想在我的.gdbinit中添加一个漂亮的打印机,它将用于从包含"value"的基类base派生的任何类型。这部分起作用:
class MyBasePrinter(gdb.printing.PrettyPrinter):
def __init__(self, val):
self.__val = val['value']
def to_string(self):
return self.__val
my_pretty_printer = gdb.printing.RegexpCollectionPrettyPrinter('my_printer')
my_printer.add_printer('Base', '^Base.*$', MyBasePrinter)
不幸的是,我在gdb中打印的是这样的东西:
$1 = {<Base class stuff> = <value>}
基类是一个CRTP模板,所以"基类的东西"相当长。或者,我可以为我的每个派生类添加一个单独的漂亮打印机,在这种情况下打印效果很好,但每次有人从Base派生新类时,都需要更改.gdbinit。我想知道是否有更好的解决方案?理想情况下,我希望看到类似的打印
$1 = <Derived> = <value>
基本和派生的最小代码示例:
template <typename RawType, typename T, template <typename> typename...Traits>
struct IntegralValue : Traits<T>... {
using raw_type = RawType;
raw_type value;
explicit IntegralValue(raw_type v) : value(v) { }
void operator=(IntegralValue const& rhs) { value = rhs.value; }
};
template <typename T> struct EqualComparable {
bool operator==(T rhs) const {
return static_cast<T const*>(this)->value == rhs.value;
}
bool operator!=(T rhs) const {
return static_cast<T const*>(this)->value != rhs.value;
}
};
template <typename T> struct Incrementable {
T operator++(int) {
auto& self = static_cast<T&>(*this);
T retval = self;
self.value++;
return retval;
}
// other increment-related operators here
// ...
};
struct SequenceNumber : IntegralValue<uint64_t, SequenceNumber, EqualComparable, Incrementable> {
explicit SequenceNumber(raw_type v) : IntegralValue(v) { }
};
struct Id : IntegralValue<uint64_t, Id, EqualComparable> {
explicit Id(raw_type v) : IntegralType(v) { }
};
auto id = Id(1);
auto s = SequenceNumber(2);
// stop gdb around here and print id and s
您的代码有:
my_pretty_printer = gdb.printing.RegexpCollectionPrettyPrinter('my_printer')
虽然gdb漂亮的打印机按照类型的名称注册是传统的——而且有像这样的助手可以让它更简单——但实际上并不需要。相反,你可以有一个识别器,它可以接受任何值,并决定是否为其构建一个漂亮的打印机
请参阅手册中关于gdb.pretty_printers
的章节。您的识别器可以通过检查值的类型来查看它是否是从Base
派生的。
相关文章:
- 为什么使用 "this" 指针调用派生成员函数?
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 在派生函数中指定void*参数
- 如何通过派生类函数更改基类中的向量
- 当在带有Eigen的C++中使用GDB时,我如何才能看到更多的大矩阵
- 如何找出GDB的SIGTRAP核心转储的根本原因
- 如何委托派生类使用其父构造函数?
- 如何使用单独文件中的派生类访问友元函数对象
- 派生类销毁的最佳实践是什么
- 如何使用基类指针引用派生类成员
- 派生类是否可以在抽象工厂设计模式中具有数据成员
- 使用基类指针创建对象时,缺少派生类析构函数
- 如何引用基类的派生类?
- 存储模板类型以强制转换回派生<T>
- 需要从 istream 和 ostream 派生 iostream
- 在 C++ 中用派生类型重写成员函数
- 如何使用gdb制作一个可以漂亮地打印每个对象的C++函数
- 具有多个类、派生类的C++正向声明
- 用于从一个基派生的类的GDB漂亮的打印机
- 如何从指向GDB基类的指针确定对象是否是某个派生c++类的实例