#ifndef UDPC_THREADSAFE_LINKEDLIST_QUEUE_HPP #define UDPC_THREADSAFE_LINKEDLIST_QUEUE_HPP #include #include #include #include #include // definition template class TSLQueue { public: TSLQueue(); ~TSLQueue(); // disable copy TSLQueue(const TSLQueue &other) = delete; TSLQueue &operator=(const TSLQueue &other) = delete; // enable move TSLQueue(TSLQueue &&other); TSLQueue &operator=(TSLQueue &&other); bool push(const T &data); std::optional top(); bool pop(); std::optional top_and_pop(); std::optional top_and_pop_and_empty(bool *isEmpty); void clear(); bool empty(); template class TSLQIterWrapper { public: TSLQIterWrapper(std::conditional_t, std::list> *container, std::weak_ptr iterValid); bool isValid() const; bool next(); bool prev(); std::optional current(); private: std::conditional_t, std::list> *containerPtr; std::conditional_t::const_reverse_iterator, typename std::list::reverse_iterator>, std::conditional_t::const_iterator, typename std::list::iterator>> iter; std::weak_ptr iterValid; }; private: std::shared_ptr iterValid; }; // implementation template TSLQueue::TSLQueue() { } template TSLQueue::~TSLQueue() { } template TSLQueue::TSLQueue(TSLQueue &&other) { } template TSLQueue & TSLQueue::operator=(TSLQueue &&other) { } template template TSLQueue::TSLQIterWrapper::TSLQIterWrapper( std::conditional_t, std::list> *container, std::weak_ptr iterValid) : containerPtr(container), iterValid(iterValid) { if constexpr (isRev) { if constexpr (isConst) { iter = containerPtr->crbegin(); } else { iter = containerPtr->rbegin(); } } else { if constexpr (isConst) { iter = containerPtr->cbegin(); } else { iter = containerPtr->begin(); } } } template template bool TSLQueue::TSLQIterWrapper::isValid() const { return !iterValid.expired(); } template template bool TSLQueue::TSLQIterWrapper::next() { if(!isValid()) { return false; } if constexpr (isRev) { if(containerPtr->rend() == iter) { iterValid.reset(); return false; } else { ++iter; } } else { if(containerPtr->end() == iter) { iterValid.reset(); return false; } else { ++iter; } } return true; } template template bool TSLQueue::TSLQIterWrapper::prev() { if(!isValid()) { return false; } if constexpr (isRev) { if(containerPtr->rbegin() == iter) { iterValid.reset(); return false; } else { --iter; } } else { if(containerPtr->begin() == iter) { iterValid.reset(); return false; } else { --iter; } } return true; } template template std::optional TSLQueue::TSLQIterWrapper::current() { if(!isValid()) { return std::nullopt; } else { if constexpr (isRev) { if(containerPtr->rend() == iter) { return std::nullopt; } } else { if(containerPtr->end() == iter) { return std::nullopt; } } } return *iter; } #endif