I'm engineering library to provide functionality for one device. This device has some common operations but different algorithms to accomplish these operations. I want one function prototype for one particular operation, not a bunch of them, say instead of:
Alg1_Foo();
Alg2_Foo();
...
I want this:
Foo(alg);
But I don't want to pass alg as separate argument, because functions will have a lot of arguments even without it, they will have arguments for identification and/or authorization of the device, in argument, out argument (at least one of them), so I think it will be annoying to add alg as a separate argument.
So my idea is to provide solution like this one:
Foo(const SomeUnion& some_union);
Where:
union SomeUnion {
AlgId alg_id;
alg1::SomeStruct alg1_some_struct;
alg2::SomeStruct alg2_some_struct;
SomeUnion(alg1::SomeStruct some_struct) { alg1_some_struct = some_struct; };
SomeUnion(alg2::SomeStruct some_struct) { alg2_some_struct = some_struct; };
};
And the structures of particular algorithms will be like this:
namespace alg1 {
struct SomeStruct {
static const AlgId alg_id = ALG1;
. . .
};
}
So if want to perform say alg1, we pass appropriate structure to Foo and it works in C++, say
alg1::SomeStruct a;
Foo(a);
But I want my library to maintain possibilities of pure C. Of course I need to:
remove references and replace them with pointers;
remove namespaces (we can still emulate them with the help of structures (this thread may be helpful for those interested: Namespaces in C);
replace C++-style of defining structs to that from C and define also name in tag namespace (typedef struct tagStruct {...} Struct;);
remove functions from inside structures and unions.
But though I can't understand if it's possible to accomplish what I want to do with maintenance of C... Do you see the way? Or is it just simplier to pass alg_id as a separate argument not to bother with unions and structures (but I want to avoid it if possible)?