Async.h
Go to the documentation of this file.
1 // -*- C++ -*-
19 #ifndef COIL_ASYNC_H
20 #define COIL_ASYNC_H
21 
22 #include <coil/Task.h>
23 #include <coil/Guard.h>
24 #include <iostream>
25 
26 namespace coil
27 {
41  class Async
42  : public coil::Task
43  {
44  public:
60  Async() {}
61 
77  virtual ~Async(){}
78 
94  virtual void invoke() = 0;
95 
115  virtual bool finished() = 0;
116  };
117 
131  template <typename Object, typename Func>
132  class Async_t
133  : public Async
134  {
135  public:
136 
160  Async_t(Object* obj, Func func, bool auto_delete = false)
161  : m_obj(obj), m_func(func), m_finished(false), m_autodelete(auto_delete)
162  {
163  }
164 
180  virtual ~Async_t()
181  {
182  }
183 
203  virtual int svc()
204  {
205  m_func(m_obj);
206  {
207  Guard<Mutex> guard(m_mutex);
208  m_finished = true;
209  }
210 
211  return 0;
212  }
213 
229  virtual void finalize()
230  {
231  Task::finalize();
232  if (m_autodelete) delete this;
233  }
234 
250  virtual void invoke()
251  {
252  activate();
253  }
254 
274  virtual bool finished()
275  {
276  Guard<Mutex> guard(m_mutex);
277  return m_finished;
278  }
279  private:
280  Object* m_obj;
281  Func m_func;
283  const bool m_autodelete;
285  };
286 
300  template <typename Object, typename Func>
302  : public Async
303  {
304  public:
305 
329  Async_ref_t(Object* obj, Func& func, bool auto_delete = false)
330  : m_obj(obj), m_func(func), m_finished(false), m_autodelete(auto_delete)
331  {
332  }
333 
349  virtual ~Async_ref_t()
350  {
351  }
352 
372  virtual int svc()
373  {
374  m_func(m_obj);
375  m_finished = true;
376  return 0;
377  }
378 
394  virtual void invoke()
395  {
396  activate();
397  }
398 
418  virtual bool finished()
419  {
420  return m_finished;
421  }
422 
438  virtual void finalize()
439  {
440  Task::finalize();
441  if (m_autodelete) delete this;
442  }
443  private:
444  Object* m_obj;
445  Func& m_func;
448 
449  };
450 
548  template <typename Object, typename Func>
549  inline Async_t<Object, Func>*
550  AsyncInvoker(Object* obj, Func func, bool auto_delete = false)
551  {
552  return new Async_t<Object, Func>(obj, func, auto_delete);
553  }
554 
582  template <typename Object, typename Func>
584  AsyncInvoker(Object* obj, Func* func, bool auto_delete = false)
585  {
586  return new Async_ref_t<Object, Func>(obj, *func, auto_delete);
587  }
588 
589 
590 };
591 
592 #endif // COIL_ASYNC_H
virtual void invoke()
Asynchronous function Activation.
Definition: Async.h:394
virtual bool finished()=0
Check on completion state.
const bool m_autodelete
Definition: Async.h:283
virtual ~Async_t()
Destructor.
Definition: Async.h:180
Async_ref_t template class.
Definition: Async.h:301
virtual bool finished()
Check on completion state.
Definition: Async.h:274
Func & m_func
Definition: Async.h:445
virtual bool finished()
Check on completion state.
Definition: Async.h:418
virtual void invoke()
Asynchronous function Activation.
Definition: Async.h:250
virtual void finalize()
Finalize the asynchronous function.
Definition: Async.h:229
Mutex m_mutex
Definition: Async.h:284
bool m_finished
Definition: Async.h:282
virtual ~Async()
Destructor.
Definition: Async.h:77
Async_t template class.
Definition: Async.h:132
virtual void invoke()=0
Asynchronous invocation.
Async()
Constructor.
Definition: Async.h:60
Func m_func
Definition: Async.h:281
Async_ref_t(Object *obj, Func &func, bool auto_delete=false)
Constructor.
Definition: Async.h:329
virtual void finalize()
Finalize the asynchronous function.
Definition: Async.h:438
Async_t(Object *obj, Func func, bool auto_delete=false)
Constructor.
Definition: Async.h:160
Task class.
Async class.
Definition: Async.h:41
bool m_autodelete
Definition: Async.h:447
Guard template class.
Definition: Guard.h:41
Object * m_obj
Definition: Async.h:280
Object * m_obj
Definition: Async.h:444
virtual int svc()
Thread execution function for asynchronous invoke.
Definition: Async.h:372
virtual ~Async_ref_t()
Destructor.
Definition: Async.h:349
virtual int svc()
Thread execution function for asynchronous invoke.
Definition: Async.h:203
virtual void activate()
Create a thread.
bool m_finished
Definition: Async.h:446
virtual void finalize()
Finalizing the task.
Async_t< Object, Func > * AsyncInvoker(Object *obj, Func func, bool auto_delete=false)
Helper function for async member function summons.
Definition: Async.h:550
Common Object Interface Layer.


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Mon Feb 28 2022 23:00:42