1 // This file's extension implies that it's C, but it's really -*- C++ -*-.
3 * Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration.
7 * @file CxxUtils/CachedUniquePtr.icc
8 * @author scott snyder <snyder@bnl.gov>
10 * @brief Cached unique_ptr with atomic update.
18 * @brief Default constructor. Sets the element to null.
22 CachedUniquePtrT<T>::CachedUniquePtrT()
29 * @brief Constructor from an element.
33 CachedUniquePtrT<T>::CachedUniquePtrT (std::unique_ptr<T> elt)
34 : m_ptr (elt.release())
40 * @brief Move constructor.
44 CachedUniquePtrT<T>::CachedUniquePtrT (CachedUniquePtrT&& other) noexcept
45 : m_ptr (other.release().release())
56 CachedUniquePtrT<T>::operator= (CachedUniquePtrT&& other) noexcept
59 store (other.release());
70 CachedUniquePtrT<T>::~CachedUniquePtrT()
77 * @brief Atomically set the element. If already set, then @c elt is discarded.
78 * @param elt The new value for the element.
80 * If the current value of the element is null, then set it to @c elt.
81 * Otherwise, delete @c elt.
82 * This is done atomically.
83 * Returns the final value of the element.
87 T* CachedUniquePtrT<T>::set (std::unique_ptr<T> elt) const
89 // Set the element to ELT if it is currently null.
90 T* ptr = elt.release();
91 T* expected = nullptr;
92 if (!m_ptr.compare_exchange_strong (expected, ptr)) {
93 // Was already set. Delete the new value.
102 * @brief Store a new value to the element.
103 * Not compatible with other concurrent access.
107 void CachedUniquePtrT<T>::store (std::unique_ptr<T> elt) noexcept
109 T* old = m_ptr.exchange (elt.release());
115 * @brief Return the current value of the element.
120 CachedUniquePtrT<T>::get() const
127 * @brief Dereference the element.
132 CachedUniquePtrT<T>::operator*() const
139 * @brief Dereference the element.
144 CachedUniquePtrT<T>::operator->() const
151 * @brief Test if the element is null.
155 CachedUniquePtrT<T>::operator bool() const
157 return get() != nullptr;
162 * @brief Transfer ownership from the element: return the current value as a
163 * unique_ptr, leaving the element null.
168 CachedUniquePtrT<T>::release() noexcept
170 T* old = m_ptr.exchange (nullptr);
171 return std::unique_ptr<T> (old);
175 } // namespace CxxUtils