bump product version to 5.0.4.1
[LibreOffice.git] / solenv / gdb / boost / ptr_container.py
blob425d812f54b5834c865b8d14ddcec8a202f30fdf
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/>.
22 import gdb
23 import six
25 from boost.lib.unordered import Map, Set
27 import boost.util.printing as printing
29 std = None
31 class PtrStdPrinterBase(object):
33 def __init__(self, typename, value, seq_tag):
34 self._import_std()
35 self.typename = typename
36 self.value = value
38 # (try to) init printer of underlying std sequence and get elements
39 printer = self._get_sequence_printer(seq_tag)
40 if printer:
41 seq = value['c_']
42 if str(seq.type.strip_typedefs()).startswith('std::__debug::'):
43 seq_typename = 'std::__debug::%s' % seq_tag
44 else:
45 seq_typename = 'std::%s' % seq_tag
46 self.sequence = list(printer(seq_typename, seq).children())
47 else:
48 self.sequence = None
50 def to_string(self):
51 if self.sequence != None:
52 length = len(self.sequence)
53 if length:
54 return "%s %s" % (self.typename, self.print_size(length))
55 else:
56 return "empty %s" % self.typename
57 else:
58 return "opaque %s" % self.typename
60 def children(self):
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()
69 def __iter__(self):
70 return self
72 def __next__(self):
73 (index, value) = six.advance_iterator(self.impl)
74 return (index, value.cast(self.type).dereference())
76 def _import_std(self):
77 global std
78 if not std:
79 try:
80 import libstdcxx.v6.printers
81 std = libstdcxx.v6.printers
82 except:
83 pass
85 def _get_sequence_printer(self, typename):
86 if typename == "deque":
87 return std.StdDequePrinter
88 if typename == "list":
89 return std.StdListPrinter
90 if typename == "map":
91 return std.StdMapPrinter
92 if typename == "set":
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):
106 return 'array'
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):
117 return 'array'
119 class PtrMapPrinter(PtrStdPrinterBase):
121 def __init__(self, typename, value):
122 super(PtrMapPrinter, self).__init__(typename, value, 'map')
124 def children(self):
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()
134 self.key = True
136 def __iter__(self):
137 return self
139 def __next__(self):
140 (index, value) = six.advance_iterator(self.impl)
141 if self.key:
142 value = value.cast(self.key_type)
143 else:
144 value = value.cast(self.value_type).dereference()
145 self.key = not self.key
146 return (index, value)
148 def display_hint(self):
149 return 'map'
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()
162 def to_string(self):
163 if self.impl.empty():
164 return "empty " + self.typename
165 else:
166 return "%s with %s elements" % (self.typename, len(self.impl))
168 def children(self):
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):
178 return 'map'
180 class _iterator(six.Iterator):
182 def __init__(self, impl, value_type):
183 self.impl = impl
184 self.step = True
185 self.value = None
186 self.value_type = value_type
188 def __iter__(self):
189 return self
191 def __next__(self):
192 if self.step:
193 self.value = six.advance_iterator(self.impl)
194 value = self.value[0]
195 else:
196 value = self.value[1].cast(self.value_type).dereference()
197 self.step = not self.step
198 return ("", value)
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):
207 return 'array'
209 class _iterator(six.Iterator):
211 def __init__(self, impl, value_type):
212 self.impl = impl
213 self.value_type = value_type
215 def __iter__(self):
216 return self
218 def __next__(self):
219 return ("", six.advance_iterator(self.impl)[1].cast(self.value_type).dereference())
221 printer = None
223 def build_pretty_printers():
224 global printer
226 if printer != None:
227 return
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: