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
26
#include <cstdlib>
27
28
namespace
fawkes
{
29
30
/** @class MemAutoFree <utils/misc/autofree.h>
31
* Automatically free memory on destruction.
32
* This class can be used to free memory on destruction of the object.
33
* This is similar to many use cases of std::auto_ptr, with the difference
34
* that it calls free() to release the memory instead of delete, therefore
35
* it is meant to be used with classical memory allocations, e.g. C strings.
36
* In effect the instance of MemAutoFree takes ownership of the passed pointer.
37
* @author Tim Niemueller
38
*/
39
40
/** Constructor.
41
* @param ptr pointer to delete on destruct
42
*/
43
MemAutoFree::MemAutoFree
(
void
*ptr)
44
{
45
ptr_ = ptr;
46
}
47
48
/** Destructor.
49
* Destroys the memory chunk unless it has been released before.
50
*/
51
MemAutoFree::~MemAutoFree
()
52
{
53
if
(ptr_)
54
free(ptr_);
55
}
56
57
/** Release ownership.
58
* The instance no longer owns the pointer and memory will not be deleted
59
* on destruction.
60
*/
61
void
62
MemAutoFree::release
()
63
{
64
ptr_ = NULL;
65
}
66
67
/** Reset pointer to a different one,
68
* This will free the pointer hold up to this call and will replace it with
69
* new_ptr. It is verified that the old and new pointers are different, nothing
70
* will be done if they are the same.
71
* @param new_ptr new pointer to own
72
*/
73
void
74
MemAutoFree::reset
(
void
*new_ptr)
75
{
76
if
(ptr_ != new_ptr) {
77
if
(ptr_)
78
free(ptr_);
79
ptr_ = new_ptr;
80
}
81
}
82
83
/** Access memory pointer.
84
* @return pointer to memory, maybe NULL
85
*/
86
void
*
MemAutoFree::operator*
()
const
87
{
88
return
ptr_;
89
}
90
91
}
// end namespace fawkes
fawkes
Fawkes library namespace.
fawkes::MemAutoFree::reset
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:74
fawkes::MemAutoFree::release
void release()
Release ownership.
Definition:
autofree.cpp:62
fawkes::MemAutoFree::MemAutoFree
MemAutoFree(void *ptr)
Constructor.
Definition:
autofree.cpp:43
fawkes::MemAutoFree::operator*
void * operator*() const
Access memory pointer.
Definition:
autofree.cpp:86
fawkes::MemAutoFree::~MemAutoFree
~MemAutoFree()
Destructor.
Definition:
autofree.cpp:51
src
libs
utils
misc
autofree.cpp
Generated by
1.8.15