从Swift调用C ++-std :: vector< T> [英] Calling C++ from Swift - what is the equivalent of std::vector<T>
问题描述
我正在通过 SwiftArchitect的示例之后的桥接标头从Swift调用C ++函数. C ++函数的签名是这样的:
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
和PolylineElement
是本地C ++结构.)我在std::vector<T>
的Objective-C ++包装器中使用什么签名?
(N.B. Point
, Size
, and PolylineElement
are local C++ structs.) What signature do I use in my Objective-C++ wrapper for std::vector<T>
?
推荐答案
您正在使用矢量作为指针.当您需要在Swift中使用它时非常好.
You are using vector as pointer. It's very good when you need use it in 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 );
并在实现中执行类型转换.
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 );
在实施中:
typedef struct _vectorOfPolylineElement
{
std::vector<PolylineElement> val;
} *vectorOfPolylineElementPtr;
如果您实际上不需要GrabberInitializeAndProcess中的向量,只需使用它即可使用内存:
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 );
这篇关于从Swift调用C ++-std :: vector< T>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!