9#include <deal.II/base/config.h>
11#include <deal.II/base/exceptions.h>
12#include <deal.II/base/memory_consumption.h>
14#if DEAL_II_VERSION_GTE(9, 5, 0)
15#include <deal.II/base/mutex.h>
17#include <deal.II/base/thread_management.h>
44 template <typename Callable>
53 mutable std::optional<T>
object;
54 mutable std::atomic<
bool> object_is_initialized;
55 mutable dealii::Threads::Mutex initialization_mutex;
64 : object_is_initialized(false)
71 : object(other.object)
73 object_is_initialized.store(other.object_is_initialized.load());
79 : object(std::move(other.object))
81 object_is_initialized.store(other.object_is_initialized.load());
83 other.object_is_initialized.store(
false);
91 object = other.object;
92 object_is_initialized.store(other.object_is_initialized.load());
101 object = std::move(other.object);
102 object_is_initialized.store(other.object_is_initialized.load());
104 other.object_is_initialized.store(
false);
105 other.object.reset();
111 template <
typename T>
114 object_is_initialized.store(
false);
119 template <
typename T>
120 template <
typename Callable>
121 inline DEAL_II_ALWAYS_INLINE
void
125 if (!object_is_initialized.load(std::memory_order_acquire))
126#ifdef DEAL_II_HAVE_CXX20
130 std::lock_guard<std::mutex> lock(initialization_mutex);
140 if (!object_is_initialized.load(std::memory_order_relaxed)) {
141 Assert(
object.has_value() ==
false, dealii::ExcInternalError());
142 object.emplace(std::move(creator()));
145 object_is_initialized.store(
true, std::memory_order_release);
151 template <
typename T>
154 return (object_is_initialized &&
object.has_value());
158 template <
typename T>
161 Assert(object_is_initialized &&
object.has_value(),
163 "value() has been called but the contained object has not been "
164 "initialized. Did you forget to call 'ensure_initialized()' "
167 return object.value();
171 template <
typename T>
174 Assert(object_is_initialized &&
object.has_value(),
176 "value() has been called but the contained object has not been "
177 "initialized. Did you forget to call 'ensure_initialized()' "
180 return object.value();
Lazy & operator=(const Lazy &other)
Lazy & operator=(Lazy &&other) noexcept
Lazy(Lazy &&other) noexcept
void ensure_initialized(const Callable &creator) const