My hierarchy (in Python) looks like this:
Code: Select all
device.CAN.
queue
CAN1
message
CAN2
In C, "device" is "mp_obj_module_t" as is "CAN".
The problem is that currently, if the user says "device.CAN.queue = 55" then that'll replace "queue" with "55" . . . which is very bad because now they can no longer create queues to read from the CAN channel until they reboot the system.
Also, "del device.CAN.CAN1" will also "work" i.e. it'll delete "device.CAN.CAN1" which again, is very bad.
Code: Select all
const mp_obj_type_t mp_type_module = {
{ &mp_type_type },
.name = MP_QSTR_module,
.print = module_print,
.attr = module_attr,
};
Is there a way for me to define a new type in C so that:
(1) I can override one or more functions but inherit the ones defined in the "base" type?
In my case, I'd like to override "mp_type_module.attr"
(2) MP_OBJ_IS_TYPE(obj, &mp_type_module) returns TRUE if object "obj" is "inherited" from &mp_type_module
I had hoped const void * struct _mp_obj_type_t::parent; would do what I need but it turns out this field is only used for types created in ".py" code.
I already know (2) is not possible using current definition of MP_OBJ_IS_TYPE(o, t) because
#define MP_OBJ_IS_TYPE(o, t) (MP_OBJ_IS_OBJ(o) && (((mp_obj_base_t*)MP_OBJ_TO_PTR(o))->type == (t)))
this macro simply compares the type-ptr without doing some kind of search in the hierarchy.
I know that for the case of mp_type_module.attr, I could "lock" it by setting dict->map.is_fixed=1 after I'm done creating the hierarchy but that's not a general solution.