Alright, I have library I wrote in C that reads a file and provides access to its' data. The data is typed, so I'm using void pointer and a few accessor functions:
typedef struct nbt_tag
{
nbt_type type; /* Type of the value */
char *name; /* tag name */
void *value; /* value to be casted to the corresponding type */
} nbt_tag;
int64_t *nbt_cast_long(nbt_tag *t)
{
if (t->type != TAG_LONG) return NULL;
return (int64_t *)t->value;
}
For different types (built-ins: TAG_BYTE (char
), TAG_SHORT (int16_t
), TAG_INT (int32_t
), TAG_LONG (int64_t
), TAG_FLOAT (float
), TAG_DOUBLE (double
), TAG_STRING (char *
) and a few slightly more complex data types, TAG_List (struct nbt_list
), TAG_COMPOUND (struct nbt_compound
), TAG_BYTE_ARRAY (struct nbt_byte_array
).
I'm now trying to map this to C++ in an elegant fashion but I can't get it done...
char getByte(); // TAG_BYTE
int16_t getShort(); // TAG_SHORT
int32_t getInt(); // TAG_INT
int64_t getLong(); // TAG_LONG
float getFloat(); // TAG_FLOAT
double getDouble(); // TAG_DOUBLE
std::string getString(); // TAG_STRING
std::vector<char> getByteArray(); // TAG_BYTE_ARRAY
std::vector<Tag> getCompound(); // TAG_COMPOUND
This feels way too verbose.. any better way?