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/.
11 from collections
.abc
import Mapping
13 from collections
import Mapping
18 from libreoffice
.util
.compatibility
import use_gdb_printing
20 class SimplePrinter(object):
22 def __init__(self
, name
, function
):
24 self
.function
= function
27 def invoke(self
, val
):
30 return self
.function(self
.name
, val
)
32 class NameLookup(Mapping
):
36 self
.name_regex
= re
.compile(r
'^([\w:]+)(<.*>)?')
38 def add(self
, name
, printer
):
39 self
.map[name
] = printer
44 def __getitem__(self
, type):
45 typename
= self
._basic
_type
(type)
46 if typename
and typename
in self
.map:
47 return self
.map[typename
]
53 def _basic_type(self
, type):
54 basic_type
= self
.basic_type(type)
56 match
= self
.name_regex
.match(basic_type
)
63 if type.code
== gdb
.TYPE_CODE_REF
:
65 type = type.unqualified().strip_typedefs()
68 class FunctionLookup(Mapping
):
73 def add(self
, test
, printer
):
74 self
.map[test
] = printer
79 def __getitem__(self
, type):
80 for (test
, printer
) in six
.iteritems(self
.map):
88 class Printer(object):
90 def __init__(self
, name
):
93 self
.name_lookup
= NameLookup()
94 self
.func_lookup
= FunctionLookup()
97 def add(self
, name
, function
, lookup
= None):
98 printer
= SimplePrinter(name
, function
)
99 self
.subprinters
.append(printer
)
101 self
.name_lookup
.add(name
, printer
)
103 self
.func_lookup
.add(lookup
, printer
)
106 def __call__(self
, val
):
107 printer
= self
.name_lookup
[val
.type]
109 printer
= self
.func_lookup
[val
.type]
112 return printer
.invoke(val
)
115 def register_pretty_printer(printer
, obj
):
116 '''Registers printer with objfile'''
119 gdb
.printing
.register_pretty_printer(obj
, printer
)
123 obj
.pretty_printers
.append(printer
)
125 # vim:set shiftwidth=4 softtabstop=4 expandtab: