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/>.
23 from collections
.abc
import Mapping
25 from collections
import Mapping
30 from boost
.util
.compatibility
import use_gdb_printing
32 class SimplePrinter(object):
34 def __init__(self
, name
, function
):
36 self
.function
= function
39 def invoke(self
, val
):
42 return self
.function(self
.name
, val
)
44 class NameLookup(Mapping
):
48 self
.name_regex
= re
.compile(r
'^([\w:]+)(<.*>)?')
50 def add(self
, name
, printer
):
51 self
.map[name
] = printer
56 def __getitem__(self
, type):
57 typename
= self
._basic
_type
(type)
58 if typename
and typename
in self
.map:
59 return self
.map[typename
]
65 def _basic_type(self
, type):
66 basic_type
= self
.basic_type(type)
68 match
= self
.name_regex
.match(basic_type
)
75 if type.code
== gdb
.TYPE_CODE_REF
:
77 type = type.unqualified().strip_typedefs()
80 class FunctionLookup(Mapping
):
85 def add(self
, test
, printer
):
86 self
.map[test
] = printer
91 def __getitem__(self
, type):
92 for (test
, printer
) in six
.iteritems(self
.map):
100 class Printer(object):
102 def __init__(self
, name
):
104 self
.subprinters
= []
105 self
.name_lookup
= NameLookup()
106 self
.func_lookup
= FunctionLookup()
109 def add(self
, name
, function
, lookup
= None):
110 printer
= SimplePrinter(name
, function
)
111 self
.subprinters
.append(printer
)
113 self
.name_lookup
.add(name
, printer
)
115 self
.func_lookup
.add(lookup
, printer
)
118 def __call__(self
, val
):
119 printer
= self
.name_lookup
[val
.type]
121 printer
= self
.func_lookup
[val
.type]
124 return printer
.invoke(val
)
127 def register_pretty_printer(printer
, obj
):
128 '''Registers printer with objfile'''
131 gdb
.printing
.register_pretty_printer(obj
, printer
)
135 obj
.pretty_printers
.append(printer
)
137 # vim:set filetype=python shiftwidth=4 softtabstop=4 expandtab: