ATLAS Offline Software
Loading...
Searching...
No Matches
CachedPointer.h
Go to the documentation of this file.
1// This file's extension implies that it's C, but it's really -*- C++ -*-.
2/*
3 * Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration.
4 */
11
12
13#ifndef CXXUTILS_CACHEDPOINTER_H
14#define CXXUTILS_CACHEDPOINTER_H
15
16
17#include <atomic>
18#include <cassert>
19
20
21#if ATOMIC_POINTER_LOCK_FREE != 2
22# error Code assumes lock-free atomic pointers; see comments below.
23#endif
24
25
26namespace CxxUtils {
27
55template <class T>
57{
58public:
60 typedef const T* pointer_t;
61
62
65
66
69
70
72 CachedPointer (const CachedPointer& other) noexcept;
73
74
76 CachedPointer& operator= (const CachedPointer& other) noexcept;
77
78
80 void set (pointer_t elt) const;
81
82
84 void store (pointer_t elt);
85
86
88 pointer_t get() const;
89
90
92 const pointer_t* ptr() const;
93
94
95private:
98 mutable std::atomic<pointer_t> m_a;
100};
101
102} // namespace CxxUtils
103
104
106
107
108#endif // not CXXUTILS_CACHEDPOINTER_H
CachedPointer & operator=(const CachedPointer &other) noexcept
Assignment.
void set(pointer_t elt) const
Set the element, assuming it is currently null.
pointer_t get() const
Return the current value of the element.
pointer_t m_e
Transient.
CachedPointer()
Default constructor. Sets the element to null.
const T * pointer_t
The stored pointer type.
CachedPointer(pointer_t elt)
Constructor from an element.
std::atomic< pointer_t > m_a
The cached element, both directly and as an atomic (recall that this is a union).
const pointer_t * ptr() const
Return a pointer to the cached element.
CachedPointer(const CachedPointer &other) noexcept
Copy constructor.
void store(pointer_t elt)
Store a new value to the element.