Skip to content

Commit

Permalink
fix str output #15
Browse files Browse the repository at this point in the history
  • Loading branch information
timdewhirst committed Aug 12, 2022
1 parent 2ef770a commit 57ebc64
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 24 deletions.
14 changes: 8 additions & 6 deletions pybind/core/point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,21 @@ bool add_point_type(py::module& m)
using vector_t = vector2<T>;
using value_t = typename point_t::value_t;

auto to_str = [](const point_t& p) {
std::ostringstream ss;
ss << p;
return ss.str();
};

py::class_<point_t>(m, point_trait<T>::name())
.def(py::init())
.def(py::init<value_t, value_t>())
.def(py::self == py::self)
.def(py::self != py::self)
.def("data", &point_t::data)
.def("components", &point_t::data)
.def("__repr__",
[](const point_t& p) {
std::ostringstream ss;
ss << p;
return ss.str();
})
.def("__repr__", to_str)
.def("__str__", to_str)
.def("__getitem__",
[](const point_t& p, size_t i) {
return p[i];
Expand Down
14 changes: 8 additions & 6 deletions pybind/core/rect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ namespace py = pybind11;
void add_rect(py::module& m)
{
// size
auto to_str = [](const rect& r) {
std::ostringstream ss;
ss << r;
return ss.str();
};

py::class_<rect>(m, "rect")
.def(py::init())
.def(py::init<rect::point_t, size>())
Expand All @@ -38,10 +44,6 @@ void add_rect(py::module& m)
.def("area", &rect::area)
.def(py::self == py::self)
.def(py::self != py::self)
.def("__repr__",
[](const size& s) {
std::ostringstream ss;
ss << s;
return ss.str();
});
.def("__repr__", to_str)
.def("__str__", to_str);
}
14 changes: 8 additions & 6 deletions pybind/core/size.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ namespace py = pybind11;

void add_size(py::module& m)
{
auto to_str = [](const size& s) {
std::ostringstream ss;
ss << s;
return ss.str();
};

// size
py::class_<size>(m, "size")
.def(py::init())
Expand All @@ -31,12 +37,8 @@ void add_size(py::module& m)
.def(py::self - py::self)
.def(py::self == py::self)
.def(py::self != py::self)
.def("__repr__",
[](const size& s) {
std::ostringstream ss;
ss << s;
return ss.str();
});
.def("__repr__", to_str)
.def("__str__", to_str);
m.def("maximal_size", &maximal_size);
m.def("minimal_size", &minimal_size);
m.def("transpose", &transpose);
Expand Down
14 changes: 8 additions & 6 deletions pybind/core/vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,21 @@ bool add_vector_type(py::module& m)
using vector_t = vector2<T>;
using value_t = typename vector_t::value_t;

auto to_str = [](const vector_t& p) {
std::ostringstream ss;
ss << p;
return ss.str();
};

py::class_<vector_t>(m, vector_trait<T>::name())
.def(py::init())
.def(py::init<value_t, value_t>())
.def(py::self == py::self)
.def(py::self != py::self)
.def("data", &vector_t::data)
.def("components", &vector_t::data)
.def("__repr__",
[](const vector_t& p) {
std::ostringstream ss;
ss << p;
return ss.str();
})
.def("__repr__", to_str)
.def("__str__", to_str)
.def("__getitem__",
[](const vector_t& p, size_t i) {
return p[i];
Expand Down

0 comments on commit 57ebc64

Please sign in to comment.