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 namespace TCLAP {
27 
34 class DeferDelete {
35  class DeletableBase {
36  public:
37  virtual ~DeletableBase() {}
38  };
39 
40  template <typename T>
41  class Deletable : public DeletableBase {
42  public:
43  Deletable(T *o) : _o(o) {}
44  virtual ~Deletable() { delete _o; }
45 
46  private:
47  Deletable(const Deletable<T> &) {}
48  Deletable<T> operator=(const Deletable<T> &) {}
49 
50  T *_o;
51  };
52 
53  std::list<DeletableBase *> _toBeDeleted;
54 
55 public:
56  DeferDelete() : _toBeDeleted() {}
58  for (std::list<DeletableBase *>::iterator it = _toBeDeleted.begin();
59  it != _toBeDeleted.end(); ++it) {
60  delete *it;
61  }
62  }
63 
64  template <typename T>
65  void operator()(T *toDelete) {
66  _toBeDeleted.push_back(new Deletable<T>(toDelete));
67  }
68 };
69 
70 } // namespace TCLAP
71 
72 #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:34
void operator()(T *toDelete)
Definition: DeferDelete.h:65
Definition: Arg.h:46