How to programmatically invoke a type's `read` or `write` attribute?

I’m looking for a way to invoke the read or write function on a variable of an arbitrary type, i.e. where the type is not known a-priori.

typedef struct {
} MyStruct1 <read=readMyStruct1>;

typedef struct {
} MyStruct1 <read=readMyStruct2>;

switch (ReadUByte()) {
case 1:
    MyStruct1 ms;
    break;
case 2;
    MyStruct2 ms;
    break;
...
}

// Here ms can be one of a few dozen different types
local string str = InvokeRead(ms);

There is currently no built-in function to do what you want, although we’ve had a few requests for this and will most likely add function for this to a future release. For now there should be a few ways to get the string, but it requires reorganizing some code. Here’s one way that uses a container structure:

typedef struct {
} MyStruct1 <read=readMyStruct1>;

typedef struct {
} MyStruct2 <read=readMyStruct2>;

typedef struct {
    local ubyte tag = ReadUByte();
    switch (tag) {
    case 1:
        MyStruct1 ms;
        break;
    case 2:
        MyStruct2 ms;
        break;
    }
} BaseStruct <read=ReadBaseStruct>;

string ReadBaseStruct( struct BaseStruct &b )
{
    switch( b.tag ) {
        case 1 : return readMyStruct1(b.ms);   
        case 2 : return readMyStruct2(b.ms);
    }
    return "";
}

BaseStruct bm;

// Can now read using the ReadBaseStruct function
local string str = ReadBaseStruct(bm);

Graeme
SweetScape Software

1 Like

That’s a great suggestion. Thank you!