mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-23 05:35:13 +00:00
pre-commit clang-format, NO manual changes.
This commit is contained in:
parent
644d15054e
commit
1fa0065967
@ -13,34 +13,21 @@ namespace test_cases_for_stubgen {
|
|||||||
|
|
||||||
namespace basics {
|
namespace basics {
|
||||||
|
|
||||||
int answer() {
|
int answer() { return 42; }
|
||||||
return 42;
|
|
||||||
}
|
|
||||||
|
|
||||||
int sum(int a, int b) {
|
int sum(int a, int b) { return a + b; }
|
||||||
return a + b;
|
|
||||||
}
|
|
||||||
|
|
||||||
double midpoint(double left, double right){
|
double midpoint(double left, double right) { return left + (right - left) / 2; }
|
||||||
return left + (right - left)/2;
|
|
||||||
}
|
|
||||||
|
|
||||||
double weighted_midpoint(double left, double right, double alpha=0.5) {
|
double weighted_midpoint(double left, double right, double alpha = 0.5) {
|
||||||
return left + (right - left) * alpha;
|
return left + (right - left) * alpha;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Point {
|
struct Point {
|
||||||
|
|
||||||
enum class LengthUnit {
|
enum class LengthUnit { mm = 0, pixel, inch };
|
||||||
mm=0,
|
|
||||||
pixel,
|
|
||||||
inch
|
|
||||||
};
|
|
||||||
|
|
||||||
enum class AngleUnit {
|
enum class AngleUnit { radian = 0, degree };
|
||||||
radian=0,
|
|
||||||
degree
|
|
||||||
};
|
|
||||||
|
|
||||||
Point() : Point(0, 0) {}
|
Point() : Point(0, 0) {}
|
||||||
Point(double x, double y) : x(x), y(y) {}
|
Point(double x, double y) : x(x), y(y) {}
|
||||||
@ -52,19 +39,15 @@ struct Point {
|
|||||||
static LengthUnit length_unit;
|
static LengthUnit length_unit;
|
||||||
static AngleUnit angle_unit;
|
static AngleUnit angle_unit;
|
||||||
|
|
||||||
double length() const {
|
double length() const { return std::sqrt(x * x + y * y); }
|
||||||
return std::sqrt(x * x + y * y);
|
|
||||||
}
|
|
||||||
|
|
||||||
double distance_to(double other_x, double other_y) const {
|
double distance_to(double other_x, double other_y) const {
|
||||||
double dx = x - other_x;
|
double dx = x - other_x;
|
||||||
double dy = y - other_y;
|
double dy = y - other_y;
|
||||||
return std::sqrt(dx*dx + dy*dy);
|
return std::sqrt(dx * dx + dy * dy);
|
||||||
}
|
}
|
||||||
|
|
||||||
double distance_to(const Point& other) const {
|
double distance_to(const Point &other) const { return distance_to(other.x, other.y); }
|
||||||
return distance_to(other.x, other.y);
|
|
||||||
}
|
|
||||||
|
|
||||||
double x, y;
|
double x, y;
|
||||||
};
|
};
|
||||||
@ -76,51 +59,58 @@ const Point Point::y_axis = Point(0, 1);
|
|||||||
Point::LengthUnit Point::length_unit = Point::LengthUnit::mm;
|
Point::LengthUnit Point::length_unit = Point::LengthUnit::mm;
|
||||||
Point::AngleUnit Point::angle_unit = Point::AngleUnit::radian;
|
Point::AngleUnit Point::angle_unit = Point::AngleUnit::radian;
|
||||||
|
|
||||||
} // namespace: basics
|
} // namespace basics
|
||||||
|
|
||||||
void bind_basics(py::module& basics) {
|
void bind_basics(py::module &basics) {
|
||||||
|
|
||||||
using namespace basics;
|
using namespace basics;
|
||||||
|
|
||||||
// Functions
|
// Functions
|
||||||
basics.def("answer", &answer, "answer docstring, with end quote\""); // tests explicit docstrings
|
basics.def(
|
||||||
|
"answer", &answer, "answer docstring, with end quote\""); // tests explicit docstrings
|
||||||
basics.def("sum", &sum, "multiline docstring test, edge case quotes \"\"\"'''");
|
basics.def("sum", &sum, "multiline docstring test, edge case quotes \"\"\"'''");
|
||||||
basics.def("midpoint", &midpoint, py::arg("left"), py::arg("right"));
|
basics.def("midpoint", &midpoint, py::arg("left"), py::arg("right"));
|
||||||
basics.def("weighted_midpoint", weighted_midpoint, py::arg("left"), py::arg("right"), py::arg("alpha")=0.5);
|
basics.def("weighted_midpoint",
|
||||||
|
weighted_midpoint,
|
||||||
|
py::arg("left"),
|
||||||
|
py::arg("right"),
|
||||||
|
py::arg("alpha") = 0.5);
|
||||||
|
|
||||||
// Classes
|
// Classes
|
||||||
py::class_<Point> pyPoint(basics, "Point");
|
py::class_<Point> pyPoint(basics, "Point");
|
||||||
py::enum_<Point::LengthUnit> pyLengthUnit(pyPoint, "LengthUnit");
|
py::enum_<Point::LengthUnit> pyLengthUnit(pyPoint, "LengthUnit");
|
||||||
py::enum_<Point::AngleUnit> pyAngleUnit(pyPoint, "AngleUnit");
|
py::enum_<Point::AngleUnit> pyAngleUnit(pyPoint, "AngleUnit");
|
||||||
|
|
||||||
pyPoint
|
pyPoint.def(py::init<>())
|
||||||
.def(py::init<>())
|
|
||||||
.def(py::init<double, double>(), py::arg("x"), py::arg("y"))
|
.def(py::init<double, double>(), py::arg("x"), py::arg("y"))
|
||||||
.def("distance_to", py::overload_cast<double, double>(&Point::distance_to, py::const_), py::arg("x"), py::arg("y"))
|
.def("distance_to",
|
||||||
.def("distance_to", py::overload_cast<const Point&>(&Point::distance_to, py::const_), py::arg("other"))
|
py::overload_cast<double, double>(&Point::distance_to, py::const_),
|
||||||
|
py::arg("x"),
|
||||||
|
py::arg("y"))
|
||||||
|
.def("distance_to",
|
||||||
|
py::overload_cast<const Point &>(&Point::distance_to, py::const_),
|
||||||
|
py::arg("other"))
|
||||||
.def_readwrite("x", &Point::x)
|
.def_readwrite("x", &Point::x)
|
||||||
.def_property("y",
|
.def_property(
|
||||||
[](Point& self){ return self.y; },
|
"y",
|
||||||
[](Point& self, double value){ self.y = value; }
|
[](Point &self) { return self.y; },
|
||||||
)
|
[](Point &self, double value) { self.y = value; })
|
||||||
.def_property_readonly("length", &Point::length)
|
.def_property_readonly("length", &Point::length)
|
||||||
.def_property_readonly_static("x_axis", [](py::object /*cls*/){return Point::x_axis;})
|
.def_property_readonly_static("x_axis", [](py::object /*cls*/) { return Point::x_axis; })
|
||||||
.def_property_readonly_static("y_axis", [](py::object /*cls*/){return Point::y_axis;})
|
.def_property_readonly_static("y_axis", [](py::object /*cls*/) { return Point::y_axis; })
|
||||||
.def_readwrite_static("length_unit", &Point::length_unit)
|
.def_readwrite_static("length_unit", &Point::length_unit)
|
||||||
.def_property_static("angle_unit",
|
.def_property_static(
|
||||||
[](py::object& /*cls*/){ return Point::angle_unit; },
|
"angle_unit",
|
||||||
[](py::object& /*cls*/, Point::AngleUnit value){ Point::angle_unit = value; }
|
[](py::object & /*cls*/) { return Point::angle_unit; },
|
||||||
);
|
[](py::object & /*cls*/, Point::AngleUnit value) { Point::angle_unit = value; });
|
||||||
|
|
||||||
pyPoint.attr("origin") = Point::origin;
|
pyPoint.attr("origin") = Point::origin;
|
||||||
|
|
||||||
pyLengthUnit
|
pyLengthUnit.value("mm", Point::LengthUnit::mm)
|
||||||
.value("mm", Point::LengthUnit::mm)
|
|
||||||
.value("pixel", Point::LengthUnit::pixel)
|
.value("pixel", Point::LengthUnit::pixel)
|
||||||
.value("inch", Point::LengthUnit::inch);
|
.value("inch", Point::LengthUnit::inch);
|
||||||
|
|
||||||
pyAngleUnit
|
pyAngleUnit.value("radian", Point::AngleUnit::radian)
|
||||||
.value("radian", Point::AngleUnit::radian)
|
|
||||||
.value("degree", Point::AngleUnit::degree);
|
.value("degree", Point::AngleUnit::degree);
|
||||||
|
|
||||||
// Module-level attributes
|
// Module-level attributes
|
||||||
|
Loading…
Reference in New Issue
Block a user