DEV: update deque related containers
This commit is contained in:
+57
-23
@@ -55,17 +55,22 @@ namespace tbx {
|
||||
* pushed or popped from the deque. If the criteria match we call call the callable of type
|
||||
* \c Fn
|
||||
*
|
||||
* \tparam Data_t The char-like queued item type. Usually \c char
|
||||
* \tparam N The size of edeque
|
||||
* \tparam Fn The type of Callable
|
||||
* \tparam Data_t The char-like queued item type. Usually \c char
|
||||
* \tparam N The size of edeque
|
||||
* \tparam SemiAtomic True for semi-atomic operation. In that case the \c ring_iterator is also atomic.
|
||||
* \tparam Fn The type of Callable
|
||||
* \note
|
||||
* SemiAtomic means it is safe to access different ends from different threads. For example one thread can
|
||||
* push only from front and another can pop from back to implement a queue.
|
||||
*/
|
||||
template <typename Data_t, size_t N, typename Fn = std::function<void()>>
|
||||
class edeque : public deque<Data_t, N> {
|
||||
template <typename Data_t, size_t N, bool SemiAtomic =false, typename Fn = std::function<void()>>
|
||||
class edeque : public deque<Data_t, N, SemiAtomic> {
|
||||
public:
|
||||
// meta-identity types
|
||||
using type = edeque<Data_t, N>;
|
||||
using base_type = deque<Data_t, N>;
|
||||
using type = edeque<Data_t, N, SemiAtomic, Fn>;
|
||||
using base_type = deque<Data_t, N, SemiAtomic>;
|
||||
using callable_t = Fn;
|
||||
using range_t = typename base_type::range_t;
|
||||
|
||||
// STL
|
||||
using value_type = typename base_type::value_type;
|
||||
@@ -89,7 +94,11 @@ class edeque : public deque<Data_t, N> {
|
||||
enum class size_match { DISABLED =0, EQ, NE, LT, LE, GT, GE };
|
||||
//! \enum data_match
|
||||
//! The type of matching for data based match
|
||||
enum class data_match { DISABLED =0, MATCH, MISMATCH};
|
||||
enum class data_match { DISABLED =0, MATCH_PUSH, MATCH_POP, MISMATCH_PUSH, MISMATCH_POP};
|
||||
|
||||
// TODO: trigger mode for one-shot or repeated functionality
|
||||
// enum class trigger_mode { ONE_SHOT, REPEATED };
|
||||
|
||||
//! \struct size_trigger
|
||||
//! Size trigger data type
|
||||
struct size_trigger {
|
||||
@@ -120,7 +129,7 @@ class edeque : public deque<Data_t, N> {
|
||||
constexpr edeque () noexcept :
|
||||
base_type() { }
|
||||
|
||||
//!
|
||||
//! Size trigger constructor
|
||||
constexpr edeque (size_match match, size_t size, callable_t&& fn) :
|
||||
base_type(),
|
||||
mode_{match_mode::SIZE},
|
||||
@@ -128,7 +137,7 @@ class edeque : public deque<Data_t, N> {
|
||||
trigger_.tsize.type = match;
|
||||
trigger_.tsize.size = size;
|
||||
}
|
||||
|
||||
//! Data trigger constructor
|
||||
constexpr edeque (data_match match, Data_t value, callable_t&& fn) :
|
||||
base_type(),
|
||||
mode_{match_mode::DATA},
|
||||
@@ -186,25 +195,25 @@ class edeque : public deque<Data_t, N> {
|
||||
//! @{
|
||||
void push_front (const Data_t& it) {
|
||||
base_type::push_front(it);
|
||||
check_trigger_async_(it);
|
||||
check_trigger_push_async_(it);
|
||||
}
|
||||
Data_t pop_front () {
|
||||
Data_t t = base_type::pop_front();
|
||||
check_trigger_async_(t);
|
||||
check_trigger_pop_async_(t);
|
||||
return t;
|
||||
}
|
||||
void push_back (const Data_t& it) {
|
||||
base_type::push_back(it);
|
||||
check_trigger_async_(it);
|
||||
check_trigger_push_async_(it);
|
||||
}
|
||||
Data_t pop_back () {
|
||||
Data_t t = base_type::pop_back();
|
||||
check_trigger_async_(t);
|
||||
check_trigger_pop_async_(t);
|
||||
return t;
|
||||
}
|
||||
//! @}
|
||||
|
||||
//! \name Public interface
|
||||
//! \name Private functionality
|
||||
//! @{
|
||||
private:
|
||||
//! \brief
|
||||
@@ -230,28 +239,53 @@ class edeque : public deque<Data_t, N> {
|
||||
}
|
||||
|
||||
//! \brief
|
||||
//! Manually checks the data trigger and calls it we have match.
|
||||
//! Manually checks the data trigger on push and calls it we have match.
|
||||
//! \param it The item to check against
|
||||
//! \return True if the callable has called.
|
||||
bool check_trigger_value_ (const Data_t& it) {
|
||||
bool check_trigger_push_value_ (const Data_t& it) {
|
||||
bool match;
|
||||
switch (trigger_.tdata.type) {
|
||||
default:
|
||||
case data_match::DISABLED: match = false; break;
|
||||
case data_match::MATCH: match = (it == trigger_.tdata.value); break;
|
||||
case data_match::MISMATCH: match = (it != trigger_.tdata.value); break;
|
||||
case data_match::DISABLED: match = false; break;
|
||||
case data_match::MATCH_PUSH: match = (it == trigger_.tdata.value); break;
|
||||
case data_match::MISMATCH_PUSH: match = (it != trigger_.tdata.value); break;
|
||||
}
|
||||
if (match)
|
||||
callback_();
|
||||
return match;
|
||||
}
|
||||
|
||||
//! Wrapper for both triggers
|
||||
bool check_trigger_async_ (const Data_t& it) {
|
||||
//! \brief
|
||||
//! Manually checks the data trigger on pop and calls it we have match.
|
||||
//! \param it The item to check against
|
||||
//! \return True if the callable has called.
|
||||
bool check_trigger_pop_value_ (const Data_t& it) {
|
||||
bool match;
|
||||
switch (trigger_.tdata.type) {
|
||||
default:
|
||||
case data_match::DISABLED: match = false; break;
|
||||
case data_match::MATCH_POP: match = (it == trigger_.tdata.value); break;
|
||||
case data_match::MISMATCH_POP: match = (it != trigger_.tdata.value); break;
|
||||
}
|
||||
if (match)
|
||||
callback_();
|
||||
return match;
|
||||
}
|
||||
|
||||
//! Wrapper for both triggers at push
|
||||
bool check_trigger_push_async_ (const Data_t& it) {
|
||||
switch (mode_) {
|
||||
default:
|
||||
case match_mode::SIZE: return check_trigger_size_();
|
||||
case match_mode::DATA: return check_trigger_value_(it);
|
||||
case match_mode::DATA: return check_trigger_push_value_(it);
|
||||
}
|
||||
}
|
||||
//! Wrapper for both triggers at pop
|
||||
bool check_trigger_pop_async_ (const Data_t& it) {
|
||||
switch (mode_) {
|
||||
default:
|
||||
case match_mode::SIZE: return check_trigger_size_();
|
||||
case match_mode::DATA: return check_trigger_pop_value_(it);
|
||||
}
|
||||
}
|
||||
//! @}
|
||||
|
||||
+18
-16
@@ -49,16 +49,20 @@ namespace tbx {
|
||||
*
|
||||
* We also provide stream operators.
|
||||
*
|
||||
* \tparam Data_t The char-like queued item type. Usually \c char
|
||||
* \tparam N The size of edeque
|
||||
* \tparam Fn The type of Callable
|
||||
* \tparam Data_t The char-like queued item type. Usually \c char
|
||||
* \tparam N The size of edeque
|
||||
* \tparam SemiAtomic True for semi-atomic operation. In that case the \c ring_iterator is also atomic.
|
||||
* \tparam Fn The type of Callable
|
||||
* \note
|
||||
* SemiAtomic means it is safe to for one thread to push only from front and another can pop.
|
||||
*/
|
||||
template <typename Data_t, size_t N, typename Fn = std::function<void()>>
|
||||
class equeue : public edeque<Data_t, N, Fn> {
|
||||
template <typename Data_t, size_t N, bool SemiAtomic =false, typename Fn = std::function<void()>>
|
||||
class equeue : public edeque<Data_t, N, SemiAtomic, Fn> {
|
||||
public:
|
||||
// meta-identity types
|
||||
using equeue_t = equeue<Data_t, N>;
|
||||
using base_type = edeque<Data_t, N, Fn>;
|
||||
using equeue_t = equeue<Data_t, N, SemiAtomic, Fn>;
|
||||
using base_type = edeque<Data_t, N, SemiAtomic, Fn>;
|
||||
using range_t = typename base_type::range_t;
|
||||
|
||||
// STL
|
||||
using value_type = typename base_type::value_type;
|
||||
@@ -77,10 +81,7 @@ class equeue : public edeque<Data_t, N, Fn> {
|
||||
//! Default constructor
|
||||
constexpr equeue () noexcept : base_type() { }
|
||||
|
||||
//! fill contructor
|
||||
constexpr equeue(const Data_t& value) noexcept : base_type(value) { }
|
||||
|
||||
//! Initializer list contructor
|
||||
//! Forward constructor
|
||||
template <typename ...It>
|
||||
constexpr equeue(It&& ...it) noexcept : base_type(std::forward<It>(it)...) { }
|
||||
|
||||
@@ -125,16 +126,17 @@ class equeue : public edeque<Data_t, N, Fn> {
|
||||
*
|
||||
* This definition enables the "data << equeue" syntax for pop operation
|
||||
*
|
||||
* \tparam Data_t The char-like queued item type. Usually \c char
|
||||
* \tparam N The size of queue
|
||||
* \tparam Fn The type of Callable
|
||||
* \tparam Data_t The char-like queued item type. Usually \c char
|
||||
* \tparam N The size of queue
|
||||
* \tparam SemiAtomic True for semi-atomic operation. In that case the \c ring_iterator is also atomic.
|
||||
* \tparam Fn The type of Callable
|
||||
*
|
||||
* \param it The item to write to
|
||||
* \param q The queue to read from
|
||||
* \return Reference to the returned item
|
||||
*/
|
||||
template <typename Data_t, size_t N, typename Fn = std::function<void()>>
|
||||
Data_t& operator<< (Data_t& it, equeue<Data_t, N, Fn>& q) {
|
||||
template <typename Data_t, size_t N, bool SemiAtomic =false, typename Fn = std::function<void()>>
|
||||
Data_t& operator<< (Data_t& it, equeue<Data_t, N, SemiAtomic, Fn>& q) {
|
||||
it = q.pop();
|
||||
return it;
|
||||
}
|
||||
|
||||
+15
-10
@@ -48,15 +48,19 @@ namespace tbx {
|
||||
*
|
||||
* We also provide stream operators.
|
||||
*
|
||||
* \tparam Data_t The char-like queued item type. Usually \c char
|
||||
* \tparam N The size of queue
|
||||
* \tparam Data_t The char-like queued item type. Usually \c char
|
||||
* \tparam N The size of queue
|
||||
* \tparam SemiAtomic True for semi-atomic operation. In that case the \c ring_iterator is also atomic.
|
||||
* \note
|
||||
* SemiAtomic means it is safe to for one thread to push only from front and another can pop.
|
||||
*/
|
||||
template <typename Data_t, size_t N>
|
||||
class queue : public deque<Data_t, N> {
|
||||
template <typename Data_t, size_t N, bool SemiAtomic =false>
|
||||
class queue : public deque<Data_t, N, SemiAtomic> {
|
||||
public:
|
||||
// meta-identity types
|
||||
using queue_t = queue<Data_t, N>;
|
||||
using base_type = deque<Data_t, N>;
|
||||
using queue_t = queue<Data_t, N, SemiAtomic>;
|
||||
using base_type = deque<Data_t, N, SemiAtomic>;
|
||||
using range_t = typename base_type::range_t;
|
||||
|
||||
// STL
|
||||
using value_type = typename base_type::value_type;
|
||||
@@ -121,15 +125,16 @@ class queue : public deque<Data_t, N> {
|
||||
*
|
||||
* This definition enables the "data << queue" syntax for pop operation
|
||||
*
|
||||
* \tparam Data_t The char-like queued item type. Usually \c char
|
||||
* \tparam N The size of queue
|
||||
* \tparam Data_t The char-like queued item type. Usually \c char
|
||||
* \tparam N The size of queue
|
||||
* \tparam SemiAtomic True for semi-atomic operation. In that case the \c ring_iterator is also atomic.
|
||||
*
|
||||
* \param it The item to write to
|
||||
* \param q The queue to read from
|
||||
* \return Reference to the returned item
|
||||
*/
|
||||
template <typename Data_t, size_t N>
|
||||
constexpr Data_t& operator<< (Data_t& it, queue<Data_t, N>& q) {
|
||||
template <typename Data_t, size_t N, bool SemiAtomic =false>
|
||||
constexpr Data_t& operator<< (Data_t& it, queue<Data_t, N, SemiAtomic>& q) {
|
||||
it = q.pop();
|
||||
return it;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user