Debian Codegraph @

solenv/gdb/boost/unordered.py

raw symbols SCIP
1 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
2 # GDB pretty printers for Boost.Unordered.
3 #
4 # Copyright (C) 2012 Red Hat, Inc., David Tardon <[email protected]>
5 #
6 # This file is part of boost-gdb-printers.
7 #
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.
12 #
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.
17 #
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/>.
20
21
22 import gdb
23 import six
24
25 from boost.lib.unordered import Map, Set
26
27 import boost.util.printing as printing
28
29 class PrinterBase(object):
30 '''Contains common functionality for printing Boost.Unordered types'''
31
32 def __init__(self, typename, value, container, iterator):
33 self.typename = typename
34 self.impl = container(value)
35 self.iterator = iterator
36
37 def to_string(self):
38 if self.impl.empty():
39 return "empty " + self.typename
40 else:
41 return "%s with %s elements" % (self.typename, len(self.impl))
42
43 def children(self):
44 return self.iterator(iter(self.impl))
45
46 class UnorderedMapPrinter(PrinterBase):
47
48 def __init__(self, typename, value):
49 super(UnorderedMapPrinter, self).__init__(typename, value, Map, self._iterator)
50
51 def display_hint(self):
52 return 'map'
53
54 class _iterator(six.Iterator):
55
56 def __init__(self, impl):
57 self.impl = impl
58 self.value = None
59 self.step = True
60
61 def __iter__(self):
62 return self
63
64 def __next__(self):
65 if self.step:
66 self.value = six.advance_iterator(self.impl)
67 value = self.value[0]
68 else:
69 value = self.value[1]
70 self.step = not self.step
71 return ("", value)
72
73 class UnorderedSetPrinter(PrinterBase):
74
75 def __init__(self, typename, value):
76 super(UnorderedSetPrinter, self).__init__(typename, value, Set, self._iterator)
77
78 def display_hint(self):
79 return 'array'
80
81 class _iterator(six.Iterator):
82
83 def __init__(self, impl):
84 self.impl = impl
85
86 def __iter__(self):
87 return self
88
89 def __next__(self):
90 return ("", six.advance_iterator(self.impl)[1])
91
92 printer = None
93
94 def build_pretty_printers():
95 global printer
96
97 if printer is not None:
98 return
99
100 printer = printing.Printer("boost.unordered")
101
102 printer.add('boost::unordered_map', UnorderedMapPrinter)
103 printer.add('boost::unordered_multimap', UnorderedMapPrinter)
104 printer.add('boost::unordered_multiset', UnorderedSetPrinter)
105 printer.add('boost::unordered_set', UnorderedSetPrinter)
106
107 def register_pretty_printers(obj):
108 printing.register_pretty_printer(printer, obj)
109
110 build_pretty_printers()
111
112 # vim:set filetype=python shiftwidth=4 softtabstop=4 expandtab: