从Swift调用C ++ - std :: vector <t>的等价物

时间:2015-11-04 15:53:37

标签: swift objective-c++

我通过SwiftArchitect's example之后的桥接头从Swift调用C ++函数。 C ++函数的签名是:

long GrabberInitializeAndProcess(
    unsigned char* pbInPixels,  
    int inStride,
    unsigned char* pbOutPixels, 
    int outStride,
    int width, 
    int height,
    Point mqTopLeft, 
    Size mqSize,
    std::vector<PolylineElement> * pForegroundMarks, 
    std::vector<PolylineElement> * pBackgroundMarks,
    void* pGrabberState );

(NB PointSizePolylineElement是本地C ++结构。)我在std::vector<T>的Objective-C ++包装器中使用了什么签名?

1 个答案:

答案 0 :(得分:1)

您正在使用向量作为指针。当你需要在Swift中使用它时非常好。

您可以改为使用void*

long GrabberInitializeAndProcess(
    unsigned char* pbInPixels,  
    int inStride,
    unsigned char* pbOutPixels, 
    int outStride,
    int width, 
    int height,
    Point mqTopLeft, 
    Size mqSize,
    void * pForegroundMarks, 
    void * pBackgroundMarks,
    void* pGrabberState );

并在实施中执行类型转换。

或者如果你需要类型安全,你可以白:

typedef struct _vectorOfPolylineElement *vectorOfPolylineElementPtr;

long GrabberInitializeAndProcess(
    unsigned char* pbInPixels,  
    int inStride,
    unsigned char* pbOutPixels, 
    int outStride,
    int width, 
    int height,
    Point mqTopLeft, 
    Size mqSize,
    vectorOfPolylineElementPtr pForegroundMarks, 
    vectorOfPolylineElementPtr pBackgroundMarks,
    void* pGrabberState );

并在实施中:

typedef struct _vectorOfPolylineElement
{
    std::vector<PolylineElement> val;
} *vectorOfPolylineElementPtr;

如果你在GrabberInitializeAndProcess中实际上不需要vector,那么只需要你可以使用内存的元素:

long GrabberInitializeAndProcess(
    unsigned char* pbInPixels,  
    int inStride,
    unsigned char* pbOutPixels, 
    int outStride,
    int width, 
    int height,
    Point mqTopLeft, 
    Size mqSize,
    PolylineElement * pForegroundMarks, 
    size_t foregroundMarksCount,
    PolylineElement * pBackgroundMarks,
    size_t backgroundMarksCount,
    void* pGrabberState );