129 lines · python
1# Synthetic children provider example for class MaskedData2# to use me:3# command script import ./example.py --allow-reload4# type synthetic add MaskedData --python-class5# example.MaskedData_SyntheticChildrenProvider6 7 8class MaskedData_SyntheticChildrenProvider:9 def __init__(self, valobj, dict):10 # remember the SBValue since you will not have another chance to get it11 # :-)12 self.valobj = valobj13 14 def num_children(self):15 # you could perform calculations involving the SBValue and/or its children to determine this value16 # here, we have an hardcoded value - but since you have stored the SBValue you could use it to17 # help figure out the correct thing to return here. if you return a number N, you should be prepared to18 # answer questions about N children19 return 420 21 def has_children(self):22 # we simply say True here because we know we have 4 children23 # in general, you want to make this calculation as simple as possible24 # and return True if in doubt (you can always return num_children == 025 # later)26 return True27 28 def get_child_index(self, name):29 # given a name, return its index30 # you can return None if you don't know the answer for a given name31 if name == "value":32 return 033 # here, we are using a reserved C++ keyword as a child name - we could not do that in the source code34 # but we are free to use the names we like best in the synthetic children provider class35 # we are also not respecting the order of declaration in the C++ class itself - as long as36 # we are consistent, we can do that freely37 if name == "operator":38 return 139 if name == "mask":40 return 241 # this member does not exist in the original class - we will compute its value and show it to the user42 # when returning synthetic children, there is no need to only stick to43 # what already exists in memory44 if name == "apply()":45 return 346 return None # no clue, just say none47 48 def get_child_at_index(self, index):49 # precautionary measures50 if index < 0:51 return None52 if index > self.num_children():53 return None54 if not self.valobj.IsValid():55 return None56 if index == 0:57 return self.valobj.GetChildMemberWithName("value")58 if index == 1:59 # fetch the value of the operator60 op_chosen = self.valobj.GetChildMemberWithName("oper").GetValueAsUnsigned()61 # if it is a known value, return a descriptive string for it62 # we are not doing this in the most efficient possible way, but the code is very readable63 # and easy to maintain - if you change the values on the C++ side,64 # the same changes must be made here65 if op_chosen == 0:66 return self.valobj.CreateValueFromExpression(67 "operator", '(const char*)"none"'68 )69 elif op_chosen == 1:70 return self.valobj.CreateValueFromExpression(71 "operator", '(const char*)"AND"'72 )73 elif op_chosen == 2:74 return self.valobj.CreateValueFromExpression(75 "operator", '(const char*)"OR"'76 )77 elif op_chosen == 3:78 return self.valobj.CreateValueFromExpression(79 "operator", '(const char*)"XOR"'80 )81 elif op_chosen == 4:82 return self.valobj.CreateValueFromExpression(83 "operator", '(const char*)"NAND"'84 )85 elif op_chosen == 5:86 return self.valobj.CreateValueFromExpression(87 "operator", '(const char*)"NOR"'88 )89 else:90 return self.valobj.CreateValueFromExpression(91 "operator", '(const char*)"unknown"'92 ) # something else93 if index == 2:94 return self.valobj.GetChildMemberWithName("mask")95 if index == 3:96 # for this, we must fetch all the other elements97 # in an efficient implementation, we would be caching this data for98 # efficiency99 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 operator103 if operator == 1:104 value = value & mask105 elif operator == 2:106 value = value | mask107 elif operator == 3:108 value = value ^ mask109 elif operator == 4:110 value = ~(value & mask)111 elif operator == 5:112 value = ~(value | mask)113 else:114 pass115 value &= (116 0xFFFFFFFF # make sure Python does not extend our values to 64-bits117 )118 # return it - again, not the most efficient possible way. we should actually be pushing the computed value119 # into an SBData, and using the SBData to create an SBValue - this120 # has the advantage of readability121 return self.valobj.CreateValueFromExpression(122 "apply()", "(uint32_t)(" + str(value) + ")"123 )124 125 def update(self):126 # we do not do anything special in update - but this would be the right place to lookup127 # the data we use in get_child_at_index and cache it128 pass129