从Swift调用c++ - std::vector

时间:2022-09-01 09:36:20

I'm calling a C++ function from Swift via a bridging header following SwiftArchitect's example. The signature for the C++ function is this:

根据SwiftArchitect的示例,我通过桥接头从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 );

(N.B. Point, Size, and PolylineElement are local C++ structs.) What signature do I use in my Objective-C++ wrapper for std::vector<T>?

(注意:Point、Size和PolylineElement是本地c++结构。)我在我的objective - c++ std::vector 中使用什么签名?

1 个解决方案

#1


1  

You are using vector as pointer. It's very good when you need use it in Swift.

你用矢量作为指针。当你需要在Swift中使用时,它是非常好的。

You can use void* instead:

你可以使用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 );

And perform typecasting in implementation.

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

Or if you need type safety, you can white:

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

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 );

And in implementation:

在实现:

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

And if you actually don't need vector in GrabberInitializeAndProcess, just it elements you can work with memory:

如果你在GrabberInitializeAndProcess中不需要向量,只需要它的元素就可以使用内存:

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 );

#1


1  

You are using vector as pointer. It's very good when you need use it in Swift.

你用矢量作为指针。当你需要在Swift中使用时,它是非常好的。

You can use void* instead:

你可以使用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 );

And perform typecasting in implementation.

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

Or if you need type safety, you can white:

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

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 );

And in implementation:

在实现:

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

And if you actually don't need vector in GrabberInitializeAndProcess, just it elements you can work with memory:

如果你在GrabberInitializeAndProcess中不需要向量,只需要它的元素就可以使用内存:

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 );