Timer.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. * Copyright (c) 2016 The ZLToolKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLToolKit(https://github.com/xia-chu/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 <stdio.h>
  13. #include <functional>
  14. #include "EventPoller.h"
  15. using namespace std;
  16. namespace toolkit {
  17. class Timer {
  18. public:
  19. typedef std::shared_ptr<Timer> Ptr;
  20. /**
  21. * 构造定时器
  22. * @param second 定时器重复秒数
  23. * @param cb 定时器任务,返回true表示重复下次任务,否则不重复,如果任务中抛异常,则默认重复下次任务
  24. * @param poller EventPoller对象,可以为nullptr
  25. * @param continueWhenException 定时回调中抛异常是否继续标记
  26. */
  27. Timer(float second,
  28. const function<bool()> &cb,
  29. const EventPoller::Ptr &poller /*=nullptr*/,
  30. bool continueWhenException = true );
  31. ~Timer();
  32. private:
  33. weak_ptr<DelayTask> _tag;
  34. //定时器保持EventPoller的强引用
  35. EventPoller::Ptr _poller;
  36. };
  37. } // namespace toolkit
  38. #endif /* Timer_h */