Skip to content
Merged
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
Simplify implementation of TClass::FindClassOrBaseMethodWithId()
  • Loading branch information
amadio committed Aug 30, 2017
commit b95526d44345cfcb80461a284c0b7ffe27c3ef1b
20 changes: 8 additions & 12 deletions core/meta/src/TClass.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -4199,19 +4199,15 @@ TMethod *TClass::GetMethod(const char *method, const char *params,
/// Find a method with decl id in this class or its bases.

TMethod* TClass::FindClassOrBaseMethodWithId(DeclId_t declId) {
TFunction *f = GetMethodList()->Get(declId);
if (f) return (TMethod*)f;
if (TFunction* method = GetMethodList()->Get(declId))
return reinterpret_cast<TMethod *>(method);

TBaseClass *base;
TIter next(GetListOfBases());
while ((base = (TBaseClass *) next())) {
TClass *clBase = base->GetClassPointer();
if (clBase) {
f = clBase->FindClassOrBaseMethodWithId(declId);
if (f) return (TMethod*)f;
}
}
return 0;
for (auto item : *GetListOfBases())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One hesitation I have with this pattern is that the iterator cast is moved one level down. i.e.

while ((item = reinterpret_cast<TBaseClass *>(next()))) {
   if (auto base = item->GetClassPointer())

vs

for (auto item : *GetListOfBases())
   if (auto base = reinterpret_cast<TBaseClass *>(item)->GetClassPointer())

or more often.

for (auto item : *GetListOfBases()) {
   auto base = reinterpret_cast<TBaseClass *>(item);
   if (base->GetClassPointer())

I wonder if we can have something like

for (auto item : ContaineeIs<TBaseClass*>(GetListOfBases()))
   if (auto base = item->GetClassPointer())

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not sidestep a 5 line commit into a lenghty discussion on code patterns. If you disagree that the code is more readable after the changes, just let me know and I'll close the PR.

if (auto base = reinterpret_cast<TBaseClass *>(item)->GetClassPointer())
if (TFunction* method = base->FindClassOrBaseMethodWithId(declId))
return reinterpret_cast<TMethod *>(method);

return nullptr;
}

////////////////////////////////////////////////////////////////////////////////
Expand Down