For this world built on coding, most languages use C/C++ as their core implementation or provide interface to call C/C++ libraries for higher efficiency. New programming languages are still coming and they all has their particular superiority [1, 2]. It would be great if we can import libraries across languages, and a C++ based middle-ware is the best option to maintain efficiency and maximize the community.
One challenge is the type translation between weakly and strongly typed languages. Being a natively compiled language, C++ can not generate other C++ code at runtime. Reflection in a C++ context can only rely on external scripting languages integrated into the C++ project. However, given the capabilities of dynamic objects to expose themselves, it is also very easy to expose them to a scripting language. Therefore, dynamism is the key role to provide full reflection for strongly typed languages, where variable, functions and classes are all represented in an uniform way. And dynamism is not only beneficial for scripting language integration, but also brings advantages like more abstraction, faster compiling and less ugly hacks.
Many binding tools like SWIG , pybind11 , nbind  are developed to help the interaction between different languages and C++. However, existing binding tools need users to develop a particular wrapper for each language and brings extra dependency. A unified interface would help developers to write less wrapper code and useful for library developing and releasing.
As illustrated in Fig. 1, 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.
In brief, the main contributions are concluded as follows:
A tiny header file that makes C++11 library accessible from other programming languages. Due to the high efficiency of C++/C, other languages often use it for low level functional implementations. They often need a wrapper for helping calling C++ through the language interface protocol. Svar is designed to be a high efficient middle-ware between languages, and the porting of a language can be done by just easily porting the single Svar class. Our experiments shows that Svar maintains high efficiency and is even faster than traditional binding tools. The porting of Svar is reusable and the C++ implementation will not brings extra dependency, which also means the C++ library can be used in different languages at the same time without modification.
Enables import programming style instead of include in C++. Shared libraries are imported inside without header dependency, makes C++ programming and compiling easier. As too many headers slow down the compilation speed or even brings compile errors, C++ beginners or even experienced programmers often take long time fixing compiling problems, which makes it difficult or even impossible to use a lot third-party libraries in C++. Svar allows to import the libraries without dependencies or headers, which means modules are decoupled and compile a large project with tens of dependencies has the same complexity as a ”Hello World” project.
A generic holder for everything. The standard template library (STL) in C++ only allows to hold specific type which is already known, when developers want to hold an object with dynamic type, they don’t have a proper solution. Although the C++17 standard brings std::any and std::variant for holding different values, they are not able to be used without original type declare. Svar brings an easy and safe way to hold everything including functions and classes, while maintains usability without declaring. The contents can further be organized in a JSON style structure, which popular used in script languages. This dynamic feature brings more different design patterns which makes programming easier.
A high performance bridge among different languages. As every language has its metrics in particular areas, some functions are often implemented by different languages. It would be great if we can import libraries across languages. A C++ middle-ware is the best option to maintain efficiency and maximize the community. Svar not only helps calling C++ in other languages, but also allows to import libraries implemented by other languages, which forms a bridge among programming languages.
Ii Related Works
Source code translators and compilers. Several studies have investigated the possibility to translate programming languages for better compatibility or efficiency. Cython  tries to extend Python and compile scripting source code to C, makes writing tiny accelerated C extensions for Python easier. Instead of define static types explicitly, Shed  uses type inference techniques to determine the implicit types used in a Python program and translate typed Python programs into optimized C++. Pythran 
Reflection, dynamism and module design in C++. As most languages (Java, Python) support reflection as part of their standard specifications, C++ only support very limited reflection features though RTTI (Runtime Type Information) . Some early work [15, 16] tried to enhance C++ with reflection capabilities using MOP (Metaobject Protocol). However, these approaches are either intrusive or are not fully compliant with the C++ standard. More reflection work [17, 18] which fully compliant with the standard C++ specification are implemented and likely the recent standards of C++ (i.e., C++2x) will have support for compile time reflection. As dynamism is the key feature to solve runtime reflection, Svar implemented a dependency free single header reflection with dynamism features which is more easier to use. Moreover, the reflection also brings an unified interface for multiple other programming languages and C++ itself by a modern import style.
Svar is designed to represent everything in C++ and a dynamic type object holder is firstly designed for both performance and usability. We extend the JSON data structure to organize any possible objects including user-defined functions, classes and instances. For function calling, arguments are auto cast to and from Svar efficiently with compile time pointers, references and shared_ptr detection. 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 C++, Python and Node.js. For any other languages, developers only need to simply implement an interface to the single class Svar which can be reused by all modules, instead of repeat binding each object. Moreover, the Svar modules or even a python module can also be imported by C++ at runtime, which makes C++ more easier to compile and use since headers are not required anymore. This section will describe how Svar works for C++, as binders of other languages also use C++ interfaces and Svar provides better accessing.
Iii-a Basic Data Structure
Iii-B JSON Structure Dynamic Holder
Svar is a dynamic holder for everything, where anything can be hold by Svar. Moreover, a JSON style is used to organize objects, which brings powerful abilities to easily represent and access any data structure. Some examples are given in the following C++ code:
The above code demonstrates that JSON is supported as a subset of Svar, and user-defined classes can also be hold by Svar like scripting languages. Type information can be checked, while operators and iterators are provided to access object easily. As pointer and shared_ptr instead of value are also popular used in C++, they are supported to be hold and treated just like their original meta type.
Iii-C Function Holding and Auto Arguments Casting
Function plays the most important role in all programming languages, as some shell scripts only use string values and functions to work. Svar is not only designed to represent and use functions in C++, but also functions objects in other programming languages. Firstly, all forms of C++ functions including plain C functions, member functions, static member functions and functor objects (including lambda expressions) are supported. Secondly, we further brings support of keyword arguments and meta attributes to C++ as they are popular used in scripting languages. Thirdly, overload and auto arguments type casting are supported to correctly call the functions as expected. The function signatures are statically auto determined at compile time, and checked at runtime when the function being called. Some very simple example usages are listed as below:
Comparing to the original C++ functions, the Svar form has some advantages and can be used in some particular circumstances. Firstly, the unified form can represent different functions, which is more suitable for functions with undefined arguments. Secondly, the Svar representation maturely supports reflection, and a key-value dictionary can further represents a function package where function signatures and documentations can be maintained in the meta. Thirdly, keyword arguments are supported for more easier argument usage, and the general form can be easily porting to other languages.
Iii-D Class Reflection and Usage
Being able to access class information at runtime increases the flexibility in programming, since it allows developing generic applications like object instantiation, serialization and persistence. The factory method pattern is widely used to polymorphically instantiate objects whose concrete types are not known at compile-time. However, this pattern nature burdens programmers by requiring to implement such methods for new and already existing classes. Also, the factory method pattern requires all the candidate classes to have a common base class. An approach using reflection would not have such restrictions and will not be intrusive. Serializing an object state to a byte sequence and de-serializing it back is a common approach to communicate between loosely coupled applications. With reflection, the sender is able to serialize an object’s state and sent it to the receiver so that the receiver is able to dynamically create the objects using their descriptions and populate them with the corresponding state information. Serialization also enables a generic object persistence framework, since it is just a matter of saving the serialized content to a persistent media. Moreover, a reflection-based binding would minimize the effort required for language binding. In this case, bindings are required only for the reflection related routines, which can easily be automated. Svar implemented a MOP based class reflection which is general and easy to use:
The above example demonstrates that classes and instances are all represented by Svar, and instances constructed with Svar can be further transformed back to static type C++. Except for wrapping existing C++ classes, Svar also supports to define a class dynamically, which enables to represent class objects imported from other languages. Below is a simple sample to define and use a dynamic class:
Iii-E Class Member Operators
Class operators is supported by most programming languages as it explicitly simplified code writing especially for scientific computing. Svar use the Python style operator name definitions where arithmetic, comparison and bitwise operators are defined by a serials member functions with particular name signature. Here is a simple example to demonstrate how Svar supports operators in C++:
Iii-F Svar Module Exporting and Importing
Instead of individual variable, function or class objects, a library is combination of them, and a Svar module organize them with a key-value dictionary. This means the whole library is reflection supported and the module contents can be accessed with names, which naturally forms an unified interface for C++. Once shared library elements are exposed to the dictionary, users can view the API documentation and use the module dynamically without headers, which simplified SDK distribution and compilation. Moreover, the Svar module can further be directly imported by other languages, while module testing and using with scripting languages effectively reduce the burden of development. Here below is a simple example shows how to export C++ library to Svar module:
The above tiny demo shows that exporting variable, function or class to Svar is simple and easy to understand. Developers just need to compile the source code like ordinary shared libraries, and it can be easily used in different languages, while the exporting does not rely on the low level data structure or dependencies of those programming languages. This not only simplified the wrapper binding works for scripting languages using, but also removed extra language dependency and repeated wrapping, so that all languages can shares the same implementation for designing, testing and releasing. Moreover, for C++ programmers, the shared library can either be linked at compile time with headers, but also be used without interface headers and be imported at runtime like scripting languages. API documentations about the interface can be illustrate using the built-in command tool or directly print the Svar object contents. Here is a demo shows how to import and use the above exported shared library dynamically:
To help users import and use Svar modules in Python, a open source package is provided to load the shared libraries as python modules. Developers can install the tool with pip and directly use the loaded module just like traditional python packages:
Unlike other binding tools where Python dependency is tightly coupled with module implementations, Svar modules do not rely on extra dependencies. The same Svar module can be imported by different Python versions (or even languages) at the same time, while developers have to compile a shared library for each version using other binding tools.
By using Svar, users do not need change anything to call the same built library in different languages, and the bridge tool is also general for all Svar plugin modules. More bridge tools for other programming languages can be implemented though their interface to C/C++.
Iii-G Bridge for Languages
Svar not only help exporting C++ libraries to other languages, but also able to bridge those languages inversely. Here below is a sample code to import and use Python in C++, as Svar is used to decouple the low level of languages, programmers are able to use a library implemented by other scripting languages as usual Svar modules.
As we are able to export and import libraries implemented by different languages, Svar can also be used as a bridge to import and call modules across languages. Here below is a sample code to import and use Python module in Node.JS:
Every programming language has its eco-system, it would be nice if we can fuse them to a larger one. Some languages lack plenty third-party libraries and becomes very difficult for developers to develop some applications. Once the interfaces to Svar modules implemented, it could be much easier as rich choices are provided from Svar by importing libraries of other languages.
Svar is tiny but elegant, which not only provides a general interface for multiple programming languages, the dynamic features also helps C++ for more easier implementation of several applications. In this section, we firstly compare Svar with other existing popular language binding tools pybind11, nbind in terms of experience and efficiency. And then, several applications is introduced to demonstrate the benefits using Svar in C++ implementations. All experiments is conducted to shows that Svar is easy, efficient and ready-for-applications.
Iv-a Comparison to Other Binding Tools
developed to help the interaction between scripting languages and C++. Among them pybind11 is the most popular open-source library that is now used by the well-known deep learning projects pytorch222https://pytorch.org/
and tensorflow333https://www.tensorflow.org/ (transfered from SWIG in 2019).
We provide a little comparison between Svar and pybind11 for binding class, function and variables in Fig. 3. The code comparison shows that the usage of Svar is almost as the same of pybind11 and even more straightforward. Moreover, comparing to pybind11, Svar has the following advantages:
Dependency free and ready for multiple programming languages. Pybind11 directly use the dynamism of Python and C++ implemented modules have to compile rely on the Python dependency. This also means the modules can not be used across different Python versions. Svar however, does not brings any extra dependency to the C++ implementation and modules can be used across different Python versions and programming languages. This brings great convenience for C++ applications where Python is only used for module testing, as we do not need to separately provide a wrapper library.
Support Json style complex data structure. Pybind11 supports lots of containers which are existing in STL. However, the elements has to keep all the same and it is hard to represent complex data structures where different containers are nested with a varias types. Svar is able to represent complex data structure in a JSON style easily. This can significantly simplify the API design in some circumstances such as parameter settings.
Auto bind everything used and is even more easier to use. As illustrated in Fig. 3, pybind11 has to bind every used class explicitly, otherwise an exception will be raised. Svar instead auto binds everthing which are been hold, so that they can be further used as arguments. And exporting variables and functions can be even more easier in Svar as no paticular binding function or bridge classes are required.
More light-weighted and efficient. Although Svar does not use the dynamism of Python directly, the experiments shows it even brings higher efficiency (7 times faster in the demo case). Benifits from the light-weight design, the type conversion and function calling is even more direct and simple. The dynamism is already done in Svar and the Python object directly hold a Svar pointer.
Svar has potential to unify different binding tools since it is more lightweight, dependency free, compatible friendly and easy to use. Comparing to existing binding tools, Svar not only able to provide an unified binding functional between scripting lanuages and C++ without dependency, but also forms a general bridge for multiple languages, which enables importing and calling across lanuages. Moreover, for C++ only developers, Svar also enable dynamically library importing which simplified interface design, compile and release.
Iv-B Object Serialization
Expect for the unified interface functional, the dynamism of Svar also brings more design patten in C++. As we always need to save running data and results to file system or transfer them across computers, data serialization is one of the most basic functional in programming. Protobuf 444https://github.com/protocolbuffers/protobuf provides an efficient contract-first protocol for serializing structured data, however, messages must be specified in .proto files when the application is built, which is not suitable for unstructured payloads. JSON instead, is the most popular unstructured data format for storing and exchanging data, which is both easy for humans to read-write and also easy for machines to parse and generate. Svar builtin supports JSON parse and dump, and Table 3 shows the efficiency comparison of different existing popular engines. JSON files on public benchmark 555https://github.com/miloyip/nativejson-benchmark are used.
It should be noted that although Svar is not particular designed for serialization speed, the performance is comparable to other engines, and Svar is single-header only with less than 5k lines code. Moreover, Svar further supports memory buffers and serialize unstructured data to binary JSON formats such as CBOR (https://cbor.io/). The serialization of memory buffers with unstructured data is important for contents like images and matrices, which is more efficient than using string to represent buffers with Base64 or Hex. Table 3 illustrates that the serialization of CBOR is faster than JSON, and further acceleration can be achieved by using buffers to hold structured data.
Except for the basic data structures, user defined objects are also supported for elegant serialization and restore. Developers can either re-implement the caster template or define the buffer constructor and converter functions.
The above sample shows that Svar seamlessly combines structured objects with unstructured JSON format, and user defined data structure are serialized to and from buffer. Benefit from the dynamism of Svar, function calling can be performed directly using the restored objects with automatic type casting.
Iv-C HTTP RESTful API Module
HTTP (Hypertext Transfer Protocol) is the foundation of data communication for the World Wide Web, where hypertext documents include hyperlinks to other resources that the user can easily access, for example by a mouse click or by tapping the screen in a web browser. By using Flask 666https://github.com/pallets/flask web framework, Python developers can easily write a small server. However, if a developer knows nothing about HTTP, writing a HTTP server or client is not so easy as it does in scripting languages.
An tiny open-source Svar module, svar_http 777https://github.com/zdzhaoyong/svar_http brings new opportunity to provide and use HTTP service easily. Developers does not need to know any detail about HTTP, and Svar modules can be directly exposed as network API. Here below is a tiny demo to expose API through svar_http:
The above code demonstrates that a Svar module can be directly served through HTTP, and writing a server is very similar to exporting a Svar module, which is easy to understand and developers do not need to known any detail about the implementation of HTTP.
Interact with HTTP server is also very easy through the Svar interface. Benefit from the powerful dynamism of Svar, the network API supports keyword parameters and arguments are automatically casted to the target type.
Iv-D Topic based Data Distrubution
ROS  is an open-source robot operating system which is popular used in the robotic community for both commercial or noncommercial usages. It provides a structured communications layer above the host operating systems of a heterogenous compute cluster. However, the design of ROS is too heavy and users have to define the strictly typed data structure of messages for communication. Based on Svar, GSLAM  implemented a header-only intra-process communication utility class named Messenger, which is more light-weighted, easy-to-use and supports to publish and subscribe any class without extra cost. Moreover, data distribution can also be further bridged through network by integrate existing messaging protocols such as Kafka (http://kafka.apache.org/), MQTT (https://mqtt.org/) and NSQ (https://nsq.io/).
This paper presents a tiny programming languages interaction framework, which not only brings an unified binding tool for C++, but also forms a high performance bridge for multiple programming languages. Moreover, the dynamism brings better design pattern for various applications and enables importing modules dynamically in C++. The light-weight design makes it a tiny core to decouple and link library modules or even programming languages. We make it open-source on github and free for both commercial or noncommercial usages, and more Svar modules will be developed with supporting to more languages in the future.
-  S. Cass, “The 2017 top programming languages,” IEEE Spectrum, vol. 31, 2018.
-  R. Harper, Practical foundations for programming languages. Cambridge University Press, 2016.
-  D. M. Beazley et al., “Swig: An easy to use tool for integrating scripting languages with c and c++.” in Tcl/Tk Workshop, vol. 43, 1996, p. 74.
-  W. Jakob, J. Rhinelander, and D. Moldovan, “pybind11–seamless operability between c++ 11 and python,” URL: https://github. com/pybind/pybind11, 2017.
-  D. Abrahams and R. W. Gross-Kunstleve, “Boost. python,” 2008.
-  SteveKChiu, “A binding between c++11 and lua language,” URL: https://github.com/SteveKChiu/lua-intf, 2021.
-  Cython, “Cython, the most widely used python to c compiler,” URL: https://github.com/cython/cython, 2021.
-  S. Skin, “Shed skin, a python to c++ compiler,” URL: https://github.com/shedskin/shedskin, 2021.
-  S. Guelton, P. Brunet, M. Amini, A. Merlini, X. Corbillon, and A. Raynaud, “Pythran: Enabling static optimization of scientific python programs,” Computational Science & Discovery, vol. 8, no. 1, p. 014001, 2015.
-  X. Chen, C. Liu, and D. Song, “Tree-to-tree neural networks for program translation,” arXiv preprint arXiv:1802.03691, 2018.
-  M.-A. Lachaux, B. Roziere, L. Chanussot, and G. Lample, “Unsupervised translation of programming languages,” arXiv preprint arXiv:2006.03511, 2020.
-  I. Skochinsky, “Compiler internals: Exceptions and rtti,” Also available as http://go. yurichev. com/17294, 2012.
-  O. Jautzy and F. Lebastard, “A metalevel architecture for dynamism and reflection in c+,” 1997.
-  S. Chiba, “A metaobject protocol for c++,” in Proceedings of the tenth annual conference on Object-oriented programming systems, languages, and applications, 1995, pp. 285–299.
-  rttrorg, “C++ reflection library,” URL: https://github.com/rttrorg/rttr, 2021.
T. Devadithya, K. Chiu, and W. Lu, “C++ reflection for high performance
problem solving environments,” in Proceedings of the 2007 spring
simulation multiconference-Volume 2
. Citeseer, 2007, pp. 435–440.
-  M. Quigley, K. Conley, B. Gerkey, J. Faust, T. Foote, J. Leibs, R. Wheeler, A. Y. Ng, et al., “Ros: an open-source robot operating system,” in ICRA workshop on open source software, vol. 3, no. 3.2. Kobe, Japan, 2009, p. 5.
Y. Zhao, S. Xu, S. Bu, H. Jiang, and P. Han, “Gslam: A general slam framework
and benchmark,” in
Proceedings of the IEEE International Conference on Computer Vision. IEEE, 2019.