Files
MapLibreParticleLab/platform/qt/src/mbgl/thread_local.cpp
Tadej Novak 5b3d936521 [qt] Rename QMapboxGL to QMapLibreGL (#338)
* Rename QMapboxGL to QMapLibreGL

* Add missing QDebug includes

* Fix Qt CMake config

* Fix geojson warning

* Remove redundant OpenGL dependency

* Put everything under QMapLibreGL namespace

* Reorganise headers

Co-authored-by: Rinigus <rinigus.git@gmail.com>
2022-07-03 20:30:46 +02:00

40 lines
1.2 KiB
C++

#include <mbgl/util/thread_local.hpp>
#include <array>
#include <cassert>
#include <QThreadStorage>
namespace mbgl {
namespace util {
namespace impl {
// QThreadStorage tries to be smart and take ownership of the data, which we don't want. So we're
// wrapping it in another type which doesn't own the pointer it contains.
using StorageType = QThreadStorage<std::array<void*, 1>>;
ThreadLocalBase::ThreadLocalBase() {
static_assert(sizeof(storage) >= sizeof(StorageType), "storage is too small");
static_assert(alignof(decltype(storage)) % alignof(StorageType) == 0, "storage is incorrectly aligned");
new (&reinterpret_cast<StorageType&>(storage)) QThreadStorage<void*>();
}
ThreadLocalBase::~ThreadLocalBase() {
// ThreadLocal will not take ownership of the pointer it is managing. The pointer
// needs to be explicitly cleared before we destroy this object.
assert(!get());
reinterpret_cast<StorageType&>(storage).~QThreadStorage();
}
void* ThreadLocalBase::get() {
return reinterpret_cast<StorageType&>(storage).localData()[0];
}
void ThreadLocalBase::set(void* ptr) {
reinterpret_cast<StorageType&>(storage).setLocalData({{ ptr }});
}
} // namespace impl
} // namespace util
} // namespace mbgl