簡體   English   中英

包裝std :: vector <double> 從C ++到C用於Swift

[英]wrapping std::vector<double> from C++ to C for use in Swift

我想在Swift 4中使用一個用C ++編寫的程序。我已經閱讀並理解了如何在http://www.swiftprogrammer.info/swift_call_cpp.html中描述的如何進行創建和包裝。

然而,現實生活更復雜,我沒有C ++經驗。 我在C ++頭文件中有以下代碼。

namespace Fft {
   void transform(std::vector<double> &real, std::vector<double> &imag);
}

我有兩個問題:

  1. 如何在C中編寫函數聲明? 我不知道向量的等價物是什么。
  2. 在C中命名空間是什么意思?

您可以編寫一些使用C兼容類型的橋接函數來調用C++函數。 這是一個非常未經測試的代碼,只是為了讓您了解可以執行的操作:

namespace Fft {
   void transform(std::vector<double> &real, std::vector<double> &imag);
}

extern "C" {

struct complex_vector
{
    void* real;
    void* imag;
};

complex_vector create_complex_vector(size_t size)
{
    complex_vector cv;
    cv.real = new std::vector<double>(size);
    cv.imag = new std::vector<double>(size);
    return cv;
}

void complex_vector_push_back(complex_vector cv, double real, double imag)
{
    reinterpret_cast<std::vector<double>*>(cv.real)->push_back(real);
    reinterpret_cast<std::vector<double>*>(cv.imag)->push_back(imag);
}

void complex_vector_reserve(complex_vector cv, size_t size)
{
    reinterpret_cast<std::vector<double>*>(cv.real)->reserve(size);
    reinterpret_cast<std::vector<double>*>(cv.imag)->reserve(size);
}

void complex_vector_resize(complex_vector cv, size_t size)
{
    reinterpret_cast<std::vector<double>*>(cv.real)->resize(size);
    reinterpret_cast<std::vector<double>*>(cv.imag)->resize(size);
}

void fill_complex_vector(complex_vector cv, double* real, double* imag)
{
    auto v_real = reinterpret_cast<std::vector<double>*>(cv.real)->data();
    auto v_imag = reinterpret_cast<std::vector<double>*>(cv.imag)->data();
    auto v_size = reinterpret_cast<std::vector<double>*>(cv.real)->size();

    std::copy(real, real + v_size, v_real);
    std::copy(imag, imag + v_size, v_imag);
}

void fft_transform(complex_vector cv)
{
    Fft::transform(*reinterpret_cast<std::vector<double>*>(cv.real), *reinterpret_cast<std::vector<double>*>(cv.imag));
}

double* complex_vector_real_array(complex_vector cv)
{
    return reinterpret_cast<std::vector<double>*>(cv.real)->data();
}

double* complex_vector_imag_array(complex_vector cv)
{
    return reinterpret_cast<std::vector<double>*>(cv.imag)->data();
}

size_t complex_vector_size(complex_vector cv)
{
    return reinterpret_cast<std::vector<double>*>(cv.imag)->size();
}

void destroy_complex_vector(complex_vector cv)
{
    delete reinterpret_cast<std::vector<double>*>(cv.real);
    delete reinterpret_cast<std::vector<double>*>(cv.imag);
}

} // extern "C"

如果您將其編譯為C++那么extern "C" {}塊將使它成為可以從C調用這些函數的。

你可以編寫一個類似這樣的C程序,例如:

complex_vector cv = create_complex_vector(1024);

// fill the vector
for(int i = 0; i < complex_vector_size(cv); ++i)
    complex_vector_push_back(cv, 0.2 * i, 0.4 * i);

// call the transform
fft_transform(cv);

double* real = complex_vector_real_array(cv);
double* imag = complex_vector_imag_array(cv);
size_t size = complex_vector_size(cv);

// use your transformed data here ...

destroy_complex_vector(cv);

注意:完全未經測試的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM