Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dubzzz/py2cpp
Convert PyObject* to C++ datatypes and vice versa
https://github.com/dubzzz/py2cpp
conversion cpp python
Last synced: about 1 month ago
JSON representation
Convert PyObject* to C++ datatypes and vice versa
- Host: GitHub
- URL: https://github.com/dubzzz/py2cpp
- Owner: dubzzz
- License: mit
- Created: 2015-06-26T20:22:46.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-11-20T12:03:10.000Z (almost 8 years ago)
- Last Synced: 2024-05-02T00:04:37.851Z (7 months ago)
- Topics: conversion, cpp, python
- Language: C++
- Size: 88.9 KB
- Stars: 28
- Watchers: 2
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Py2Cpp [![Build Status](https://travis-ci.org/dubzzz/Py2Cpp.svg?branch=master)](https://travis-ci.org/dubzzz/Py2Cpp)[![Coverage Status](https://coveralls.io/repos/dubzzz/Py2Cpp/badge.svg?branch=master&service=github)](https://coveralls.io/github/dubzzz/Py2Cpp?branch=master)
## Metaprogramming serving Py->C++ and C++->Py
This library makes it easy to convert Python objects (Python C API) to standard C++ datatypes. It is based on a single header file that uses templates and variadic templates in order to perform the conversions. It requires C++11 or superior.
## How to use it?
### Python objects towards C++ instances
#### Basic usages: std datatypes
The file ```src/py2cpp.hpp``` defines the functor ```CppBuilder```, which is responsible to build the corresponding C++ element of a given PyObject instance automatically. A same functor can be use for several PyObject.
For the moment, the conversion from Python to C++ can generate combinations of the following datatypes:
- ```PyObject*```
- ```bool```
- ```int``` and ```unsigned int```
- ```long``` and ```unsigned long```
- ```long long``` and ```unsigned long long```
- ```double```
- ```std::string``` and ```std::wstring```
- ```std::map``` -- from ```dict```
- ```std::set``` -- from ```set```
- ```std::tuple``` -- from ```tuple```
- ```std::vector``` -- from ```list```Converting a PyObject* to a C++ element is as simple as: ```T my_cpp_elt { CppBuilder()(py_object) };``` where ```T``` should be replace by the conjonction of datatypes you want.
For instance, if a PyObject* contains a ```dict(tuple(int,int,double),list(int))```, you can easily convert it into C++ datatypes by using:
```
auto out { CppBuilder,std::vector>>()(py_object) };
```#### More advance usages: fill your own struct/class
To describe...