1 //===-- Timeout.h -----------------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef liblldb_Timeout_h_
10 #define liblldb_Timeout_h_
12 #include "llvm/ADT/Optional.h"
13 #include "llvm/Support/Chrono.h"
14 #include "llvm/Support/FormatProviders.h"
16 namespace lldb_private {
18 // A general purpose class for representing timeouts for various APIs. It's
19 // basically an llvm::Optional<std::chrono::duration<int64_t, Ratio>>, but we
20 // customize it a bit to enable the standard chrono implicit conversions (e.g.
21 // from Timeout<std::milli> to Timeout<std::micro>.
23 // The intended meaning of the values is:
24 // - llvm::None - no timeout, the call should wait forever - 0 - poll, only
25 // complete the call if it will not block - >0 - wait for a given number of
26 // units for the result
27 template <typename Ratio>
28 class Timeout : public llvm::Optional<std::chrono::duration<int64_t, Ratio>> {
30 template <typename Ratio2> using Dur = std::chrono::duration<int64_t, Ratio2>;
31 template <typename Rep2, typename Ratio2>
32 using EnableIf = std::enable_if<
33 std::is_convertible<std::chrono::duration<Rep2, Ratio2>,
34 std::chrono::duration<int64_t, Ratio>>::value>;
36 using Base = llvm::Optional<Dur<Ratio>>;
39 Timeout(llvm::NoneType none) : Base(none) {}
40 Timeout(const Timeout &other) = default;
42 template <typename Ratio2,
43 typename = typename EnableIf<int64_t, Ratio2>::type>
44 Timeout(const Timeout<Ratio2> &other)
45 : Base(other ? Base(Dur<Ratio>(*other)) : llvm::None) {}
47 template <typename Rep2, typename Ratio2,
48 typename = typename EnableIf<Rep2, Ratio2>::type>
49 Timeout(const std::chrono::duration<Rep2, Ratio2> &other)
50 : Base(Dur<Ratio>(other)) {}
53 } // namespace lldb_private
56 template<typename Ratio>
57 struct format_provider<lldb_private::Timeout<Ratio>, void> {
58 static void format(const lldb_private::Timeout<Ratio> &timeout,
59 raw_ostream &OS, StringRef Options) {
60 typedef typename lldb_private::Timeout<Ratio>::value_type Dur;
65 format_provider<Dur>::format(*timeout, OS, Options);
70 #endif // liblldb_Timeout_h_