0

I have been trying to implement reflection for a project. More or less its worked up already, but im not finding a workaround for the allocation of the struct without writing huge boilerplate, cause the different structs declaration come from a game, and there are more than a hundred.

The problem is, when loading an archive from the game it has all the struct names. I need to allocate the struct by getting the c type string converted to a token. Like this:

Struct1Name* inst = ...malloc...;

So converting the "Struct1Name" string as it comes from the file, but somehow paste it as a token. Need to do this for each struct present in the archive, which of course will have different names.

Struct1Name is defined as follows, for example:

typedef struct {float f1;bool b1;}Struct1Name;

I dont know what steps im missing here...and im not able to come up with a solution.

9
  • 5
    You need a different data-structure to keep name/type and "structure". And please don't tag multiple different languages. C and C++ should almost never be tagged together. Commented Oct 14 at 21:05
  • 5
    Consider using a serialisation library / toolkit, for example Protocol Buffers. Commented Oct 14 at 21:05
  • 3
    Also note that reflection is incredibly hard, and you're better of with either serialization (as mentioned by @RichardCritten) or by using an existing reflection framework. Don't reinvent the wheel, especially for such a complex thing. Commented Oct 14 at 21:07
  • 4
    A macro is very rarely the right solution. Commented Oct 14 at 21:24
  • 2
    This is the wrong answer, but I don't think there's a right one. Based on your description, this suggests some kind of script (perl or otherwise) to scan the archive and generate the appropriate code, which can be built as a shared library and loaded at runtime. Commented Oct 14 at 21:30

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.