Skip to content

Commit 5025eda

Browse files
Implement GetGlobalOperator
1 parent 36028bd commit 5025eda

File tree

2 files changed

+63
-32
lines changed

2 files changed

+63
-32
lines changed

clingwrapper/src/clingwrapper.cxx

Lines changed: 61 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,36 +1577,67 @@ Cppyy::TCppMethod_t Cppyy::GetMethodTemplate(
15771577
// }
15781578
// return n1;
15791579
// }
1580-
//
1581-
// Cppyy::TCppIndex_t Cppyy::GetGlobalOperator(
1582-
// TCppType_t scope, const std::string& lc, const std::string& rc, const std::string& opname)
1583-
// {
1584-
// // Find a global operator function with a matching signature; prefer by-ref, but
1585-
// // fall back on by-value if that fails.
1586-
// std::string lcname1 = TClassEdit::CleanType(lc.c_str());
1587-
// const std::string& rcname = rc.empty() ? rc : type_remap(TClassEdit::CleanType(rc.c_str()), lcname1);
1588-
// const std::string& lcname = type_remap(lcname1, rcname);
1589-
//
1590-
// std::string proto = lcname + "&" + (rc.empty() ? rc : (", " + rcname + "&"));
1591-
// if (scope == (cppyy_scope_t)GLOBAL_HANDLE) {
1592-
// TFunction* func = gROOT->GetGlobalFunctionWithPrototype(opname.c_str(), proto.c_str());
1593-
// if (func) retIsDatamemberPresent>GetGlobalFunctionWithPrototype(opname.c_str(), proto.c_str());
1594-
// if (func) return (TCppIndex_t)new_CallWrapper(func);
1595-
// } else {
1596-
// TClassRef& cr = type_from_handle(scope);
1597-
// if (cr.GetClass()) {
1598-
// TFunction* func = cr->GetMethodWithPrototype(opname.c_str(), proto.c_str());
1599-
// if (func) return (TCppIndex_t)cr->GetListOfMethods()->IndexOf(func);
1600-
// proto = lcname + (rc.empty() ? rc : (", " + rcname));
1601-
// func = cr->GetMethodWithPrototype(opname.c_str(), proto.c_str());
1602-
// if (func) return (TCppIndex_t)cr->GetListOfMethods()->IndexOf(func);
1603-
// }
1604-
// }
1605-
//
1606-
// // failure ...
1607-
// return (TCppIndex_t)-1;
1608-
// }
1609-
//
1580+
1581+
Cppyy::TCppMethod_t Cppyy::GetGlobalOperator(
1582+
TCppType_t scope, const std::string& lc, const std::string& rc, const std::string& opname)
1583+
{
1584+
if ((lc.find('<') != std::string::npos) || (rc.find('<') != std::string::npos)) {
1585+
// arguments of templated types
1586+
return nullptr;
1587+
}
1588+
1589+
std::vector<TCppScope_t> overloads;
1590+
if (opname == "+")
1591+
Cpp::GetOperator(scope, Cpp::Operator::OP_Plus, overloads);
1592+
else if (opname == "-")
1593+
Cpp::GetOperator(scope, Cpp::Operator::OP_Minus, overloads);
1594+
else if (opname == "*")
1595+
Cpp::GetOperator(scope, Cpp::Operator::OP_Star, overloads);
1596+
else if (opname == "/")
1597+
Cpp::GetOperator(scope, Cpp::Operator::OP_Slash, overloads);
1598+
else if (opname == "<")
1599+
Cpp::GetOperator(scope, Cpp::Operator::OP_Less, overloads);
1600+
else if (opname == "<=")
1601+
Cpp::GetOperator(scope, Cpp::Operator::OP_LessEqual, overloads);
1602+
else if (opname == ">")
1603+
Cpp::GetOperator(scope, Cpp::Operator::OP_Greater, overloads);
1604+
else if (opname == ">=")
1605+
Cpp::GetOperator(scope, Cpp::Operator::OP_GreaterEqual, overloads);
1606+
else if (opname == "==")
1607+
Cpp::GetOperator(scope, Cpp::Operator::OP_EqualEqual, overloads);
1608+
else if (opname == "!=")
1609+
Cpp::GetOperator(scope, Cpp::Operator::OP_ExclaimEqual, overloads);
1610+
else if (opname == "<<")
1611+
Cpp::GetOperator(scope, Cpp::Operator::OP_LessLess, overloads);
1612+
else if (opname == ">>")
1613+
Cpp::GetOperator(scope, Cpp::Operator::OP_GreaterGreater, overloads);
1614+
else if (opname == "&")
1615+
Cpp::GetOperator(scope, Cpp::Operator::OP_Amp, overloads);
1616+
else if (opname == "|")
1617+
Cpp::GetOperator(scope, Cpp::Operator::OP_Pipe, overloads);
1618+
1619+
for (auto overload: overloads) {
1620+
if (Cpp::IsTemplatedFunction(overload))
1621+
continue;
1622+
1623+
TCppType_t lhs_type = Cpp::GetFunctionArgType(overload, 0);
1624+
if (lc != Cpp::GetTypeAsString(Cpp::GetUnderlyingType(lhs_type)))
1625+
continue;
1626+
1627+
if ((!rc.empty()) && (Cpp::GetFunctionNumArgs(overload) == 2)) {
1628+
TCppType_t rhs_type = Cpp::GetFunctionArgType(overload, 1);
1629+
if (rc != Cpp::GetTypeAsString(Cpp::GetUnderlyingType(rhs_type)))
1630+
continue;
1631+
} else {
1632+
continue;
1633+
}
1634+
1635+
return overload;
1636+
}
1637+
1638+
return nullptr;
1639+
}
1640+
16101641
// // method properties ---------------------------------------------------------
16111642
bool Cppyy::IsDeletedMethod(TCppMethod_t method)
16121643
{

clingwrapper/src/cpp_cppyy.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,8 @@ namespace Cppyy {
314314
TCppScope_t scope, const std::string& name, const std::string& proto);
315315

316316
RPY_EXPORTED
317-
TCppIndex_t GetGlobalOperator(
318-
TCppType_t scope, const std::string& lc, const std::string& rc, const std::string& op) { assert(0 && "GetGlobalOperator");return -1; }
317+
TCppMethod_t GetGlobalOperator(
318+
TCppType_t scope, const std::string& lc, const std::string& rc, const std::string& op);
319319

320320
// method properties ---------------------------------------------------------
321321
RPY_EXPORTED

0 commit comments

Comments
 (0)