Panda3D

circBuffer.h

00001 // Filename: circBuffer.h
00002 // Created by:  drose (08Feb99)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #ifndef CIRCBUFFER_H
00016 #define CIRCBUFFER_H
00017 
00018 #include "pandabase.h"
00019 
00020 ////////////////////////////////////////////////////////////////////
00021 //       Class : CircBuffer
00022 // Description : This class implements a queue of some type via a
00023 //               circular buffer.  The circular buffer has the
00024 //               advantage that no synchronization is required when
00025 //               one process adds to the queue while another process
00026 //               extracts.  It works for any kind of Thing that has a
00027 //               valid assignment operator and copy constructor
00028 //               defined.
00029 ////////////////////////////////////////////////////////////////////
00030 template<class Thing, int max_size>
00031 class CircBuffer {
00032 public:
00033   INLINE CircBuffer();
00034   INLINE ~CircBuffer();
00035 
00036   // Methods that are safe to call without synchronization primitives
00037   // from either thread.
00038   INLINE int size() const;
00039 
00040   // Methods that are safe to call without synchronization primitives
00041   // only from the reader thread.
00042   INLINE bool empty() const;
00043 
00044   INLINE const Thing &front() const;
00045   INLINE Thing &front();
00046   INLINE void pop_front();
00047 
00048   INLINE const Thing &operator[] (int n) const;
00049   INLINE Thing &operator[] (int n);
00050 
00051   // Methods that are safe to call without synchronization primitives
00052   // only from the writer thread.
00053   INLINE bool full() const;
00054 
00055   INLINE const Thing &back() const;
00056   INLINE Thing &back();
00057   INLINE void push_back(const Thing &t);
00058 
00059   INLINE void clear();
00060 
00061 private:
00062   Thing _array[max_size+1];
00063   int _in, _out;
00064 };
00065 
00066 #include "circBuffer.I"
00067 
00068 #endif
 All Classes Functions Variables Enumerations