mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-22 21:25:13 +00:00
pre-commit clang-format, NO manual changes.
This commit is contained in:
parent
644d15054e
commit
1fa0065967
@ -13,60 +13,43 @@ 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) {}
|
||||||
|
|
||||||
static const Point origin;
|
static const Point origin;
|
||||||
static const Point x_axis;
|
static const Point x_axis;
|
||||||
static const Point y_axis;
|
static const Point y_axis;
|
||||||
|
|
||||||
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;
|
||||||
};
|
};
|
||||||
|
|
||||||
const Point Point::origin = Point(0, 0);
|
const Point Point::origin = Point(0, 0);
|
||||||
@ -76,56 +59,63 @@ 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(
|
||||||
basics.def("sum", &sum, "multiline docstring test, edge case quotes \"\"\"'''");
|
"answer", &answer, "answer docstring, with end quote\""); // tests explicit docstrings
|
||||||
basics.def("midpoint", &midpoint, py::arg("left"), py::arg("right"));
|
basics.def("sum", &sum, "multiline docstring test, edge case quotes \"\"\"'''");
|
||||||
basics.def("weighted_midpoint", weighted_midpoint, py::arg("left"), py::arg("right"), py::arg("alpha")=0.5);
|
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);
|
||||||
|
|
||||||
// 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",
|
||||||
.def("distance_to", py::overload_cast<double, double>(&Point::distance_to, py::const_), py::arg("x"), py::arg("y"))
|
py::overload_cast<double, double>(&Point::distance_to, py::const_),
|
||||||
.def("distance_to", py::overload_cast<const Point&>(&Point::distance_to, py::const_), py::arg("other"))
|
py::arg("x"),
|
||||||
.def_readwrite("x", &Point::x)
|
py::arg("y"))
|
||||||
.def_property("y",
|
.def("distance_to",
|
||||||
[](Point& self){ return self.y; },
|
py::overload_cast<const Point &>(&Point::distance_to, py::const_),
|
||||||
[](Point& self, double value){ self.y = value; }
|
py::arg("other"))
|
||||||
)
|
.def_readwrite("x", &Point::x)
|
||||||
.def_property_readonly("length", &Point::length)
|
.def_property(
|
||||||
.def_property_readonly_static("x_axis", [](py::object /*cls*/){return Point::x_axis;})
|
"y",
|
||||||
.def_property_readonly_static("y_axis", [](py::object /*cls*/){return Point::y_axis;})
|
[](Point &self) { return self.y; },
|
||||||
.def_readwrite_static("length_unit", &Point::length_unit)
|
[](Point &self, double value) { self.y = value; })
|
||||||
.def_property_static("angle_unit",
|
.def_property_readonly("length", &Point::length)
|
||||||
[](py::object& /*cls*/){ return Point::angle_unit; },
|
.def_property_readonly_static("x_axis", [](py::object /*cls*/) { return Point::x_axis; })
|
||||||
[](py::object& /*cls*/, Point::AngleUnit value){ Point::angle_unit = value; }
|
.def_property_readonly_static("y_axis", [](py::object /*cls*/) { return Point::y_axis; })
|
||||||
);
|
.def_readwrite_static("length_unit", &Point::length_unit)
|
||||||
|
.def_property_static(
|
||||||
|
"angle_unit",
|
||||||
|
[](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
|
||||||
basics.attr("PI") = std::acos(-1);
|
basics.attr("PI") = std::acos(-1);
|
||||||
basics.attr("__version__") = "0.0.1";
|
basics.attr("__version__") = "0.0.1";
|
||||||
}
|
}
|
||||||
|
|
||||||
struct UserType {
|
struct UserType {
|
||||||
|
Loading…
Reference in New Issue
Block a user