ATLAS Offline Software
Loading...
Searching...
No Matches
ExtrUniquePtrHolder.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4
5#ifndef TRKEXTOOLS_EXTRUNIQUEPTRHOLDER_H
6#define TRKEXTOOLS_EXTRUNIQUEPTRHOLDER_H
7
20
21#include <algorithm>
22#include <memory>
23#include <vector>
24
25namespace Trk{
26
27template <typename T>
28using CacheOwnedPtr = T*;
29
30template <typename T>
32
34 // ctor with capacity
35 ExtrUniquePtrHolder(size_t capacity) { m_elements.reserve(capacity); }
37 // delete all the rest
42
44 CacheOwnedPtr<T> push(std::unique_ptr<T> input) {
45 if (input == nullptr) {
46 return nullptr;
47 }
48 m_elements.push_back(std::move(input));
49 return m_elements.back().get();
50 }
51
56 std::unique_ptr<T> move(CacheOwnedPtr<T> input) {
57 if (input == nullptr) {
58 return nullptr;
59 }
60 // start from the last as this is more likely
61 auto itr = std::find_if(
62 m_elements.rbegin(), m_elements.rend(),
63 [&input](const std::unique_ptr<T>& x) { return x.get() == input; });
64 if (itr == m_elements.crend()) {
65 return nullptr;
66 }
67 return std::unique_ptr<T>(std::move(*itr));
68 }
69 // The vector of unique_ptr
70 std::vector<std::unique_ptr<T>> m_elements;
71};
72
73} // namespace Trk
74
75#endif
Ensure that the ATLAS eigen extensions are properly loaded.
@ x
Definition ParamDefs.h:55
T * CacheOwnedPtr
std::vector< std::unique_ptr< T > > m_elements
ExtrUniquePtrHolder & operator=(const ExtrUniquePtrHolder &)=delete
std::unique_ptr< T > move(CacheOwnedPtr< T > input)
Release a cached ptr.
CacheOwnedPtr< T > push(std::unique_ptr< T > input)
push a new element to the vector and return a ptr to it
ExtrUniquePtrHolder(const ExtrUniquePtrHolder &)=delete
ExtrUniquePtrHolder(ExtrUniquePtrHolder &&)=delete
ExtrUniquePtrHolder(size_t capacity)
ExtrUniquePtrHolder & operator=(ExtrUniquePtrHolder &&)=delete