UniSet  2.6.0
LT_Object.h
00001 /*
00002  * Copyright (c) 2015 Pavel Vainerman.
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU Lesser General Public License as
00006  * published by the Free Software Foundation, version 2.1.
00007  *
00008  * This program is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00011  * Lesser General Lesser Public License for more details.
00012  *
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00015  */
00016 // --------------------------------------------------------------------------
00020 //---------------------------------------------------------------------------
00021 #ifndef Object_LT_H_
00022 #define Object_LT_H_
00023 //--------------------------------------------------------------------------
00024 #include <deque>
00025 #include "UniSetTypes.h"
00026 #include "MessageType.h"
00027 #include "PassiveTimer.h"
00028 #include "Exceptions.h"
00029 //---------------------------------------------------------------------------
00030 namespace uniset
00031 {
00032 class UniSetObject;
00033 
00034 //---------------------------------------------------------------------------
00096 class LT_Object
00097 {
00098     public:
00099         LT_Object();
00100         virtual ~LT_Object();
00101 
00102 
00110         virtual timeout_t askTimer( uniset::TimerId timerid, timeout_t timeMS, clock_t ticks = -1,
00111                                     uniset::Message::Priority p = uniset::Message::High );
00112 
00113 
00119         timeout_t checkTimers( UniSetObject* obj );
00120 
00122         //inline timeout_t getSleepTimeMS(){ return sleepTime; }
00123 
00124 
00129         timeout_t getTimeInterval( uniset::TimerId timerid );
00130 
00135         timeout_t getTimeLeft( uniset::TimerId timerid );
00136 
00137     protected:
00138 
00140         virtual std::string getTimerName( int id );
00141 
00143         struct TimerInfo
00144         {
00145             TimerInfo() {};
00146             TimerInfo( uniset::TimerId id, timeout_t timeMS, clock_t cnt, uniset::Message::Priority p ):
00147                 id(id),
00148                 curTimeMS(timeMS),
00149                 priority(p),
00150                 curTick(cnt - 1)
00151             {
00152                 tmr.setTiming(timeMS);
00153             };
00154 
00155             inline void reset()
00156             {
00157                 curTimeMS = tmr.getInterval();
00158                 tmr.reset();
00159             }
00160 
00161             uniset::TimerId id = { 0 };    
00162             timeout_t curTimeMS = { 0 };        
00163             uniset::Message::Priority priority = { uniset::Message::High }; 
00169             clock_t curTick = { -1 };
00170 
00171             // таймер с меньшим временем ожидания имеет больший приоритет
00172             bool operator < ( const TimerInfo& ti ) const
00173             {
00174                 return curTimeMS > ti.curTimeMS;
00175             }
00176 
00177             PassiveTimer tmr;
00178         };
00179 
00180         class Timer_eq: public std::unary_function<TimerInfo, bool>
00181         {
00182             public:
00183                 Timer_eq(uniset::TimerId t): tid(t) {}
00184 
00185                 inline bool operator()(const TimerInfo& ti) const
00186                 {
00187                     return ( ti.id == tid );
00188                 }
00189 
00190             protected:
00191                 uniset::TimerId tid;
00192         };
00193 
00194         typedef std::deque<TimerInfo> TimersList;
00195 
00196         timeout_t sleepTime; 
00198         TimersList getTimersList();
00199 
00200     private:
00201         TimersList tlst;
00202 
00204         uniset::uniset_rwmutex lstMutex;
00205         PassiveTimer tmLast;
00206 };
00207 // -------------------------------------------------------------------------
00208 } // end of uniset namespace
00209 //--------------------------------------------------------------------------
00210 #endif