1 //===-- Timeout.h -----------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_Timeout_h_
11 #define liblldb_Timeout_h_
13 #include "llvm/ADT/Optional.h"
14 #include "llvm/Support/Chrono.h"
15 #include "llvm/Support/FormatProviders.h"
17 namespace lldb_private {
19 // A general purpose class for representing timeouts for various APIs. It's
20 // basically an llvm::Optional<std::chrono::duration<int64_t, Ratio>>, but we
21 // customize it a bit to enable the standard chrono implicit conversions (e.g.
22 // from Timeout<std::milli> to Timeout<std::micro>.
24 // The intended meaning of the values is:
25 // - llvm::None - no timeout, the call should wait forever - 0 - poll, only
26 // complete the call if it will not block - >0 - wait for a given number of
27 // units for the result
28 template <typename Ratio>
29 class Timeout : public llvm::Optional<std::chrono::duration<int64_t, Ratio>> {
31 template <typename Ratio2> using Dur = std::chrono::duration<int64_t, Ratio2>;
32 template <typename Rep2, typename Ratio2>
33 using EnableIf = std::enable_if<
34 std::is_convertible<std::chrono::duration<Rep2, Ratio2>,
35 std::chrono::duration<int64_t, Ratio>>::value>;
37 using Base = llvm::Optional<Dur<Ratio>>;
40 Timeout(llvm::NoneType none) : Base(none) {}
41 Timeout(const Timeout &other) = default;
43 template <typename Ratio2,
44 typename = typename EnableIf<int64_t, Ratio2>::type>
45 Timeout(const Timeout<Ratio2> &other)
46 : Base(other ? Base(Dur<Ratio>(*other)) : llvm::None) {}
48 template <typename Rep2, typename Ratio2,
49 typename = typename EnableIf<Rep2, Ratio2>::type>
50 Timeout(const std::chrono::duration<Rep2, Ratio2> &other)
51 : Base(Dur<Ratio>(other)) {}
54 } // namespace lldb_private
57 template<typename Ratio>
58 struct format_provider<lldb_private::Timeout<Ratio>, void> {
59 static void format(const lldb_private::Timeout<Ratio> &timeout,
60 raw_ostream &OS, StringRef Options) {
61 typedef typename lldb_private::Timeout<Ratio>::value_type Dur;
66 format_provider<Dur>::format(*timeout, OS, Options);
71 #endif // liblldb_Timeout_h_