1 # Formatters for classes that derive from Message.
4 # command script import ./example.py
5 # type summary add --expand --recognizer-function --python-function example.message_summary example.is_message_type
6 # type synth add --recognizer-function --python-class example.MessageChildProvider example.is_message_type
11 def is_message_type(t
, internal_dict
):
12 for base
in t
.get_bases_array():
13 if base
.GetName() == "Message":
18 def message_summary(value
, internal_dict
):
19 # Could have used a summary string as well. All the work is done by the child
24 class MessageChildProvider
:
25 def __init__(self
, value
, internal_dict
):
27 self
.synthetic_children
= self
._analyze
_children
(value
)
29 def has_children(self
):
30 return self
.num_children() > 0
32 def num_children(self
):
33 return len(self
.synthetic_children
)
35 def get_child_index(self
, name
):
36 for index
, child
in enumerate(self
.synthetic_children
):
37 if child
.GetName() == name
:
41 def get_child_at_index(self
, index
):
42 return self
.synthetic_children
[index
]
44 def _rename_sbvalue(self
, value
):
45 # We want to display the field with its original name without a trailing
46 # underscore. So we create a new SBValue with the same type and address but
48 name
= value
.GetName()
49 assert name
.endswith("_")
51 return value
.CreateValueFromAddress(
52 new_name
, value
.GetLoadAddress(), value
.GetType()
55 def _analyze_children(self
, value
):
57 for i
in range(value
.GetNumChildren()):
58 child
= value
.GetChildAtIndex(i
)
59 child_name
= child
.GetName()
60 if child_name
.startswith("_"):
61 continue # Internal field, skip
62 # Normal field. Check presence bit.
63 presence_bit
= value
.GetChildMemberWithName("_has_" + child_name
)
64 if presence_bit
.GetValueAsUnsigned() != 0:
65 result
.append(self
._rename
_sbvalue
(child
))