ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
PhysicsAnalysis
CommonTools
ExpressionEvaluation
ExpressionEvaluation
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
9
namespace
ExpressionParsing
{
11
template
<
class
T, std::memory_order MEMORY_ORDER>
12
class
PointerCache
13
{
14
public
:
15
PointerCache
() =
default
;
16
~PointerCache
() {
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
ExpressionParsing::PointerCache::setIfUnset
void setIfUnset(const T &obj)
set a pointer without handing over ownership
Definition
PointerCache.h:23
ExpressionParsing::PointerCache::operator->
const T * operator->() const
dereference the pointer
Definition
PointerCache.h:36
ExpressionParsing::PointerCache::PointerCache
PointerCache()=default
ExpressionParsing::PointerCache::~PointerCache
~PointerCache()
Definition
PointerCache.h:16
ExpressionParsing::PointerCache::setIfUnset
void setIfUnset(std::unique_ptr< T > &&ptr)
set a pointer andhand over ownership
Definition
PointerCache.h:27
ExpressionParsing::PointerCache::setPointer
void setPointer(const T *ptr, bool owner)
Definition
PointerCache.h:42
ExpressionParsing::PointerCache::m_owner
std::atomic< bool > m_owner
Definition
PointerCache.h:57
ExpressionParsing::PointerCache::m_ptr
std::atomic< const T * > m_ptr
Definition
PointerCache.h:56
ExpressionParsing
Namespace holding all the expression evaluation code.
Definition
ExpressionParser.h:26
Generated on
for ATLAS Offline Software by
1.17.0