non-fundamental out parameters in C++ function calls
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi all,
currently, I'm trying to integrate the esmini library into a MATLAB project. When editing the generated mlx file, I noticed that using non-primitive refence type parameters leads to errors, for example when trying to implement the wrapper for SE_GetObjectState:
/**
Get the state of specified object
@param index Index of the object. Note: not ID
@param state Pointer/reference to a SE_ScenarioObjectState struct to be filled in
@return 0 if successful, -1 if not
*/
SE_DLL_API int SE_GetObjectState(int index, SE_ScenarioObjectState *state);
The MATLAB implementation should, therefore, look like this:
SE_GetObjectStateDefinition = addFunction(libDef, ...
"int SE_GetObjectState(int index,SE_ScenarioObjectState * state)", ...
"MATLABName", "clib.esminiLib.SE_GetObjectState", ...
"Description", "clib.esminiLib.SE_GetObjectState Representation of C++ function SE_GetObjectState."); % Modify help description values as needed.
defineArgument(SE_GetObjectStateDefinition, "index", "int32");
defineArgument(SE_GetObjectStateDefinition, "state", "clib.esminiLib.SE_ScenarioObjectState", "output", 1); % '<MLTYPE>' can be clib.esminiLib.SE_ScenarioObjectState, or clib.array.esminiLib.SE_ScenarioObjectState
defineOutput(SE_GetObjectStateDefinition, "RetVal", "int32");
validate(SE_GetObjectStateDefinition);
Note, that the DIRECTION of the second parameter is "output".
When validating the definition, I'm receiving the following vague error:
Error using clibgen.MethodDefinition/verifyDirection (line 197)
Invalid direction specified for argument 'state'. Expected direction is 'input'.
Error in clibgen.FunctionDefinition/defineArgument (line 328)
clibgen.MethodDefinition.verifyDirection(varargin{1}, argAnnotation, argType);
Error in defineesminiLib (line 679)
defineArgument(SE_GetObjectStateDefinition, "state", "clib.array.esminiLib.SE_ScenarioObjectState", "output", 1); %
'<MLTYPE>' can be clib.esminiLib.SE_ScenarioObjectState, or clib.array.esminiLib.SE_ScenarioObjectState
I assume the explanation for is lies in this comment in clibgen.MethodDefinition/verifyDirection:
% Direction for reference to non-fundamental type can be "input" only
% for pointer to non-fundamental must be "input" either
% it is a scalar or a C++ array
% excludes void*.
But I'm not really sure if this is the underlying issue since the MATLAB documentation on Limitations to C/C++ support seems to not include this case.
Assuming that non-primitive reference types could not be used, how would the workaround look like? Implementing some helper functions in C++? In this case to initialize the object, calling the function above and returning it?
2 Kommentare
kate_w
am 22 Feb. 2022
Hello,
I had similar issue. I wanted to pass pointer to typedef struct object to function (which was later compiled in MATLAB). Fields of this struct are filled in my function.
In MATLAB I set DIRECTION of this to non-fundamental type to "input" and shape to 1. After MATLAB build I created object of this type and just passed it to the function. After return from the function, fields of my non-fundamental type object were filled as expected.
Cedric Gilbert
am 22 Apr. 2023
Got the same issue.
Have a method 'X' that compute some values from two 4x1 arrays (i.e. A anb B) into a 4x1 array (out).
Type of array are double but method X typedefed i.e. typdedef mydouble double.
Then matlab method within Cmex is defined as :
X:mymethod(clib.array.X.mydouble,clib.array.X.mydouble,clib.array.X.mydouble).
for instance mymethod is out(i) = A(i)*B(i) looped with i<4 etc...
Called from matlab :
A = [1 1 1 1]; B = [1 1 1 1];out = [0 0 0 0];
X.mymethod(A,B,out);
result in out = [0 0 0 0] while it should be [1 1 1 1];
Then I assume the 'input' MLTYPE value does not allow modification within the calleb method.
Thx
Antworten (0)
Siehe auch
Kategorien
Mehr zu Build MATLAB Interface to C++ Library finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!