blob: 86976d4e7a33a25676ec1012edf0ed57eafa7683 [file] [log] [blame]
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <functional>
#include <tuple>
#include <type_traits>
#include <utility>
#include "polyfills/base/check.h"
#include "base/functional/invoke.h"
namespace gurl_base {
// C++14 implementation of C++17's std::clamp():
// Please note that the C++ spec makes it undefined behavior to call std::clamp
// with a value of `lo` that compares greater than the value of `hi`. This
// implementation uses a GURL_CHECK to enforce this as a hard restriction.
template <typename T, typename Compare>
constexpr const T& clamp(const T& v, const T& lo, const T& hi, Compare comp) {
GURL_CHECK(!comp(hi, lo));
return comp(v, lo) ? lo : comp(hi, v) ? hi : v;
template <typename T>
constexpr const T& clamp(const T& v, const T& lo, const T& hi) {
return gurl_base::clamp(v, lo, hi, std::less<T>{});
// C++14 implementation of C++17's std::apply():
namespace internal {
template <class F, class Tuple, std::size_t... I>
constexpr decltype(auto) apply_impl(F&& f,
Tuple&& t,
std::index_sequence<I...>) {
return gurl_base::invoke(std::forward<F>(f),
} // namespace internal
template <class F, class Tuple>
constexpr decltype(auto) apply(F&& f, Tuple&& t) {
return internal::apply_impl(
std::forward<F>(f), std::forward<Tuple>(t),
} // namespace base