00001 /* -*- mode: c++ -*- */ 00002 #ifndef __DAQpp__Module_h__ 00003 #define __DAQpp__Module_h__ 00004 00005 #include <map> 00006 #include <DAQ++/DAQObject.h> 00007 #include <DAQ++/RunCommand.h> 00008 00009 namespace DAQpp 00010 { 00048 class Module : public DAQObject, public Observer 00049 { 00050 private: 00057 RunCommand *cmd; 00061 bool local; 00062 00069 void init(); 00076 void update(Observable *o, Argument *arg); 00077 00093 static std::allocator<char> data_allocator; 00094 00101 static std::map<void *, int> allocator_map; 00102 public: 00106 Module(const DAQid &id); 00107 00109 virtual ~Module(); 00110 00111 // operations 00113 00115 virtual int PrepareForRun() = 0; 00117 virtual int Start(int mode = 0) = 0; 00119 virtual int Stop(int mode = 0) = 0; 00121 virtual int Pause() = 0; 00123 virtual int Continue() = 0; 00125 virtual int Reset() = 0; 00127 virtual int GetReady() = 0; 00129 virtual int Trigger() = 0; 00131 virtual void flush() = 0; 00133 virtual bool hasData() = 0; 00135 00145 virtual char *getData(int &size) = 0; 00146 00153 static char *allocate_real(int size) 00154 { 00155 char *ptr = data_allocator.allocate(size); 00156 allocator_map[(void *)ptr] = size; 00157 return ptr; 00158 } 00159 00164 static void deallocate_real(char *ptr) 00165 { 00166 std::map<void *, int>::iterator ip; 00167 ip = allocator_map.find( (void *)ptr); 00168 if (ip != allocator_map.end()) 00169 data_allocator.deallocate((char *)ip->first, ip->second); 00170 } 00171 00180 virtual char *allocate(int size) 00181 { 00182 return allocate_real(size); 00183 } 00184 00193 virtual void deallocate(char *ptr) 00194 { 00195 deallocate_real(ptr); 00196 } 00197 00199 bool is_global() 00200 { 00201 return !local; 00202 } 00203 00207 void in_global(RunCommand *rc = 0) 00208 { 00209 if ( rc ) 00210 RegisterDAQ(rc); 00211 else 00212 { 00213 if ( cmd ) 00214 RegisterDAQ(cmd); 00215 } 00216 local = false; 00217 } 00218 00220 bool is_local() 00221 { 00222 return local; 00223 } 00229 void in_local() 00230 { 00231 if ( cmd ) 00232 UnRegisterDAQ(); 00233 local = true; 00234 } 00235 00240 void RegisterDAQ(RunCommand *rc) 00241 { 00242 if ( rc ) 00243 { 00244 cmd = rc; 00245 rc->addObserver(*this); 00246 } 00247 } 00252 void UnRegisterDAQ(bool reset = false) 00253 { 00254 if ( cmd ) 00255 { 00256 cmd->deleteObserver(*this); 00257 if (reset) 00258 cmd = 0; 00259 } 00260 } 00261 }; 00262 } 00263 00264 00265 #endif 00266 00267 00268