Util/fixed_typemap.hpp: Fix thread-safety issue with init

Now safe as long as no- object is created
This commit is contained in:
Elad Ashkenazi 2023-12-15 16:32:33 +02:00
parent 2190419b83
commit 14c92efd38

View File

@ -396,15 +396,17 @@ namespace stx
}
// Explicitly initialize object of type T possibly with dynamic type As and arguments
template <typename T, typename As = T, typename... Args>
template <typename T, typename As = T, typename... Args> requires (std::is_constructible_v<std::decay_t<As>, Args&&...>)
As* init(Args&&... args) noexcept
{
if (std::exchange(m_init[stx::typeindex<typeinfo, std::decay_t<T>, std::decay_t<As>>()], true))
if (m_init[stx::typeindex<typeinfo, std::decay_t<T>, std::decay_t<As>>()])
{
// Already exists, recreation is not supported (may be added later)
return nullptr;
}
m_init[stx::typeindex<typeinfo, std::decay_t<T>, std::decay_t<As>>()] = true;
As* obj = nullptr;
g_tls_serialize_name = get_name<T, As>();
@ -471,7 +473,7 @@ namespace stx
{
if (is_init<T>())
{
[[likely]] return &get<T>();
[[likely]] return std::addressof(get<T>());
}
[[unlikely]] return nullptr;