From 21f8c0d1d68b370f25e6097ae61c0169e13940a7 Mon Sep 17 00:00:00 2001 From: Matthias Maier Date: Sun, 29 Oct 2023 17:07:03 -0500 Subject: [PATCH] base/memory_consumption.h: add specialization for std::optional --- include/deal.II/base/memory_consumption.h | 34 +++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/include/deal.II/base/memory_consumption.h b/include/deal.II/base/memory_consumption.h index e37784d155..0f2f362925 100644 --- a/include/deal.II/base/memory_consumption.h +++ b/include/deal.II/base/memory_consumption.h @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -220,6 +221,14 @@ namespace MemoryConsumption inline std::size_t memory_consumption(const std::pair &p); + /** + * Determine an estimate of the amount of memory in bytes consumed by a + * value wrapped in a std::optional. + */ + template + inline std::size_t + memory_consumption(const std::optional &o); + /** * Calculate the memory consumption of a pointer. * @@ -380,6 +389,31 @@ namespace MemoryConsumption + template + inline std::size_t + memory_consumption(const std::optional &o) + { + if (o.has_value()) + { + // + // If the optional carries a value then we query the contained + // value for memory consumption and estimate the size of the + // control overhead. + // + return memory_consumption(o.value()) + sizeof(o) - sizeof(A); + } + else + { + // + // The optional contains no value, so simply return its plain size + // (consisting of space for the value and control overhead): + // + return sizeof(o); + } + } + + + template inline std::size_t memory_consumption(const T *const) -- 2.39.5