mardi 31 août 2021

Best practice for compatibility with 32-bit systems for softwares using version dependent data files?

In the easy case, when the program is independent of any external data-base, one could write something of the form:

#include <stdint.h>
#if UINTPTR_MAX == 0xffffffff
/* 32-bit */
typedef some_type_for_32_bit_version Type;

#elif UINTPTR_MAX == 0xffffffffffffffff
/* 64-bit */
typedef some_type_for_64_bit_version Type;
#endif

And continue to work with the desired type.

Now suppose the program reads some meta/data file at the beginning, some bit represents if the file is built for 32-bit or 64-bit (files built for 32-bit should work on 64-bit as well, files built for 64-bit should only work on 64-bit). The program may operate pretty much the same for both cases, but has subtle differences, e.g. some variable is uint32_t for 32-bit and uint64_t for 64-bit.

A bad solution would be to start by reading that bit, have different version of that variable and any struct/union/function that use it, and have a ton of unnecessary if statements.

I thought of having some loader program that reads that byte, writes to some #define in another file, runs a compiler and finally runs the generated program - but that seems too nasty and I'm not thrilled by the idea of having to suffer compilation time at every run.

Is there any general design for that? Something specific to c? c++?

Aucun commentaire:

Enregistrer un commentaire