forked from UWQuickstep/quickstep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScopedDeleter.hpp
108 lines (90 loc) · 2.83 KB
/
ScopedDeleter.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
**/
#ifndef QUICKSTEP_UTILITY_SCOPED_DELETER_HPP_
#define QUICKSTEP_UTILITY_SCOPED_DELETER_HPP_
#include <utility>
#include <vector>
#include "utility/Macros.hpp"
namespace quickstep {
/** \addtogroup Utility
* @{
*/
/**
* @brief RAII helper object that holds pointers to new-allocated objects of
* any class and deletes them when it goes out of scope.
**/
class ScopedDeleter {
public:
/**
* @brief Constructor.
**/
ScopedDeleter() {
}
/**
* @brief Destructor. Deletes any objects held by this ScopedDeleter.
**/
~ScopedDeleter() {
deleteAll();
}
/**
* @brief Add an object to the pool of objects to be deleted by this
* ScopedDeleter.
*
* @param object A pointer to an object that will be deleted when this
* ScopedDeleter goes out of scope. Must be allocated with new and
* should not be deleted by any other code.
**/
template <typename T>
inline void addObject(T *object) {
objects_.emplace_back(&DeleteObject<T>, object);
}
/**
* @brief Create an object and add it to the pool of objects to be deleted by
* this ScopedDeleter.
*
* @param arguments Forwarded varadic arguments for constructing the object.
**/
template <typename T, typename ...ArgTypes>
inline T* createObject(ArgTypes &&...arguments) {
T* object = new T(std::forward<ArgTypes>(arguments)...);
objects_.emplace_back(&DeleteObject<T>, object);
return object;
}
/**
* @brief Delete all objects held by this ScopedDeleter without destroying
* it.
**/
void deleteAll() {
for (std::pair<DeleterFunction, void*> &item : objects_) {
(*item.first)(item.second);
}
objects_.clear();
}
private:
typedef void (*DeleterFunction)(void*);
template <typename T>
static void DeleteObject(void *object) {
delete static_cast<T*>(object);
}
std::vector<std::pair<DeleterFunction, void*>> objects_;
DISALLOW_COPY_AND_ASSIGN(ScopedDeleter);
};
/** @} */
} // namespace quickstep
#endif // QUICKSTEP_UTILITY_SCOPED_DELETER_HPP_