@@ -2062,6 +2062,14 @@ inline function get_type_overload(const void *this_ptr, const detail::type_info
2062
2062
return overload;
2063
2063
}
2064
2064
2065
+ /* * \rst
2066
+ Try to retrieve a python method by the provided name from the instance pointed to by the this_ptr.
2067
+
2068
+ :this_ptr: The pointer to the object the overload should be retrieved for. This should be the first
2069
+ non-trampoline class encountered in the inheritance chain.
2070
+ :name: The name of the overloaded Python method to retrieve.
2071
+ :return: The Python method by this name from the object or an empty function wrapper.
2072
+ \endrst */
2065
2073
template <class T > function get_overload (const T *this_ptr, const char *name) {
2066
2074
auto tinfo = detail::get_type_info (typeid (T));
2067
2075
return tinfo ? get_type_overload (this_ptr, tinfo, name) : function ();
@@ -2080,17 +2088,66 @@ template <class T> function get_overload(const T *this_ptr, const char *name) {
2080
2088
} \
2081
2089
}
2082
2090
2091
+ /* * \rst
2092
+ Macro to populate the virtual method in the trampoline class. This macro tries to look up a method named 'fn'
2093
+ from the Python side, deals with the :ref:`gil` and necessary argument conversions to call this method and return
2094
+ the appropriate type. See :ref:`overriding_virtuals` for more information. This macro should be used when the method
2095
+ name in C is not the same as the method name in Python. For example with `__str__`.
2096
+
2097
+ .. code-block:: cpp
2098
+
2099
+ std::string toString() override {
2100
+ PYBIND11_OVERLOAD_NAME(
2101
+ std::string, // Return type (ret_type)
2102
+ Animal, // Parent class (cname)
2103
+ toString, // Name of function in C++ (name)
2104
+ "__str__", // Name of method in Python (fn)
2105
+ );
2106
+ }
2107
+ \endrst */
2083
2108
#define PYBIND11_OVERLOAD_NAME (ret_type, cname, name, fn, ...) \
2084
2109
PYBIND11_OVERLOAD_INT (PYBIND11_TYPE (ret_type), PYBIND11_TYPE (cname), name, __VA_ARGS__) \
2085
2110
return cname::fn (__VA_ARGS__)
2086
2111
2112
+ /* * \rst
2113
+ Macro for pure virtual functions, this function is identical to :c:macro:`PYBIND11_OVERLOAD_NAME`, except that it
2114
+ throws if no overload can be found.
2115
+ \endrst */
2087
2116
#define PYBIND11_OVERLOAD_PURE_NAME (ret_type, cname, name, fn, ...) \
2088
2117
PYBIND11_OVERLOAD_INT (PYBIND11_TYPE (ret_type), PYBIND11_TYPE (cname), name, __VA_ARGS__) \
2089
2118
pybind11::pybind11_fail (" Tried to call pure virtual function \" " PYBIND11_STRINGIFY (cname) " ::" name " \" " );
2090
2119
2120
+ /* * \rst
2121
+ Macro to populate the virtual method in the trampoline class. This macro tries to look up the method
2122
+ from the Python side, deals with the :ref:`gil` and necessary argument conversions to call this method and return
2123
+ the appropriate type. This macro should be used if the method name in C and in Python are identical.
2124
+ See :ref:`overriding_virtuals` for more information.
2125
+
2126
+ .. code-block:: cpp
2127
+
2128
+ class PyAnimal : public Animal {
2129
+ public:
2130
+ // Inherit the constructors
2131
+ using Animal::Animal;
2132
+
2133
+ // Trampoline (need one for each virtual function)
2134
+ std::string go(int n_times) override {
2135
+ PYBIND11_OVERLOAD_PURE(
2136
+ std::string, // Return type (ret_type)
2137
+ Animal, // Parent class (cname)
2138
+ go, // Name of function in C++ (must match Python name) (fn)
2139
+ n_times // Argument(s) (...)
2140
+ );
2141
+ }
2142
+ };
2143
+ \endrst */
2091
2144
#define PYBIND11_OVERLOAD (ret_type, cname, fn, ...) \
2092
2145
PYBIND11_OVERLOAD_NAME (PYBIND11_TYPE (ret_type), PYBIND11_TYPE (cname), #fn, fn, __VA_ARGS__)
2093
2146
2147
+ /* * \rst
2148
+ Macro for pure virtual functions, this function is identical to :c:macro:`PYBIND11_OVERLOAD`, except that it throws
2149
+ if no overload can be found.
2150
+ \endrst */
2094
2151
#define PYBIND11_OVERLOAD_PURE (ret_type, cname, fn, ...) \
2095
2152
PYBIND11_OVERLOAD_PURE_NAME (PYBIND11_TYPE (ret_type), PYBIND11_TYPE (cname), #fn, fn, __VA_ARGS__)
2096
2153
0 commit comments