From: Matthias Maier Date: Sun, 29 Oct 2023 22:07:03 +0000 (-0500) Subject: base/memory_consumption.h: add specialization for std::optional X-Git-Tag: relicensing~343^2~1 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=21f8c0d1d68b370f25e6097ae61c0169e13940a7;p=dealii.git base/memory_consumption.h: add specialization for std::optional --- 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)