apply clang-tidy modernize-use-override
[folly.git] / folly / futures / ScheduledExecutor.h
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #pragma once
18
19 #include <folly/Executor.h>
20 #include <chrono>
21 #include <memory>
22 #include <stdexcept>
23
24 namespace folly {
25   // An executor that supports timed scheduling. Like RxScheduler.
26   class ScheduledExecutor : public virtual Executor {
27    public:
28      // Reality is that better than millisecond resolution is very hard to
29      // achieve. However, we reserve the right to be incredible.
30      typedef std::chrono::microseconds Duration;
31      typedef std::chrono::steady_clock::time_point TimePoint;
32
33      ~ScheduledExecutor() override = default;
34
35      void add(Func) override = 0;
36
37      /// Alias for add() (for Rx consistency)
38      void schedule(Func&& a) { add(std::move(a)); }
39
40      /// Schedule a Func to be executed after dur time has elapsed
41      /// Expect millisecond resolution at best.
42      void schedule(Func&& a, Duration const& dur) {
43        scheduleAt(std::move(a), now() + dur);
44      }
45
46      /// Schedule a Func to be executed at time t, or as soon afterward as
47      /// possible. Expect millisecond resolution at best. Must be threadsafe.
48      virtual void scheduleAt(Func&& /* a */, TimePoint const& /* t */) {
49        throw std::logic_error("unimplemented");
50      }
51
52      /// Get this executor's notion of time. Must be threadsafe.
53      virtual TimePoint now() {
54        return std::chrono::steady_clock::now();
55      }
56   };
57 }