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