2010-05-10 443 views
6

最近我開始在我的系統上使用優秀的boost :: unordered_map,但有一個缺點:我無法確定如何檢查它的內容。在gdb上打印給我一個table_和一個buckets_,但沒有找到這些項目在哪裏。任何人都有這方面的線索?gdb上的漂亮打印boost :: unordered_map

回答

10

對於那些想要打印機的人,我設法創建了一個。這裏是代碼:

class BoostUnorderedMapPrinter: 
    "prints a boost::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.current_bucket = 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::hash_node<std::allocator< %s >, boost::unordered_detail::ungrouped>" % pair).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_map" 
+10

我知道快更換不好我有點晚了,但是如何在GDB中加載(並使用)這臺漂亮的打印機? – 2013-01-09 00:17:25

+2

感謝您發佈此信息。它不適用於更高版本(我已經測試過1.58+),但是我在今天早上接受並更新了1.58。在稍微多飛行一段時間後,如果對你來說沒問題,我會向https://github.com/ruediger/Boost-Pretty-Printer發送一個拉請求。 – 2016-12-02 20:45:52

2

在典型的散列表實現中,存儲區包含一個鏈表的頭部,該鏈表實際上包含與此特定散列相對應的值。因此我敢打賭buckets_

另一種選擇:現在有許多用於gdb的python漂亮打印機庫,我認爲你可以找到一個可以與C++ 0x一起工作並檢查它在哪裏尋找值的地方。

+0

當然,我檢查了海灣合作委員會TR1實現和結構是很不同的,因爲我發現升壓實施比TR1對應 – scooterman 2010-05-11 13:20:02