1 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 from collections
import Mapping
15 from libreoffice
.util
.compatibility
import use_gdb_printing
17 class SimplePrinter(object):
19 def __init__(self
, name
, function
):
21 self
.function
= function
24 def invoke(self
, val
):
27 return self
.function(self
.name
, val
)
29 class NameLookup(Mapping
):
33 self
.name_regex
= re
.compile('^([\w:]+)(<.*>)?')
35 def add(self
, name
, printer
):
36 self
.map[name
] = printer
41 def __getitem__(self
, type):
42 typename
= self
._basic
_type
(type)
43 if typename
and typename
in self
.map:
44 return self
.map[typename
]
50 def _basic_type(self
, type):
51 basic_type
= self
.basic_type(type)
53 match
= self
.name_regex
.match(basic_type
)
60 if type.code
== gdb
.TYPE_CODE_REF
:
62 type = type.unqualified().strip_typedefs()
65 class FunctionLookup(Mapping
):
70 def add(self
, test
, printer
):
71 self
.map[test
] = printer
76 def __getitem__(self
, type):
77 for (test
, printer
) in six
.iteritems(self
.map):
85 class Printer(object):
87 def __init__(self
, name
):
90 self
.name_lookup
= NameLookup()
91 self
.func_lookup
= FunctionLookup()
94 def add(self
, name
, function
, lookup
= None):
95 printer
= SimplePrinter(name
, function
)
96 self
.subprinters
.append(printer
)
98 self
.name_lookup
.add(name
, printer
)
100 self
.func_lookup
.add(lookup
, printer
)
103 def __call__(self
, val
):
104 printer
= self
.name_lookup
[val
.type]
106 printer
= self
.func_lookup
[val
.type]
109 return printer
.invoke(val
)
112 def register_pretty_printer(printer
, obj
):
113 '''Registers printer with objfile'''
116 gdb
.printing
.register_pretty_printer(obj
, printer
)
120 obj
.pretty_printers
.append(printer
)
122 # vim:set shiftwidth=4 softtabstop=4 expandtab: