Async.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00019 #ifndef COIL_ASYNC_H
00020 #define COIL_ASYNC_H
00021 
00022 #include <coil/Task.h>
00023 #include <coil/Guard.h>
00024 #include <iostream>
00025 
00026 namespace coil
00027 {
00041   class Async
00042     : public coil::Task
00043   {
00044   public:
00060     Async() {}
00061 
00077     virtual ~Async(){}
00078 
00094     virtual void invoke() = 0;
00095 
00115     virtual bool finished() = 0;
00116   };
00117   
00131   template <typename Object, typename Func>
00132   class Async_t
00133     : public Async
00134   {
00135   public:
00136 
00160     Async_t(Object* obj, Func func, bool auto_delete = false)
00161       : m_obj(obj), m_func(func), m_finished(false), m_autodelete(auto_delete)
00162     {
00163     }
00164 
00180     virtual ~Async_t()
00181     {
00182     }
00183     
00203     virtual int svc()
00204     {
00205       m_func(m_obj);
00206       {
00207         Guard<Mutex> guard(m_mutex);
00208         m_finished = true;
00209       }
00210       
00211       return 0;
00212     }
00213 
00229     virtual void finalize()
00230     {
00231       Task::finalize();
00232       if (m_autodelete) delete this;
00233     }
00234 
00250     virtual void invoke()
00251     {
00252       activate();
00253     }
00254 
00274     virtual bool finished()
00275     {
00276       Guard<Mutex> guard(m_mutex);
00277       return m_finished;
00278     }
00279   private:
00280     Object* m_obj;
00281     Func m_func;
00282     bool m_finished;
00283     const bool m_autodelete;
00284     Mutex m_mutex;
00285   };
00286   
00300   template <typename Object, typename Func>
00301   class Async_ref_t
00302     : public Async
00303   {
00304   public:
00305 
00329     Async_ref_t(Object* obj, Func& func, bool auto_delete = false)
00330       : m_obj(obj), m_func(func), m_finished(false), m_autodelete(auto_delete)
00331     {
00332     }
00333 
00349     virtual ~Async_ref_t()
00350     {
00351     }
00352     
00372     virtual int svc()
00373     {
00374       m_func(m_obj);
00375       m_finished = true;
00376       return 0;
00377     }
00378 
00394     virtual void invoke()
00395     {
00396       activate();
00397     }
00398 
00418     virtual bool finished()
00419     {
00420       return m_finished;
00421     }
00422 
00438     virtual void finalize()
00439     {
00440       Task::finalize();
00441       if (m_autodelete) delete this;
00442     }
00443   private:
00444     Object* m_obj;
00445     Func& m_func;
00446     bool m_finished;
00447     bool m_autodelete;
00448     
00449   };
00450   
00548   template <typename Object, typename Func>
00549   inline Async_t<Object, Func>*
00550   AsyncInvoker(Object* obj, Func func, bool auto_delete = false)
00551   {
00552     return new Async_t<Object, Func>(obj, func, auto_delete);
00553   }
00554 
00582   template <typename Object, typename Func>
00583   inline Async_ref_t<Object, Func>*
00584   AsyncInvoker(Object* obj, Func* func, bool auto_delete = false)
00585   {
00586     return new Async_ref_t<Object, Func>(obj, *func, auto_delete);
00587   }
00588 
00589 
00590 };
00591 
00592 #endif // COIL_ASYNC_H


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sun Mar 26 2017 03:37:14