小C++头文件为多种程序设计语言提供统一接口
Svar: A Tiny C++ Header Brings Unified Interface for Multiple programming Languages
摘要:
在过去的几十年中,出现了许多类型的程序设计语言,它们中的大多数都提供了调用C++或C的接口,以便高效地实现。Svar的目的是为多种语言设计一个高效、轻量级、通用的中间件,同时将脚本语言的动态特性以一种直观的方式引入到C++中。首先,设计了一个类似JSON的数据结构的Svar类来保存C++中存在的所有内容,包括基本值、函数或用户定义的类和对象。其次,通过编译时指针、引用和共享\_ptr检测,参数可以有效地自动转换为Svar。第三,类和函数与字符串名称绑定以支持反射,这意味着共享库中的所有函数和类都可以导出到一个Svar对象,该对象也可以调用Svar模块。Svar模块可以通过不同的语言访问,本文演示了如何在Python和Node.js中导入和使用Svar模块。此外,C++还可以在运行时导入Svar模块甚至Python模块,这使得C++更容易编译和使用,因为不再需要头文件。我们将Svar的性能与两种最先进的Python和Node.js绑定工具进行了比较,结果表明Svar是高效,优雅和通用的。这个项目的核心是一个小小的现代C++头文件,不到5000行代码,没有额外的依赖。为了帮助开发人员使用Svar,所有相关的源代码都可以在http://github.com/zdzhaoyong/Svar上公开,包括文档和基准测试。
英文摘要:
There are numerous types of programming languages developed in the last decades, and most of them provide interface to call C++ or C for high efficiency implementation. The motivation of Svar is to design an efficient, light-weighted and general middle-ware for multiple languages, meanwhile, brings the dynamism features from script language to C++ in a straightforward way. Firstly, a Svar class with JSON like data structure is designed to hold everything exists in C++, including basic values, functions or user defined classes and objects. Secondly, arguments are auto cast to and from Svar efficiently with compile time pointers, references and shared\_ptr detection. Thirdly, classes and functions are binded with string names to support reflection, this means all functions and classes in a shared library can be exported to a Svar object, which also calls a Svar module. The Svar modules can be accessed by different languages and this paper demonstrates how to import and use a Svar module in Python and Node.js. Moreover, the Svar modules or even a python module can also be imported by C++ at runtime, which makes C++ easier to compile and use since headers are not required anymore. We compare the performance of Svar with two state-of-the-art binding tool for Python and Node.js, and the result demonstrates that Svar is efficient, elegant and general. The core of this project is one single tiny modern C++ header with less than 5000 lines code without extra dependency. To help developers using Svar, all the source codes related are public available on http://github.com/zdzhaoyong/Svar, including documentations and benchmarks.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。