]> https://gitweb.dealii.org/ - dealii.git/commitdiff
base/memory_consumption.h: add specialization for std::optional<T>
authorMatthias Maier <tamiko@43-1.org>
Sun, 29 Oct 2023 22:07:03 +0000 (17:07 -0500)
committerMatthias Maier <tamiko@43-1.org>
Sun, 29 Oct 2023 22:07:54 +0000 (17:07 -0500)
include/deal.II/base/memory_consumption.h

index e37784d155bd833bf4f813fcdfaab387982bc894..0f2f362925fa8251c13e128ecef403538e2b5675 100644 (file)
@@ -24,6 +24,7 @@
 #include <cstddef>
 #include <cstring>
 #include <memory>
+#include <optional>
 #include <string>
 #include <type_traits>
 #include <vector>
@@ -220,6 +221,14 @@ namespace MemoryConsumption
   inline std::size_t
   memory_consumption(const std::pair<A, B> &p);
 
+  /**
+   * Determine an estimate of the amount of memory in bytes consumed by a
+   * value wrapped in a std::optional.
+   */
+  template <typename A>
+  inline std::size_t
+  memory_consumption(const std::optional<A> &o);
+
   /**
    * Calculate the memory consumption of a pointer.
    *
@@ -380,6 +389,31 @@ namespace MemoryConsumption
 
 
 
+  template <typename A>
+  inline std::size_t
+  memory_consumption(const std::optional<A> &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 <typename T>
   inline std::size_t
   memory_consumption(const T *const)

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.