2858 |
28 Sep 12 |
peter |
1 |
#ifndef _theplu_yat_utility_deleter |
2858 |
28 Sep 12 |
peter |
2 |
#define _theplu_yat_utility_deleter |
2858 |
28 Sep 12 |
peter |
3 |
|
2858 |
28 Sep 12 |
peter |
// $Id$ |
2858 |
28 Sep 12 |
peter |
5 |
|
2858 |
28 Sep 12 |
peter |
6 |
/* |
4089 |
07 Sep 21 |
peter |
Copyright (C) 2012, 2021 Peter Johansson |
2858 |
28 Sep 12 |
peter |
8 |
|
2858 |
28 Sep 12 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
2858 |
28 Sep 12 |
peter |
10 |
|
2858 |
28 Sep 12 |
peter |
The yat library is free software; you can redistribute it and/or |
2858 |
28 Sep 12 |
peter |
modify it under the terms of the GNU General Public License as |
2858 |
28 Sep 12 |
peter |
published by the Free Software Foundation; either version 3 of the |
2858 |
28 Sep 12 |
peter |
License, or (at your option) any later version. |
2858 |
28 Sep 12 |
peter |
15 |
|
2858 |
28 Sep 12 |
peter |
The yat library is distributed in the hope that it will be useful, |
2858 |
28 Sep 12 |
peter |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
2858 |
28 Sep 12 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
2858 |
28 Sep 12 |
peter |
General Public License for more details. |
2858 |
28 Sep 12 |
peter |
20 |
|
2858 |
28 Sep 12 |
peter |
You should have received a copy of the GNU General Public License |
2858 |
28 Sep 12 |
peter |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
2858 |
28 Sep 12 |
peter |
23 |
*/ |
2858 |
28 Sep 12 |
peter |
24 |
|
4022 |
04 Jan 21 |
peter |
25 |
#include <new> // for delete operator |
4022 |
04 Jan 21 |
peter |
26 |
|
2858 |
28 Sep 12 |
peter |
27 |
namespace theplu { |
2858 |
28 Sep 12 |
peter |
28 |
namespace yat { |
2858 |
28 Sep 12 |
peter |
29 |
namespace utility { |
2858 |
28 Sep 12 |
peter |
30 |
|
2858 |
28 Sep 12 |
peter |
31 |
/** |
2858 |
28 Sep 12 |
peter |
\brief Functor that deletes an object. |
2858 |
28 Sep 12 |
peter |
33 |
|
2858 |
28 Sep 12 |
peter |
The class is typically used as second argument to a |
4022 |
04 Jan 21 |
peter |
std::shared_ptr, which enables to decide at runtime whether the |
2858 |
28 Sep 12 |
peter |
shared_ptr should own the pointee, i.e., should the pointee be |
2858 |
28 Sep 12 |
peter |
destructed at destruction of last shared_ptr pointing to the |
2858 |
28 Sep 12 |
peter |
pointee. |
2858 |
28 Sep 12 |
peter |
39 |
|
2858 |
28 Sep 12 |
peter |
\since New in yat 0.10 |
2858 |
28 Sep 12 |
peter |
41 |
*/ |
2858 |
28 Sep 12 |
peter |
42 |
class Deleter |
2858 |
28 Sep 12 |
peter |
43 |
{ |
2858 |
28 Sep 12 |
peter |
44 |
public: |
2858 |
28 Sep 12 |
peter |
45 |
/** |
2858 |
28 Sep 12 |
peter |
\brief constructor |
2858 |
28 Sep 12 |
peter |
47 |
|
2858 |
28 Sep 12 |
peter |
\param active decides whether functor will delete object or |
2858 |
28 Sep 12 |
peter |
not. See operator(). |
2858 |
28 Sep 12 |
peter |
50 |
*/ |
2858 |
28 Sep 12 |
peter |
51 |
Deleter(bool active=true); |
2858 |
28 Sep 12 |
peter |
52 |
|
2858 |
28 Sep 12 |
peter |
53 |
/** |
2858 |
28 Sep 12 |
peter |
\return true if class is activated. |
2858 |
28 Sep 12 |
peter |
55 |
*/ |
2858 |
28 Sep 12 |
peter |
56 |
bool active(void) const; |
2858 |
28 Sep 12 |
peter |
57 |
|
2858 |
28 Sep 12 |
peter |
58 |
/** |
2858 |
28 Sep 12 |
peter |
Delete \a ptr if active() otherwise do nothing |
2858 |
28 Sep 12 |
peter |
60 |
*/ |
2858 |
28 Sep 12 |
peter |
61 |
template<typename T> |
2858 |
28 Sep 12 |
peter |
62 |
void operator()(T* ptr) const |
2858 |
28 Sep 12 |
peter |
63 |
{ |
2858 |
28 Sep 12 |
peter |
64 |
if (active_) |
2858 |
28 Sep 12 |
peter |
65 |
delete ptr; |
2858 |
28 Sep 12 |
peter |
66 |
} |
2858 |
28 Sep 12 |
peter |
67 |
|
2858 |
28 Sep 12 |
peter |
68 |
private: |
2858 |
28 Sep 12 |
peter |
69 |
bool active_; |
2858 |
28 Sep 12 |
peter |
70 |
}; |
2858 |
28 Sep 12 |
peter |
71 |
|
2858 |
28 Sep 12 |
peter |
72 |
}}} // of namespace utility, yat, and theplu |
2858 |
28 Sep 12 |
peter |
73 |
|
2858 |
28 Sep 12 |
peter |
74 |
#endif |