]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Simplify the VectorMemory::Pointer class. 4925/head
authorWolfgang Bangerth <bangerth@colostate.edu>
Mon, 17 Jul 2017 23:17:01 +0000 (17:17 -0600)
committerWolfgang Bangerth <bangerth@colostate.edu>
Tue, 22 Aug 2017 00:01:10 +0000 (18:01 -0600)
Specifically, it looks a lot like std::unique_ptr except that it
does something different during destruction. This can easily be
modeled using a custom deleter with std::unique_ptr and avoids
a couple of dozen lines of code.

include/deal.II/algorithms/newton.templates.h
include/deal.II/algorithms/theta_timestepping.templates.h
include/deal.II/lac/vector_memory.h

index 88e00e10b468c1b111921c51a75ce97ae8e51cf2..c3bf3c08d22346cdb5b5c979a9adb6f33f07145f 100644 (file)
@@ -117,12 +117,12 @@ namespace Algorithms
     AnyData src2;
     src1.add<const VectorType *>(&u, "Newton iterate");
     src1.merge(in);
-    src2.add<const VectorType *>(res, "Newton residual");
+    src2.add<const VectorType *>(res.get(), "Newton residual");
     src2.merge(src1);
     AnyData out1;
-    out1.add<VectorType *>(res, "Residual");
+    out1.add<VectorType *>(res.get(), "Residual");
     AnyData out2;
-    out2.add<VectorType *>(Du, "Update");
+    out2.add<VectorType *>(Du.get(), "Update");
 
     unsigned int step = 0;
     // fill res with (f(u), v)
@@ -135,9 +135,9 @@ namespace Algorithms
         AnyData tmp;
         VectorType *p = &u;
         tmp.add<const VectorType *>(p, "solution");
-        p = Du;
+        p = Du.get();
         tmp.add<const VectorType *>(p, "update");
-        p = res;
+        p = res.get();
         tmp.add<const VectorType *>(p, "residual");
         *data_out << step;
         *data_out << tmp;
@@ -166,9 +166,9 @@ namespace Algorithms
             AnyData tmp;
             VectorType *p = &u;
             tmp.add<const VectorType *>(p, "solution");
-            p = Du;
+            p = Du.get();
             tmp.add<const VectorType *>(p, "update");
-            p = res;
+            p = res.get();
             tmp.add<const VectorType *>(p, "residual");
             *data_out << step;
             *data_out << tmp;
index 12b41ea89e4d359354eb6bed0c2fe7e8a6366d94..d332393b85940b7ffb75c57c56003be442c1d6bc 100644 (file)
@@ -105,9 +105,9 @@ namespace Algorithms
     AnyData src2;
 
     AnyData out1;
-    out1.add<VectorType *>(aux, "Solution");
+    out1.add<VectorType *>(aux.get(), "Solution");
     // The data provided to the inner solver
-    src2.add<const VectorType *>(aux, "Previous time");
+    src2.add<const VectorType *>(aux.get(), "Previous time");
     src2.add<const VectorType *>(&solution, "Previous iterate");
     src2.add<const double *>(&d_implicit.time, "Time");
     src2.add<const double *>(&d_implicit.step, "Timestep");
index a382cf607b378799d62ce3eb50775c4f60a0840f..405aeb686ef3bdf5d8b222294bfd964bdf61ccdb 100644 (file)
@@ -25,6 +25,7 @@
 
 #include <vector>
 #include <iostream>
+#include <memory>
 
 DEAL_II_NAMESPACE_OPEN
 
@@ -105,12 +106,13 @@ public:
                    "vector has not actually been allocated by the same pool before.");
 
   //@}
+
   /**
    * A class that looks like a pointer for all practical purposes and that
    * upon construction time allocates a vector from a VectorMemory object
-   * (or an object of a derived class) that is passed to the constructor.
-   * The destructor then automatically returns the vector's ownership to
-   * the same VectorMemory object.
+   * (or an object of a class derived from VectorMemory) that is passed
+   * to the constructor of this class. The destructor then automatically
+   * returns the vector's ownership to the same VectorMemory object.
    *
    * Pointers of this type are therefore safe in the sense that they automatically
    * call VectorMemory::free() when they are destroyed, whether that happens
@@ -118,45 +120,27 @@ public:
    * exception unwinding. These kinds of object thus relieve the user from
    * using vector management functions explicitly.
    *
+   * In many senses, this class acts like <code>std::unique_ptr</code> in that
+   * it is the unique owner of a chunk of memory that it frees upon destruction.
+   * The main differences to <code>std::unique_ptr</code> are (i) that it
+   * allocates memory from a memory pool upon construction, and (ii) that the
+   * memory is not destroyed using `operator delete` but returned to the
+   * VectorMemory pool.
+   *
    * @author Guido Kanschat, 2009
    */
-  class Pointer
+  class Pointer : public std::unique_ptr<VectorType, std::function<void (VectorType *)> >
   {
   public:
     /**
      * Constructor, automatically allocating a vector from @p mem.
      */
     Pointer(VectorMemory<VectorType> &mem);
-    /**
-     * Destructor, automatically releasing the vector from the memory #pool.
-     */
-    ~Pointer();
-
-    /**
-     * Conversion to regular pointer.
-     */
-    operator VectorType *() const;
-
-    /**
-     * Dereferencing operator.
-     */
-    VectorType &operator * () const;
 
     /**
-     * Dereferencing operator.
-     */
-    VectorType *operator -> () const;
-
-  private:
-    /**
-     * The memory pool used.
-     */
-    SmartPointer<VectorMemory<VectorType>,Pointer> pool;
-
-    /**
-     * The pointer to the vector.
+     * Destructor, automatically releasing the vector from the memory #pool.
      */
-    VectorType *v;
+    ~Pointer() = default;
   };
 };
 
@@ -368,47 +352,12 @@ template <typename VectorType>
 inline
 VectorMemory<VectorType>::Pointer::Pointer(VectorMemory<VectorType> &mem)
   :
-  pool(&mem, typeid(*this).name()), v(nullptr)
+  std::unique_ptr<VectorType, std::function<void (VectorType *)> >
+  (mem.alloc(), [&mem](VectorType *v)
 {
-  v = pool->alloc();
-}
-
-
-template <typename VectorType>
-inline
-VectorMemory<VectorType>::Pointer::~Pointer()
-{
-  try
-    {
-      pool->free(v);
-    }
-  catch (...)
-    {}
-}
-
-
-template <typename VectorType>
-inline
-VectorMemory<VectorType>::Pointer::operator VectorType *() const
-{
-  return v;
-}
-
-
-template <typename VectorType>
-inline
-VectorType &VectorMemory<VectorType>::Pointer::operator * () const
-{
-  return *v;
-}
-
-
-template <typename VectorType>
-inline
-VectorType *VectorMemory<VectorType>::Pointer::operator -> () const
-{
-  return v;
-}
+  mem.free(v);
+})
+{}
 
 
 #endif // DOXYGEN

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.