proxygen
folly::detail::CheckOverflowToDuration< IsFloatingPoint > Struct Template Reference

#include <Conv.h>

Static Public Member Functions

template<typename Tgt , typename SubsecondRatio , typename Seconds , typename Subseconds >
static ConversionCode check (Seconds seconds, Subseconds subseconds)
 

Detailed Description

template<bool IsFloatingPoint>
struct folly::detail::CheckOverflowToDuration< IsFloatingPoint >

Check for overflow when converting to a duration type that is second granularity or finer (e.g., nanoseconds, milliseconds, seconds)

This assumes the input is normalized, with subseconds >= 0 and subseconds less than 1 second.

Definition at line 268 of file Conv.h.

Member Function Documentation

template<bool IsFloatingPoint>
template<typename Tgt , typename SubsecondRatio , typename Seconds , typename Subseconds >
static ConversionCode folly::detail::CheckOverflowToDuration< IsFloatingPoint >::check ( Seconds  seconds,
Subseconds  subseconds 
)
inlinestatic

Definition at line 274 of file Conv.h.

References LIKELY, max, folly::NEGATIVE_OVERFLOW, folly::POSITIVE_OVERFLOW, folly::SUCCESS, folly::to_signed(), folly::to_unsigned(), UNLIKELY, and value.

274  {
275  static_assert(
276  Tgt::period::num == 1,
277  "this implementation should only be used for subsecond granularity "
278  "duration types");
279  static_assert(
281  static_assert(
282  SubsecondRatio::num == 1, "subsecond numerator should always be 1");
283 
284  if (LIKELY(seconds >= 0)) {
285  constexpr auto maxCount = std::numeric_limits<typename Tgt::rep>::max();
286  constexpr auto maxSeconds = maxCount / Tgt::period::den;
287 
288  auto unsignedSeconds = to_unsigned(seconds);
289  if (LIKELY(unsignedSeconds < maxSeconds)) {
291  }
292 
293  if (UNLIKELY(unsignedSeconds == maxSeconds)) {
294  constexpr auto maxRemainder =
295  maxCount - (maxSeconds * Tgt::period::den);
296  constexpr auto maxSubseconds =
297  (maxRemainder * SubsecondRatio::den) / Tgt::period::den;
298  if (subseconds <= 0) {
300  }
301  if (to_unsigned(subseconds) <= maxSubseconds) {
303  }
304  }
308  } else {
309  constexpr auto minCount =
310  to_signed(std::numeric_limits<typename Tgt::rep>::lowest());
311  constexpr auto minSeconds = (minCount / Tgt::period::den);
312  if (LIKELY(seconds >= minSeconds)) {
314  }
315 
316  if (UNLIKELY(seconds == minSeconds - 1)) {
317  constexpr auto maxRemainder =
318  minCount - (minSeconds * Tgt::period::den) + Tgt::period::den;
319  constexpr auto maxSubseconds =
320  (maxRemainder * SubsecondRatio::den) / Tgt::period::den;
321  if (subseconds <= 0) {
323  }
324  if (subseconds >= maxSubseconds) {
326  }
327  }
329  }
330  }
LogLevel max
Definition: LogLevel.cpp:31
#define LIKELY(x)
Definition: Likely.h:47
constexpr auto to_unsigned(T const &t) -> typename std::make_unsigned< T >::type
Definition: Utility.h:399
static const char *const value
Definition: Conv.cpp:50
constexpr auto to_signed(T const &t) -> typename std::make_signed< T >::type
Definition: Utility.h:389
#define UNLIKELY(x)
Definition: Likely.h:48

The documentation for this struct was generated from the following file: