From 1ed1ce9506e65f76fe5236194516f23d662bba0b Mon Sep 17 00:00:00 2001 From: Vaxry <43317083+vaxerski@users.noreply.github.com> Date: Sun, 5 May 2024 17:16:00 +0100 Subject: 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. --- src/protocols/OutputManagement.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'src/protocols/OutputManagement.cpp') diff --git a/src/protocols/OutputManagement.cpp b/src/protocols/OutputManagement.cpp index 18be636d..9ebcba07 100644 --- a/src/protocols/OutputManagement.cpp +++ b/src/protocols/OutputManagement.cpp @@ -18,7 +18,7 @@ COutputManager::COutputManager(SP resource_) : resource(re LOGM(LOG, "Creating new configuration"); const auto RESOURCE = PROTO::outputManagement->m_vConfigurations.emplace_back( - std::make_shared(std::make_shared(resource->client(), resource->version(), id), self.lock())); + makeShared(makeShared(resource->client(), resource->version(), id), self.lock())); if (!RESOURCE->good()) { resource->noMemory(); @@ -49,7 +49,7 @@ void COutputManager::makeAndSendNewHead(CMonitor* pMonitor) { return; const auto RESOURCE = - PROTO::outputManagement->m_vHeads.emplace_back(std::make_shared(std::make_shared(resource->client(), resource->version(), 0), pMonitor)); + PROTO::outputManagement->m_vHeads.emplace_back(makeShared(makeShared(resource->client(), resource->version(), 0), pMonitor)); if (!RESOURCE->good()) { resource->noMemory(); @@ -205,8 +205,7 @@ void COutputHead::updateMode() { } void COutputHead::makeAndSendNewMode(wlr_output_mode* mode) { - const auto RESOURCE = - PROTO::outputManagement->m_vModes.emplace_back(std::make_shared(std::make_shared(resource->client(), resource->version(), 0), mode)); + const auto RESOURCE = PROTO::outputManagement->m_vModes.emplace_back(makeShared(makeShared(resource->client(), resource->version(), 0), mode)); if (!RESOURCE->good()) { resource->noMemory(); @@ -275,7 +274,7 @@ COutputConfiguration::COutputConfiguration(SP resour } const auto RESOURCE = PROTO::outputManagement->m_vConfigurationHeads.emplace_back( - std::make_shared(std::make_shared(resource->client(), resource->version(), id), PMONITOR)); + makeShared(makeShared(resource->client(), resource->version(), id), PMONITOR)); if (!RESOURCE->good()) { resource->noMemory(); @@ -326,7 +325,7 @@ COutputConfiguration::COutputConfiguration(SP resour else resource->sendFailed(); - owner.lock()->sendDone(); + owner->sendDone(); }); } @@ -359,8 +358,8 @@ bool COutputConfiguration::applyTestConfiguration(bool test) { newRule.name = PMONITOR->szName; if (head->committedProperties & COutputConfigurationHead::eCommittedProperties::OUTPUT_HEAD_COMMITTED_MODE) { - newRule.resolution = {head->state.mode.lock()->getMode()->width, head->state.mode.lock()->getMode()->height}; - newRule.refreshRate = head->state.mode.lock()->getMode()->refresh / 1000.F; + newRule.resolution = {head->state.mode->getMode()->width, head->state.mode->getMode()->height}; + newRule.refreshRate = head->state.mode->getMode()->refresh / 1000.F; } else if (head->committedProperties & COutputConfigurationHead::eCommittedProperties::OUTPUT_HEAD_COMMITTED_CUSTOM_MODE) { newRule.resolution = head->state.customMode.size; newRule.refreshRate = head->state.customMode.refresh / 1000.F; @@ -539,7 +538,7 @@ COutputManagementProtocol::COutputManagementProtocol(const wl_interface* iface, } void COutputManagementProtocol::bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id) { - const auto RESOURCE = m_vManagers.emplace_back(std::make_shared(std::make_shared(client, ver, id))); + const auto RESOURCE = m_vManagers.emplace_back(makeShared(makeShared(client, ver, id))); if (!RESOURCE->good()) { wl_client_post_no_memory(client); -- cgit v1.2.3