-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
80 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include "test_move_arg.h" | ||
#include <pybind11/pybind11.h> | ||
#include <pybind11/iostream.h> | ||
#include <sstream> | ||
|
||
namespace py = pybind11; | ||
|
||
PYBIND11_MODULE(test_move_arg, m) { | ||
py::class_<Item>(m, "Item") | ||
.def(py::init<int>(), py::call_guard<py::scoped_ostream_redirect>()) | ||
.def("__repr__", [](const Item& item) { | ||
std::stringstream ss; | ||
ss << "py " << item; | ||
return ss.str(); | ||
}, py::call_guard<py::scoped_ostream_redirect>()); | ||
|
||
m.def("access", [](const Item& item) { | ||
std::cout << "access " << item << "\n"; | ||
}, py::call_guard<py::scoped_ostream_redirect>()); | ||
|
||
#if 0 // rvalue arguments fail during compilation | ||
m.def("consume", [](Item&& item) { | ||
std::cout << "consume " << item << "\n "; | ||
Item sink(std::move(item)); | ||
std::cout << " old: " << item << "\n new: " << sink << "\n"; | ||
}, py::call_guard<py::scoped_ostream_redirect>()); | ||
#endif | ||
|
||
m.def("working", [](int&& a) { std::cout << a << "\n"; }, | ||
py::call_guard<py::scoped_ostream_redirect>()); | ||
} |
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,34 @@ | ||
#pragma once | ||
#include <memory> | ||
#include <vector> | ||
#include <iostream> | ||
|
||
class Item; | ||
std::ostream& operator<<(std::ostream& os, const Item& item); | ||
|
||
/** Item class requires unique instances, i.e. only supports move construction */ | ||
class Item | ||
{ | ||
public: | ||
Item(int value) : value_(std::make_unique<int>(value)) { | ||
std::cout<< "new " << *this << "\n"; | ||
} | ||
~Item() { | ||
std::cout << "destroy " << *this << "\n"; | ||
} | ||
Item(const Item&) = delete; | ||
Item(Item&& other) { | ||
std::cout << "move " << other << " -> "; | ||
value_ = std::move(other.value_); | ||
std::cout << *this << "\n"; | ||
} | ||
|
||
std::unique_ptr<int> value_; | ||
}; | ||
|
||
std::ostream& operator<<(std::ostream& os, const Item& item) { | ||
os << "item " << &item << "("; | ||
if (item.value_) os << *item.value_; | ||
os << ")"; | ||
return os; | ||
} |
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,14 @@ | ||
import pytest | ||
from test_move_arg import Item, access | ||
|
||
|
||
def test(): | ||
item = Item(42) | ||
other = item | ||
access(item) | ||
print(item) | ||
del item | ||
print(other) | ||
|
||
if __name__ == "__main__": | ||
test() |