00001
00002 #ifndef __DAQpp_Monitor_h__
00003 #define __DAQpp_Monitor_h__
00004 #include <queue>
00005 #include <memory>
00006 #include <DAQ++/Thread.h>
00007
00008 namespace DAQpp
00009 {
00020 class Event
00021 {
00022 private:
00024 int _size;
00026 char *_data;
00027
00029 void cpy(const Event &e);
00030
00032 static std::allocator<Event> event_allocator;
00034 static std::allocator<char> data_allocator;
00035
00036 void free_data()
00037 {
00038 data_allocator.deallocate(_data, _size);
00039 _data = 0;
00040 }
00041 public:
00043 Event(int s, const char *d);
00045 Event(const Event &e) : _size(0), _data(0)
00046 {
00047 cpy(e);
00048 }
00050 Event &operator=(const Event &e)
00051 {
00052 if ( &e != this ) cpy(e);
00053 return *this;
00054 }
00055
00057 ~Event();
00058
00060 int size() const
00061 {
00062 return _size;
00063 }
00065 const char *data() const
00066 {
00067 return _data;
00068 }
00069
00071 inline void* operator new(size_t);
00072
00073 inline void operator delete(void*);
00074
00075 };
00076
00077
00078
00079 inline void* Event::operator new(size_t)
00080 {
00081 void* anEvent;
00082 anEvent = event_allocator.allocate(1);
00083 return anEvent;
00084 }
00085
00086
00087 inline void Event::operator delete(void* anEvent)
00088 {
00089 event_allocator.deallocate((Event *)anEvent, 1);
00090 }
00091
00115 class Monitor : public std::queue<Event *>, public Thread, public Condition
00116 {
00117 private:
00119 unsigned long mxsize;
00121 Mutex mtx;
00122
00123 public:
00127 static Event *MonitorDie;
00132 static Event *MonitorTimeOut;
00137 Monitor(int sz = 50);
00139 ~Monitor();
00140
00149 void push_event( Event *ev );
00150
00170 Event *get_event(int timeout = -1);
00171
00173 bool condition()
00174 {
00175 return !empty();
00176 }
00177
00179 void set_max_size(unsigned long s)
00180 {
00181 mxsize = s;
00182 }
00183
00185 unsigned long get_max_size() const
00186 {
00187 return mxsize;
00188 }
00189
00191 bool full()
00192 {
00193 return size() > mxsize;
00194 }
00195
00197 void clean();
00198
00204 void start_monitor();
00205
00207 void stop_monitor();
00208
00209 };
00210
00211 }
00212
00213 #endif