1 # Synthetic children provider example for class MaskedData
3 # command script import ./example.py --allow-reload
4 # type synthetic add MaskedData --python-class
5 # example.MaskedData_SyntheticChildrenProvider
8 class MaskedData_SyntheticChildrenProvider
:
9 def __init__(self
, valobj
, dict):
10 # remember the SBValue since you will not have another chance to get it
14 def num_children(self
):
15 # you could perform calculations involving the SBValue and/or its children to determine this value
16 # here, we have an hardcoded value - but since you have stored the SBValue you could use it to
17 # help figure out the correct thing to return here. if you return a number N, you should be prepared to
18 # answer questions about N children
21 def has_children(self
):
22 # we simply say True here because we know we have 4 children
23 # in general, you want to make this calculation as simple as possible
24 # and return True if in doubt (you can always return num_children == 0
28 def get_child_index(self
, name
):
29 # given a name, return its index
30 # you can return None if you don't know the answer for a given name
33 # here, we are using a reserved C++ keyword as a child name - we could not do that in the source code
34 # but we are free to use the names we like best in the synthetic children provider class
35 # we are also not respecting the order of declaration in the C++ class itself - as long as
36 # we are consistent, we can do that freely
37 if name
== "operator":
41 # this member does not exist in the original class - we will compute its value and show it to the user
42 # when returning synthetic children, there is no need to only stick to
43 # what already exists in memory
46 return None # no clue, just say none
48 def get_child_at_index(self
, index
):
49 # precautionary measures
52 if index
> self
.num_children():
54 if self
.valobj
.IsValid() == False:
57 return self
.valobj
.GetChildMemberWithName("value")
59 # fetch the value of the operator
60 op_chosen
= self
.valobj
.GetChildMemberWithName("oper").GetValueAsUnsigned()
61 # if it is a known value, return a descriptive string for it
62 # we are not doing this in the most efficient possible way, but the code is very readable
63 # and easy to maintain - if you change the values on the C++ side,
64 # the same changes must be made here
66 return self
.valobj
.CreateValueFromExpression(
67 "operator", '(const char*)"none"'
70 return self
.valobj
.CreateValueFromExpression(
71 "operator", '(const char*)"AND"'
74 return self
.valobj
.CreateValueFromExpression(
75 "operator", '(const char*)"OR"'
78 return self
.valobj
.CreateValueFromExpression(
79 "operator", '(const char*)"XOR"'
82 return self
.valobj
.CreateValueFromExpression(
83 "operator", '(const char*)"NAND"'
86 return self
.valobj
.CreateValueFromExpression(
87 "operator", '(const char*)"NOR"'
90 return self
.valobj
.CreateValueFromExpression(
91 "operator", '(const char*)"unknown"'
94 return self
.valobj
.GetChildMemberWithName("mask")
96 # for this, we must fetch all the other elements
97 # in an efficient implementation, we would be caching this data for
99 value
= self
.valobj
.GetChildMemberWithName("value").GetValueAsUnsigned()
100 operator
= self
.valobj
.GetChildMemberWithName("oper").GetValueAsUnsigned()
101 mask
= self
.valobj
.GetChildMemberWithName("mask").GetValueAsUnsigned()
102 # compute the masked value according to the operator
110 value
= ~
(value
& mask
)
112 value
= ~
(value | mask
)
116 0xFFFFFFFF # make sure Python does not extend our values to 64-bits
118 # return it - again, not the most efficient possible way. we should actually be pushing the computed value
119 # into an SBData, and using the SBData to create an SBValue - this
120 # has the advantage of readability
121 return self
.valobj
.CreateValueFromExpression(
122 "apply()", "(uint32_t)(" + str(value
) + ")"
126 # we do not do anything special in update - but this would be the right place to lookup
127 # the data we use in get_child_at_index and cache it