Source code

Revision control

Other Tools

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