00001 00003 00004 // 00005 // Copyright (c) 1999-2004 California Institute of Technology 00006 // Copyright (c) 2004-2007 University of Southern California 00007 // Rob Peters <rjpeters at usc dot edu> 00008 // 00009 // created: Fri Jun 25 12:45:05 1999 00010 // commit: $Id: trialevent.h 10065 2007-04-12 05:54:56Z rjpeters $ 00011 // $HeadURL: file:///lab/rjpeters/svnrepo/code/trunk/groovx/src/visx/trialevent.h $ 00012 // 00013 // -------------------------------------------------------------------- 00014 // 00015 // This file is part of GroovX. 00016 // [http://ilab.usc.edu/rjpeters/groovx/] 00017 // 00018 // GroovX is free software; you can redistribute it and/or modify it 00019 // under the terms of the GNU General Public License as published by 00020 // the Free Software Foundation; either version 2 of the License, or 00021 // (at your option) any later version. 00022 // 00023 // GroovX is distributed in the hope that it will be useful, but 00024 // WITHOUT ANY WARRANTY; without even the implied warranty of 00025 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00026 // General Public License for more details. 00027 // 00028 // You should have received a copy of the GNU General Public License 00029 // along with GroovX; if not, write to the Free Software Foundation, 00030 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 00031 // 00033 00034 #ifndef GROOVX_VISX_TRIALEVENT_H_UTC20050626084015_DEFINED 00035 #define GROOVX_VISX_TRIALEVENT_H_UTC20050626084015_DEFINED 00036 00037 #include "io/io.h" 00038 00039 #include "nub/ref.h" 00040 #include "nub/timer.h" 00041 00042 #include "tcl-io/tclprocwrapper.h" 00043 00044 #include <vector> 00045 00046 namespace rutz 00047 { 00048 template <class T> class fwd_iter; 00049 template <class T> class shared_ptr; 00050 } 00051 00052 namespace nub 00053 { 00054 class scheduler; 00055 } 00056 00057 class output_file; 00058 class Trial; 00059 00060 // ####################################################### 00061 // ======================================================= 00062 00064 00074 class TrialEvent : public io::serializable 00075 { 00076 protected: 00078 TrialEvent(unsigned int msec); 00079 00080 public: 00082 virtual ~TrialEvent() throw(); 00083 00084 virtual void read_from(io::reader& reader); 00085 virtual void write_to(io::writer& writer) const; 00086 00088 unsigned int getDelay() const { return itsRequestedDelay; } 00089 00091 void setDelay(unsigned int msec) { itsRequestedDelay = msec; } 00092 00094 bool is_pending() const { return itsTimer.is_pending(); } 00095 00097 00111 unsigned int schedule(rutz::shared_ptr<nub::scheduler> s, 00112 Trial& trial, 00113 unsigned int minimum_msec = 0); 00114 00116 00120 void cancel(); 00121 00123 00127 virtual void invoke(Trial& trial) = 0; 00128 00129 private: 00130 void invokeTemplate(); 00131 00132 TrialEvent(const TrialEvent&); 00133 TrialEvent& operator=(const TrialEvent&); 00134 00135 nub::timer itsTimer; 00136 unsigned int itsRequestedDelay; 00137 Trial* itsTrial; 00138 00139 // Diagnostic stuff 00140 mutable double itsEstimatedOffset; 00141 mutable double itsTotalOffset; 00142 mutable double itsTotalError; 00143 mutable int itsInvokeCount; 00144 }; 00145 00146 00147 00148 // ####################################################### 00149 // ======================================================= 00150 00152 00154 class NullTrialEvent : public TrialEvent 00155 { 00156 protected: 00158 NullTrialEvent(unsigned int msec = 0); 00159 00161 virtual ~NullTrialEvent() throw(); 00162 00163 virtual void invoke(Trial&); 00164 00165 public: 00167 static NullTrialEvent* make() { return new NullTrialEvent; } 00168 }; 00169 00170 // ####################################################### 00171 // ======================================================= 00172 00174 class TrialMemFuncEvent : public TrialEvent 00175 { 00176 public: 00177 typedef void (Trial::* CallbackType)(); 00178 00180 virtual rutz::fstring obj_typename() const; 00181 00183 static TrialMemFuncEvent* make(CallbackType callback, 00184 const rutz::fstring& type, 00185 unsigned int msec = 0); 00186 00187 protected: 00189 TrialMemFuncEvent(CallbackType callback, 00190 const rutz::fstring& type, 00191 unsigned int msec = 0); 00192 00194 virtual ~TrialMemFuncEvent() throw(); 00195 00196 virtual void invoke(Trial& trial); 00197 00198 private: 00199 CallbackType itsCallback; 00200 rutz::fstring itsTypename; 00201 }; 00202 00203 // ####################################################### 00204 // ======================================================= 00205 00206 // Here's a set of creator functions that return different 00207 // TrialMemFuncEvent objects bound to different Trial member functions. 00208 00210 TrialEvent* makeAbortTrialEvent(); 00212 TrialEvent* makeDrawEvent(); 00214 TrialEvent* makeRenderEvent(); 00216 TrialEvent* makeEndTrialEvent(); 00218 TrialEvent* makeNextNodeEvent(); 00220 TrialEvent* makeAllowResponsesEvent(); 00222 TrialEvent* makeDenyResponsesEvent(); 00224 TrialEvent* makeUndrawEvent(); 00226 TrialEvent* makeRenderBackEvent(); 00228 TrialEvent* makeRenderFrontEvent(); 00230 TrialEvent* makeSwapBuffersEvent(); 00232 TrialEvent* makeClearBufferEvent(); 00234 TrialEvent* makeFinishDrawingEvent(); 00235 00236 00237 // ####################################################### 00238 // ======================================================= 00239 00241 class FileWriteEvent : public TrialEvent 00242 { 00243 protected: 00245 FileWriteEvent(unsigned int msec = 0); 00246 00248 virtual ~FileWriteEvent() throw(); 00249 00250 virtual void invoke(Trial& trial); 00251 00252 public: 00254 static FileWriteEvent* make() { return new FileWriteEvent; } 00255 00256 virtual void read_from(io::reader& reader); 00257 virtual void write_to(io::writer& writer) const; 00258 00260 int getByte() const; 00261 00263 void setByte(int b); 00264 00266 nub::ref<output_file> getFile() const; 00267 00269 void setFile(nub::ref<output_file> file); 00270 00271 private: 00272 nub::ref<output_file> itsFile; 00273 int itsByte; 00274 }; 00275 00276 // ####################################################### 00277 // ======================================================= 00278 00280 class GenericEvent : public TrialEvent 00281 { 00282 protected: 00284 GenericEvent(unsigned int msec = 0); 00285 00287 virtual ~GenericEvent() throw(); 00288 00289 virtual void invoke(Trial& trial); 00290 00291 public: 00293 static GenericEvent* make() { return new GenericEvent; } 00294 00295 virtual void read_from(io::reader& reader); 00296 virtual void write_to(io::writer& writer) const; 00297 00299 rutz::fstring getCallback() const; 00300 00302 void setCallback(const rutz::fstring& script); 00303 00304 private: 00305 nub::ref<tcl::ProcWrapper> itsCallback; 00306 }; 00307 00308 // ####################################################### 00309 // ======================================================= 00310 00312 00317 class MultiEvent : public TrialEvent 00318 { 00319 protected: 00321 MultiEvent(unsigned int msec = 0); 00322 00324 virtual ~MultiEvent() throw(); 00325 00326 virtual void invoke(Trial& trial); 00327 00328 public: 00330 static MultiEvent* make() { return new MultiEvent; } 00331 00332 virtual void read_from(io::reader& reader); 00333 virtual void write_to(io::writer& writer) const; 00334 00336 rutz::fwd_iter<const nub::ref<TrialEvent> > getEvents() const; 00337 00339 unsigned int addEvent(nub::ref<TrialEvent> event); 00340 00342 void eraseEventAt(unsigned int index); 00343 00345 void clearEvents(); 00346 00347 private: 00348 std::vector<nub::ref<TrialEvent> > itsEvents; 00349 }; 00350 00351 static const char __attribute__((used)) vcid_groovx_visx_trialevent_h_utc20050626084015[] = "$Id: trialevent.h 10065 2007-04-12 05:54:56Z rjpeters $ $HeadURL: file: 00352 #endif // !GROOVX_VISX_TRIALEVENT_H_UTC20050626084015_DEFINED