From 0402442929165d4ccff7fd429f3a17db9e8164ba Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Tue, 22 Oct 2019 20:24:23 +0900 Subject: [PATCH] Begin work on TSLQueue --- cpp_impl/src/TSLQueue.hpp | 183 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 cpp_impl/src/TSLQueue.hpp diff --git a/cpp_impl/src/TSLQueue.hpp b/cpp_impl/src/TSLQueue.hpp new file mode 100644 index 0000000..5a0253a --- /dev/null +++ b/cpp_impl/src/TSLQueue.hpp @@ -0,0 +1,183 @@ +#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