1 //===-- Timeout.h -----------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef liblldb_Timeout_h_ 11 #define liblldb_Timeout_h_ 12 13 #include "llvm/ADT/Optional.h" 14 #include "llvm/Support/Chrono.h" 15 #include "llvm/Support/FormatProviders.h" 16 17 namespace lldb_private { 18 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>. 23 // 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>> { 30 private: 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>; 36 37 using Base = llvm::Optional<Dur<Ratio>>; 38 39 public: Timeout(llvm::NoneType none)40 Timeout(llvm::NoneType none) : Base(none) {} 41 Timeout(const Timeout &other) = default; 42 43 template <typename Ratio2, 44 typename = typename EnableIf<int64_t, Ratio2>::type> Timeout(const Timeout<Ratio2> & other)45 Timeout(const Timeout<Ratio2> &other) 46 : Base(other ? Base(Dur<Ratio>(*other)) : llvm::None) {} 47 48 template <typename Rep2, typename Ratio2, 49 typename = typename EnableIf<Rep2, Ratio2>::type> Timeout(const std::chrono::duration<Rep2,Ratio2> & other)50 Timeout(const std::chrono::duration<Rep2, Ratio2> &other) 51 : Base(Dur<Ratio>(other)) {} 52 }; 53 54 } // namespace lldb_private 55 56 namespace llvm { 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; 62 63 if (!timeout) 64 OS << "<infinite>"; 65 else 66 format_provider<Dur>::format(*timeout, OS, Options); 67 } 68 }; 69 } 70 71 #endif // liblldb_Timeout_h_ 72