00001 /*======================================================================= 00002 * Copyright 1991-1996, Silicon Graphics, Inc. 00003 * ALL RIGHTS RESERVED 00004 * 00005 * UNPUBLISHED -- Rights reserved under the copyright laws of the United 00006 * States. Use of a copyright notice is precautionary only and does not 00007 * imply publication or disclosure. 00008 * 00009 * U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND: 00010 * Use, duplication or disclosure by the Government is subject to restrictions 00011 * as set forth in FAR 52.227.19(c)(2) or subparagraph (c)(1)(ii) of the Rights 00012 * in Technical Data and Computer Software clause at DFARS 252.227-7013 and/or 00013 * in similar or successor clauses in the FAR, or the DOD or NASA FAR 00014 * Supplement. Contractor/manufacturer is Silicon Graphics, Inc., 00015 * 2011 N. Shoreline Blvd. Mountain View, CA 94039-7311. 00016 * 00017 * THE CONTENT OF THIS WORK CONTAINS CONFIDENTIAL AND PROPRIETARY 00018 * INFORMATION OF SILICON GRAPHICS, INC. ANY DUPLICATION, MODIFICATION, 00019 * DISTRIBUTION, OR DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS STRICTLY 00020 * PROHIBITED WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF SILICON 00021 * GRAPHICS, INC. 00022 **=======================================================================*/ 00023 /*======================================================================= 00024 ** Author : Nick Thompson (MMM yyyy) 00025 ** Modified by : Paul Strauss (MMM yyyy) 00026 ** Modified by : Gavin Bell (MMM yyyy) 00027 **=======================================================================*/ 00028 /*======================================================================= 00029 *** THE CONTENT OF THIS WORK IS PROPRIETARY TO FEI S.A.S, (FEI S.A.S.), *** 00030 *** AND IS DISTRIBUTED UNDER A LICENSE AGREEMENT. *** 00031 *** *** 00032 *** REPRODUCTION, DISCLOSURE, OR USE, IN WHOLE OR IN PART, OTHER THAN AS *** 00033 *** SPECIFIED IN THE LICENSE ARE NOT TO BE UNDERTAKEN EXCEPT WITH PRIOR *** 00034 *** WRITTEN AUTHORIZATION OF FEI S.A.S. *** 00035 *** *** 00036 *** RESTRICTED RIGHTS LEGEND *** 00037 *** USE, DUPLICATION, OR DISCLOSURE BY THE GOVERNMENT OF THE CONTENT OF THIS *** 00038 *** WORK OR RELATED DOCUMENTATION IS SUBJECT TO RESTRICTIONS AS SET FORTH IN *** 00039 *** SUBPARAGRAPH (C)(1) OF THE COMMERCIAL COMPUTER SOFTWARE RESTRICTED RIGHT *** 00040 *** CLAUSE AT FAR 52.227-19 OR SUBPARAGRAPH (C)(1)(II) OF THE RIGHTS IN *** 00041 *** TECHNICAL DATA AND COMPUTER SOFTWARE CLAUSE AT DFARS 52.227-7013. *** 00042 *** *** 00043 *** COPYRIGHT (C) 1996-2014 BY FEI S.A.S, *** 00044 *** BORDEAUX, FRANCE *** 00045 *** ALL RIGHTS RESERVED *** 00046 **=======================================================================*/ 00047 /*======================================================================= 00048 ** Modified by : VSG (MMM YYYY) 00049 **=======================================================================*/ 00050 00051 00052 #ifndef _SO_SENSOR_ 00053 #define _SO_SENSOR_ 00054 00055 #include <Inventor/SbBasic.h> 00056 #include <Inventor/threads/SbThreadSpinlock.h> 00057 00058 class SoField; 00059 class SoMField; 00060 class SoSensor; 00061 00065 typedef void SoSensorCB(void *data, SoSensor *sensor); 00066 00068 // 00069 // Class: SoSensor 00070 // 00071 // Abstract base class for all sensors. Defines basic callback 00072 // definition (explicit or in constructor) and scheduling and 00073 // triggering methods. 00074 // 00076 00097 class SoSensor { 00098 00099 public: 00100 00104 SoSensor() 00105 { 00106 func = NULL; 00107 funcData = NULL; 00108 #ifdef _WIN32 00109 dwThreadId=GetCurrentThreadId(); 00110 #endif 00111 m_schedule = FALSE; 00112 } 00117 SoSensor(SoSensorCB *f, void *d) 00118 { 00119 func = f; 00120 funcData = d; 00121 #ifdef _WIN32 00122 dwThreadId=GetCurrentThreadId(); 00123 #endif 00124 m_schedule = FALSE; 00125 } 00126 00127 // Virtual destructor so that subclasses are deleted properly 00128 #ifndef HIDDEN_FROM_DOC 00129 virtual ~SoSensor(); 00130 #endif // HIDDEN_FROM_DOC 00131 00135 inline void setFunction(SoSensorCB *f, void *userData); 00136 00144 void setFunction(SoSensorCB *f) 00145 { 00146 func = f; 00147 #ifdef _WIN32 00148 dwThreadId=GetCurrentThreadId(); 00149 #endif 00150 00151 } 00152 00157 void setData(void *d) { funcData = d; } 00158 00163 SoSensorCB * getFunction() const { return func; } 00164 00169 void * getData() const { return funcData; } 00170 00171 // Schedules the sensor for triggering at the appropriate time 00172 virtual void schedule() = 0; 00173 00174 // Unschedules sensor to keep it from being triggered 00175 virtual void unschedule() = 0; 00176 00182 virtual SbBool isScheduled() const 00183 { return m_schedule; } 00184 00185 private: 00186 00187 // take critical section ownership 00188 inline void lock() 00189 { m_mutex.lock(); } 00190 00191 // release critical section ownership 00192 inline void unlock() 00193 { m_mutex.unlock(); } 00194 00195 00196 // This must be called only by the SensorManager when it add/remove the sensor from its queues. 00197 // with the queue locked (in Multithread) to be ensure that this flag reflect the real state 00198 // of the sensor. 00199 inline void setScheduleFlag(SbBool flag) 00200 { m_schedule = flag; }; 00201 00202 #ifdef _WIN32 00203 DWORD getThreadId() { return dwThreadId;}; 00204 void setThreadId(DWORD id) { dwThreadId=id;}; 00205 #endif 00206 00207 // Initialize static members, etc. 00208 static void initClass(); 00209 00210 // Triggers the sensor, calling its callback function 00211 virtual void trigger(); 00212 00213 // This returns TRUE if this sensor should precede sensor s in 00214 // whichever queue this sensor would be in. 00215 virtual SbBool isBefore(const SoSensor *s) const = 0; 00216 00217 // Sets/returns the next sensor in whichever queue the sensor is in 00218 void setNextInQueue(SoSensor *next) { nextInQueue = next; } 00219 SoSensor * getNextInQueue() const { return nextInQueue; } 00220 00221 private: 00222 SoSensorCB * func; // Callback function 00223 void * funcData; // Data to pass to callback 00224 00225 #ifdef _WIN32 00226 DWORD dwThreadId; // thread to which this sensor belongs 00227 00228 private: 00229 #else 00230 private: 00231 #endif 00232 SoSensor *nextInQueue; // Next sensor in queue 00233 private: 00234 SbBool m_schedule; // Whether sensor is scheduled in a queue or not. 00235 SbThreadSpinlock m_mutex; 00236 }; 00237 00238 void SoSensor::setFunction(SoSensorCB *f, void *userData) 00239 { 00240 setFunction(f); 00241 setData(userData); 00242 } 00243 00244 #endif /* _SO_SENSOR_ */ 00245 00246 00247