From 0172619642b035629f25bc8dc53d945d72bb2a2d Mon Sep 17 00:00:00 2001 From: Martok Date: Sun, 12 Feb 2023 22:32:41 +0100 Subject: [PATCH] Issue #2046 - Move ScopedICUObject into builtin/intl/ScopedICUObject.h --- js/src/builtin/Intl.cpp | 27 +--------------- js/src/builtin/intl/ScopedICUObject.h | 44 +++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 26 deletions(-) create mode 100644 js/src/builtin/intl/ScopedICUObject.h diff --git a/js/src/builtin/Intl.cpp b/js/src/builtin/Intl.cpp index 494a0c0473..f24a221fdb 100644 --- a/js/src/builtin/Intl.cpp +++ b/js/src/builtin/Intl.cpp @@ -22,6 +22,7 @@ #include "jscntxt.h" #include "jsobj.h" +#include "builtin/intl/ScopedICUObject.h" #include "builtin/IntlTimeZoneData.h" #include "ds/Sort.h" #include "unicode/plurrule.h" @@ -183,32 +184,6 @@ icuLocale(const char* locale) return locale; } -// Simple RAII for ICU objects. Unfortunately, ICU's C++ API is uniformly -// unstable, so we can't use its smart pointers for this. -template -class ScopedICUObject -{ - T* ptr_; - - public: - explicit ScopedICUObject(T* ptr) - : ptr_(ptr) - {} - - ~ScopedICUObject() { - if (ptr_) - Delete(ptr_); - } - - // In cases where an object should be deleted on abnormal exits, - // but returned to the caller if everything goes well, call forget() - // to transfer the object just before returning. - T* forget() { - T* tmp = ptr_; - ptr_ = nullptr; - return tmp; - } -}; // The inline capacity we use for the char16_t Vectors. static const size_t INITIAL_CHAR_BUFFER_SIZE = 32; diff --git a/js/src/builtin/intl/ScopedICUObject.h b/js/src/builtin/intl/ScopedICUObject.h new file mode 100644 index 0000000000..4a2e88186d --- /dev/null +++ b/js/src/builtin/intl/ScopedICUObject.h @@ -0,0 +1,44 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef builtin_intl_ScopedICUObject_h +#define builtin_intl_ScopedICUObject_h + +/* + * A simple RAII class to assure ICU objects are automatically deallocated at + * scope end. Unfortunately, ICU's C++ API is uniformly unstable, so we can't + * use its smart pointers for this. + */ + +namespace js { + +template +class ScopedICUObject +{ + T* ptr_; + + public: + explicit ScopedICUObject(T* ptr) + : ptr_(ptr) + {} + + ~ScopedICUObject() { + if (ptr_) + Delete(ptr_); + } + + // In cases where an object should be deleted on abnormal exits, + // but returned to the caller if everything goes well, call forget() + // to transfer the object just before returning. + T* forget() { + T* tmp = ptr_; + ptr_ = nullptr; + return tmp; + } +}; + +} // namespace js + +#endif /* builtin_intl_ScopedICUObject_h */ \ No newline at end of file