ActivePivot是一个Java解决方案,如何在其中重用现有的C ++库?
我考虑基于ActivePivot的CVA(CounterParty价值调整)项目,我想重用我现有的C ++代码,通过ActivePivot将我的Collateral逻辑应用于双精度聚合数组。是否有一个特殊的后处理器来调用C ++代码?
答案 0 :(得分:3)
ActivePivot后处理器是普通的Java类。没什么特别的。因此,您可以使用任何现有技术,因此在Java程序中调用C ++ DLL函数。
这可以通过JNA和BridJ实现。在大多数情况下,我不认为JNI,您不需要使用这样的低级API。
例如,与BridJ: 鉴于C ++标题看起来像:
__declspec(dllexport) int multiply(double multiplier, int size, double* const vector);
我做了以下课程:
import org.bridj.BridJ;
import org.bridj.Pointer;
import org.bridj.ann.Library;
import org.bridj.ann.Runtime;
import org.bridj.cpp.CPPRuntime;
// Generated with http://code.google.com/p/jnaerator/
@Library(CPP_Collateral.JNA_LIBRARY_NAME)
@Runtime(CPPRuntime.class)
public class CPP_Collateral {
public static final String JNA_LIBRARY_NAME = "dummy";
static {
// In eclipse, the DLL will be loaded from a resource folder
// Else, one should add a program property: -Djava.library.path
BridJ.addLibraryPath("src/main/resources/DLL");
BridJ.register();
}
/**
* My dummy.dll has one method:
* int multiply(double multiplier, int size, double* const vector)
*/
public static native int multiply(double multiplier, int size, Pointer<Double> vector);
}
我的IPostProcessor只是
@Override
protected Object doEvaluation(ILocation location, Object[] underlyingMeasures) throws QuartetException {
double[] asArray = (double[]) underlyingMeasures[0];
if (asArray == null) {
return null;
} else {
// Size of the array
int size = asArray.length;
// Allocate a Pointer to provide the double[] to the C++ DLL
Pointer<Double> pCount = allocateDoubles(size);
pCount.setDoubles(asArray);
CPP_Collateral.multiply(2D, size, pCount);
// Read again: the double[] is copied back in the heap
return pCount.getDoubles();
}
}
在性能方面,我在这里工作了大小10000的2.000双[],影响大约是100毫秒