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/>.
24 class Unordered(object):
25 '''Common representation of Boost.Unordered types'''
27 def __init__(self
, value
, extractor
):
29 self
.extractor
= extractor
30 self
.node_type
= self
._node
_type
()
33 table
= self
.value
['table_']
35 return int(table
['size_'])
40 table
= self
.value
['table_']
41 buckets
= table
['buckets_']
43 first
= table
['cached_begin_bucket_']
44 last
= buckets
+ table
['bucket_count_']
47 return self
._iterator
(first
, last
, self
.node_type
, self
.extractor
)
50 return not self
.value
['table_']['buckets_']
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
!= None
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
77 self
.node
= self
.node
.dereference()['next_']
79 # we finished the current bucket: go on to the next non-empty one
81 while not self
.node
and self
.bucket
!= self
.last_bucket
:
83 self
.node
= self
.bucket
.dereference()['next_']
85 # sorry, no node available
86 if not self
.node
or self
.node
== self
.bucket
:
89 mapped
= self
._value
()
90 return (self
.extractor
.key(mapped
), self
.extractor
.value(mapped
))
93 assert self
.node
!= self
.bucket
# bucket node has no value
94 assert self
.node
!= 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):
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):
126 def value(self
, node
):
129 # vim:set filetype=python shiftwidth=4 softtabstop=4 expandtab: