inlib  1.2.0
/Users/barrand/private/dev/softinex/old/inexlib-1.2/inlib/inlib/sys/base_timer
Go to the documentation of this file.
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
00002 // See the file inlib.license for terms.
00003 
00004 #ifndef inlib_sys_base_timer
00005 #define inlib_sys_base_timer
00006 
00007 #include "atime"
00008 
00009 namespace inlib {
00010 
00011 class base_timer {
00012 public:
00013   virtual void action() = 0;
00014 public:
00015   base_timer(unsigned int a_interval)
00016   :m_interval(a_interval)
00017   ,m_started(false)
00018   ,m_prev(atime(0,0))
00019   {}
00020   virtual ~base_timer(){}
00021 public:
00022   base_timer(const base_timer& a_from)
00023   :m_interval(a_from.m_interval)
00024   ,m_started(false)
00025   ,m_prev(atime(0,0))
00026   {}
00027   base_timer& operator=(const base_timer& a_from){
00028     m_interval = a_from.m_interval;
00029     m_started = false;
00030     m_prev = atime(0,0);
00031     return *this;
00032   }
00033 public:
00034   void set_interval(unsigned int a_interval) {m_interval = a_interval;}
00035   unsigned int interval() const {return m_interval;}
00036   void start() {
00037     m_started = true;
00038     m_prev = atime::now();
00039   }
00040   void stop() {
00041     m_started = false;
00042   }
00043   bool active() const {return m_started;}
00044   void check_time_out() {
00045     if(!m_started) return;
00046     atime elaps = atime::elapsed(m_prev);
00047     atime::num_t secs = elaps.seconds();
00048     atime::num_t micro_secs = elaps.micro_seconds();
00049     atime::num_t milli_secs = secs*1000+micro_secs/1000;
00050     if((unsigned int)milli_secs>m_interval) {
00051       action();
00052       m_prev = atime::now();    
00053     }
00054   }  
00055 protected:
00056   unsigned int m_interval; //milliseconds.
00057   bool m_started;
00058   atime m_prev;
00059 };
00060 
00061 }
00062 
00063 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines