tclap 1.4.0
DeferDelete.h
Go to the documentation of this file.
1// -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2
3/******************************************************************************
4 *
5 * file: DeferDelete.h
6 *
7 * Copyright (c) 2020, Google LLC
8 * All rights reserved.
9 *
10 * See the file COPYING in the top directory of this distribution for
11 * more information.
12 *
13 * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
14 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19 * DEALINGS IN THE SOFTWARE.
20 *
21 *****************************************************************************/
22
23#ifndef TCLAP_DEFER_DELETE_H
24#define TCLAP_DEFER_DELETE_H
25
26#include <list>
27
28namespace TCLAP {
29
37 class DeletableBase {
38 public:
39 virtual ~DeletableBase() {}
40 };
41
42 template <typename T>
43 class Deletable : public DeletableBase {
44 public:
45 Deletable(T *o) : _o(o) {}
46 virtual ~Deletable() { delete _o; }
47
48 private:
49 Deletable(const Deletable<T> &) {}
50 Deletable<T> operator=(const Deletable<T> &) {}
51
52 T *_o;
53 };
54
55 std::list<DeletableBase *> _toBeDeleted;
56
57public:
58 DeferDelete() : _toBeDeleted() {}
60 for (std::list<DeletableBase *>::iterator it = _toBeDeleted.begin();
61 it != _toBeDeleted.end(); ++it) {
62 delete *it;
63 }
64 }
65
66 template <typename T>
67 void operator()(T *toDelete) {
68 _toBeDeleted.push_back(new Deletable<T>(toDelete));
69 }
70};
71
72} // namespace TCLAP
73
74#endif // TCLAP_DEFER_DELETE_H
DeferDelete can be used by objects that need to allocate arbitrary other objects to live for the dura...
Definition DeferDelete.h:36
void operator()(T *toDelete)
Definition DeferDelete.h:67
Definition Arg.h:52