The title of this question is a little strange-sounding, but I could think of no better way to word it. My problem is this; I have a type inside a project called AmbiguousType
, which is a union in this format.
Declarations.h
(utility header file)
typedef union AmbiguousType
{
u32 unsigned32;
u64 unsigned64;
i32 signed32;
i64 signed64;
} AmbiguousType;
Now, I have many helper functions to better facilitate the use of this type. I was running through these functions, trying to puzzle out any ways to make them more efficient / less bulky, and realized something; most of the functions have a switch
statement as their main body. Take, for example, this function, which is used to assign to a given ambiguous type.
Declarations.c
(utility source file)
/**
* @brief Assign a value to an ambiguous type.
* @param affected The affected variable.
* @param member What state are we making the ambiguous type?
* @param value The value that state will have.
*/
void AssignAmbiguousType(AmbiguousType* affected, AmbiguousTypeSpecifier member,
void* value)
{
switch (member)
{
// Note that "VPTT" is a helper macro to turn a void pointer into the given type.
// U__ - unsigned __ bit integer.
// I__ - signed __ bit integer.
case unsigned32: affected->unsigned32 = VPTT(u32, value); return;
case unsigned64: affected->unsigned64 = VPTT(u64, value); return;
case signed32: affected->signed32 = VPTT(i32, value); return;
case signed64: affected->signed64 = VPTT(i64, value); return;
}
}
My question is, is there a pragmatic way to remove this switch
statement from the function, while keeping its use intact? It seems redundant to me, but I cannot figure out what could solve this issue.
I have tried using macros to simply combine tokens (##
), which obviously didn't work since the parameters are only known at runtime. I am at a loss here, though. Is there even a solution to this problem, or is this the best it'll get?
union
type entirely: just pass around a singleu64
value because it can be the source of reads of smaller typed values and the target of writes to smaller integer types.AmbiguousType
a member ofAmbiguousStruct
that has a 2nd member which is a pointer to a set of functions to use with this instance ofAmbiguousStruct
.u64
as a type-tag, provided the OP is okay with reducing it to a 56-bit integer?)uint62_t
, yet this approach (2 bits or a byte) still does not rid theswitch()
.