internal: new shared_ptr and weak_ptr implementation (#5883)

moves std::shared_ptrs to a new implementation

Advantages:
- you can dereference a weak_ptr directly. This will obviously segfault on a nullptr deref if it's expired.
   - this is useful to avoid the .lock() hell where we are 100% sure the pointer _should_ be valid. (and if it isn't, it should throw.)
- weak_ptrs are still valid while the SP is being destroyed.
   - reasoning: while an object (e.g. CWindow) is being destroyed, its `weak_ptr self` should be accessible (the sp is still alive, and so is CWindow), but it's not because by stl it's already expired (to prevent resurrection)
   - this impl solves it differently. w_p is expired, but can still be dereferenced and used. Creating `s_p`s is not possible anymore, though.
   - this is useful in destructors and callbacks.
This commit is contained in:
Vaxry
2024-05-05 17:16:00 +01:00
committed by GitHub
parent 589f758d94
commit 1ed1ce9506
88 changed files with 899 additions and 414 deletions

View File

@@ -58,7 +58,7 @@ static void onDestroy(void* owner, void* data) {
// IMPL
PHLLS CLayerSurface::create(wlr_layer_surface_v1* pWLRLS) {
PHLLS pLS = std::shared_ptr<CLayerSurface>(new CLayerSurface);
PHLLS pLS = SP<CLayerSurface>(new CLayerSurface);
auto PMONITOR = g_pCompositor->getMonitorFromOutput(pWLRLS->output);
@@ -176,7 +176,7 @@ void CLayerSurface::onMap() {
if ((uint64_t)monitorID != PMONITOR->ID) {
const auto POLDMON = g_pCompositor->getMonitorFromID(monitorID);
for (auto it = POLDMON->m_aLayerSurfaceLayers[layer].begin(); it != POLDMON->m_aLayerSurfaceLayers[layer].end(); it++) {
if (*it == self.lock()) {
if (*it == self) {
PMONITOR->m_aLayerSurfaceLayers[layer].emplace_back(std::move(*it));
POLDMON->m_aLayerSurfaceLayers[layer].erase(it);
break;
@@ -235,7 +235,7 @@ void CLayerSurface::onUnmap() {
std::erase_if(g_pInputManager->m_dExclusiveLSes, [this](const auto& other) { return !other.lock() || other.lock() == self.lock(); });
if (!g_pInputManager->m_dExclusiveLSes.empty())
g_pCompositor->focusSurface(g_pInputManager->m_dExclusiveLSes[0].lock()->layerSurface->surface);
g_pCompositor->focusSurface(g_pInputManager->m_dExclusiveLSes[0]->layerSurface->surface);
if (!g_pCompositor->getMonitorFromID(monitorID) || g_pCompositor->m_bUnsafeState) {
Debug::log(WARN, "Layersurface unmapping on invalid monitor (removed?) ignoring.");
@@ -284,7 +284,7 @@ void CLayerSurface::onUnmap() {
foundSurface = g_pCompositor->vectorToLayerSurface(g_pInputManager->getMouseCoordsInternal(), &PMONITOR->m_aLayerSurfaceLayers[ZWLR_LAYER_SHELL_V1_LAYER_TOP],
&surfaceCoords, &pFoundLayerSurface);
if (!foundSurface && g_pCompositor->m_pLastWindow.lock() && g_pCompositor->isWorkspaceVisible(g_pCompositor->m_pLastWindow.lock()->m_pWorkspace)) {
if (!foundSurface && g_pCompositor->m_pLastWindow.lock() && g_pCompositor->isWorkspaceVisible(g_pCompositor->m_pLastWindow->m_pWorkspace)) {
// if there isn't any, focus the last window
const auto PLASTWINDOW = g_pCompositor->m_pLastWindow.lock();
g_pCompositor->focusWindow(nullptr);
@@ -325,7 +325,7 @@ void CLayerSurface::onCommit() {
const auto POLDMON = g_pCompositor->getMonitorFromID(monitorID);
for (auto it = POLDMON->m_aLayerSurfaceLayers[layer].begin(); it != POLDMON->m_aLayerSurfaceLayers[layer].end(); it++) {
if (*it == self.lock()) {
if (*it == self) {
PMONITOR->m_aLayerSurfaceLayers[layer].emplace_back(std::move(*it));
POLDMON->m_aLayerSurfaceLayers[layer].erase(it);
break;
@@ -341,7 +341,7 @@ void CLayerSurface::onCommit() {
if (layer != layerSurface->current.layer) {
for (auto it = PMONITOR->m_aLayerSurfaceLayers[layer].begin(); it != PMONITOR->m_aLayerSurfaceLayers[layer].end(); it++) {
if (*it == self.lock()) {
if (*it == self) {
PMONITOR->m_aLayerSurfaceLayers[layerSurface->current.layer].emplace_back(std::move(*it));
PMONITOR->m_aLayerSurfaceLayers[layer].erase(it);
break;