Ignore:
Timestamp:
10/16/13 20:50:21 (11 years ago)
Author:
tbretz
Message:
Remove fSort; replaced the possibility to define a wrapper class which overwrites begin()
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Mars/mcore/Queue.h

    r17219 r17227  
    66#include <condition_variable>
    77
    8 template<class T>
     8// The second template argument must support:
     9//    iterator it = begin();   // get the next element to be processed
     10//    erase(it);               // erase the processed element from the queue
     11//    push_back();             // add a new element to the queue
     12//    emplace_back();          // emplace a new element to the queue
     13//    splice();                // used to efficiently implement post with mutex
     14
     15template<class T, class List=std::list<T>>
    916class Queue
    1017{
    1118    size_t fSize;                 // Only necessary for before C++11
    12     bool   fSort;                 // Sort the list before processing
    13 
    14     std::list<T> fList;
     19
     20    List fList;
    1521
    1622    std::mutex fMutex;             // Mutex needed for the conditional
     
    5460
    5561            // If thread got just woken up, move back the state to kRun
    56             if (fState == kTrigger)
     62            if (fState==kTrigger)
    5763                fState = kRun;
    5864
     
    6672
    6773            // get the first entry from the (sorted) list
    68             const auto it = fSort ? min_element(fList.begin(), fList.end()) : fList.begin();
     74            const auto it = fList.begin();
    6975
    7076            // Theoretically, we can loose a signal here, but this is
     
    7682            // been posted (or the number of events in the queue has
    7783            // changed)  [allowed>0], in the case processing was
    78             // successfull [alloed==0], the next event will be processed
     84            // successfull [allowed==0], the next event will be processed
    7985            // immediately.
    8086            if (!fCallback || !fCallback(*it))
     
    8894                continue;
    8995
    90             if (fSort)
    91                 fList.erase(it);
    92             else
    93                 fList.pop_front() ;
     96            fList.erase(it);
    9497
    9598            fSize--;
     
    105108
    106109public:
    107     Queue(const callback &f, bool sort=false, bool startup=true) : fSize(0), fSort(sort), fState(kIdle), fCallback(f)
     110    Queue(const callback &f, bool startup=true) : fSize(0), fState(kIdle), fCallback(f)
    108111    {
    109112        if (startup)
     
    111114    }
    112115
    113     Queue(const Queue<T>& q) : fSize(0), fSort(q.fSort), fState(kIdle), fCallback(q.fCallback)
    114     {
    115     }
    116 
    117     Queue<T>& operator = (const Queue<T>& q)
     116    Queue(const Queue<T,List>& q) : fSize(0), fState(kIdle), fCallback(q.fCallback)
     117    {
     118    }
     119
     120    Queue<T,List>& operator = (const Queue<T,List>& q)
    118121    {
    119122        fSize     = 0;
    120         fSort     = q.fSort;
    121123        fState    = kIdle;
    122124        fCallback = q.fCallback;
     
    229231
    230232#ifdef __GXX_EXPERIMENTAL_CXX0X__
    231     bool move(std::list<T>&& x, typename std::list<T>::iterator i)
     233    bool move(List&& x, typename List::iterator i)
    232234#else
    233     bool move(std::list<T>& x, typename std::list<T>::iterator i)
     235    bool move(List& x, typename List::iterator i)
    234236#endif
    235237    {
     
    247249
    248250#ifdef __GXX_EXPERIMENTAL_CXX0X__
    249     bool move(std::list<T>& x, typename std::list<T>::iterator i) { return move(std::move(x), i); }
     251    bool move(List& x, typename List::iterator i) { return move(std::move(x), i); }
    250252#endif
    251253
Note: See TracChangeset for help on using the changeset viewer.