TerrainGen coverage


terraingen/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
49 / 54
90.7%
Branches:
17 / 25
68.0%
Functions:
15 / 15
100.0%

python/src/trampolines.h
Line Branch Exec Source
1 /// @file
2 /// @brief Calling the methods a Python subclass overrides, for the trampolines.
3 #pragma once
4
5 #include "common.h"
6
7 #include <nanobind/trampoline.h>
8
9 #include <optional>
10 #include <utility>
11
12 namespace terraingen::python {
13
14 /// An argument passed to Python by reference rather than copied, such as a raster the
15 /// override changes in place.
16 template <typename T> struct ByReference {
17 T& value;
18 };
19
20 877068 template <typename T> decltype(auto) PythonArgument(T&& argument) {
21 877068 return std::forward<T>(argument);
22 }
23
24 508 template <typename T> nb::object PythonArgument(ByReference<T> argument) {
25
2/2
nanobind::object terraingen::python::PythonArgument<terraingen::RandomStream>(terraingen::python::ByReference<terraingen::RandomStream>):
✓ Branch 2 taken 503 times.
nanobind::object terraingen::python::PythonArgument<terraingen::Raster<float> >(terraingen::python::ByReference<terraingen::Raster<float> >):
✓ Branch 2 taken 5 times.
1016 return nb::cast(&argument.value, nb::rv_policy::reference);
26 }
27
28 /// Calls the Python override `name` if the subclass defines one. The ticket holds the GIL
29 /// while the arguments are wrapped and the result converted.
30 /// @param args The arguments; wrap ones to pass by reference in `ByReference`.
31 /// @return What the override returned as `Result`, or nothing without an override.
32 template <typename Result, typename... Args>
33 140847 std::optional<Result> CallOverride(const nb::detail::trampoline& trampoline, const char* name,
34 Args&&... args) {
35 140847 const nb::detail::ticket ticket(trampoline, name, nb::detail::str_hash(name), false);
36
4/6
std::optional<double> terraingen::python::CallOverride<double, terraingen::python::ByReference<terraingen::RandomStream>, double&, double&>(nanobind::detail::trampoline const&, char const*, terraingen::python::ByReference<terraingen::RandomStream>&&, double&, double&):
✓ Branch 0 taken 4018 times.
✓ Branch 1 taken 503 times.
std::optional<float> terraingen::python::CallOverride<float, float&, double const&, double const&, terraingen::LayerContext const&>(nanobind::detail::trampoline const&, char const*, float&, double const&, double const&, terraingen::LayerContext const&):
✗ Branch 0 not taken.
✓ Branch 1 taken 136325 times.
std::optional<terraingen::Vec2d> terraingen::python::CallOverride<terraingen::Vec2d>(nanobind::detail::trampoline const&, char const*):
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
140847 if (!ticket.key.is_valid()) {
37 4019 return std::nullopt;
38 }
39
3/4
std::optional<double> terraingen::python::CallOverride<double, terraingen::python::ByReference<terraingen::RandomStream>, double&, double&>(nanobind::detail::trampoline const&, char const*, terraingen::python::ByReference<terraingen::RandomStream>&&, double&, double&):
✓ Branch 5 taken 503 times.
✓ Branch 8 taken 503 times.
std::optional<float> terraingen::python::CallOverride<float, float&, double const&, double const&, terraingen::LayerContext const&>(nanobind::detail::trampoline const&, char const*, float&, double const&, double const&, terraingen::LayerContext const&):
✓ Branch 9 taken 136325 times.
std::optional<terraingen::Vec2d> terraingen::python::CallOverride<terraingen::Vec2d>(nanobind::detail::trampoline const&, char const*):
✗ Branch 1 not taken.
274159 return nb::cast<Result>(
40
2/3
std::optional<double> terraingen::python::CallOverride<double, terraingen::python::ByReference<terraingen::RandomStream>, double&, double&>(nanobind::detail::trampoline const&, char const*, terraingen::python::ByReference<terraingen::RandomStream>&&, double&, double&):
✓ Branch 1 taken 503 times.
std::optional<float> terraingen::python::CallOverride<float, float&, double const&, double const&, terraingen::LayerContext const&>(nanobind::detail::trampoline const&, char const*, float&, double const&, double const&, terraingen::LayerContext const&):
✓ Branch 1 taken 136325 times.
std::optional<terraingen::Vec2d> terraingen::python::CallOverride<terraingen::Vec2d>(nanobind::detail::trampoline const&, char const*):
✗ Branch 1 not taken.
410987 trampoline.base().attr(ticket.key)(PythonArgument(std::forward<Args>(args))...));
41 }
42
43 /// Calls the Python override `name`, ignoring its result, if the subclass defines one, and
44 /// returns whether it did.
45 template <typename... Args>
46 18 bool CallOverrideIgnoringResult(const nb::detail::trampoline& trampoline, const char* name,
47 Args&&... args) {
48 18 const nb::detail::ticket ticket(trampoline, name, nb::detail::str_hash(name), false);
49
3/4
bool terraingen::python::CallOverrideIgnoringResult<terraingen::python::ByReference<terraingen::Raster<float> > >(nanobind::detail::trampoline const&, char const*, terraingen::python::ByReference<terraingen::Raster<float> >&&):
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
bool terraingen::python::CallOverrideIgnoringResult<terraingen::python::ByReference<terraingen::Raster<float> >, terraingen::LayerContext const&>(nanobind::detail::trampoline const&, char const*, terraingen::python::ByReference<terraingen::Raster<float> >&&, terraingen::LayerContext const&):
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5 times.
18 if (!ticket.key.is_valid()) {
50 13 return false;
51 }
52
3/6
bool terraingen::python::CallOverrideIgnoringResult<terraingen::python::ByReference<terraingen::Raster<float> > >(nanobind::detail::trampoline const&, char const*, terraingen::python::ByReference<terraingen::Raster<float> >&&):
✗ Branch 1 not taken.
✗ Branch 5 not taken.
✗ Branch 8 not taken.
bool terraingen::python::CallOverrideIgnoringResult<terraingen::python::ByReference<terraingen::Raster<float> >, terraingen::LayerContext const&>(nanobind::detail::trampoline const&, char const*, terraingen::python::ByReference<terraingen::Raster<float> >&&, terraingen::LayerContext const&):
✓ Branch 1 taken 5 times.
✓ Branch 7 taken 5 times.
✓ Branch 10 taken 5 times.
10 trampoline.base().attr(ticket.key)(PythonArgument(std::forward<Args>(args))...);
53 5 return true;
54 }
55
56 /// Calls the Python method `name` the subclass must define and converts its result with
57 /// `convert`, while the GIL is held.
58 /// @throws nb::type_error (raised in Python) when the subclass does not define it.
59 template <typename Convert, typename... Args>
60 66177 auto CallRequired(const nb::detail::trampoline& trampoline, const char* name, Convert convert,
61 Args&&... args) {
62 66177 const nb::detail::ticket ticket(trampoline, name, nb::detail::str_hash(name), true);
63 264708 return convert(trampoline.base().attr(ticket.key)(PythonArgument(std::forward<Args>(args))...));
64 }
65
66 /// Whether the Python subclass behind `trampoline` overrides `name`.
67 9 inline bool Overrides(const nb::detail::trampoline& trampoline, const char* name) {
68 9 const nb::detail::ticket ticket(trampoline, name, nb::detail::str_hash(name), false);
69 18 return ticket.key.is_valid();
70 }
71
72 } // namespace terraingen::python
73