]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/beast/core/detail/clamp.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / beast / core / detail / clamp.hpp
1 //
2 // Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/boostorg/beast
8 //
9
10 #ifndef BOOST_BEAST_CORE_DETAIL_CLAMP_HPP
11 #define BOOST_BEAST_CORE_DETAIL_CLAMP_HPP
12
13 #include <cstdlib>
14 #include <limits>
15 #include <type_traits>
16
17 namespace boost {
18 namespace beast {
19 namespace detail {
20
21 template<class UInt>
22 static
23 std::size_t
24 clamp(UInt x)
25 {
26 if(x >= (std::numeric_limits<std::size_t>::max)())
27 return (std::numeric_limits<std::size_t>::max)();
28 return static_cast<std::size_t>(x);
29 }
30
31 template<class UInt>
32 static
33 std::size_t
34 clamp(UInt x, std::size_t limit)
35 {
36 if(x >= limit)
37 return limit;
38 return static_cast<std::size_t>(x);
39 }
40
41 // return `true` if x + y > z, which are unsigned
42 template<
43 class U1, class U2, class U3>
44 constexpr
45 bool
46 sum_exceeds(U1 x, U2 y, U3 z)
47 {
48 static_assert(
49 std::is_unsigned<U1>::value &&
50 std::is_unsigned<U2>::value &&
51 std::is_unsigned<U3>::value, "");
52 return y > z || x > z - y;
53 }
54
55 } // detail
56 } // beast
57 } // boost
58
59 #endif