Issue #2046 - Move ScopedICUObject into builtin/intl/ScopedICUObject.h

This commit is contained in:
Martok 2023-02-12 22:32:41 +01:00 committed by roytam1
commit 0172619642
2 changed files with 45 additions and 26 deletions

View file

@ -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 <typename T, void (Delete)(T*)>
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;

View file

@ -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 <typename T, void (Delete)(T*)>
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 */