3

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

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>?

Community
  • 1
  • 1
dumbledad
  • 16,305
  • 23
  • 120
  • 273
  • 1
    `NSArray *`, you're wrapper will be responsible for bridging the two types. – Joe Nov 04 '15 at 15:57
  • I'm having trouble wrapping the PolylineElement (it's a simple struct of four ints) but I'll put that in as a separate question. Do you want to add your comment as an answer and I'll mark it as such? – dumbledad Nov 04 '15 at 17:25

1 Answers1

2

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

You can use void* instead:

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:

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 );
Cy-4AH
  • 4,370
  • 2
  • 15
  • 22