how to create C-type that inherits another C-type

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
jickster
Posts: 506
Joined: Thu Sep 07, 2017 8:57 pm

how to create C-type that inherits another C-type

Post by jickster » Wed Aug 01, 2018 6:21 pm

I know how to create types using C and how to use inheritance in .py but how do you make one type defined in C inherit another type defined in C?

User avatar
dhylands
Posts: 2848
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: how to create C-type that inherits another C-type

Post by dhylands » Wed Aug 01, 2018 10:57 pm

Well you don't really inherit in C. But the normal way is to include the base structure as the first element of the derived structure.

Something like this:

Code: Select all

typedef struct {
  int a;
  int b;
} base_t;

typedef struct {
  base_t base;
  int c;
} derived_t;

derived_t foo;
You'd then access the fields using foo.base.a, foo.base.b, and foo.c. &foo and &foo.base are the same, so you can pass pointers to foo around as if they were base_t * or derived_t *.

If you don't want the additional dereference, then you need to use macros to create the elements of the base structure. Something like this:

Code: Select all

#define BASE_FIELDS \
  int a; \
  int b;
  
typedef struct {
  BASE_FIELDS
} base_t;

typedef struct {
  BASE_FIELDS
  int c;
} derived_t;

derived_t foo;

jickster
Posts: 506
Joined: Thu Sep 07, 2017 8:57 pm

how to create C-type that inherits another C-type

Post by jickster » Wed Aug 01, 2018 11:03 pm

dhylands wrote:Well you don't really inherit in C. But the normal way is to include the base structure as the first element of the derived structure.

Something like this:

Code: Select all

typedef struct {
  int a;
  int b;
} base_t;

typedef struct {
  base_t base;
  int c;
} derived_t;

derived_t foo;
You'd then access the fields using foo.base.a, foo.base.b, and foo.c. &foo and &foo.base are the same, so you can pass pointers to foo around as if they were base_t * or derived_t *.

If you don't want the additional dereference, then you need to use macros to create the elements of the base structure. Something like this:

Code: Select all

#define BASE_FIELDS \
  int a; \
  int b;
  
typedef struct {
  BASE_FIELDS
} base_t;

typedef struct {
  BASE_FIELDS
  int c;
} derived_t;

derived_t foo;
But with the current runtime how would you access the members in .locals_dict of the base with a reference to derived?

If base has member b1 and you do

Code: Select all

d = derived()
d.b1()
I haven’t seen the path through runtime code that deals with this.

Also MP_OBJ_IS_TYPE(obj, base) doesn’t currently work if obj is a derived.

User avatar
dhylands
Posts: 2848
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: how to create C-type that inherits another C-type

Post by dhylands » Wed Aug 01, 2018 11:33 pm

OK- I was talking about how you might go about it in C in general and not specifically in MicroPython.

MicroPython doesn't currently have any mechanisms to use any type of C inheritance, so changes would need to be made to support whatever mechanism you decided to use.

jickster
Posts: 506
Joined: Thu Sep 07, 2017 8:57 pm

Re: how to create C-type that inherits another C-type

Post by jickster » Thu Aug 02, 2018 1:20 am

dhylands wrote:OK- I was talking about how you might go about it in C in general and not specifically in MicroPython.

MicroPython doesn't currently have any mechanisms to use any type of C inheritance, so changes would need to be made to support whatever mechanism you decided to use.
Will this be implemented?

Post Reply