tags:

views:

108

answers:

1

I have a C module which is created by the Real-time Workshop based on a Simulink Model. This modules provides three public functions:

int init();
int calc(double *inputarray, double *outputarray);
int term();

Based on the contents of the outputarray, I can model a class called OutputThing.

I want to integrate those functions in a wrapper class called WrapperModule. Right now I have a class that looks like this:

class WrapperModule {
public:
    int initialize();
    OutputThing calc(...);
    int terminate();
};

My problem is, how to properly design a wrapper method for the calc() Function. I want to avoid to create a method with an array/vector as its single argument. But identifying the correct arguments from the vector is tricky and I dislike the idea of having a method with 6 or more arguments.

Bertrand Meyer in his OOSC book suggests the use of setter methods. Something like:

class WrapperModule {
public:
    int initialize();
    void set_foo(double f);
    void set_bar(double b);
    OutputThing calc();
    int terminate();
};

Any ideas? I'm not sure which approach would be better.

+3  A: 

If you are able to also abstract inputarray to an InputThing class I'd suggest the following. This also better encapsulates the initialisation/termination using C++ construction/destruction.

class WrapperModule {
public:
    // Calls init()
    WrapperModule();

    // Calls term()
    ~WrapperModule();

    // Calls calc()
    OutputThing calculate(const InputThing& inputThing);
};

If necessary, InputThing could have accessor and mutator (get/set) functions to prevent it needing a constructor taking many arguments.

Matthew Murdoch
InputThing and OutputThing can be std::vector<double> which can be used with C style function as it guarantees that the items are in continuos memory locations.
Naveen
Additionaly, I think it were best if both InputThing and OutputThing, closely coupled to the calculate function, be defined as an inner class of the WrapperModule class.
xtofl
@Naveen Jens suggests in his question that there is some structure to the output array which he wants to model with OutputThing so a vector of doubles may not cut it. Doing this may make sense for InputThing, though.
Matthew Murdoch
Yes, init() and term() are a one time thing. An InputThing could be used to group several attributes/arguments. Besides a few meta information I can extract a real type from the output vector. The input vectors contains a few possible types that could be used to model classes.
Jens