#include <cstdlib>
#include <memory>
#include <mutex>
+#include <optional>
#include <RB/RingBuffer.hpp>
TSQueue &operator=(TSQueue &&other);
bool push(const T &data);
- T top();
+ std::optional<T> top();
bool pop();
- T top_and_pop();
+ std::optional<T> top_and_pop();
void clear();
void changeCapacity(unsigned int newCapacity);
unsigned int size();
}
template <typename T>
-T TSQueue<T>::top() {
+std::optional<T> TSQueue<T>::top() {
std::lock_guard<std::mutex> lock(mutex);
- T value = rb.top();
+ std::optional<T> value = std::nullopt;
+ if(!rb.empty()) {
+ value = rb.top();
+ }
return value;
}
}
template <typename T>
-T TSQueue<T>::top_and_pop() {
+std::optional<T> TSQueue<T>::top_and_pop() {
std::lock_guard<std::mutex> lock(mutex);
- T value = rb.top();
- rb.pop();
+ std::optional<T> value = std::nullopt;
+ if(!rb.empty()) {
+ value = rb.top();
+ rb.pop();
+ }
return value;
}
bool isResending = false;
if(!iter->second.priorityPkts.empty()) {
// TODO verify getting struct copy is valid
- pInfo = iter->second.priorityPkts.top();
+ pInfo = std::move(iter->second.priorityPkts.top().value());
iter->second.priorityPkts.pop();
isResending = true;
} else {
- pInfo = iter->second.sendPkts.top();
+ pInfo = std::move(iter->second.sendPkts.top().value());
iter->second.sendPkts.pop();
}
std::unique_ptr<char[]> buf = std::make_unique<char[]>(20 + pInfo.dataSize);