lazy_now.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_TASK_SEQUENCE_MANAGER_LAZY_NOW_H_
  5. #define BASE_TASK_SEQUENCE_MANAGER_LAZY_NOW_H_
  6. #include "base/base_export.h"
  7. #include "base/optional.h"
  8. #include "base/time/time.h"
  9. namespace base {
  10. class TickClock;
  11. namespace sequence_manager {
  12. // Now() is somewhat expensive so it makes sense not to call Now() unless we
  13. // really need to and to avoid subsequent calls if already called once.
  14. // LazyNow objects are expected to be short-living to represent accurate time.
  15. class BASE_EXPORT LazyNow {
  16. public:
  17. explicit LazyNow(TimeTicks now);
  18. explicit LazyNow(const TickClock* tick_clock);
  19. LazyNow(const LazyNow&) = delete;
  20. LazyNow& operator=(const LazyNow&) = delete;
  21. LazyNow(LazyNow&& move_from) noexcept;
  22. // Result will not be updated on any subsesequent calls.
  23. TimeTicks Now();
  24. bool has_value() const { return !!now_; }
  25. private:
  26. const TickClock* tick_clock_; // Not owned.
  27. Optional<TimeTicks> now_;
  28. };
  29. } // namespace sequence_manager
  30. } // namespace base
  31. #endif // BASE_TASK_SEQUENCE_MANAGER_LAZY_NOW_H_