bump product version to 5.0.4.1
[LibreOffice.git] / solenv / gdb / boost / util / printing.py
blob1d5d0bac9fc6ebc4d6ab2df8058abea19a820f8e
1 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
2 # Printer interface adaptor.
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 from collections import Mapping
23 import gdb
24 import re
25 import six
27 from boost.util.compatibility import use_gdb_printing
29 class SimplePrinter(object):
31 def __init__(self, name, function):
32 self.name = name
33 self.function = function
34 self.enabled = True
36 def invoke(self, val):
37 if not self.enabled:
38 return None
39 return self.function(self.name, val)
41 class NameLookup(Mapping):
43 def __init__(self):
44 self.map = {}
45 self.name_regex = re.compile('^([\w:]+)(<.*>)?')
47 def add(self, name, printer):
48 self.map[name] = printer
50 def __len__(self):
51 return len(self.map)
53 def __getitem__(self, type):
54 typename = self._basic_type(type)
55 if typename and typename in self.map:
56 return self.map[typename]
57 return None
59 def __iter__(self):
60 return self.map
62 def _basic_type(self, type):
63 basic_type = self.basic_type(type)
64 if basic_type:
65 match = self.name_regex.match(basic_type)
66 if match:
67 return match.group(1)
68 return None
70 @staticmethod
71 def basic_type(type):
72 if type.code == gdb.TYPE_CODE_REF:
73 type = type.target()
74 type = type.unqualified().strip_typedefs()
75 return type.tag
77 class FunctionLookup(Mapping):
79 def __init__(self):
80 self.map = {}
82 def add(self, test, printer):
83 self.map[test] = printer
85 def __len__(self):
86 return len(self.map)
88 def __getitem__(self, type):
89 for (test, printer) in six.iteritems(self.map):
90 if test(type):
91 return printer
92 return None
94 def __iter__(self):
95 return self.map
97 class Printer(object):
99 def __init__(self, name):
100 self.name = name
101 self.subprinters = []
102 self.name_lookup = NameLookup()
103 self.func_lookup = FunctionLookup()
104 self.enabled = True
106 def add(self, name, function, lookup = None):
107 printer = SimplePrinter(name, function)
108 self.subprinters.append(printer)
109 if not lookup:
110 self.name_lookup.add(name, printer)
111 else:
112 self.func_lookup.add(lookup, printer)
115 def __call__(self, val):
116 printer = self.name_lookup[val.type]
117 if not printer:
118 printer = self.func_lookup[val.type]
120 if printer:
121 return printer.invoke(val)
122 return None
124 def register_pretty_printer(printer, obj):
125 '''Registers printer with objfile'''
127 if use_gdb_printing:
128 gdb.printing.register_pretty_printer(obj, printer)
129 else:
130 if obj is None:
131 obj = gdb
132 obj.pretty_printers.append(printer)
134 # vim:set filetype=python shiftwidth=4 softtabstop=4 expandtab: