c# - SWIG:以 OO 方式包装 C API

标签 c# c swig

我有一个 C(不是 C++)库,它始终使用函数的第一个参数作为上下文对象(我们称之为类型 t_context),并且我想使用 SWIG 生成 C# 包装器保持这种调用风格(即,不要将函数或多或少地隔离,而是将它们包装为某个类中的方法,并通过来自 this 对象的引用来访问 t_context方法)。

示例(C 签名):

void my_lib_function(t_context *ctx, int some_param);

所需的 C# API:

class Context
{
    // SWIG generated struct reference
    private SWIG_t_context_ptr ctx;

    public void my_lib_function(int some_param)
    {
        // call SWIG generated my_lib_function with ctx
    }
}

如果有人向我指出一个 SWIG 为使用此 API 风格的现有 C(同样:不是 C++)库生成的包装器,我也会很高兴;我找不到任何东西。

或者,除了 SWIG 之外,是否有适用于 C 到 C# 用例的包装生成器,可以提供对 API 的更多控制(可能通过公开用于代码生成的模板)?

最佳答案

为了解决这个问题,我创建了以下迷你头文件来演示我们(可能)真正关心的所有部分。我这样做的目标是:

  1. C# 用户甚至不应该意识到这里发生了任何非 OO 的事情。
  2. 如果可能的话,SWIG 模块的维护者不必回显所有内容并手动编写大量代理函数。

为了开始,我编写了以下头文件 test.h:

#ifndef TEST_H
#define TEST_H

struct context;
typedef struct context context_t;

void init_context(context_t **new);

void fini_context(context_t *new);

void context_func1(context_t *ctx, int arg1);

void context_func2(context_t *ctx, const char *arg1, double arg2);

#endif

以及相应的 test.c 和一些 stub 实现:

#include <stdlib.h>
#include "test.h"

struct context {};
typedef struct context context_t;

void init_context(context_t **new) {
  *new = malloc(sizeof **new);
}

void fini_context(context_t *new) {
  free(new);
}

void context_func1(context_t *ctx, int arg1) {
  (void)ctx;
  (void)arg1;
}

void context_func2(context_t *ctx, const char *arg1, double arg2) {
  (void)ctx;
  (void)arg1;
  (void)arg2;
}

我们需要解决一些不同的问题才能使其成为一个简洁、可用的 OO C# 界面。我将一次一个地解决它们,并在最后提出我的首选解决方案。 (对于Python,这个问题可以通过更简单的方式解决,但是这里的解决方案将适用于Python、Java、C#以及可能的其他)

问题 1:构造函数和析构函数。

通常在 OO 风格的 C API 中,您会编写某种构造函数和析构函数来封装您的任何设置(可能是不透明的)。为了以合理的方式将它们呈现给目标语言,我们可以使用 %extend 编写看起来很像 C++ 构造函数/析构函数的内容,但在 SWIG 处理后仍然像 C 一样出现。

%module test

%{
#include "test.h"
%}
    
%rename(Context) context; // Make it more C# like
%nodefaultctor context; // Suppress behaviour that doesn't work for opaque types
%nodefaultdtor context;
struct context {}; // context is opaque, so we need to add this to make SWIG play

%extend context {
  context() {
    context_t *tmp;
    init_context(&tmp);
    // we return context_t * from our "constructor", which becomes $self
    return tmp;
  }

  ~context() {
    // $self is the current object
    fini_context($self);
  }
}

问题2:成员函数

我的设置方式允许我们使用一个可爱的技巧。当我们说:

%extend context {
  void func();
}

SWIG 然后生成一个如下所示的 stub :

SWIGEXPORT void SWIGSTDCALL CSharp_Context_func(void * jarg1) {
  struct context *arg1 = (struct context *) 0 ;

  arg1 = (struct context *)jarg1; 
  context_func(arg1);
}

从中可以得到的两点是:

  1. 实现扩展 context::func 调用的函数称为 context_func
  2. 有一个隐式“this”等价参数始终作为参数 1 进入此函数

上面的内容几乎与我们最初在 C 端包装的内容相匹配。因此,要包装它,我们可以简单地执行以下操作:

%module test

%{
#include "test.h"
%}
    
%rename(Context) context;
%nodefaultctor context;
%nodefaultdtor context;
struct context {}; 

%extend context {
  context() {
    context_t *tmp;
    init_context(&tmp);
    return tmp;
  }

  ~context() {
    fini_context($self);
  }

  void func1(int arg1);

  void func2(const char *arg1, double arg2);
}

这并不完全符合我的目标第二点以及我所希望的,你必须手动写出函数声明(除非你使用 %include 的技巧并保持他们是单独的头文件)。使用 Python,您可以在导入时将所有部分组合在一起并使其更加简单,但我看不到一种巧妙的方法来枚举与模式匹配的所有函数到 SWIG 生成 .cs 文件时的正确位置。

这足以让我使用以下代码进行测试(使用 Mono):

using System;
 
public class Run
{
    static public void Main()
    {
        Context ctx = new Context();
        ctx.func2("", 0.0);
    }
}

other variants of C OO style design, using function pointers这是可以解决的和类似的问题looking at Java我过去曾说过。

关于c# - SWIG:以 OO 方式包装 C API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39719317/

相关文章:

c# - 我怎样才能让希伯来语中的这个字符串替换工作?

C# 加权随机数

c# - 如何强行关闭 TcpListener

c - 内存泄漏和指针

java - Char* 在 JNI 调用中损坏

c - 为 SWIG 简单 Lua 示例创建共享库 DLL 时出错 (Windows 7)

c++ - 使用 SWIG 将 C++ char* 转换为 Java 中的 char[] 而不是 String

c# - 如何从 C# 中的 QueryPerformanceCounter 获取刻度?

c++ - 如何在 C++/C 代码中执行多个 gnuplot 命令

c++ - 释放双指针上的内存