1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
# Helper classes for working with Boost.Unordered.
#
# Copyright (C) 2012 Red Hat, Inc., David Tardon <dtardon@redhat.com>
#
# This file is part of boost-gdb-printers.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import gdb
import six
class Unordered(object):
'''Common representation of Boost.Unordered types'''
def __init__(self, value, extractor):
self.value = value
self.extractor = extractor
self.node_type = self._node_type()
def __len__(self):
table = self.value['table_']
if table['buckets_']:
return int(table['size_'])
else:
return 0
def __iter__(self):
table = self.value['table_']
buckets = table['buckets_']
if buckets:
first = table['cached_begin_bucket_']
last = buckets + table['bucket_count_']
else:
first = last = None
return self._iterator(first, last, self.node_type, self.extractor)
def empty(self):
return not self.value['table_']['buckets_']
def _node_type(self):
hash_table = self.value['table_'].type.fields()[0]
assert hash_table.is_base_class
hash_buckets = hash_table.type.fields()[0]
assert hash_buckets.is_base_class
node_type = gdb.lookup_type("%s::node" % hash_buckets.type)
assert node_type != None
return node_type
class _iterator(six.Iterator):
'''Iterator for Boost.Unordered types'''
def __init__(self, first_bucket, last_bucket, node_type, extractor):
self.bucket = first_bucket
self.last_bucket = last_bucket
self.node = self.bucket
self.node_type = node_type
self.value_type = self._value_type()
self.extractor = extractor
def __iter__(self):
return self
def __next__(self):
if self.node:
self.node = self.node.dereference()['next_']
# we finished the current bucket: go on to the next non-empty one
if not self.node:
while not self.node and self.bucket != self.last_bucket:
self.bucket += 1
self.node = self.bucket.dereference()['next_']
# sorry, no node available
if not self.node or self.node == self.bucket:
raise StopIteration()
mapped = self._value()
return (self.extractor.key(mapped), self.extractor.value(mapped))
def _value(self):
assert self.node != self.bucket # bucket node has no value
assert self.node != None
node = self.node.dereference().cast(self.node_type)
return node['data_'].cast(self.value_type)
def _value_type(self):
value_base = self.node_type.fields()[1]
assert value_base.is_base_class
return value_base.type.template_argument(0)
class Map(Unordered):
def __init__(self, value):
super(Map, self).__init__(value, self._extractor())
class _extractor(object):
def key(self, node):
return node['first']
def value(self, node):
return node['second']
class Set(Unordered):
def __init__(self, value):
super(Set, self).__init__(value, self._extractor())
class _extractor(object):
def key(self, node):
return None
def value(self, node):
return node
# vim:set filetype=python shiftwidth=4 softtabstop=4 expandtab:
|