| 1 |
/** |
| 2 |
* This file contains the pybind11 reference implementation for the stugen tests, |
| 3 |
* and was originally inspired by: |
| 4 |
* |
| 5 |
* https://github.com/sizmailov/pybind11-mypy-demo |
| 6 |
* |
| 7 |
* Copyright (c) 2016 The Pybind Development Team, All rights reserved. |
| 8 |
* |
| 9 |
* Redistribution and use in source and binary forms, with or without |
| 10 |
* modification, are permitted provided that the following conditions are met: |
| 11 |
* |
| 12 |
* 1. Redistributions of source code must retain the above copyright notice, this |
| 13 |
* list of conditions and the following disclaimer. |
| 14 |
* |
| 15 |
* 2. Redistributions in binary form must reproduce the above copyright notice, |
| 16 |
* this list of conditions and the following disclaimer in the documentation |
| 17 |
* and/or other materials provided with the distribution. |
| 18 |
* |
| 19 |
* 3. Neither the name of the copyright holder nor the names of its contributors |
| 20 |
* may be used to endorse or promote products derived from this software |
| 21 |
* without specific prior written permission. |
| 22 |
* |
| 23 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 24 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 25 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 26 |
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 27 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 28 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 29 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 30 |
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 31 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 |
* |
| 34 |
* You are under no obligation whatsoever to provide any bug fixes, patches, or |
| 35 |
* upgrades to the features, functionality or performance of the source code |
| 36 |
* ("Enhancements") to anyone; however, if you choose to make your Enhancements |
| 37 |
* available either publicly, or directly to the author of this software, without |
| 38 |
* imposing a separate written license agreement for such Enhancements, then you |
| 39 |
* hereby grant the following license: a non-exclusive, royalty-free perpetual |
| 40 |
* license to install, use, modify, prepare derivative works, incorporate into |
| 41 |
* other computer software, distribute, and sublicense such enhancements or |
| 42 |
* derivative works thereof, in binary and source code form. |
| 43 |
*/ |
| 44 |
|
| 45 |
#include <cmath> |
| 46 |
#include <filesystem> |
| 47 |
#include <optional> |
| 48 |
#include <utility> |
| 49 |
#include <vector> |
| 50 |
|
| 51 |
#include <pybind11/pybind11.h> |
| 52 |
#include <pybind11/stl.h> |
| 53 |
#include <pybind11/stl/filesystem.h> |
| 54 |
|
| 55 |
namespace py = pybind11; |
| 56 |
|
| 57 |
// ---------------------------------------------------------------------------- |
| 58 |
// Dedicated test cases |
| 59 |
// ---------------------------------------------------------------------------- |
| 60 |
|
| 61 |
std::vector<float> funcReturningVector() |
| 62 |
{ |
| 63 |
return std::vector<float>{1.0, 2.0, 3.0}; |
| 64 |
} |
| 65 |
|
| 66 |
std::pair<int, float> funcReturningPair() |
| 67 |
{ |
| 68 |
return std::pair{42, 1.0}; |
| 69 |
} |
| 70 |
|
| 71 |
std::optional<int> funcReturningOptional() |
| 72 |
{ |
| 73 |
return std::nullopt; |
| 74 |
} |
| 75 |
|
| 76 |
std::filesystem::path funcReturningPath() |
| 77 |
{ |
| 78 |
return std::filesystem::path{"foobar"}; |
| 79 |
} |
| 80 |
|
| 81 |
namespace dummy_sub_namespace { |
| 82 |
struct HasNoBinding{}; |
| 83 |
} |
| 84 |
|
| 85 |
// We can enforce the case of an incomplete signature by referring to a type in |
| 86 |
// some namespace that doesn't have a pybind11 binding. |
| 87 |
dummy_sub_namespace::HasNoBinding funcIncompleteSignature() |
| 88 |
{ |
| 89 |
return dummy_sub_namespace::HasNoBinding{}; |
| 90 |
} |
| 91 |
|
| 92 |
struct TestStruct |
| 93 |
{ |
| 94 |
int field_readwrite; |
| 95 |
int field_readwrite_docstring; |
| 96 |
int field_readonly; |
| 97 |
}; |
| 98 |
|
| 99 |
struct StaticMethods |
| 100 |
{ |
| 101 |
static int some_static_method(int a, int b) { return 42; } |
| 102 |
static int overloaded_static_method(int value) { return 42; } |
| 103 |
static double overloaded_static_method(double value) { return 1.0; } |
| 104 |
}; |
| 105 |
|
| 106 |
// Bindings |
| 107 |
|
| 108 |
void bind_test_cases(py::module& m) { |
| 109 |
m.def("func_returning_vector", &funcReturningVector); |
| 110 |
m.def("func_returning_pair", &funcReturningPair); |
| 111 |
m.def("func_returning_optional", &funcReturningOptional); |
| 112 |
m.def("func_returning_path", &funcReturningPath); |
| 113 |
|
| 114 |
m.def("func_incomplete_signature", &funcIncompleteSignature); |
| 115 |
|
| 116 |
py::class_<TestStruct>(m, "TestStruct") |
| 117 |
.def_readwrite("field_readwrite", &TestStruct::field_readwrite) |
| 118 |
.def_readwrite("field_readwrite_docstring", &TestStruct::field_readwrite_docstring, "some docstring") |
| 119 |
.def_property_readonly( |
| 120 |
"field_readonly", |
| 121 |
[](const TestStruct& x) { |
| 122 |
return x.field_readonly; |
| 123 |
}, |
| 124 |
"some docstring"); |
| 125 |
|
| 126 |
// Static methods |
| 127 |
py::class_<StaticMethods> pyStaticMethods(m, "StaticMethods"); |
| 128 |
|
| 129 |
pyStaticMethods |
| 130 |
.def_static( |
| 131 |
"some_static_method", |
| 132 |
&StaticMethods::some_static_method, R"#(None)#", py::arg("a"), py::arg("b")) |
| 133 |
.def_static( |
| 134 |
"overloaded_static_method", |
| 135 |
py::overload_cast<int>(&StaticMethods::overloaded_static_method), py::arg("value")) |
| 136 |
.def_static( |
| 137 |
"overloaded_static_method", |
| 138 |
py::overload_cast<double>(&StaticMethods::overloaded_static_method), py::arg("value")); |
| 139 |
} |
| 140 |
|
| 141 |
// ---------------------------------------------------------------------------- |
| 142 |
// Original demo |
| 143 |
// ---------------------------------------------------------------------------- |
| 144 |
|
| 145 |
namespace demo { |
| 146 |
|
| 147 |
int answer() { |
| 148 |
return 42; |
| 149 |
} |
| 150 |
|
| 151 |
int sum(int a, int b) { |
| 152 |
return a + b; |
| 153 |
} |
| 154 |
|
| 155 |
double midpoint(double left, double right){ |
| 156 |
return left + (right - left)/2; |
| 157 |
} |
| 158 |
|
| 159 |
double weighted_midpoint(double left, double right, double alpha=0.5) { |
| 160 |
return left + (right - left) * alpha; |
| 161 |
} |
| 162 |
|
| 163 |
struct Point { |
| 164 |
|
| 165 |
enum class LengthUnit { |
| 166 |
mm=0, |
| 167 |
pixel, |
| 168 |
inch |
| 169 |
}; |
| 170 |
|
| 171 |
enum class AngleUnit { |
| 172 |
radian=0, |
| 173 |
degree |
| 174 |
}; |
| 175 |
|
| 176 |
Point() : Point(0, 0) {} |
| 177 |
Point(double x, double y) : x(x), y(y) {} |
| 178 |
|
| 179 |
static const Point origin; |
| 180 |
static const Point x_axis; |
| 181 |
static const Point y_axis; |
| 182 |
|
| 183 |
static LengthUnit length_unit; |
| 184 |
static AngleUnit angle_unit; |
| 185 |
|
| 186 |
double length() const { |
| 187 |
return std::sqrt(x * x + y * y); |
| 188 |
} |
| 189 |
|
| 190 |
double distance_to(double other_x, double other_y) const { |
| 191 |
double dx = x - other_x; |
| 192 |
double dy = y - other_y; |
| 193 |
return std::sqrt(dx*dx + dy*dy); |
| 194 |
} |
| 195 |
|
| 196 |
double distance_to(const Point& other) const { |
| 197 |
return distance_to(other.x, other.y); |
| 198 |
} |
| 199 |
|
| 200 |
std::vector<double> as_vector() |
| 201 |
{ |
| 202 |
return std::vector<double>{x, y}; |
| 203 |
} |
| 204 |
|
| 205 |
double x, y; |
| 206 |
}; |
| 207 |
|
| 208 |
const Point Point::origin = Point(0, 0); |
| 209 |
const Point Point::x_axis = Point(1, 0); |
| 210 |
const Point Point::y_axis = Point(0, 1); |
| 211 |
|
| 212 |
Point::LengthUnit Point::length_unit = Point::LengthUnit::mm; |
| 213 |
Point::AngleUnit Point::angle_unit = Point::AngleUnit::radian; |
| 214 |
|
| 215 |
} // namespace: demo |
| 216 |
|
| 217 |
// Bindings |
| 218 |
|
| 219 |
void bind_demo(py::module& m) { |
| 220 |
|
| 221 |
using namespace demo; |
| 222 |
|
| 223 |
// Functions |
| 224 |
m.def("answer", &answer, "answer docstring, with end quote\""); // tests explicit docstrings |
| 225 |
m.def("sum", &sum, "multiline docstring test, edge case quotes \"\"\"'''"); |
| 226 |
m.def("midpoint", &midpoint, py::arg("left"), py::arg("right")); |
| 227 |
m.def("weighted_midpoint", weighted_midpoint, py::arg("left"), py::arg("right"), py::arg("alpha")=0.5); |
| 228 |
|
| 229 |
// Classes |
| 230 |
py::class_<Point> pyPoint(m, "Point"); |
| 231 |
py::enum_<Point::LengthUnit> pyLengthUnit(pyPoint, "LengthUnit"); |
| 232 |
py::enum_<Point::AngleUnit> pyAngleUnit(pyPoint, "AngleUnit"); |
| 233 |
|
| 234 |
pyPoint |
| 235 |
.def(py::init<>()) |
| 236 |
.def(py::init<double, double>(), py::arg("x"), py::arg("y")) |
| 237 |
.def("distance_to", py::overload_cast<double, double>(&Point::distance_to, py::const_), py::arg("x"), py::arg("y")) |
| 238 |
.def("distance_to", py::overload_cast<const Point&>(&Point::distance_to, py::const_), py::arg("other")) |
| 239 |
.def("as_list", &Point::as_vector) |
| 240 |
.def_readwrite("x", &Point::x, "some docstring") |
| 241 |
.def_property("y", |
| 242 |
[](Point& self){ return self.y; }, |
| 243 |
[](Point& self, double value){ self.y = value; } |
| 244 |
) |
| 245 |
.def_property_readonly("length", &Point::length) |
| 246 |
.def_property_readonly_static("x_axis", [](py::object cls){return Point::x_axis;}) |
| 247 |
.def_property_readonly_static("y_axis", [](py::object cls){return Point::y_axis;}, "another docstring") |
| 248 |
.def_readwrite_static("length_unit", &Point::length_unit) |
| 249 |
.def_property_static("angle_unit", |
| 250 |
[](py::object& /*cls*/){ return Point::angle_unit; }, |
| 251 |
[](py::object& /*cls*/, Point::AngleUnit value){ Point::angle_unit = value; } |
| 252 |
); |
| 253 |
|
| 254 |
pyPoint.attr("origin") = Point::origin; |
| 255 |
|
| 256 |
pyLengthUnit |
| 257 |
.value("mm", Point::LengthUnit::mm) |
| 258 |
.value("pixel", Point::LengthUnit::pixel) |
| 259 |
.value("inch", Point::LengthUnit::inch); |
| 260 |
|
| 261 |
pyAngleUnit |
| 262 |
.value("radian", Point::AngleUnit::radian) |
| 263 |
.value("degree", Point::AngleUnit::degree); |
| 264 |
|
| 265 |
// Module-level attributes |
| 266 |
m.attr("PI") = std::acos(-1); |
| 267 |
m.attr("__version__") = "0.0.1"; |
| 268 |
} |
| 269 |
|
| 270 |
// ---------------------------------------------------------------------------- |
| 271 |
// Module entry point |
| 272 |
// ---------------------------------------------------------------------------- |
| 273 |
|
| 274 |
PYBIND11_MODULE(pybind11_fixtures, m) { |
| 275 |
bind_test_cases(m); |
| 276 |
|
| 277 |
auto demo = m.def_submodule("demo"); |
| 278 |
bind_demo(demo); |
| 279 |
} |