My Project
 All Classes Files Functions Variables Enumerations Enumerator Friends Macros Pages
DoubleBuffer.h
Go to the documentation of this file.
1 // $Id: DoubleBuffer.h,v 1.1.1.1 2009/07/06 23:57:25 eml Exp $
2 
3 #ifndef GCP_UTIL_DOUBLEBUFFER_H
4 #define GCP_UTIL_DOUBLEBUFFER_H
5 
16 #include <iostream>
17 
18 #include "gcp/util/common/Mutex.h"
19 
20 namespace gcp {
21 
22  namespace util {
23 
24  class DoubleBuffer {
25  public:
26 
27  //-----------------------------------------------------------------------
28  // A struct for associated a buffer with a lock
29  //-----------------------------------------------------------------------
30 
31  struct BufferLock {
32 
33  // public members
34 
35  void* data_;
36  gcp::util::Mutex lock_;
37 
38  // Constructor
39 
40  BufferLock() {
41  data_ = 0;
42  }
43 
44  void lock() {
45  lock_.lock();
46  }
47 
48  void unlock() {
49  lock_.unlock();
50  }
51 
52  };
53 
54  //-----------------------------------------------------------------------
55  // Methods of DoubleBuffer class
56  //-----------------------------------------------------------------------
57 
61  DoubleBuffer();
62 
66  void operator=(const DoubleBuffer& objToBeAssigned);
67 
71  void operator=(DoubleBuffer& objToBeAssigned);
72 
76  friend std::ostream& operator<<(std::ostream& os, DoubleBuffer& obj);
77 
81  virtual ~DoubleBuffer();
82 
83  //-----------------------------------------------------------------------
84  // Raw methods for access the buffers
85 
86  // Get the current read buffer
87 
88  void* grabReadBuffer();
89  void releaseReadBuffer();
90 
91  // Get the current write buffer
92 
93  void* grabWriteBuffer();
94  void releaseWriteBuffer();
95 
96  void switchBuffers();
97 
98  protected:
99 
100  BufferLock buf1_;
101  BufferLock buf2_;
102 
103  BufferLock* readBuf_;
104  BufferLock* writeBuf_;
105 
106  }; // End class DoubleBuffer
107 
108  } // End namespace util
109 } // End namespace gcp
110 
111 
112 
113 #endif // End #ifndef GCP_UTIL_DOUBLEBUFFER_H
void unlock()
Definition: Mutex.cc:104
void releaseWriteBuffer()
Definition: DoubleBuffer.cc:86
virtual ~DoubleBuffer()
Definition: DoubleBuffer.cc:51
void * grabReadBuffer()
Definition: DoubleBuffer.cc:60
void lock()
Definition: Mutex.cc:72
Definition: DoubleBuffer.h:31
Definition: Mutex.h:16
friend std::ostream & operator<<(std::ostream &os, DoubleBuffer &obj)
void switchBuffers()
Definition: DoubleBuffer.cc:94
Definition: DoubleBuffer.h:24
void * grabWriteBuffer()
Definition: DoubleBuffer.cc:69
DoubleBuffer()
Definition: DoubleBuffer.cc:12
void operator=(const DoubleBuffer &objToBeAssigned)
Definition: DoubleBuffer.cc:25
void releaseReadBuffer()
Definition: DoubleBuffer.cc:78