首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我可以在ActivePivot中使用我的C++ DLL吗?

我可以在ActivePivot中使用我的C++ DLL吗?
EN

Stack Overflow用户
提问于 2012-09-26 01:53:49
回答 1查看 253关注 0票数 2

作为一个ActivePivot解决方案,我如何重用其中的现有C++库?

我考虑了一个基于ActivePivot的CVA (CounterParty值调整)项目,我想重用我现有的C++代码,将我的附属逻辑应用于ActivePivot的doubles聚合数组。有没有特殊的后处理器来调用C++代码?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-26 02:05:35

Java后处理器是一个普通的ActivePivot类。它没有什么特别之处。因此您可以使用任何现有技术,因此可以在C++程序内部调用Java函数。

例如,这可以通过JNA和BridJ来实现。我不认为JNI在大多数情况下都是如此,您不需要使用如此低级的API。

例如,对于BridJ:给定一个C++头,如下所示:

代码语言:javascript
复制
__declspec(dllexport) int multiply(double multiplier, int size, double* const vector);

我做了下面的类:

代码语言:javascript
复制
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是简单的

代码语言:javascript
复制
@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 double[],其影响约为100ms

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12588480

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档