tclap 1.6.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 <memory>
27#include <vector>
28
29namespace TCLAP {
30
38 class DeletableBase {
39 public:
40 virtual ~DeletableBase() = default;
41 };
42
43 template <typename T>
44 class Deletable : public DeletableBase {
45 public:
46 Deletable(T *o) : _o(o) {}
47 ~Deletable() override { delete _o; }
48
49 Deletable(const Deletable<T> &) = delete;
50 Deletable<T> &operator=(const Deletable<T> &) = delete;
51
52 private:
53 T *_o;
54 };
55
56 std::vector<std::unique_ptr<DeletableBase>> _toBeDeleted;
57
58public:
59 DeferDelete() : _toBeDeleted() {}
60
61 template <typename T>
62 void operator()(T *toDelete) {
63 _toBeDeleted.push_back(std::make_unique<Deletable<T>>(toDelete));
64 }
65};
66
67} // namespace TCLAP
68
69#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:37
void operator()(T *toDelete)
Definition DeferDelete.h:62
Definition Arg.h:54