Fawkes API Fawkes Development Version

autofree.cpp

00001 
00002 /***************************************************************************
00003  *  autofree.cpp - Automatic Freeer
00004  *
00005  *  Created: Thu Nov 26 13:17:42 2009
00006  *  Copyright  2005-2009  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #include <utils/misc/autofree.h>
00025 #include <cstdlib>
00026 
00027 namespace fawkes {
00028 #if 0 /* just to make Emacs auto-indent happy */
00029 }
00030 #endif
00031 
00032 /** @class MemAutoFree <utils/misc/autofree.h>
00033  * Automatically free memory on destruction.
00034  * This class can be used to free memory on destruction of the object.
00035  * This is similar to many use cases of std::auto_ptr, with the difference
00036  * that it calls free() to release the memory instead of delete, therefore
00037  * it is meant to be used with classical memory allocations, e.g. C strings.
00038  * In effect the instance of MemAutoFree takes ownership of the passed pointer.
00039  * @author Tim Niemueller
00040  */
00041 
00042 /** Constructor.
00043  * @param ptr pointer to delete on destruct
00044  */
00045 MemAutoFree::MemAutoFree(void *ptr)
00046 {
00047   __ptr = ptr;
00048 }
00049 
00050 
00051 /** Destructor.
00052  * Destroys the memory chunk unless it has been released before.
00053  */
00054 MemAutoFree::~MemAutoFree()
00055 {
00056   if (__ptr)  free(__ptr);
00057 }
00058 
00059 
00060 /** Release ownership.
00061  * The instance no longer owns the pointer and memory will not be deleted
00062  * on destruction.
00063  */
00064 void
00065 MemAutoFree::release()
00066 {
00067   __ptr = NULL;
00068 }
00069 
00070 
00071 /** Reset pointer to a different one,
00072  * This will free the pointer hold up to this call and will replace it with
00073  * new_ptr. It is verified that the old and new pointers are different, nothing
00074  * will be done if they are the same.
00075  * @param new_ptr new pointer to own
00076  */
00077 void
00078 MemAutoFree::reset(void *new_ptr)
00079 {
00080   if (__ptr != new_ptr) {
00081     if (__ptr)  free(__ptr);
00082     __ptr = new_ptr;
00083   }
00084 }
00085 
00086 
00087 } // end namespace fawkes
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends