From 787f7e6ba60dc76cb0c2121878573e421da8ef27 Mon Sep 17 00:00:00 2001 From: Guido Kanschat Date: Thu, 12 Mar 2009 20:23:07 +0000 Subject: [PATCH] check in untested automatic vector memory pointer git-svn-id: https://svn.dealii.org/trunk@18482 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/lac/include/lac/vector_memory.h | 94 ++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 2 deletions(-) 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() -- 2.39.5