Skip to content

Commit

Permalink
[cleanup] use std::optional for cConcurrentQueue::try_pop
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarod42 committed Aug 20, 2024
1 parent a2e17a0 commit ca8fed7
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 24 deletions.
5 changes: 2 additions & 3 deletions src/lib/game/logic/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,9 @@ void cClient::runClientJobs()
//------------------------------------------------------------------------------
void cClient::handleNetMessages()
{
std::unique_ptr<cNetMessage> message;
while (eventQueue.try_pop (message))
while (auto message = eventQueue.try_pop())
{
if (handleNetMessage (*message))
if (handleNetMessage (**message))
{
return;
}
Expand Down
5 changes: 2 additions & 3 deletions src/lib/game/logic/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,9 @@ void cServer::run()
{
while (!exit)
{
std::unique_ptr<cNetMessage> message;
while (eventQueue.try_pop (message))
while (const auto message = eventQueue.try_pop())
{
run (*message);
run (**message);
}

//TODO: gameinit: start timer, when all clients are ready
Expand Down
9 changes: 3 additions & 6 deletions src/lib/game/startup/lobbyclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ void cLobbyClient::pushMessage (std::unique_ptr<cNetMessage> message)
//------------------------------------------------------------------------------
std::unique_ptr<cNetMessage> cLobbyClient::popMessage()
{
std::unique_ptr<cNetMessage> message;
messageQueue.try_pop (message);
return message;
return messageQueue.try_pop().value_or (nullptr);
}

//------------------------------------------------------------------------------
Expand All @@ -73,10 +71,9 @@ void cLobbyClient::run()
client->run();
return;
}
std::unique_ptr<cNetMessage> message;
while (messageQueue.try_pop (message))
while (const auto message = messageQueue.try_pop())
{
handleNetMessage (*message);
handleNetMessage (**message);
}
}

Expand Down
10 changes: 3 additions & 7 deletions src/lib/game/startup/lobbyserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ void cLobbyServer::pushMessage (std::unique_ptr<cNetMessage> message)
//------------------------------------------------------------------------------
std::unique_ptr<cNetMessage> cLobbyServer::popMessage()
{
std::unique_ptr<cNetMessage> message;
messageQueue.try_pop (message);
return message;
return messageQueue.try_pop().value_or (nullptr);
}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -136,11 +134,9 @@ eOpenServerResult cLobbyServer::startServer (int port)
//------------------------------------------------------------------------------
void cLobbyServer::run()
{
std::unique_ptr<cNetMessage> message;

while (messageQueue.try_pop (message))
while (const auto message = messageQueue.try_pop())
{
handleNetMessage (*message);
handleNetMessage (**message);
}
}

Expand Down
11 changes: 6 additions & 5 deletions src/lib/utility/thread/concurrentqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <deque>
#include <mutex>
#include <optional>

template <typename T>
class cConcurrentQueue
Expand All @@ -36,7 +37,7 @@ class cConcurrentQueue
public:
void push (const T& value);
void push (T&& value);
bool try_pop (T& destination);
std::optional<T> try_pop();
void clear();

size_type safe_size() const;
Expand Down Expand Up @@ -67,16 +68,16 @@ void cConcurrentQueue<T>::push (T&& value)

//------------------------------------------------------------------------------
template <typename T>
bool cConcurrentQueue<T>::try_pop (T& destination)
std::optional<T> cConcurrentQueue<T>::try_pop()
{
std::unique_lock<std::mutex> lock (mutex);

if (internalQueue.empty()) return false;
if (internalQueue.empty()) return std::nullopt;

destination = std::move (internalQueue.front());
auto res = std::move (internalQueue.front());
internalQueue.pop_front();

return true;
return res;
}

//------------------------------------------------------------------------------
Expand Down

0 comments on commit ca8fed7

Please sign in to comment.