I'm working on a code base in which we have several configurable types. One of those types is 64 bit integer. When we compile for platforms that have no native 64 bit integer type, we simple represent 64 bit integers using a struct similar to
typedef struct {
unsigned int hi, lo;
} int64;
In order to make this type useful, all common operations are defined as functions such as
int64 int64_add(int64 x, int64 y);
On platforms where a native 64 bit integer type is available, these operations simply look like
#define int64_add(x, y) ((x) + (y))
Anyway, on to the question. I am implementing some functionality regarding time and I want to represent my time using the 64 bit integer:
typedef int64 mytime;
I also want all the common operations available to the int64 type to be available for my time type as well:
#define mytime_add(x, y) (mytime) int64_add((int64) (x), (int64) (y))
The problem with this is that the casts between the types mytime and int64 isn't allowed in C (as far as I can tell anyhow). Is there any way to do this without having to reimplement all the add, sub, mul, div, etc functions for the mytime type?
One option is of course to never do the mytime typedef and simply use int64 everywhere I need to represent time. The problem with this is that I'm not sure if I always want to represent time as a 64 bit integer. Then there's the issue of readable code as well... :-)