ATLAS Offline Software
Loading...
Searching...
No Matches
PointerCache.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3 */
4#ifndef EXPRESSIONPARSING_POINTERCACHE_H
5#define EXPRESSIONPARSING_POINTERCACHE_H
6#include <atomic>
7#include <memory>
8
9namespace ExpressionParsing {
11 template <class T, std::memory_order MEMORY_ORDER>
13 {
14 public:
15 PointerCache() = default;
17 if (m_owner) {
18 delete m_ptr.load();
19 }
20 }
21
23 void setIfUnset(const T &obj) {
24 setPointer(&obj, false);
25 }
26
27 void setIfUnset(std::unique_ptr<T> &&ptr) {
28 setPointer(ptr.release(), true);
29 }
30
31 operator bool() const {
32 return m_ptr != nullptr;
33 }
34
36 const T *operator->() const {
37 assert (m_ptr);
38 return m_ptr.load(MEMORY_ORDER);
39 }
40
41 private:
42 void setPointer(const T *ptr, bool owner) {
43 while (m_ptr == nullptr) {
44 const T *expected=nullptr;
45 if (m_ptr.compare_exchange_weak(expected, ptr, MEMORY_ORDER)) break;
46 }
47 if (m_ptr == ptr) {
48 m_owner = owner;
49 }
50 else {
51 if (owner) {
52 delete ptr;
53 }
54 }
55 }
56 std::atomic<const T *> m_ptr;
57 std::atomic<bool> m_owner;
58 };
59}
60#endif
void setIfUnset(const T &obj)
set a pointer without handing over ownership
const T * operator->() const
dereference the pointer
void setIfUnset(std::unique_ptr< T > &&ptr)
set a pointer andhand over ownership
void setPointer(const T *ptr, bool owner)
std::atomic< bool > m_owner
std::atomic< const T * > m_ptr
Namespace holding all the expression evaluation code.