Fawkes API  Fawkes Development Version
autofree.cpp
1 
2 /***************************************************************************
3  * autofree.cpp - Automatic Freeer
4  *
5  * Created: Thu Nov 26 13:17:42 2009
6  * Copyright 2005-2009 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <utils/misc/autofree.h>
25 #include <cstdlib>
26 
27 namespace fawkes {
28 #if 0 /* just to make Emacs auto-indent happy */
29 }
30 #endif
31 
32 /** @class MemAutoFree <utils/misc/autofree.h>
33  * Automatically free memory on destruction.
34  * This class can be used to free memory on destruction of the object.
35  * This is similar to many use cases of std::auto_ptr, with the difference
36  * that it calls free() to release the memory instead of delete, therefore
37  * it is meant to be used with classical memory allocations, e.g. C strings.
38  * In effect the instance of MemAutoFree takes ownership of the passed pointer.
39  * @author Tim Niemueller
40  */
41 
42 /** Constructor.
43  * @param ptr pointer to delete on destruct
44  */
46 {
47  __ptr = ptr;
48 }
49 
50 
51 /** Destructor.
52  * Destroys the memory chunk unless it has been released before.
53  */
55 {
56  if (__ptr) free(__ptr);
57 }
58 
59 
60 /** Release ownership.
61  * The instance no longer owns the pointer and memory will not be deleted
62  * on destruction.
63  */
64 void
66 {
67  __ptr = NULL;
68 }
69 
70 
71 /** Reset pointer to a different one,
72  * This will free the pointer hold up to this call and will replace it with
73  * new_ptr. It is verified that the old and new pointers are different, nothing
74  * will be done if they are the same.
75  * @param new_ptr new pointer to own
76  */
77 void
78 MemAutoFree::reset(void *new_ptr)
79 {
80  if (__ptr != new_ptr) {
81  if (__ptr) free(__ptr);
82  __ptr = new_ptr;
83  }
84 }
85 
86 
87 } // end namespace fawkes
Fawkes library namespace.
void reset(void *new_ptr)
Reset pointer to a different one, This will free the pointer hold up to this call and will replace it...
Definition: autofree.cpp:78
void release()
Release ownership.
Definition: autofree.cpp:65
MemAutoFree(void *ptr)
Constructor.
Definition: autofree.cpp:45
~MemAutoFree()
Destructor.
Definition: autofree.cpp:54