-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add class_held_as_shared__regex / handle shared_ptr holder for classes
cpp_to_python: standard_type_replacements, smart_ptr before vector
- Loading branch information
Showing
13 changed files
with
294 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include "api_marker.h" | ||
#include <memory> | ||
#include <vector> | ||
|
||
struct SmartElem { | ||
int x = 0; | ||
}; | ||
|
||
|
||
MY_API inline std::shared_ptr<SmartElem> make_shared_elem(int x) | ||
{ | ||
auto r = std::make_shared<SmartElem>(); | ||
r->x = x; | ||
return r; | ||
} | ||
|
||
|
||
class ElemContainer | ||
{ | ||
public: | ||
ElemContainer(): | ||
vec { {1}, {2}}, | ||
shared_ptr(make_shared_elem(3)), | ||
vec_shared_ptrs { make_shared_elem(4), make_shared_elem(5) } | ||
{ | ||
} | ||
|
||
std::vector<SmartElem> vec; | ||
std::shared_ptr<SmartElem> shared_ptr; | ||
std::vector<std::shared_ptr<SmartElem>> vec_shared_ptrs; | ||
}; | ||
|
||
|
||
// The signature below is incompatible with pybind11: | ||
// void change_unique_elem(std::unique_ptr<Elem>& elem, int x) { ... } | ||
// Reason: such a signature might change the pointer value! Example: | ||
// void reset_unique_elem(std::unique_ptr<Elem>& elem) { elem.reset(new Elem()); } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// ============================================================================ | ||
// This file was autogenerated | ||
// It is presented side to side with its source: smart_ptr.h | ||
// It is not used in the compilation | ||
// (see integration_tests/bindings/pybind_mylib.cpp which contains the full binding | ||
// code, including this code) | ||
// ============================================================================ | ||
|
||
#include <pybind11/pybind11.h> | ||
#include <pybind11/stl.h> | ||
#include <pybind11/functional.h> | ||
#include <pybind11/numpy.h> | ||
#include "mylib_main/mylib.h" | ||
|
||
namespace py = pybind11; | ||
|
||
// <litgen_glue_code> // Autogenerated code below! Do not edit! | ||
|
||
// </litgen_glue_code> // Autogenerated code end | ||
|
||
|
||
void py_init_module_mylib(py::module& m) | ||
{ | ||
// <litgen_pydef> // Autogenerated code below! Do not edit! | ||
//////////////////// <generated_from:smart_ptr.h> //////////////////// | ||
auto pyClassSmartElem = | ||
py::class_<SmartElem, std::shared_ptr<SmartElem>> | ||
(m, "SmartElem", "") | ||
.def(py::init<>([]( | ||
int x = 0) | ||
{ | ||
auto r = std::make_unique<SmartElem>(); | ||
r->x = x; | ||
return r; | ||
}) | ||
, py::arg("x") = 0 | ||
) | ||
.def_readwrite("x", &SmartElem::x, "") | ||
; | ||
|
||
|
||
m.def("make_shared_elem", | ||
make_shared_elem, py::arg("x")); | ||
|
||
|
||
auto pyClassElemContainer = | ||
py::class_<ElemContainer> | ||
(m, "ElemContainer", "") | ||
.def(py::init<>()) | ||
.def_readwrite("vec", &ElemContainer::vec, "") | ||
.def_readwrite("shared_ptr", &ElemContainer::shared_ptr, "") | ||
.def_readwrite("vec_shared_ptrs", &ElemContainer::vec_shared_ptrs, "") | ||
; | ||
//////////////////// </generated_from:smart_ptr.h> //////////////////// | ||
|
||
// </litgen_pydef> // Autogenerated code end | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# ============================================================================ | ||
# This file was autogenerated | ||
# It is presented side to side with its source: smart_ptr.h | ||
# (see integration_tests/bindings/lg_mylib/__init__pyi which contains the full | ||
# stub code, including this code) | ||
# ============================================================================ | ||
from typing import List | ||
|
||
# type: ignore | ||
|
||
# <litgen_stub> // Autogenerated code below! Do not edit! | ||
#################### <generated_from:smart_ptr.h> #################### | ||
|
||
class SmartElem: | ||
x: int = 0 | ||
def __init__(self, x: int = 0) -> None: | ||
"""Auto-generated default constructor with named params""" | ||
pass | ||
|
||
def make_shared_elem(x: int) -> SmartElem: | ||
pass | ||
|
||
class ElemContainer: | ||
def __init__(self) -> None: | ||
pass | ||
vec: List[SmartElem] | ||
shared_ptr: SmartElem | ||
vec_shared_ptrs: List[SmartElem] | ||
|
||
# The signature below is incompatible with pybind11: | ||
# None change_unique_elem(std::unique_ptr<Elem>& elem, int x) { ... } | ||
# Reason: such a signature might change the pointer value! Example: | ||
# None reset_unique_elem(std::unique_ptr<Elem>& elem) { elem.reset(new Elem()); } | ||
#################### </generated_from:smart_ptr.h> #################### | ||
|
||
# </litgen_stub> // Autogenerated code end! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import lg_mylib | ||
|
||
|
||
def test_make_shared_elem(): | ||
elem = lg_mylib.make_shared_elem(3) | ||
assert elem.x == 3 | ||
|
||
|
||
def test_elem_container(): | ||
container = lg_mylib.ElemContainer() | ||
|
||
assert len(container.vec) == 2 | ||
assert container.vec[0].x == 1 | ||
|
||
assert container.shared_ptr is not None | ||
assert container.shared_ptr.x == 3 | ||
|
||
assert len(container.vec_shared_ptrs) == 2 | ||
assert container.vec_shared_ptrs[0].x == 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters