I'm writing a c#
/ cpp
mixed program and need to call some cpp
functions inside c#
.
For most of the functions, I can just use PInvoke approach to call extern cpp funcs.
// cpp part
void compute(DataWrapper* dataHolderFromCSharp){
computeNatively(dataHolderFromCSharp);
}
// c# part
void useData(cppData){
var cppData = cppWrapper.compute(preRes);
doSthWith(cppData);
}
However, some cpp
functions will need a pre-computed class from other cpp
functions.
Instead of re-compute such a "pre-computed" class every time, I'd like to store it somewhere so that it lives through the lifecycle of the main c#
program.
// cpp part
void preCompute(DataWrapper* dataHolderFromCSharp, customClass & preComputeRes){
precomputeNatively(dataHolderFromCSharp, preComputeRes);
}
void compute(DataWrapper* dataHolderFromCSharp, customClass* preComputeRes, DataWrapper* workData){
preCompute(dataHolderFromCSharp, preComputeRes);
doComputation(preComputeRes, workData);
}
void computeOverride(DataWrapper* dataHolderFromCSharp, DataWrapper* workData){
customClass* preComputeRes,
preCompute(dataHolderFromCSharp, preComputeRes);
doComputation(preComputeRes, workData);
}
// c# part
void useData(cppData){
cppWrapper.computeOverride(DataWrapper, otherDataWrapper);
}
But this will cause the preCompute()
to run every time.
Instead, I want to do something like the following:
// c# part
static IntPtr preRes;
void useData(cppData){
if (signalChanged)
preRes = cppWrapper.preCompute(); // how to store the precomputed data?
cppWrapper.compute(DataWrapper, preRes, otherDataWrapper);
}
The question is what is the proper way to store (on the c#
side) such an access point of class (computed the on the cpp
side) so that I can re-use it without re-compute it every time?
Creating a static class in the cpp
side in memory and send a pointer to the c#
side so that I can always access it?