Wiselib
wiselib.testing/util/pstl/queue_static.h
Go to the documentation of this file.
00001 /*
00002  *      Author: Juan Farré, UPC
00003  */
00004 
00005 #ifndef __WISELIB_INTERNAL_INTERFACE_STL_QUEUE_STATIC_H_
00006 #define __WISELIB_INTERNAL_INTERFACE_STL_QUEUE_STATIC_H_
00007 
00008 #include <string.h>
00009 
00010 namespace wiselib {
00011 
00012 template<class OsModel_P, class Value_P, typename OsModel_P::size_t QUEUE_SIZE>
00013 class queue_static {
00014 public:
00015    typedef Value_P value_type;
00016    typedef value_type *pointer;
00017    typedef value_type &reference;
00018    typedef value_type const &const_reference;
00019    typedef typename OsModel_P::size_t size_type;
00020    typedef queue_static<OsModel_P,Value_P,QUEUE_SIZE> queue_type;
00021 
00022    queue_static(): front_(0), size_(0) {
00023    }
00024 
00025    queue_static(queue_static const &q): front_(q.front_), size_(q.size()) {
00026       memcpy(queue,q.queue,sizeof(queue));
00027    }
00028 
00029    queue_static &operator=(queue_static const &q) {
00030       if(this==&q)
00031       return;
00032       front_=q.front_;
00033       size_=q.size();
00034       memcpy(queue,q.queue,sizeof(queue));
00035       return *this;
00036    }
00037 
00038    size_type max_size() const {
00039       return QUEUE_SIZE;
00040    }
00041 
00042    size_type capacity() const {
00043       return max_size();
00044    }
00045 
00046    size_type size() const {
00047       return size_;
00048    }
00049 
00050    bool empty() const {
00051       return size() == 0;
00052    }
00053 
00054    bool full() const {
00055       return size() == max_size();
00056    }
00057 
00058    reference front() {
00059       return queue[front_];
00060    }
00061 
00062    const_reference front() const {
00063       return front();
00064    }
00065 
00066    reference back() {
00067       return queue[(front_+size()-1)%max_size()];
00068    }
00069 
00070    const_reference back() const {
00071       return back();
00072    }
00073 
00074    void push(const_reference x) {
00075       if(!full()) {
00076          ++size_;
00077          back()=x;
00078       }
00079    }
00080 
00081    void pop() {
00082       if(!empty()) {
00083          --size_;
00084          ++front_;
00085          front_%=max_size();
00086       }
00087    }
00088 
00089    void clear() {
00090       size_=0;
00091    }
00092 
00093 private:
00094    value_type queue[QUEUE_SIZE];
00095    size_type front_;
00096    size_type size_;
00097 };
00098 
00099 }
00100 
00101 #endif /* __WISELIB_INTERNAL_INTERFACE_STL_QUEUE_STATIC_H_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines