From: kanschat Date: Thu, 12 Mar 2009 20:23:07 +0000 (+0000) Subject: check in untested automatic vector memory pointer X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6d402f11b99ed5fc093e3b37bf35cdec0976d125;p=dealii-svn.git check in untested automatic vector memory pointer git-svn-id: https://svn.dealii.org/trunk@18482 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/lac/include/lac/vector_memory.h b/deal.II/lac/include/lac/vector_memory.h index 5f44ee8e23..85cc842c3f 100644 --- a/deal.II/lac/include/lac/vector_memory.h +++ b/deal.II/lac/include/lac/vector_memory.h @@ -2,7 +2,7 @@ // $Id$ // Version: $Name$ // -// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by the deal.II authors +// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 by the deal.II authors // // This file is subject to QPL and may not be distributed // without copyright and license information. Please refer @@ -15,7 +15,7 @@ #include -#include +#include #include #include #include @@ -114,6 +114,54 @@ class VectorMemory : public Subscriptor DeclException0(ExcNotAllocatedHere); //@} +/** + * Pointer to vectors allocated from VectorMemory objects. This + * pointer is safe in the sense that it automatically calls free() + * when it is destroyed, thus relieving the user from using vector + * management functions at all. + * + * @author Guido Kanschat, 2009 + */ + class Pointer + { + public: + /** + * Constructor, automatically + * allocating a vector from + * @p mem. + */ + Pointer(VectorMemory& mem); + /** + * Destructor, automatically + * releasing the vector from + * the memory #pool. + */ + ~Pointer(); + + /** + * Conversion to regular pointer. + */ + operator VECTOR* () const; + + /** + * Dereferencing operator. + */ + VECTOR& operator * () const; + + /** + * Dereferencing operator. + */ + VECTOR * operator -> () const; + private: + /** + * The memory pool used. + */ + SmartPointer > pool; + /** + * The pointer to the vector. + */ + VECTOR* v; + }; }; @@ -351,6 +399,48 @@ class GrowingVectorMemory : public VectorMemory /* --------------------- inline functions ---------------------- */ +template +inline +VectorMemory::Pointer::Pointer(VectorMemory& mem) + : + pool(&mem), v(0) +{ + v = pool->alloc(); +} + + +template +inline +VectorMemory::Pointer::~Pointer() +{ + pool->free(v); +} + + +template +inline +VectorMemory::Pointer::operator VECTOR* () const +{ + return v; +} + + +template +inline +VECTOR & VectorMemory::Pointer::operator * () const +{ + return *v; +} + + +template +inline +VECTOR * VectorMemory::Pointer::operator -> () const +{ + return v; +} + + template inline GrowingVectorMemory::Pool::Pool()