Project

General

Profile

Support #20849

safety in art::DataViewImpl::getPointerByLabel(InputTag const& tag)

Added by Pavel Murat 11 months ago. Updated 10 months ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Category:
-
Target version:
-
Start date:
09/15/2018
Due date:
% Done:

100%

Estimated time:
Scope:
Internal
Experiment:
Mu2e
SSI Package:
Duration:

Description

the function below is very handy.

It would be even more handy if, in case the corresponding product is not found,
it returned a nullptr instead of unsuccessfully trying to dereference it.

-many thanks, Pasha

template <typename PROD>
inline PROD const*
art::DataViewImpl::getPointerByLabel(InputTag const& tag) const
{
  Handle<PROD> h;
  getByLabel(tag, h);
  return &(*h);
}

History

#1 Updated by Kyle Knoepfel 11 months ago

  • Description updated (diff)

#2 Updated by Kyle Knoepfel 11 months ago

  • Status changed from New to Feedback

We'd like to discuss with you some possibilities that do not require checking whether a pointer is null. We will contact you to set up a meeting.

#3 Updated by Kyle Knoepfel 11 months ago

  • Occurs In 2.10.04 added
  • Occurs In deleted (2.02.04)

#4 Updated by Kyle Knoepfel 11 months ago

Although getPointerByLabel is part of the public interface of Event (through art::DataViewImpl) we suspect exposing the bare pointer to the object is not the best design. We may decide to deprecate the function at some point, but let's first address the particular issue you brought up.

It sounds to me as if you would like to do something like:

auto p = e.getPointerByLabel<Product>(tag);
if (p == nullptr) {
  return; // or something similar
}

Something equivalent is already provided using the art::Handle system. Consider the following:

Handle<Product> h;
if (!e.getByLabel(tag, h)) {
  return; 
}

// h is guaranteed to be valid if getByLabel returns true.
assert(h.isValid());  

The same number of lines are required as in the bare-pointer case--and no fragile dereferencing of a null pointer is possible. With C++17, the encapsulation is further improved:

// Using C++17
if (Handle<Product> h; !e.getByLabel(tag, h)) {
  return;
}

Please let us know if the art::Handle solutions are still inadequate. We are happy to discuss further.

#5 Updated by Kyle Knoepfel 10 months ago

  • Tracker changed from Bug to Support
  • Occurs In deleted (2.10.04)

Did the art::Handle solution meet the need? If not, please let us know, otherwise we would like to close this issue.

#6 Updated by Kyle Knoepfel 10 months ago

  • Status changed from Feedback to Closed
  • % Done changed from 0 to 100

We are closing this issue due to discussion with Rob.



Also available in: Atom PDF