views:

270

answers:

4

I have a C function named SetParams(...) with a variable number of arguments. This function sets up a static data structure (let us name it Data). SetParams is used with pairs of arguments, e.g. SetParams("paramA", paramA_value, "paramB", paramB_value) etc. It can also be called many times, e.g.

SetParams("paramA", paramA_value);
SetParams("paramB", paramB_value);
...

When all 'parameters' have been set, another function is called (let us name it Execute) that takes no args:

Execute();
// uses data from static 'Data' and performs error_handling and execution

I was wondering if I could structure this kind of code in a more object-oriented way. So, I would like some advice, especially for error-handling, since some pairs of args may contradict others.

+1  A: 

I would recommend using a linked list to store your params and put all your methods as function pointers to a struct.

struct MyClass {
  struct LinkedList* params;
  void (*setParams)(...);
  void (*execute)()
}

the linked list would be a key value pair

struct LinkedList {
   struct LinkedList *next;
   char * key;
   char * value;
}
Charles Ma
A: 

I dont know how you have your SetParams implemented, from the sound it just does a little bit of parsing and storing and forwards error handling downstream to the Execute call. Since you are using variable length arguments, are you using the va_* macros? Doing so with a format string might allow you to insert the error handling into your SetParams call and allow Execute to just iterate over the values and do its thing.

Generally, if you have a function that handles setting parameters that should be where you manage errors associated with setting parameters. Errors encountered in the execution of command should be addressed in the execute function.

ezpz
I could probably catch many errors in SetParams. But there is a possibility that something could be defined with many 'SetParams' calls, and i would mark it as an error since i wouldn't know that other 'SetParams' could be called to add to the problem definition.
A: 

You cannot do it this way, because in C variadic functions don't know the number of arguments you've supplied, so you need somehow let function know it, like specifying number of params as first parameter or use printf way, when number of parameters can be found from format string.

qrdl
@John Then you should specify it in your post.
qrdl
+2  A: 

The general practice for creating an object oriented design in C is for every method you have you will pass in a reference to a struct which is used to store all the classes member variables. In otherwords in C++ where you'd have listObj.clear() you have in C list_clear(&listObj).

This is kind of ugly, but it's necessary unless you want to use static member variables and limit the implementation to being used only once.

In the example below, notice how in each method a reference to a struct ParamUtilObj is passed in.

// --- paramUtil.h

// Stores all the objects member variables (public and private)
struct ParamUtilObj {
  int paramCnt;
  char param1[25];
  char param2[25];
  ...
};

bool paramUtil_initialize( struct* ParamUtilObj pData );
bool paramUtil_addParam( struct* ParamUtilObj pData, const char* pKey, const char* pValue );
bool paramUtil_execute( struct* ParamUtilObj pData);

With respect to variadic methods. I'd try to avoid them if possible and just add them in one at a time. The business logic to validate the params is an entirely different topic in my opinion. I'd need more info to recommend the best approach. But... It seems to me since you're going to have to do validation such as if( MethodA ) then check for presence of some other argument... it might be easier to create several SetParam methods for each MethodType which the user could specify in the script.

blak3r