Wiselib
wiselib.stable/external_interface/lorien/lorien_timer.h
Go to the documentation of this file.
00001 /***************************************************************************
00002  ** This file is part of the generic algorithm library Wiselib.           **
00003  ** Copyright (C) 2008,2009 by the Wisebed (www.wisebed.eu) project.      **
00004  **                                                                       **
00005  ** The Wiselib is free software: you can redistribute it and/or modify   **
00006  ** it under the terms of the GNU Lesser General Public License as        **
00007  ** published by the Free Software Foundation, either version 3 of the    **
00008  ** License, or (at your option) any later version.                       **
00009  **                                                                       **
00010  ** The Wiselib is distributed in the hope that it will be useful,        **
00011  ** but WITHOUT ANY WARRANTY; without even the implied warranty of        **
00012  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         **
00013  ** GNU Lesser General Public License for more details.                   **
00014  **                                                                       **
00015  ** You should have received a copy of the GNU Lesser General Public      **
00016  ** License along with the Wiselib.                                       **
00017  ** If not, see <http://www.gnu.org/licenses/>.                           **
00018  ***************************************************************************/
00019 #ifndef CONNECTOR_LORIEN_TIMER_H
00020 #define CONNECTOR_LORIEN_TIMER_H
00021 
00022 #include "external_interface/lorien/lorien_types.h"
00023 #include "util/delegates/delegate.hpp"
00024 #include "util/pstl/vector_static.h"
00025 extern "C" {
00026 #include "lorien.h"
00027 }
00028 
00029 namespace wiselib
00030 {
00031 
00032    namespace lorien
00033    {
00034       typedef delegate1<void, void*> timer_delegate_t;
00035 
00036       struct TimerItem
00037       {
00038          timer_delegate_t cb;
00039          void *userdata;
00040       };
00041    }
00042 
00050    template<typename OsModel_P>
00051    class LorienTimerModel
00052    {
00053    public:
00054       typedef OsModel_P OsModel;
00055 
00056       typedef LorienTimerModel<OsModel> self_type;
00057       typedef self_type* self_pointer_t;
00058 
00059       typedef vector_static<OsModel, lorien::TimerItem, 10> TimerItemVector;
00060       typedef typename TimerItemVector::iterator TimerItemVectorIterator;
00061 
00062       typedef uint32_t millis_t;
00063       // --------------------------------------------------------------------
00064       enum ErrorCodes
00065       {
00066          SUCCESS = OsModel::SUCCESS,
00067          ERR_UNSPEC = OsModel::ERR_UNSPEC
00068       };
00069       // -----------------------------------------------------------------
00070       LorienTimerModel()
00071       {
00072       }
00073       // -----------------------------------------------------------------
00074       void init( Component *comp )
00075       {
00076          comp_ = comp;
00077          if ( timer_items_.empty() )
00078             timer_items_.assign( 10, lorien::TimerItem() );
00079       }
00080       // --------------------------------------------------------------------
00081       template<typename T, void (T::*TMethod)(void*)>
00082       int set_timer(millis_t millis, T *obj_pnt, void *userdata )
00083       {
00084          lorien::TimerItem *item = free_timer_item();
00085          if (!item)
00086             {
00087             return ERR_UNSPEC;
00088             }
00089 
00090          item->cb = lorien::timer_delegate_t::from_method<T, TMethod>( obj_pnt );
00091          item->userdata = userdata;
00092 
00093          ((ITimer*) ((LXState*) comp_ -> state) -> timer -> userRecp) -> setTimer(((LXState*) comp_ -> state) -> timer -> ifHostComponent, comp_, millis, item);
00094          return SUCCESS;
00095       }
00096       // --------------------------------------------------------------------
00097       static void timer_elapsed( void *ptr )
00098       {
00099          lorien::TimerItem *item = (lorien::TimerItem*)ptr;
00100          item->cb( item->userdata );
00101 
00102          item->cb = lorien::timer_delegate_t();
00103       }
00104 
00105    private:
00106       lorien::TimerItem* free_timer_item()
00107       {
00108          for ( TimerItemVectorIterator
00109                   it = timer_items_.begin();
00110                   it != timer_items_.end();
00111                   ++it )
00112          {
00113             if ( !(it->cb) )
00114                return &(*it);
00115          }
00116 
00117          return 0;
00118       }
00119 
00120       Component *comp_;
00121       TimerItemVector timer_items_;
00122    };
00123 }
00124 
00125 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines