C programming multiple storage backends

Posted by ahjmorton on Programmers See other posts from Programmers or by ahjmorton
Published on 2013-10-18T20:37:05Z Indexed on 2013/10/18 22:15 UTC
Read the original article Hit count: 99

Filed under:
|

I am starting a side project in C which requires multiple storage backends to be driven by a particular piece of logic. These storage backends would each be linked with the decision of which one to use specified at runtime.

So for example if I invoke my program with one set of parameters it will perform the operations in memory but if I change the program configuration it would write to disk.

The underlying idea is that each storage backend should implement the same protocol. In other words the logic for performing operations should need to know which backend it is operating on.

Currently the way I have thought to provide this indirection is to have a struct of function pointers with the logic calling these function pointers. Essentially the struct would contain all the operations needed to implement the higher level logic E.g.

struct Context {
    void (* doPartOfDoOp)(void)
    int (* getResult)(void); 
}

//logic.h
void doOp(Context * context) {
    //bunch of stuff
    context->doPartOfDoOp();
}

int getResult(Context * context) {
    //bunch of stuff
    return context->getResult();
} 

My questions is if this way of solving the problem is one a C programmer would understand? I am a Java developer by trade but enjoy using C/++. Essentially the Context struct provides an interface like level of indirection. However I would like to know if there is a more idiomatic way of achieving this.

© Programmers or respective owner

Related posts about c

    Related posts about interfaces