• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_TASK_COMMON_LAZY_NOW_H_
6 #define BASE_TASK_COMMON_LAZY_NOW_H_
7 
8 #include "base/base_export.h"
9 #include "base/memory/raw_ptr_exclusion.h"
10 #include "base/time/time.h"
11 #include "third_party/abseil-cpp/absl/types/optional.h"
12 
13 namespace base {
14 
15 class TickClock;
16 
17 // Now() is somewhat expensive so it makes sense not to call Now() unless we
18 // really need to and to avoid subsequent calls if already called once.
19 // LazyNow objects are expected to be short-living to represent accurate time.
20 class BASE_EXPORT LazyNow {
21  public:
22   explicit LazyNow(TimeTicks now);
23   explicit LazyNow(absl::optional<TimeTicks> now, const TickClock* tick_clock);
24   explicit LazyNow(const TickClock* tick_clock);
25   LazyNow(const LazyNow&) = delete;
26   LazyNow& operator=(const LazyNow&) = delete;
27 
28   LazyNow(LazyNow&& move_from) noexcept;
29 
30   // Result will not be updated on any subsesequent calls.
31   TimeTicks Now();
32 
has_value()33   bool has_value() const { return !!now_; }
34 
35  private:
36   absl::optional<TimeTicks> now_;
37   // `tick_clock_` is not a raw_ptr<TickClock> as a performance optimization:
38   // The pointee doesn't need UaF protection (it has the same lifetime as the
39   // theead/sequence).
40   RAW_PTR_EXCLUSION const TickClock* tick_clock_;  // Not owned.
41 };
42 
43 }  // namespace base
44 
45 #endif  // BASE_TASK_COMMON_LAZY_NOW_H_
46