Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:58

0001 //===-- Timeout.h -----------------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLDB_UTILITY_TIMEOUT_H
0010 #define LLDB_UTILITY_TIMEOUT_H
0011 
0012 #include "llvm/Support/Chrono.h"
0013 #include "llvm/Support/FormatProviders.h"
0014 #include <optional>
0015 
0016 namespace lldb_private {
0017 
0018 // A general purpose class for representing timeouts for various APIs. It's
0019 // basically an std::optional<std::chrono::duration<int64_t, Ratio>>, but we
0020 // customize it a bit to enable the standard chrono implicit conversions (e.g.
0021 // from Timeout<std::milli> to Timeout<std::micro>.
0022 //
0023 // The intended meaning of the values is:
0024 // - std::nullopt - no timeout, the call should wait forever - 0 - poll, only
0025 // complete the call if it will not block - >0 - wait for a given number of
0026 // units for the result
0027 template <typename Ratio>
0028 class Timeout : public std::optional<std::chrono::duration<int64_t, Ratio>> {
0029 private:
0030   template <typename Ratio2> using Dur = std::chrono::duration<int64_t, Ratio2>;
0031   template <typename Rep2, typename Ratio2>
0032   using EnableIf = std::enable_if<
0033       std::is_convertible<std::chrono::duration<Rep2, Ratio2>,
0034                           std::chrono::duration<int64_t, Ratio>>::value>;
0035 
0036   using Base = std::optional<Dur<Ratio>>;
0037 
0038 public:
0039   Timeout(std::nullopt_t none) : Base(none) {}
0040 
0041   template <typename Ratio2,
0042             typename = typename EnableIf<int64_t, Ratio2>::type>
0043   Timeout(const Timeout<Ratio2> &other)
0044       : Base(other ? Base(Dur<Ratio>(*other)) : std::nullopt) {}
0045 
0046   template <typename Rep2, typename Ratio2,
0047             typename = typename EnableIf<Rep2, Ratio2>::type>
0048   Timeout(const std::chrono::duration<Rep2, Ratio2> &other)
0049       : Base(Dur<Ratio>(other)) {}
0050 };
0051 
0052 } // namespace lldb_private
0053 
0054 namespace llvm {
0055 template<typename Ratio>
0056 struct format_provider<lldb_private::Timeout<Ratio>, void> {
0057   static void format(const lldb_private::Timeout<Ratio> &timeout,
0058                      raw_ostream &OS, StringRef Options) {
0059     typedef typename lldb_private::Timeout<Ratio>::value_type Dur;
0060 
0061     if (!timeout)
0062       OS << "<infinite>";
0063     else
0064       format_provider<Dur>::format(*timeout, OS, Options);
0065   }
0066 };
0067 }
0068 
0069 #endif // LLDB_UTILITY_TIMEOUT_H