Skip to content

Commit f5c0838

Browse files
committed
Deprecate PYBIND11_OVERLOAD_* and get_overload in favor of PYBIND11_OVERRIDE_* and get_override
1 parent 9230ac4 commit f5c0838

File tree

1 file changed

+72
-25
lines changed

1 file changed

+72
-25
lines changed

include/pybind11/pybind11.h

Lines changed: 72 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2071,16 +2071,17 @@ error_already_set::~error_already_set() {
20712071
}
20722072
}
20732073

2074-
inline function get_type_overload(const void *this_ptr, const detail::type_info *this_type, const char *name) {
2075-
handle self = detail::get_object_handle(this_ptr, this_type);
2074+
PYBIND11_NAMESPACE_BEGIN(detail)
2075+
inline function get_type_override(const void *this_ptr, const type_info *this_type, const char *name) {
2076+
handle self = get_object_handle(this_ptr, this_type);
20762077
if (!self)
20772078
return function();
20782079
handle type = self.get_type();
20792080
auto key = std::make_pair(type.ptr(), name);
20802081

20812082
/* Cache functions that aren't overloaded in Python to avoid
20822083
many costly Python dictionary lookups below */
2083-
auto &cache = detail::get_internals().inactive_overload_cache;
2084+
auto &cache = get_internals().inactive_overload_cache;
20842085
if (cache.find(key) != cache.end())
20852086
return function();
20862087

@@ -2127,26 +2128,27 @@ inline function get_type_overload(const void *this_ptr, const detail::type_info
21272128

21282129
return overload;
21292130
}
2131+
PYBIND11_NAMESPACE_END(detail)
21302132

21312133
/** \rst
21322134
Try to retrieve a python method by the provided name from the instance pointed to by the this_ptr.
21332135
2134-
:this_ptr: The pointer to the object the overload should be retrieved for. This should be the first
2135-
non-trampoline class encountered in the inheritance chain.
2136-
:name: The name of the overloaded Python method to retrieve.
2136+
:this_ptr: The pointer to the object the overriden method should be retrieved for. This should be
2137+
the first non-trampoline class encountered in the inheritance chain.
2138+
:name: The name of the overridden Python method to retrieve.
21372139
:return: The Python method by this name from the object or an empty function wrapper.
21382140
\endrst */
2139-
template <class T> function get_overload(const T *this_ptr, const char *name) {
2141+
template <class T> function get_override(const T *this_ptr, const char *name) {
21402142
auto tinfo = detail::get_type_info(typeid(T));
2141-
return tinfo ? get_type_overload(this_ptr, tinfo, name) : function();
2143+
return tinfo ? detail::get_type_override(this_ptr, tinfo, name) : function();
21422144
}
21432145

2144-
#define PYBIND11_OVERLOAD_INT(ret_type, cname, name, ...) \
2146+
#define PYBIND11_OVERRIDE_IMPL(ret_type, cname, name, ...) \
21452147
do { \
21462148
pybind11::gil_scoped_acquire gil; \
2147-
pybind11::function overload = pybind11::get_overload(static_cast<const cname *>(this), name); \
2148-
if (overload) { \
2149-
auto o = overload(__VA_ARGS__); \
2149+
pybind11::function override = pybind11::get_override(static_cast<const cname *>(this), name); \
2150+
if (override) { \
2151+
auto o = override(__VA_ARGS__); \
21502152
if (pybind11::detail::cast_is_temporary_value_reference<ret_type>::value) { \
21512153
static pybind11::detail::overload_caster_t<ret_type> caster; \
21522154
return pybind11::detail::cast_ref<ret_type>(std::move(o), caster); \
@@ -2164,27 +2166,27 @@ template <class T> function get_overload(const T *this_ptr, const char *name) {
21642166
.. code-block:: cpp
21652167
21662168
std::string toString() override {
2167-
PYBIND11_OVERLOAD_NAME(
2169+
PYBIND11_OVERRIDE_NAME(
21682170
std::string, // Return type (ret_type)
21692171
Animal, // Parent class (cname)
21702172
"__str__", // Name of method in Python (name)
21712173
toString, // Name of function in C++ (fn)
21722174
);
21732175
}
21742176
\endrst */
2175-
#define PYBIND11_OVERLOAD_NAME(ret_type, cname, name, fn, ...) \
2177+
#define PYBIND11_OVERRIDE_NAME(ret_type, cname, name, fn, ...) \
21762178
do { \
2177-
PYBIND11_OVERLOAD_INT(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__); \
2179+
PYBIND11_OVERRIDE_IMPL(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__); \
21782180
return cname::fn(__VA_ARGS__); \
21792181
} while (false)
21802182

21812183
/** \rst
2182-
Macro for pure virtual functions, this function is identical to :c:macro:`PYBIND11_OVERLOAD_NAME`, except that it
2183-
throws if no overload can be found.
2184+
Macro for pure virtual functions, this function is identical to :c:macro:`PYBIND11_OVERRIDE_NAME`, except that it
2185+
throws if no override can be found.
21842186
\endrst */
2185-
#define PYBIND11_OVERLOAD_PURE_NAME(ret_type, cname, name, fn, ...) \
2187+
#define PYBIND11_OVERRIDE_PURE_NAME(ret_type, cname, name, fn, ...) \
21862188
do { \
2187-
PYBIND11_OVERLOAD_INT(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__); \
2189+
PYBIND11_OVERRIDE_IMPL(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__); \
21882190
pybind11::pybind11_fail("Tried to call pure virtual function \"" PYBIND11_STRINGIFY(cname) "::" name "\""); \
21892191
} while (false)
21902192

@@ -2203,7 +2205,7 @@ template <class T> function get_overload(const T *this_ptr, const char *name) {
22032205
22042206
// Trampoline (need one for each virtual function)
22052207
std::string go(int n_times) override {
2206-
PYBIND11_OVERLOAD_PURE(
2208+
PYBIND11_OVERRIDE_PURE(
22072209
std::string, // Return type (ret_type)
22082210
Animal, // Parent class (cname)
22092211
go, // Name of function in C++ (must match Python name) (fn)
@@ -2212,15 +2214,60 @@ template <class T> function get_overload(const T *this_ptr, const char *name) {
22122214
}
22132215
};
22142216
\endrst */
2215-
#define PYBIND11_OVERLOAD(ret_type, cname, fn, ...) \
2216-
PYBIND11_OVERLOAD_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), #fn, fn, __VA_ARGS__)
2217+
#define PYBIND11_OVERRIDE(ret_type, cname, fn, ...) \
2218+
PYBIND11_OVERRIDE_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), #fn, fn, __VA_ARGS__)
22172219

22182220
/** \rst
2219-
Macro for pure virtual functions, this function is identical to :c:macro:`PYBIND11_OVERLOAD`, except that it throws
2220-
if no overload can be found.
2221+
Macro for pure virtual functions, this function is identical to :c:macro:`PYBIND11_OVERRIDE`, except that it throws
2222+
if no override can be found.
22212223
\endrst */
2224+
#define PYBIND11_OVERRIDE_PURE(ret_type, cname, fn, ...) \
2225+
PYBIND11_OVERRIDE_PURE_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), #fn, fn, __VA_ARGS__)
2226+
2227+
2228+
// Deprecated versions
2229+
2230+
PYBIND11_DEPRECATED("get_type_overload has been deprecated")
2231+
inline function get_type_overload(const void *this_ptr, const detail::type_info *this_type, const char *name) {
2232+
return detail::get_type_override(this_ptr, this_type, name);
2233+
}
2234+
2235+
template <class T>
2236+
PYBIND11_DEPRECATED("get_overload has been deprecated in favor of get_override")
2237+
inline function get_overload(const T *this_ptr, const char *name) {
2238+
return get_override(this_ptr, name);
2239+
}
2240+
2241+
PYBIND11_NAMESPACE_BEGIN(detail)
2242+
PYBIND11_DEPRECATED("PYBIND11_OVERLOAD_* macros have been deprecated in favor of PYBIND11_OVERRIDE_*")
2243+
static inline void PYBIND11_OVERLOAD_deprecated() {}
2244+
PYBIND11_NAMESPACE_END(detail)
2245+
2246+
#define PYBIND11_OVERLOAD_INT(ret_type, cname, name, ...) \
2247+
do { \
2248+
pybind11::detail::PYBIND11_OVERLOAD_deprecated(); \
2249+
PYBIND11_OVERRIDE_IMPL(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__); \
2250+
} while (false)
2251+
#define PYBIND11_OVERLOAD_NAME(ret_type, cname, name, fn, ...) \
2252+
do { \
2253+
pybind11::detail::PYBIND11_OVERLOAD_deprecated(); \
2254+
PYBIND11_OVERRIDE_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, fn, __VA_ARGS__); \
2255+
} while (false)
2256+
#define PYBIND11_OVERLOAD_PURE_NAME(ret_type, cname, name, fn, ...) \
2257+
do { \
2258+
pybind11::detail::PYBIND11_OVERLOAD_deprecated(); \
2259+
PYBIND11_OVERRIDE_PURE_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, fn, __VA_ARGS__); \
2260+
} while (false)
2261+
#define PYBIND11_OVERLOAD(ret_type, cname, fn, ...) \
2262+
do { \
2263+
pybind11::detail::PYBIND11_OVERLOAD_deprecated(); \
2264+
PYBIND11_OVERRIDE(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), fn, __VA_ARGS__); \
2265+
} while (false)
22222266
#define PYBIND11_OVERLOAD_PURE(ret_type, cname, fn, ...) \
2223-
PYBIND11_OVERLOAD_PURE_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), #fn, fn, __VA_ARGS__)
2267+
do { \
2268+
pybind11::detail::PYBIND11_OVERLOAD_deprecated(); \
2269+
PYBIND11_OVERRIDE_PURE(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), fn, __VA_ARGS__); \
2270+
} while (false)
22242271

22252272
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
22262273

0 commit comments

Comments
 (0)