Skip to content

Commit 895a37e

Browse files
committed
Use GetTemplatedMethods and fix logic for templated functions in ProxyWrappers
This deprecates `IsTemplatedConstructor` and performs the underlying purpose of the code block. Handle non-templated constructors and templated methods in the first loop and un-instantiated template methods and templated constructors in the second. This PR also refactors loops in Dispatcher.cxx and ProxyWrapper.cxx to no longer use index based interfaces to retrieve template method info
1 parent e638846 commit 895a37e

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

src/Cppyy.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,14 +270,13 @@ namespace Cppyy {
270270
std::string GetMethodPrototype(TCppMethod_t, bool show_formal_args);
271271
CPPYY_IMPORT
272272
bool IsConstMethod(TCppMethod_t);
273-
273+
CPPYY_IMPORT
274+
void GetTemplatedMethods(TCppScope_t scope, std::vector<TCppMethod_t> &methods);
274275
CPPYY_IMPORT
275276
TCppIndex_t GetNumTemplatedMethods(TCppScope_t scope, bool accept_namespace = false);
276277
CPPYY_IMPORT
277278
std::string GetTemplatedMethodName(TCppScope_t scope, TCppIndex_t imeth);
278279
CPPYY_IMPORT
279-
bool IsTemplatedConstructor(TCppScope_t scope, TCppIndex_t imeth);
280-
CPPYY_IMPORT
281280
bool ExistsMethodTemplate(TCppScope_t scope, const std::string& name);
282281
CPPYY_IMPORT
283282
bool IsTemplatedMethod(TCppMethod_t method);

src/Dispatcher.cxx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,10 @@ bool CPyCppyy::InsertDispatcher(CPPScope* klass, PyObject* bases, PyObject* dct,
337337

338338
// support for templated ctors in single inheritance (TODO: also multi possible?)
339339
if (base_infos.size() == 1) {
340-
const Cppyy::TCppIndex_t nTemplMethods = Cppyy::GetNumTemplatedMethods(binfo.btype);
341-
for (Cppyy::TCppIndex_t imeth = 0; imeth < nTemplMethods; ++imeth) {
342-
if (Cppyy::IsTemplatedConstructor(binfo.btype, imeth)) {
340+
std::vector<Cppyy::TCppMethod_t> templ_methods;
341+
Cppyy::GetTemplatedMethods(binfo.btype, templ_methods);
342+
for (auto &method : templ_methods) {
343+
if (Cppyy::IsConstructor(method)) {
343344
any_ctor_found = true;
344345
has_tmpl_ctors += 1;
345346
break; // one suffices to map as argument packs are used

src/ProxyWrappers.cxx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,9 @@ static int BuildScopeProxyDict(Cppyy::TCppScope_t scope, PyObject* pyclass, cons
189189
// special case trackers
190190
bool setupSetItem = false;
191191
bool isConstructor = Cppyy::IsConstructor(method);
192-
bool isTemplate = isConstructor ? false : Cppyy::IsTemplatedMethod(method);
192+
193+
// Here isTemplate means to ignore templated constructors, that is handled in the next loop.
194+
bool isTemplate = Cppyy::IsTemplatedMethod(method) && !isConstructor;
193195
bool isStubbedOperator = false;
194196

195197
// filter empty names (happens for namespaces, is bug?)
@@ -225,8 +227,7 @@ static int BuildScopeProxyDict(Cppyy::TCppScope_t scope, PyObject* pyclass, cons
225227
}
226228

227229
// template members; handled by adding a dispatcher to the class
228-
bool storeOnTemplate = false; //XXX
229-
//isTemplate ? true : (!isConstructor && Cppyy::ExistsMethodTemplate(scope, mtCppName));
230+
bool storeOnTemplate = isTemplate;
230231
if (storeOnTemplate) {
231232
sync_templates(pyclass, mtCppName, mtName);
232233
// continue processing to actually add the method so that the proxy can find
@@ -293,14 +294,16 @@ static int BuildScopeProxyDict(Cppyy::TCppScope_t scope, PyObject* pyclass, cons
293294
}
294295
}
295296

297+
296298
// add proxies for un-instantiated/non-overloaded templated methods
297-
const Cppyy::TCppIndex_t nTemplMethods = isNamespace ? 0 : Cppyy::GetNumTemplatedMethods(scope);
298-
for (Cppyy::TCppIndex_t imeth = 0; imeth < nTemplMethods; ++imeth) {
299-
const std::string mtCppName = Cppyy::GetTemplatedMethodName(scope, imeth);
299+
std::vector<Cppyy::TCppMethod_t> templ_methods;
300+
Cppyy::GetTemplatedMethods(scope, templ_methods);
301+
for (auto &method : templ_methods) {
302+
const std::string mtCppName = Cppyy::GetMethodName(method);
300303
// the number of arguments isn't known until instantiation and as far as C++ is concerned, all
301304
// same-named operators are simply overloads; so will pre-emptively add both names if with and
302305
// without arguments differ, letting the normal overload mechanism resolve on call
303-
bool isConstructor = Cppyy::IsTemplatedConstructor(scope, imeth);
306+
bool isConstructor = Cppyy::IsConstructor(method);
304307

305308
// first add with no arguments
306309
std::string mtName0 = isConstructor ? "__init__" : Utility::MapOperatorName(mtCppName, false);

0 commit comments

Comments
 (0)