https://github.com/csiro-hydroinformatics/c-interop
Reusable functions for marshalling data between C, C++ and other programming languages
https://github.com/csiro-hydroinformatics/c-interop
glue-code interop interoperability marshalling-data python rcpp
Last synced: 22 days ago
JSON representation
Reusable functions for marshalling data between C, C++ and other programming languages
- Host: GitHub
- URL: https://github.com/csiro-hydroinformatics/c-interop
- Owner: csiro-hydroinformatics
- License: other
- Created: 2017-02-12T05:30:31.000Z (about 9 years ago)
- Default Branch: main
- Last Pushed: 2025-06-16T06:05:54.000Z (8 months ago)
- Last Synced: 2025-09-10T06:42:45.775Z (5 months ago)
- Topics: glue-code, interop, interoperability, marshalling-data, python, rcpp
- Language: Python
- Homepage:
- Size: 343 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Reusable functions for marshalling data between C, C++ and other programming languages
[](https://github.com/csiro-hydroinformatics/c-interop/blob/master/LICENSE.txt)

[](https://cinterop.readthedocs.io/en/latest/?badge=latest)
**Python package**: [](https://github.com/csiro-hydroinformatics/c-interop/blob/master/bindings/python/cinterop/LICENSE.txt)  [](https://cinterop.readthedocs.io/en/latest/?badge=latest) [](https://codecov.io/gh/csiro-hydroinformatics/c-interop) master: [](https://github.com/csiro-hydroinformatics/c-interop/actions/workflows/python-cinterop.yml) testing: [](https://github.com/csiro-hydroinformatics/c-interop/actions/workflows/python-cinterop.yml)
## Purpose and context
This repository contains reusable material for interoperability between C/C++ and other languages such as python, R, etc. It is using template-only C++, avoiding code duplications across several projects. The code design also stems from learning over the years to prevent some "gotchas" one can get into when diving deep in native interop accross compilation modules.
## Content
The `C++` header files cover interop for:
* common base types (characters, numeric, and vectors thereof)
* boost date-time
* time series (inherited from its usage in time step modelling systems)
The repo also includes interop packages for several higher level languages, that helps handling consistently (including memory management...) some of the data stemming from the C/C++ glue code:
* python package [cinterop](./bindings/python/cinterop/), also [available on pypi](https://pypi.org/project/cinterop/)
* R package [cinterop](./bindings/R/pkgs/cinterop/)
* Matlab [reference counting helpers](./bindings/matlab/)
## Related work, use cases
One use case arising from hydrologic forecasting is [uchronia-time-series](https://github.com/csiro-hydroinformatics/uchronia-time-series/).
Some features in the present repository depend on related material:
* [moirai](https://github.com/csiro-hydroinformatics/moirai), a C++ library designed to help handling C++ objects from so-called opaque pointers, via a C API.
* The python package [refcount](https://github.com/csiro-hydroinformatics/pyrefcount) for reference counting external resources.
Another resource is a [blog post](https://jmp75.github.io/work-blog/posts/2022-09-03-c-api-wrapper-generation.html) on generating programming language bindings to a C API.
## Example
For instance this library defines a template function `to_custom_character_vector` to convert C `char**` types to something useful for the higher level language at hand.
```c++
namespace cinterop
{
namespace utils
{
template
T to_custom_character_vector(char** names, int size, bool cleanup);
```
One specialization of this is a conversion to Rcpp's `CharacterVector` type for interop with R, which would be used like this:
```c++
int size;
char** values = GetEnsembleDatasetDataIdentifiers(dataLibrary->get(), &size);
CharacterVector cv = to_custom_character_vector(values, size, true);
```
## Related work
* [native interop glue code generation](https://github.com/csiro-hydroinformatics/c-api-wrapper-generation)
* An R package pupetting the former - I have yet to open source it though.