Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create copy constructor for Python #10

Closed
larsgeb opened this issue Jun 27, 2020 · 1 comment
Closed

Create copy constructor for Python #10

larsgeb opened this issue Jun 27, 2020 · 1 comment
Assignees
Labels
cpp-enhancement An enhancement of the code that can likely be done completely on the C++ side. enhancement New feature or request pybind-enhancement An enhancement of the code that must likely be done on the Python/C++ interface. unexpected-behaviour
Milestone

Comments

@larsgeb
Copy link
Owner

larsgeb commented Jun 27, 2020

In Python, a lot of objects are mvoed around. When someone now creates and fdModel class and copies it, they actually reference the same object:

import psvWave

a = psvWave.fdModel("conf.ini")
b = a
a.set_model(...)

m = b.get_model() # same as the model just set.

I think this classifies as unexpected behaviour. There is no available copy constructor, and therefore to duplicate an fdModel object with its current state one has to go through the exercise of running all statements up to the duplication point multiple times.

To hide some of this complexity, a copy constructor should be created in C++ and interfaced to Python.

@larsgeb larsgeb added enhancement New feature or request pybind-enhancement An enhancement of the code that must likely be done on the Python/C++ interface. cpp-enhancement An enhancement of the code that can likely be done completely on the C++ side. unexpected-behaviour labels Jun 27, 2020
@larsgeb larsgeb added this to the 0.2.0 milestone Jun 27, 2020
@larsgeb larsgeb self-assigned this Jun 27, 2020
@larsgeb
Copy link
Owner Author

larsgeb commented Jul 7, 2020

Added in 376eaa2.

Method that calls the C++ copy constructor:

psvWave/src/psvWave.cpp

Lines 324 to 327 in 376eaa2

fdModelExtended *copy() {
// std::cout << "Returning" << std::endl;
return std::move(new fdModelExtended(*this));
}

Python binding for the copy method:

psvWave/src/psvWave.cpp

Lines 370 to 373 in 376eaa2

.def("copy", &fdModelExtended::copy,
"copy() -> psvWave.fdModel\n"
"\n"
"Returns a copy of the object, duplicating all members.")

@larsgeb larsgeb closed this as completed Jul 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cpp-enhancement An enhancement of the code that can likely be done completely on the C++ side. enhancement New feature or request pybind-enhancement An enhancement of the code that must likely be done on the Python/C++ interface. unexpected-behaviour
Projects
None yet
Development

No branches or pull requests

1 participant