C++/Python Type Mapping
dict
We map Python's dict
to C++'s dict
:
#ifndef CUSTOM_DICT
using dict = std::shared_ptr<std::unordered_map<std::string, ipipe::any>>;
#else
// todo : custom dict with bidirectional binding between C++ and Python
#endif
C++ to Python
Basic Type Conversion
cpp | python | Remarks |
---|---|---|
std::string | bytes | |
cv::Mat | numpy.ndarray | |
at::Tensor | torch.Tensor | |
int | int | |
float | float | |
double | float | |
unsigned int | int | |
char | str | Inherited from Pybind11. |
unsigned char | int | |
bool | bool |
Container Conversion
For containers or nested containers, the conversion rules are as follows (where T is the basic type from the above table):
cpp | python | Remarks |
---|---|---|
std::unordered_map<std::string, T> | dict[str, T] | |
std::unordered_set<T> | set[T] | T must be hashable |
std::vector<T> | list[T] | T is not an algebraic type |
std::vector<T> | numpy.ndarray[T] | T is an algebraic type and not bool |
std::vector<std::vector<T>> | list[numpy.ndarray[T]] | T is an algebraic type and not bool |
std::map<std::string, T> | Not supported | |
std::set<T> | Not supported |
Nested Types
The conversion of any
to Python types also supports the following nested types:
cpp | python | Remarks |
---|---|---|
std::vector<any> | list[Any] | Can be nested infinitely |
std::unordered_map<std::string, any> | dict[str, Any] | Can be nested infinitely, 'data' key will be ignored |
C++ Wrapper
For non-containers that fail to convert using the above rules, a custom type will be returned to Python:
cpp | python | Remarks |
---|---|---|
any | torchpipe.any |
Python to C++
Basic Type Conversion
python | cpp | Remarks |
---|---|---|
str | std::string | |
bytes | std::string | |
numpy.ndarray | Not supported | |
torch.Tensor | at::Tensor | |
bool | bool | |
float | float | precision loss |
int | int |
Container Conversion
For containers or nested containers, the conversion table is as follows (where T is the basic type from the above table):
python | cpp | Remarks |
---|---|---|
dict[str, T] | std::unordered_map<std::string, T> | |
set[T] | std::unordered_set<T> | T must be hashable, i.e. T != torch.Tensor |
list[T] | std::vector<T> |
When the container on the Python side is empty, the C++ side cannot perceive the specific type. In this case, the framework will automatically handle such situations in a compatible way, so there is no need to pay extra attention to this scenario when using it.