Timer.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /*
  2. * Copyright (c) 2016 The ZLToolKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLToolKit(https://github.com/ZLMediaKit/ZLToolKit).
  5. *
  6. * Use of this source code is governed by MIT license that can be found in the
  7. * LICENSE file in the root of the source tree. All contributing project authors
  8. * may be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef Timer_h
  11. #define Timer_h
  12. #include <functional>
  13. #include "EventPoller.h"
  14. namespace toolkit {
  15. class Timer {
  16. public:
  17. using Ptr = std::shared_ptr<Timer>;
  18. /**
  19. * 构造定时器
  20. * @param second 定时器重复秒数
  21. * @param cb 定时器任务,返回true表示重复下次任务,否则不重复,如果任务中抛异常,则默认重复下次任务
  22. * @param poller EventPoller对象,可以为nullptr
  23. */
  24. Timer(float second, const std::function<bool()> &cb, const EventPoller::Ptr &poller);
  25. ~Timer();
  26. private:
  27. std::weak_ptr<EventPoller::DelayTask> _tag;
  28. //定时器保持EventPoller的强引用
  29. EventPoller::Ptr _poller;
  30. };
  31. } // namespace toolkit
  32. #endif /* Timer_h */