1 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
2 # GDB pretty printers for Boost.Pointer Container.
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/>.
25 from boost
.lib
.unordered
import Map
, Set
27 import boost
.util
.printing
as printing
31 class PtrStdPrinterBase(object):
33 def __init__(self
, typename
, value
, seq_tag
):
35 self
.typename
= typename
38 # (try to) init printer of underlying std sequence and get elements
39 printer
= self
._get
_sequence
_printer
(seq_tag
)
42 if str(seq
.type.strip_typedefs()).startswith('std::__debug::'):
43 seq_typename
= 'std::__debug::%s' % seq_tag
45 seq_typename
= 'std::%s' % seq_tag
46 self
.sequence
= list(printer(seq_typename
, seq
).children())
51 if self
.sequence
!= None:
52 length
= len(self
.sequence
)
54 return "%s %s" % (self
.typename
, self
.print_size(length
))
56 return "empty %s" % self
.typename
58 return "opaque %s" % self
.typename
61 return self
._iterator
(self
.sequence
, self
.value
.type.template_argument(0))
63 class _iterator(six
.Iterator
):
65 def __init__(self
, sequence
, type):
66 self
.impl
= iter(sequence
)
67 self
.type = type.pointer()
73 (index
, value
) = six
.advance_iterator(self
.impl
)
74 return (index
, value
.cast(self
.type).dereference())
76 def _import_std(self
):
80 import libstdcxx
.v6
.printers
81 std
= libstdcxx
.v6
.printers
85 def _get_sequence_printer(self
, typename
):
86 if typename
== "deque":
87 return std
.StdDequePrinter
88 if typename
== "list":
89 return std
.StdListPrinter
91 return std
.StdMapPrinter
93 return std
.StdSetPrinter
94 if typename
== "vector":
95 return std
.StdVectorPrinter
97 class PtrSequencePrinter(PtrStdPrinterBase
):
99 def __init__(self
, typename
, value
, seq_tag
):
100 super(PtrSequencePrinter
, self
).__init
__(typename
, value
, seq_tag
)
102 def print_size(self
, size
):
103 return "of length %s" % size
105 def display_hint(self
):
108 class PtrSetPrinter(PtrStdPrinterBase
):
110 def __init__(self
, typename
, value
):
111 super(PtrSetPrinter
, self
).__init
__(typename
, value
, 'set')
113 def print_size(self
, size
):
114 return "with %s elements" % size
116 def display_hint(self
):
119 class PtrMapPrinter(PtrStdPrinterBase
):
121 def __init__(self
, typename
, value
):
122 super(PtrMapPrinter
, self
).__init
__(typename
, value
, 'map')
125 type = self
.value
.type
126 return self
._iterator
(self
.sequence
, type.template_argument(0), type.template_argument(1))
128 class _iterator(six
.Iterator
):
130 def __init__(self
, sequence
, key_type
, value_type
):
131 self
.impl
= iter(sequence
)
132 self
.key_type
= key_type
133 self
.value_type
= value_type
.pointer()
140 (index
, value
) = six
.advance_iterator(self
.impl
)
142 value
= value
.cast(self
.key_type
)
144 value
= value
.cast(self
.value_type
).dereference()
145 self
.key
= not self
.key
146 return (index
, value
)
148 def display_hint(self
):
151 def print_size(self
, size
):
152 return "with %s elements" % (size
/ 2)
154 class PtrBoostPrinterBase(object):
156 def __init__(self
, typename
, value
, container
, iterator
, value_type
):
157 self
.typename
= typename
158 self
.impl
= container(value
['c_'])
159 self
.iterator
= iterator
160 self
.value_type
= value_type
.pointer()
163 if self
.impl
.empty():
164 return "empty " + self
.typename
166 return "%s with %s elements" % (self
.typename
, len(self
.impl
))
169 return self
.iterator(iter(self
.impl
), self
.value_type
)
171 class PtrUnorderedMapPrinter(PtrBoostPrinterBase
):
173 def __init__(self
, typename
, value
):
174 super(PtrUnorderedMapPrinter
, self
).__init
__(typename
, value
, Map
, self
._iterator
,
175 value
.type.template_argument(1))
177 def display_hint(self
):
180 class _iterator(six
.Iterator
):
182 def __init__(self
, impl
, value_type
):
186 self
.value_type
= value_type
193 self
.value
= six
.advance_iterator(self
.impl
)
194 value
= self
.value
[0]
196 value
= self
.value
[1].cast(self
.value_type
).dereference()
197 self
.step
= not self
.step
200 class PtrUnorderedSetPrinter(PtrBoostPrinterBase
):
202 def __init__(self
, typename
, value
):
203 super(PtrUnorderedSetPrinter
, self
).__init
__(typename
, value
, Set
, self
._iterator
,
204 value
.type.template_argument(0))
206 def display_hint(self
):
209 class _iterator(six
.Iterator
):
211 def __init__(self
, impl
, value_type
):
213 self
.value_type
= value_type
219 return ("", six
.advance_iterator(self
.impl
)[1].cast(self
.value_type
).dereference())
223 def build_pretty_printers():
229 printer
= printing
.Printer("boost.ptr_container")
231 printer
.add('boost::ptr_deque', (lambda t
, v
: PtrSequencePrinter(t
, v
, "deque")))
232 printer
.add('boost::ptr_list', (lambda t
, v
: PtrSequencePrinter(t
, v
, "list")))
233 printer
.add('boost::ptr_map', PtrMapPrinter
)
234 printer
.add('boost::ptr_multimap', PtrMapPrinter
)
235 printer
.add('boost::ptr_multiset', PtrSetPrinter
)
236 printer
.add('boost::ptr_set', PtrSetPrinter
)
237 printer
.add('boost::ptr_unordered_map', PtrUnorderedMapPrinter
)
238 printer
.add('boost::ptr_unordered_multimap', PtrUnorderedMapPrinter
)
239 printer
.add('boost::ptr_unordered_multiset', PtrUnorderedSetPrinter
)
240 printer
.add('boost::ptr_unordered_set', PtrUnorderedSetPrinter
)
241 printer
.add('boost::ptr_vector', (lambda t
, v
: PtrSequencePrinter(t
, v
, "vector")))
243 def register_pretty_printers(obj
):
244 printing
.register_pretty_printer(printer
, obj
)
246 build_pretty_printers()
248 # vim:set filetype=python shiftwidth=4 softtabstop=4 expandtab: