• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_ALLOC_BASE_CXX17_BACKPORTS_H_
6 #define BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_ALLOC_BASE_CXX17_BACKPORTS_H_
7 
8 #include <functional>
9 #include <tuple>
10 #include <type_traits>
11 #include <utility>
12 
13 #include "base/allocator/partition_allocator/partition_alloc_check.h"
14 
15 namespace partition_alloc::internal::base {
16 
17 // C++14 implementation of C++17's std::clamp():
18 // https://en.cppreference.com/w/cpp/algorithm/clamp
19 // Please note that the C++ spec makes it undefined behavior to call std::clamp
20 // with a value of `lo` that compares greater than the value of `hi`. This
21 // implementation uses a CHECK to enforce this as a hard restriction.
22 template <typename T, typename Compare>
clamp(const T & v,const T & lo,const T & hi,Compare comp)23 constexpr const T& clamp(const T& v, const T& lo, const T& hi, Compare comp) {
24   PA_CHECK(!comp(hi, lo));
25   return comp(v, lo) ? lo : comp(hi, v) ? hi : v;
26 }
27 
28 template <typename T>
clamp(const T & v,const T & lo,const T & hi)29 constexpr const T& clamp(const T& v, const T& lo, const T& hi) {
30   return base::clamp(v, lo, hi, std::less<T>{});
31 }
32 
33 }  // namespace partition_alloc::internal::base
34 
35 #endif  // BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_ALLOC_BASE_CXX17_BACKPORTS_H_
36