我正在尝试为boost :: unordered_map添加一台漂亮的打印机,方法是在现有的boost pretty打印机列表中添加一台新的打印机: https://sourceware.org/gdb/wiki/STLSupport https://github.com/ruediger/Boost-Pretty-Printer
然而,当我尝试打印地图时,会多次打印很多值并且会出现一些不需要的字符。我不确定应该对我现有的代码进行哪些修改,使其仅打印一次,不包含任何不需要的字符。这是我在myMap.cpp中更新myMap的循环:
for (int i=0; i<=10; i++)
{
string s = to_string(i);
myMap[s] = i;
}
当我在gdb中打印myMap作为
时(gdb)p myMap
我得到如下结果:
$1 = boost::unordered::detail = {"1" = 1, "0" = 0, "2" = 2, "1" = 1, "0" = 0, "3" = 3,
"2" = 2, "1" = 1, "0" = 0, "4" = 4, "3" = 3, "2" = 2, "1" = 1, "0" = 0, "5" = 5, "4" = 4,
"3" = 3, "2" = 2, "1" = 1, "0" = 0, "6" = 6, "5" = 5, "4" = 4, "3" = 3, "2" = 2, "1" = 1,
"0" = 0, "7" = 7, "10" = 10, "6" = 6, "5" = 5, "4" = 4, "3" = 3, "2" = 2, "1" = 1,
"0" = 0, "8" = 8, "7" = 7, "10" = 10, "6" = 6, "5" = 5, "4" = 4, "3" = 3, "2" = 2,
"1" = 1, "0" = 0, "9" = 9, "8" = 8, "7" = 7, "10" = 10, "6" = 6, "5" = 5, "4" = 4,
"3" = 3, "2" = 2, "1" = 1, "0" = 0,
" \204`", '\000' <repeats 13 times>, "\321\n\002", '\000' <repeats 181 times>... = 0,
"9" = 9, "8" = 8, "7" = 7, "10" = 10, "6" = 6, "5" = 5, "4" = 4, "3" = 3, "2" = 2,
"1" = 1, "0" = 0}
我的漂亮打印机的python代码如下所示:
@_register_printer
class BoostUnorderedMap:
"Pretty Printer for boost::unordered_map"
printer_name = 'boost::unordered_map'
version = '1.40'
type_name_re = '^boost::unordered::unordered_map<.*>$'
class _iterator:
def __init__(self,fields):
type_1 = fields.val.type.template_argument(0)
type_2 = fields.val.type.template_argument(1)
self.buckets = fields.val['table_']['buckets_']
self.bucket_count = fields.val['table_']['bucket_count_']
self.bucket_size = fields.val['table_']['size_']
self.current_bucket = 0
self.value = 0
self.iteration = 0
self.temp_node = 0
self.bucket_to_start = 0
pair = "std::pair<%s const, %s>" % (type_1, type_2)
self.pair_pointer = gdb.lookup_type(pair).pointer()
self.base_pointer = gdb.lookup_type("boost::unordered::detail::value_base< %s >" % pair).pointer()
self.node_pointer = gdb.lookup_type("boost::unordered::detail::ptr_node<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> > const, int> >").pointer()
self.node = self.buckets[self.current_bucket]['next_']
def __iter__(self):
return self
def next(self):
while not self.node:
self.current_bucket = self.current_bucket + 1
if self.current_bucket >= self.bucket_count:
raise StopIteration
self.node = self.buckets[self.current_bucket]['next_']
iterator = self.node.cast(self.node_pointer).cast(self.base_pointer).cast(self.pair_pointer).dereference()
self.node = self.node['next_']
return ('%s' % iterator['first'], iterator['second'])
def __init__(self, val):
self.val = val
def children(self):
return self._iterator(self)
def to_string(self):
return "boost::unordered::detail"
任何帮助都将不胜感激。
答案 0 :(得分:0)
如果不了解有关Boost内部的更多信息,很难确切地知道出了什么问题。如果是我,我可能会破解“下一步”方法来添加一些调试打印以查看出错的地方。
无论如何,有一件事在你的Python代码中脱颖而出。现在你要返回像这样的子元素:
return ('%s' % iterator['first'], iterator['second'])
但是,使用此API的常用方法是将地图的键和值作为单独的子项返回,然后使用返回“map”的display_hint方法。参见
https://sourceware.org/gdb/onlinedocs/gdb/Pretty-Printing-API.html
您可以在下一个方法中使用“yield”轻松完成此操作。