Debian Codegraph @

intl/components/src/ScopedICUObject.h

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 #ifndef intl_components_ScopedICUObject_h
5 #define intl_components_ScopedICUObject_h
6
7 /*
8 * A simple RAII class to assure ICU objects are automatically deallocated at
9 * scope end. Unfortunately, ICU's C++ API is uniformly unstable, so we can't
10 * use its smart pointers for this.
11 */
12
13 namespace mozilla::intl {
14
15 template <typename T, void(Delete)(T*)>
16 class ScopedICUObject {
17 T* ptr_;
18
19 public:
20 explicit ScopedICUObject(T* ptr) : ptr_(ptr) {}
21
22 ~ScopedICUObject() {
23 if (ptr_) {
24 Delete(ptr_);
25 }
26 }
27
28 // In cases where an object should be deleted on abnormal exits,
29 // but returned to the caller if everything goes well, call forget()
30 // to transfer the object just before returning.
31 T* forget() {
32 T* tmp = ptr_;
33 ptr_ = nullptr;
34 return tmp;
35 }
36 };
37
38 } // namespace mozilla::intl
39
40 #endif /* intl_components_ScopedICUObject_h */