Are there some kind of generics available?

For example, the format uses arrays with an uchar prefix to tell how long they are in number of elements. I want to write that definition once and then reuse it for every kind of type that can be contained in the array. Is something like that possible or do I have to manually write every single combination that is used?

struct generic_array (type param_type) {
  uchar len;
  param_type contents[len] <optimize=false>;
};

I managed to find that you can give arguments to structs, but I don’t see a way to say that the argument is a type and not data.

There is no way currently to pass a type to a struct. The best way is to probably use an enum to specify the type and then use a switch statement in the function. Something like:

enum TYPE { TYPE_INT, TYPE_UCHAR };

struct generic_array (TYPE param_type) {
  local uchar len;
  switch (param_type) {
      case 0 : int contents[len]; break;
      case 1 : uchar contents[len]; break;
  }  
};

generic_array a(TYPE_UCHAR);

Graeme Sweet
SweetScape Software

1 Like