Skip to content

Commit 5b24b1f

Browse files
committed
Add longest inheritance path interface
1 parent 29ff5f0 commit 5b24b1f

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

clingwrapper/src/clingwrapper.cxx

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,57 @@ Cppyy::TCppIndex_t Cppyy::GetNumBases(TCppScope_t klass)
12111211
return Cpp::GetNumBases(klass);
12121212
}
12131213

1214+
////////////////////////////////////////////////////////////////////////////////
1215+
/// \fn Cppyy::TCppIndex_t Cppyy::GetNumBasesLongestBranch(TCppScope_t klass)
1216+
/// \brief Retrieve number of base classes in the longest branch of the
1217+
/// inheritance tree of the input class.
1218+
/// \param[in] klass The class to start the retrieval process from.
1219+
///
1220+
/// This is a helper function for Cppyy::GetNumBasesLongestBranch.
1221+
/// Given an inheritance tree, the function assigns weight 1 to each class that
1222+
/// has at least one base. Starting from the input class, the function is
1223+
/// called recursively on all the bases. For each base the return value is one
1224+
/// (the weight of the base itself) plus the maximum value retrieved for their
1225+
/// bases in turn. For example, given the following inheritance tree:
1226+
///
1227+
/// ~~~{.cpp}
1228+
/// class A {}; class B: public A {};
1229+
/// class X {}; class Y: public X {}; class Z: public Y {};
1230+
/// class C: public B, Z {};
1231+
/// ~~~
1232+
///
1233+
/// calling this function on an instance of `C` will return 3, the steps
1234+
/// required to go from C to X.
1235+
Cppyy::TCppIndex_t Cppyy::GetNumBasesLongestBranch(TCppScope_t klass) {
1236+
std::vector<TCppScope_t> directbases;
1237+
for (TCppIndex_t ibase = 0; ibase < GetNumBases(klass); ++ibase)
1238+
directbases.push_back(GetScope(GetBaseName(klass, ibase)));
1239+
return directbases.size();
1240+
// if (directbases.empty()) {
1241+
// // This is a leaf with no bases
1242+
// return 0;
1243+
// }
1244+
1245+
// else {
1246+
// // If there is at least one direct base
1247+
// std::vector<Cppyy::TCppIndex_t> nbases_branches;
1248+
// nbases_branches.reserve(ndirectbases);
1249+
// // Traverse all direct bases of the current class and call the function
1250+
// // recursively
1251+
// for (auto baseclass : TRangeDynCast<TBaseClass>(directbases)) {
1252+
// if (!baseclass)
1253+
// continue;
1254+
// if (auto baseclass_tclass = baseclass->GetClassPointer()) {
1255+
// nbases_branches.emplace_back(GetLongestInheritancePath(baseclass_tclass));
1256+
// }
1257+
// }
1258+
// // Get longest path among the direct bases of the current class
1259+
// auto longestbranch = std::max_element(std::begin(nbases_branches), std::end(nbases_branches));
1260+
// // Add 1 to include the current class in the count
1261+
// return 1 + *longestbranch;
1262+
// }
1263+
}
1264+
12141265
std::string Cppyy::GetBaseName(TCppType_t klass, TCppIndex_t ibase)
12151266
{
12161267
return Cpp::GetName(Cpp::GetBaseClass(klass, ibase));

clingwrapper/src/cpp_cppyy.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ namespace Cppyy {
232232
RPY_EXPORTED
233233
TCppIndex_t GetNumBases(TCppScope_t klass);
234234
RPY_EXPORTED
235+
TCppIndex_t GetNumBasesLongestBranch(TCppScope_t klass);
236+
RPY_EXPORTED
235237
std::string GetBaseName(TCppScope_t klass, TCppIndex_t ibase);
236238
RPY_EXPORTED
237239
TCppScope_t GetBaseScope(TCppScope_t klass, TCppIndex_t ibase);

0 commit comments

Comments
 (0)