From 2ebcddf9de17d0478d4957a9c695f237e6a289b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Leclercq?= Date: Sun, 23 Jan 2022 00:16:09 +0100 Subject: [PATCH] Core/CallOnExit: Rework to use template instead of std::function --- include/Nazara/Core/CallOnExit.hpp | 17 ++++---- include/Nazara/Core/CallOnExit.inl | 32 +++++++-------- src/Nazara/Network/TcpClient.cpp | 51 +++++++++--------------- src/Nazara/Utility/Formats/MTLParser.cpp | 34 ++++++++-------- src/Nazara/Utility/Formats/OBJParser.cpp | 51 +++++++++++------------- 5 files changed, 85 insertions(+), 100 deletions(-) diff --git a/include/Nazara/Core/CallOnExit.hpp b/include/Nazara/Core/CallOnExit.hpp index 834727e75..217c14b0c 100644 --- a/include/Nazara/Core/CallOnExit.hpp +++ b/include/Nazara/Core/CallOnExit.hpp @@ -8,29 +8,32 @@ #define NAZARA_CORE_CALLONEXIT_HPP #include -#include +#include namespace Nz { + template class CallOnExit { - using Func = std::function; - public: - CallOnExit(Func func = nullptr); + CallOnExit() = default; + CallOnExit(F&& functor); CallOnExit(const CallOnExit&) = delete; CallOnExit(CallOnExit&&) noexcept = delete; ~CallOnExit(); - void CallAndReset(Func func = nullptr); - void Reset(Func func = nullptr); + void CallAndReset(); + void Reset(); CallOnExit& operator=(const CallOnExit&) = delete; CallOnExit& operator=(CallOnExit&&) noexcept = default; private: - Func m_func; + std::optional m_functor; }; + + template + CallOnExit(F) -> CallOnExit; } #include diff --git a/include/Nazara/Core/CallOnExit.inl b/include/Nazara/Core/CallOnExit.inl index e0e04bd9d..cc7eb8da4 100644 --- a/include/Nazara/Core/CallOnExit.inl +++ b/include/Nazara/Core/CallOnExit.inl @@ -15,24 +15,24 @@ namespace Nz */ /*! - * \brief Constructs a CallOnExit object with a function + * \brief Constructs a CallOnExit object with a functor * * \param func Function to call on exit */ - - inline CallOnExit::CallOnExit(Func func) : - m_func(func) + template + CallOnExit::CallOnExit(F&& functor) : + m_functor(std::move(functor)) { } /*! * \brief Destructs the object and calls the function */ - - inline CallOnExit::~CallOnExit() + template + CallOnExit::~CallOnExit() { - if (m_func) - m_func(); + if (m_functor) + (*m_functor)(); } /*! @@ -40,13 +40,13 @@ namespace Nz * * \param func Function to call on exit */ - - inline void CallOnExit::CallAndReset(Func func) + template + void CallOnExit::CallAndReset() { - if (m_func) - m_func(); + if (m_functor) + (*m_functor)(); - Reset(func); + m_functor.reset(); } /*! @@ -54,10 +54,10 @@ namespace Nz * * \param func Function to call on exit */ - - inline void CallOnExit::Reset(Func func) + template + void CallOnExit::Reset() { - m_func = func; + m_functor.reset(); } } diff --git a/src/Nazara/Network/TcpClient.cpp b/src/Nazara/Network/TcpClient.cpp index 83099009a..41fc3018f 100644 --- a/src/Nazara/Network/TcpClient.cpp +++ b/src/Nazara/Network/TcpClient.cpp @@ -45,15 +45,11 @@ namespace Nz Disconnect(); Open(remoteAddress.GetProtocol()); - CallOnExit restoreBlocking; + CallOnExit restoreBlocking([this] { SocketImpl::SetBlocking(m_handle, true); }); if (m_isBlockingEnabled) - { SocketImpl::SetBlocking(m_handle, false); - restoreBlocking.Reset([this] () - { - SocketImpl::SetBlocking(m_handle, true); - }); - } + else + restoreBlocking.Reset(); SocketState state = SocketImpl::Connect(m_handle, remoteAddress, &m_lastError); m_peerAddress = (state != SocketState::NotConnected) ? remoteAddress : IpAddress::Invalid; @@ -363,14 +359,13 @@ namespace Nz std::size_t totalByteSent = 0; - CallOnExit updateSent; - if (sent) + CallOnExit updateSent([sent, &totalByteSent] { - updateSent.Reset([sent, &totalByteSent] () - { - *sent = totalByteSent; - }); - } + *sent = totalByteSent; + }); + + if (!sent) + updateSent.Reset(); while (totalByteSent < size || !IsBlockingEnabled()) { @@ -582,15 +577,11 @@ namespace Nz { NazaraAssert(m_handle != SocketImpl::InvalidHandle, "Invalid handle"); - CallOnExit restoreBlocking; - if (!m_isBlockingEnabled) - { - SocketImpl::SetBlocking(m_handle, true); - restoreBlocking.Reset([this] () - { - SocketImpl::SetBlocking(m_handle, false); - }); - } + CallOnExit restoreBlocking([this] { SocketImpl::SetBlocking(m_handle, true); }); + if (m_isBlockingEnabled) + SocketImpl::SetBlocking(m_handle, false); + else + restoreBlocking.Reset(); std::size_t received; if (!Receive(buffer, size, &received)) @@ -630,15 +621,11 @@ namespace Nz NazaraAssert(buffer, "Invalid buffer"); NazaraAssert(m_handle != SocketImpl::InvalidHandle, "Invalid handle"); - CallOnExit restoreBlocking; - if (!m_isBlockingEnabled) - { - SocketImpl::SetBlocking(m_handle, true); - restoreBlocking.Reset([this] () - { - SocketImpl::SetBlocking(m_handle, false); - }); - } + CallOnExit restoreBlocking([this] { SocketImpl::SetBlocking(m_handle, true); }); + if (m_isBlockingEnabled) + SocketImpl::SetBlocking(m_handle, false); + else + restoreBlocking.Reset(); std::size_t sent; if (!Send(buffer, size, &sent)) diff --git a/src/Nazara/Utility/Formats/MTLParser.cpp b/src/Nazara/Utility/Formats/MTLParser.cpp index 4de0e7726..2afd1d157 100644 --- a/src/Nazara/Utility/Formats/MTLParser.cpp +++ b/src/Nazara/Utility/Formats/MTLParser.cpp @@ -33,17 +33,16 @@ namespace Nz { m_currentStream = &stream; - // Force stream in text mode, reset it at the end - Nz::CallOnExit resetTextMode; - if ((stream.GetStreamOptions() & StreamOption::Text) == 0) + // force stream in text mode, reset it at the end + CallOnExit resetTextMode([&stream] { - stream.EnableTextMode(true); + stream.EnableTextMode(false); + }); - resetTextMode.Reset([&stream] () - { - stream.EnableTextMode(false); - }); - } + if ((stream.GetStreamOptions() & StreamOption::Text) == 0) + stream.EnableTextMode(true); + else + resetTextMode.Reset(); m_keepLastLine = false; m_lineCount = 0; @@ -489,17 +488,16 @@ namespace Nz { m_currentStream = &stream; - // Force stream in text mode, reset it at the end - Nz::CallOnExit resetTextMode; - if ((stream.GetStreamOptions() & StreamOption::Text) == 0) + // force stream in text mode, reset it at the end + CallOnExit resetTextMode([&stream] { - stream.EnableTextMode(true); + stream.EnableTextMode(false); + }); - resetTextMode.Reset([&stream] () - { - stream.EnableTextMode(false); - }); - } + if ((stream.GetStreamOptions() & StreamOption::Text) == 0) + stream.EnableTextMode(true); + else + resetTextMode.Reset(); m_outputStream.str({}); diff --git a/src/Nazara/Utility/Formats/OBJParser.cpp b/src/Nazara/Utility/Formats/OBJParser.cpp index 997d7220e..ca7dbd49f 100644 --- a/src/Nazara/Utility/Formats/OBJParser.cpp +++ b/src/Nazara/Utility/Formats/OBJParser.cpp @@ -21,17 +21,16 @@ namespace Nz m_keepLastLine = false; m_lineCount = 0; - // Force stream in text mode, reset it at the end - Nz::CallOnExit resetTextMode; - if ((stream.GetStreamOptions() & StreamOption::Text) == 0) + // force stream in text mode, reset it at the end + CallOnExit resetTextMode([&stream] { - stream.EnableTextMode(true); + stream.EnableTextMode(false); + }); - resetTextMode.Reset([&stream] () - { - stream.EnableTextMode(false); - }); - } + if ((stream.GetStreamOptions() & StreamOption::Text) == 0) + stream.EnableTextMode(true); + else + resetTextMode.Reset(); unsigned int failureCount = 0; while (Advance(false)) @@ -93,17 +92,16 @@ namespace Nz m_keepLastLine = false; m_lineCount = 0; - // Force stream in text mode, reset it at the end - Nz::CallOnExit resetTextMode; - if ((stream.GetStreamOptions() & StreamOption::Text) == 0) + // force stream in text mode, reset it at the end + CallOnExit resetTextMode([&stream] { - stream.EnableTextMode(true); + stream.EnableTextMode(false); + }); - resetTextMode.Reset([&stream] () - { - stream.EnableTextMode(false); - }); - } + if ((stream.GetStreamOptions() & StreamOption::Text) == 0) + stream.EnableTextMode(true); + else + resetTextMode.Reset(); std::string matName, meshName; matName = meshName = "default"; @@ -491,17 +489,16 @@ namespace Nz { m_currentStream = &stream; - // Force stream in text mode, reset it at the end - Nz::CallOnExit resetTextMode; - if ((stream.GetStreamOptions() & StreamOption::Text) == 0) + // force stream in text mode, reset it at the end + CallOnExit resetTextMode([&stream] { - stream.EnableTextMode(true); + stream.EnableTextMode(false); + }); - resetTextMode.Reset([&stream] () - { - stream.EnableTextMode(false); - }); - } + if ((stream.GetStreamOptions() & StreamOption::Text) == 0) + stream.EnableTextMode(true); + else + resetTextMode.Reset(); m_outputStream.str({});