timer.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #pragma once
  2. #include "sol/sol.hpp"
  3. #include "imodule.h"
  4. #include "core/structs.h"
  5. #include "base/singleton.hpp"
  6. #include "util/thread.h"
  7. #include "util/queue.hpp"
  8. #include <map>
  9. #include <mutex>
  10. namespace module
  11. {
  12. struct timer_info {
  13. std::string filepath;
  14. std::string name;
  15. std::string funname;
  16. bool loop = false;
  17. timestamp exec_msec = 0;
  18. timestamp msec = 0;
  19. luastate* lua = nullptr;
  20. sol::function function;
  21. };
  22. /// <summary>
  23. /// 定时器
  24. /// </summary>
  25. class timer:public ylib::singleton<timer>,private ylib::ithread {
  26. public:
  27. timer();
  28. ~timer();
  29. /// <summary>
  30. /// 增加
  31. /// </summary>
  32. /// <param name="name"></param>
  33. /// <param name="filepath"></param>
  34. /// <param name="funname"></param>
  35. /// <param name="msec"></param>
  36. /// <param name="loop"></param>
  37. /// <returns></returns>
  38. static std::string add(const std::string& name,const std::string& filepath,const std::string& funname,int msec,bool loop);
  39. /// <summary>
  40. /// 移除
  41. /// </summary>
  42. /// <param name="name"></param>
  43. static void remove(const std::string& name);
  44. static void regist(sol::state* lua);
  45. private:
  46. virtual bool run();
  47. public:
  48. std::map<std::string,timer_info> m_list;
  49. ylib::queue<std::string> m_removed;
  50. std::mutex m_mutex;
  51. bool m_insert = false;
  52. };
  53. }