//===-- Timeout.h -----------------------------------------------*- C++ -*-===// // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// #ifndef liblldb_Timeout_h_ #define liblldb_Timeout_h_ #include "llvm/ADT/Optional.h" #include "llvm/Support/Chrono.h" #include "llvm/Support/FormatProviders.h" namespace lldb_private { // A general purpose class for representing timeouts for various APIs. It's // basically an llvm::Optional>, but we // customize it a bit to enable the standard chrono implicit conversions (e.g. // from Timeout to Timeout. // // The intended meaning of the values is: // - llvm::None - no timeout, the call should wait forever - 0 - poll, only // complete the call if it will not block - >0 - wait for a given number of // units for the result template class Timeout : public llvm::Optional> { private: template using Dur = std::chrono::duration; template using EnableIf = std::enable_if< std::is_convertible, std::chrono::duration>::value>; using Base = llvm::Optional>; public: Timeout(llvm::NoneType none) : Base(none) {} Timeout(const Timeout &other) = default; template ::type> Timeout(const Timeout &other) : Base(other ? Base(Dur(*other)) : llvm::None) {} template ::type> Timeout(const std::chrono::duration &other) : Base(Dur(other)) {} }; } // namespace lldb_private namespace llvm { template struct format_provider, void> { static void format(const lldb_private::Timeout &timeout, raw_ostream &OS, StringRef Options) { typedef typename lldb_private::Timeout::value_type Dur; if (!timeout) OS << ""; else format_provider::format(*timeout, OS, Options); } }; } #endif // liblldb_Timeout_h_