home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 February / Chip_2001-02_cd1.bin / bonus / demos / CS / exp / SOURCES / GLENGINE / switchers.h < prev    next >
C/C++ Source or Header  |  2000-07-16  |  1KB  |  50 lines

  1. /*
  2.  * Switchers
  3.  *
  4.  * Switching mechanisms, used to switch between static
  5.  * variables (in the sense of their state time dependen-
  6.  * cy) and dynamic variables (as defined in dynamic.h).
  7.  */
  8.  
  9. #ifndef __OGL2_SWITCHERS__
  10. #define __OGL2_SWITCHERS__
  11.  
  12. #include "dynamic.h"
  13.  
  14. extern "C++" {
  15.  
  16. template <class __dtype, class __type>
  17. class DynamicSwitch {
  18.   Dynamic<__dtype, __type> *dyn;
  19.   __dtype st;
  20.   bool static_active;
  21.   bool strong_bind;
  22. public:
  23.   DynamicSwitch () : static_active(true), strong_bind(false) {}
  24.   ~DynamicSwitch () {
  25.     if(strong_bind) delete dyn;
  26.   }
  27.   __dtype Val (__type time = 0.0) {
  28.     if(static_active) return st;
  29.     return dyn->operator()(time);
  30.   }
  31.   void operator () (const __dtype& var) {
  32.     if(strong_bind) {
  33.       delete dyn;
  34.       strong_bind = false;
  35.     }
  36.     static_active = true;
  37.     st = var;
  38.   }
  39.   void operator () (Dynamic<__dtype, __type> *ptr, bool strong = false) {
  40.     if(strong_bind) delete dyn;
  41.     static_active = false;
  42.     dyn = ptr;
  43.     strong_bind = strong;
  44.   }
  45. };
  46.  
  47. } // extern "C++"
  48.  
  49. #endif
  50.