我正在制作一套框架。我想支持多种编程语言

发布于 2024-12-07 10:27:56 字数 168 浏览 1 评论 0原文

我正在制作一套框架。

我想支持多种编程语言。

我的问题是我应该使用什么语言作为基础?

有没有办法进行多重绑定?

我想支持所有这些编程语言。

C, C++, Java, Ruby, Perl, Python.

I'm Making a set of frameworks.

I want to support multiple programming languages.

My question is what language should i use as the base?

and is there a way to multiple bindings?

I want to support all these programming languages.

C, C++, Java, Ruby, Perl, Python.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜柠檬 2024-12-14 10:27:56

我将用 C++ 实现该库。如果您需要 C 访问,那么可以通过提供带有附加“this”参数的包装函数来手动轻松完成此操作。

在开始之前,请阅读 SWIG 文档,特别是其限制和需要避免的事项。如果您在设计 C++ 接口时考虑到了 SWIG,您可以毫不费力地获得为您生成的许多语言的绑定。

编辑:这是 C++ 类的 C 包装器的快速示例。假设这是要包装的 C++ 类,我们将其称为 test.h

class Test {
public:
    Test();
    int do_something(char* arg);
private:
    is_valid(); // see below for why you need this method
};

这是您的 C 标头 test_c.h

typedef void* TestHandle;
TestHandle newTest();
int deleteTest(TestHandle h);
int Test_do_something(TestHandle h, char* arg);

您的 C 实现将是一个带有以下内容的 C++ 文件 : C 函数,例如 test_c.cpp

extern "C" TestHandle newTest()
{
    return (void*)new Test();
}

extern "C" int deleteTest(TestHandle h)
{
    Test* this = static_cast<Test*>(h);
    if (!this->is_valid())
        return -1; // here we define -1 as "invalid handle" error
    delete this;
    return 0; // here we define 0 as the "ok" error code
}

extern "C" int Test_do_something(TestHandle h, char* arg)
{
    Test* this = static_cast<Test*>(h);
    if (!this->is_valid())
        return -1; // here we define -1 as "invalid handle" error
    return this->do_something(arg);
}   

is_valid() 方法可以保证您不会传递错误的句柄。例如,您可以在所有实例中存储幻数,然后is_valid() 只是确保幻数存在。

I would implement the library in C++. If you need C access, then this is easily done by hand, by providing wrapper functions that take an additional "this" argument.

Before you start though, read the SWIG documentation, in particular its limitations and things to avoid. If you design your C++ interface with SWIG in mind you can get the bindings for a lot of languages generated for you without effort.

Edit: Here is a quick example of a C wrapper for a C++ class. Let's say this is the C++ class to wrap, let's call it test.h:

class Test {
public:
    Test();
    int do_something(char* arg);
private:
    is_valid(); // see below for why you need this method
};

This is your C header test_c.h:

typedef void* TestHandle;
TestHandle newTest();
int deleteTest(TestHandle h);
int Test_do_something(TestHandle h, char* arg);

And your C implementation will be a C++ file with C functions, let's say test_c.cpp:

extern "C" TestHandle newTest()
{
    return (void*)new Test();
}

extern "C" int deleteTest(TestHandle h)
{
    Test* this = static_cast<Test*>(h);
    if (!this->is_valid())
        return -1; // here we define -1 as "invalid handle" error
    delete this;
    return 0; // here we define 0 as the "ok" error code
}

extern "C" int Test_do_something(TestHandle h, char* arg)
{
    Test* this = static_cast<Test*>(h);
    if (!this->is_valid())
        return -1; // here we define -1 as "invalid handle" error
    return this->do_something(arg);
}   

The is_valid() method is there to guarantee that you are not passed a bad handle. For example, you can store a magic number in all your instances, then is_valid() just ensures the magic number is there.

没有你我更好 2024-12-14 10:27:56

查看 GObject — 它是一个开源 C 库,为 C 添加了面向对象的编程功能,并且可以透明地为各种语言创建绑定。

它的同伴 Vala 是一种面向对象的语言,可以编译为 C+GObject 代码,以帮助减少 C+GObject 的冗长。

Check out GObject — it's an open-source C library that adds object-oriented programming functionality for C, and can transparently create bindings for a variety of languages.

And its companion, Vala, is an object-oriented language that compiles down to C+GObject code, to help cut down on the verbosity of C+GObject.

断桥再见 2024-12-14 10:27:56

查看 SWIG,“简化的包装器和接口生成器”。给定 C 或 C++ 代码,它可以以编程方式生成各种语言的绑定。

Check out SWIG, "Simplified Wrapper and Interface Generator". Given C or C++ code, it can programmatically generate bindings for a variety of languages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文