AsyncTask.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include "AsyncTask.h"
  2. #include "core/oxygine.h"
  3. #include <typeinfo>
  4. namespace oxygine
  5. {
  6. AsyncTask::AsyncTask() : _status(status_not_started), _mainThreadSync(false)
  7. {
  8. }
  9. AsyncTask::~AsyncTask()
  10. {
  11. }
  12. void AsyncTask::run()
  13. {
  14. _prerun();
  15. log::messageln("AsyncTask::run %d - %s", getObjectID(), typeid(*this).name());
  16. OX_ASSERT(_status == status_not_started);
  17. _status = status_inprogress;
  18. sync([ = ]()
  19. {
  20. _run();
  21. });
  22. }
  23. void AsyncTask::_complete()
  24. {
  25. log::messageln("AsyncTask::_complete %d - %s", getObjectID(), typeid(*this).name());
  26. _status = status_completed;
  27. _onFinal(false);
  28. _finalize(false);
  29. _onComplete();
  30. _dispatchComplete();
  31. }
  32. void AsyncTask::_dispatchComplete()
  33. {
  34. AsyncTaskEvent ev(COMPLETE, this);
  35. dispatchEvent(&ev);
  36. }
  37. void AsyncTask::_error()
  38. {
  39. log::messageln("AsyncTask::_error %d - %s", getObjectID(), typeid(*this).name());
  40. _status = status_failed;
  41. _onFinal(true);
  42. _finalize(true);
  43. _onError();
  44. AsyncTaskEvent ev(ERROR, this);
  45. dispatchEvent(&ev);
  46. }
  47. void AsyncTask::onComplete()
  48. {
  49. sync([ = ]()
  50. {
  51. _complete();
  52. });
  53. }
  54. void AsyncTask::onError()
  55. {
  56. sync([ = ]()
  57. {
  58. _error();
  59. });
  60. }
  61. }