2

I am using a C library in my Objective C project. The C library offers the following function

void processData(...);

which can be used with 1, 2 or 3 parameters, where the first parameter is mandatory and can have different types int, double, long, float and the other two arguments are optional and have int and long values and can be in whatever order.

Examples of use of this function are:

int myInt = 2;
double myDouble = 1.23;
int dataQuality = 1;
long dataTimestamp= GET_NOW();

processData(myInt);
processData(myInt, dataQuality);
processData(myDouble, dataQuality, dataTimestamp);
processData(myDouble, dataTimestamp);

I need to make an Objetive C wrapper that uses DataType class to call processDatawith the correct parameters. The Data class has getters that allows to get the data type (first argument), its value and whether the second and third arguments have value and their value.

The problem is how to make this expansion? I think it must be done at compile time, and I think the only mechanism available in C to do so is macros. But I have never used them. The implementation should be something like this (the following is pseudocode, where the arguments list is evaluated at runtime, something that I guess should be replaced by macros in order to evaluate the arguments at compile time):

-(void) objetiveCProcessData: (Data) d {
      argumentList = {}
      switch (d.getDataType()) {
         case INT_TYPE:
              append(argumentList, d.getValueAsInt()); // <-- appends a value with type `int`
              break;
         case DOUBLE_TYPE:
              append(argumentList, d.getValueAsDouble()); // <-- appends a value with type `double`
              break;
         ...
      }

      if (d.hasQuality()) {
           append(argumentList, d.getQuality());
      }

      if (d.hasTimeStamp()) {
           append(argumentList, d.getTimestamp());
      }

      // Call to the C function with correct number and type of arguments
      processData(argumentList);          
}
José D.
  • 4,175
  • 7
  • 28
  • 47
  • i might be mistaken, but... it seems like what you need is a macro in standard c99 to allow 'varargs' to a c function? maybe this is a good lead? http://stackoverflow.com/questions/5588855/standard-alternative-to-gccs-va-args-trick or this? https://www.eskimo.com/~scs/cclass/int/sx11b.html – pestophagous Jul 08 '15 at 17:17
  • if the `Data` class has getters, and if you rely on calling the getters, then i do not see how you could accomplish that at compile time. (??) – pestophagous Jul 08 '15 at 17:19

0 Answers0