Skip to content

Commit a17cd85

Browse files
committed
Return a typed pointer if possible
1 parent 1dc42a9 commit a17cd85

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

include/podio/Frame.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ class Frame {
220220
/// @returns A const pointer to a collection if it is available or a nullptr
221221
/// if it is not
222222
template <ObjectType O>
223-
inline const podio::CollectionBase* get(const O& object) const;
223+
inline const typename O::collection_type* get(const O& object) const;
224224

225225
/// (Destructively) move a collection into the Frame and get a reference to
226226
/// the inserted collection back for further use.
@@ -416,9 +416,9 @@ inline const podio::CollectionBase* Frame::get(const std::string& name) const {
416416
}
417417

418418
template <ObjectType O>
419-
inline const podio::CollectionBase* Frame::get(const O& object) const {
419+
inline const typename O::collection_type* Frame::get(const O& object) const {
420420
const auto name = m_self->getIDTable().name(object.id().collectionID);
421-
return get(name.value_or(""));
421+
return dynamic_cast<const typename O::collection_type*>(get(name.value_or("")));
422422
}
423423

424424
inline void Frame::put(std::unique_ptr<podio::CollectionBase> coll, const std::string& name) {

tests/unittests/frame.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -444,10 +444,9 @@ TEST_CASE("Frame get from object", "[frame][basics]") {
444444
REQUIRE(frameClusters[0] == objectClusters[0]);
445445

446446
const auto* collPtr = event.get(cluster);
447+
STATIC_REQUIRE(std::is_same_v<decltype(collPtr), const ExampleClusterCollection*>);
447448
REQUIRE(collPtr != nullptr);
448-
const auto* clusterCollPtr = dynamic_cast<const ExampleClusterCollection*>(collPtr);
449-
REQUIRE(clusterCollPtr != nullptr);
450-
REQUIRE((*clusterCollPtr)[0] == cluster);
449+
REQUIRE((*collPtr)[0] == cluster);
451450
}
452451

453452
TEST_CASE("EIC-Jana2 cleanup use case", "[memory-management][492][174]") {

0 commit comments

Comments
 (0)