Le mardi 15 septembre 2009 à 12:01 +0100, G. Perendia a écrit :
- C++ mexGetVariablePtr() can access either pointers to global structure
(with option global), or pointer to the structure local to the calling Matlab function (with option local) which would be the same as what would be passed to the called C++ function as argument.
If C++ code changes and needs additional parameter, then it only need to call different param from the interface.
This is precisely what I don't like in the general case. In some cases, I think it is better to have the explicit set of parameters in the function prototype (as arguments), so that one can see immediately what are those used by the function.
For example suppose that an internal function of the DLL uses only one option. It makes no sense to pass the pointer to a structure containing all the options used by the calling Matlab function, because it gives the impression that this function depends on the whole options set: this breaks modularity...
But as a general rule I don't claim that your framework is useless. I think that you have to make different decisions depending on the situation: * if all the functions in the calling tree of a given DLL need all the options passed by the calling Matlab function, then it makes sense to use your framework * on the contrary, if the deepest functions in the calling tree need only a small subset of the options passed by the calling Matlab function, then I think you should disaggregate the options and pass as arguments to the deepest functions only those that are needed.
- Other advantage of the mexGetVariablePtr() based interface is that it
allows for easy update of the structures (needed by estimate), including real-time, interactive updates if any future parallelisation requires that real time facility.
This is a good point provided that you add lock/mutex features.
- I will however start working on altering the code as you suggested and
create one structure to pass it from Matlab, i.e. create it before calling C++.
This is not my point, as I hope to have made clear in this message.
Is it ok if I still use mexGetVariablePtr() to pass it by reference or shall I pass it as argument (also by reference) anyway? An alternative is to use mexGetVariable() which copies the structures locally.
Of course mexGetVariablePtr() is better, in order to avoid unnecessary copying.
Also note that, as a general rule, my opinion is that we should use passing-by-reference when possible. Passing-by-pointer and passing-by-value (except for integers and doubles) should be avoided whenever possible.