Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bindings/pyroot/cppyy/CPyCppyy/src/CPPMethod.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ int CPyCppyy::CPPMethod::GetPriority()
const std::string& clean_name = TypeManip::clean_type(aname, false);
Cppyy::TCppScope_t scope = Cppyy::GetScope(clean_name);
if (scope)
priority += (int)Cppyy::GetNumBases(scope);
priority += static_cast<int>(Cppyy::GetNumBasesLongestBranch(scope));

if (Cppyy::IsEnum(clean_name))
priority -= 100;
Expand Down
2 changes: 2 additions & 0 deletions bindings/pyroot/cppyy/CPyCppyy/src/Cppyy.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ namespace Cppyy {
CPPYY_IMPORT
TCppIndex_t GetNumBases(TCppType_t type);
CPPYY_IMPORT
TCppIndex_t GetNumBasesLongestBranch(TCppType_t type);
CPPYY_IMPORT
std::string GetBaseName(TCppType_t type, TCppIndex_t ibase);
CPPYY_IMPORT
bool IsSubtype(TCppType_t derived, TCppType_t base);
Expand Down
2 changes: 2 additions & 0 deletions bindings/pyroot/cppyy/cppyy-backend/clingwrapper/src/capi.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ extern "C" {
RPY_EXPORTED
int cppyy_num_bases(cppyy_type_t type);
RPY_EXPORTED
int cppyy_num_bases_longest_branch(cppyy_type_t type);
RPY_EXPORTED
char* cppyy_base_name(cppyy_type_t type, int base_index);
RPY_EXPORTED
int cppyy_is_subtype(cppyy_type_t derived, cppyy_type_t base);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1219,6 +1219,83 @@ Cppyy::TCppIndex_t Cppyy::GetNumBases(TCppType_t klass)
return (TCppIndex_t)0;
}

////////////////////////////////////////////////////////////////////////////////
/// \fn Cppyy::TCppIndex_t GetLongestInheritancePath(TClass *klass)
/// \brief Retrieve number of base classes in the longest branch of the
/// inheritance tree of the input class.
/// \param[in] klass The class to start the retrieval process from.
///
/// This is a helper function for Cppyy::GetNumBasesLongestBranch.
/// Given an inheritance tree, the function assigns weight 1 to each class that
/// has at least one base. Starting from the input class, the function is
/// called recursively on all the bases. For each base the return value is one
/// (the weight of the base itself) plus the maximum value retrieved for their
/// bases in turn. For example, given the following inheritance tree:
///
/// ~~~{.cpp}
/// class A {}; class B: public A {};
/// class X {}; class Y: public X {}; class Z: public Y {};
/// class C: public B, Z {};
/// ~~~
///
/// calling this function on an instance of `C` will return 3, the steps
/// required to go from C to X.
Cppyy::TCppIndex_t GetLongestInheritancePath(TClass *klass)
{

auto directbases = klass->GetListOfBases();
if (!directbases) {
// This is a leaf with no bases
return 0;
}
auto ndirectbases = directbases->GetSize();
if (ndirectbases == 0) {
// This is a leaf with no bases
return 0;
} else {
// If there is at least one direct base
std::vector<Cppyy::TCppIndex_t> nbases_branches;
nbases_branches.reserve(ndirectbases);

// Traverse all direct bases of the current class and call the function
// recursively
for (auto baseclass : TRangeDynCast<TBaseClass>(directbases)) {
if (!baseclass)
continue;
if (auto baseclass_tclass = baseclass->GetClassPointer()) {
nbases_branches.emplace_back(GetLongestInheritancePath(baseclass_tclass));
}
}

// Get longest path among the direct bases of the current class
auto longestbranch = std::max_element(std::begin(nbases_branches), std::end(nbases_branches));

// Add 1 to include the current class in the count
return 1 + *longestbranch;
}
}

////////////////////////////////////////////////////////////////////////////////
/// \fn Cppyy::TCppIndex_t Cppyy::GetNumBasesLongest(TCppType_t klass)
/// \brief Retrieve number of base classes in the longest branch of the
/// inheritance tree.
/// \param[in] klass The class to start the retrieval process from.
///
/// The function converts the input class to a `TClass *` and calls
/// GetLongestInheritancePath.
Cppyy::TCppIndex_t Cppyy::GetNumBasesLongestBranch(TCppType_t klass)
{

const auto &cr = type_from_handle(klass);

if (auto klass_tclass = cr.GetClass()) {
return GetLongestInheritancePath(klass_tclass);
}

// In any other case, return zero
return 0;
}

std::string Cppyy::GetBaseName(TCppType_t klass, TCppIndex_t ibase)
{
TClassRef& cr = type_from_handle(klass);
Expand Down Expand Up @@ -2383,6 +2460,10 @@ int cppyy_num_bases(cppyy_type_t type) {
return (int)Cppyy::GetNumBases(type);
}

int cppyy_num_bases_longest_branch(cppyy_type_t type) {
return (int)Cppyy::GetNumBasesLongestBranch(type);
}

char* cppyy_base_name(cppyy_type_t type, int base_index) {
return cppstring_to_cstring(Cppyy::GetBaseName (type, base_index));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ namespace Cppyy {
RPY_EXPORTED
TCppIndex_t GetNumBases(TCppType_t type);
RPY_EXPORTED
TCppIndex_t GetNumBasesLongestBranch(TCppType_t type);
RPY_EXPORTED
std::string GetBaseName(TCppType_t type, TCppIndex_t ibase);
RPY_EXPORTED
bool IsSubtype(TCppType_t derived, TCppType_t base);
Expand Down