Coverage Report

Created: 2025-08-28 06:22

/src/libtorrent/include/libtorrent/units.hpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
3
Copyright (c) 2016-2017, Alden Torres
4
Copyright (c) 2016-2021, Arvid Norberg
5
Copyright (c) 2019, Steven Siloti
6
Copyright (c) 2020, Silver Zachara
7
All rights reserved.
8
9
Redistribution and use in source and binary forms, with or without
10
modification, are permitted provided that the following conditions
11
are met:
12
13
    * Redistributions of source code must retain the above copyright
14
      notice, this list of conditions and the following disclaimer.
15
    * Redistributions in binary form must reproduce the above copyright
16
      notice, this list of conditions and the following disclaimer in
17
      the documentation and/or other materials provided with the distribution.
18
    * Neither the name of the author nor the names of its
19
      contributors may be used to endorse or promote products derived
20
      from this software without specific prior written permission.
21
22
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
POSSIBILITY OF SUCH DAMAGE.
33
34
*/
35
36
#ifndef TORRENT_UNITS_HPP
37
#define TORRENT_UNITS_HPP
38
39
#include <cstdint>
40
#include <string>
41
#include <limits>
42
#include <iosfwd>
43
#include <type_traits>
44
45
#include "libtorrent/config.hpp"
46
47
namespace libtorrent {
48
namespace aux {
49
  template <typename Tag>
50
  struct difference_tag;
51
52
#if TORRENT_USE_IOSTREAM
53
  template <typename T>
54
  struct type_to_print_as
55
  {
56
    using type = typename std::conditional<sizeof(T) < sizeof(int), int, T>::type;
57
  };
58
#endif
59
60
61
  template<typename UnderlyingType, typename Tag
62
    , typename Cond = typename std::enable_if<std::is_integral<UnderlyingType>::value>::type>
63
  struct strong_typedef
64
  {
65
    using underlying_type = UnderlyingType;
66
    using diff_type = strong_typedef<UnderlyingType, difference_tag<Tag>>;
67
68
    constexpr strong_typedef(strong_typedef const& rhs) noexcept = default;
69
    constexpr strong_typedef(strong_typedef&& rhs) noexcept = default;
70
    strong_typedef() noexcept = default;
71
#if TORRENT_ABI_VERSION == 1
72
18.6M
    constexpr strong_typedef(UnderlyingType val) : m_val(val) {}
libtorrent::aux::strong_typedef<int, libtorrent::aux::piece_index_tag, void>::strong_typedef(int)
Line
Count
Source
72
4.42M
    constexpr strong_typedef(UnderlyingType val) : m_val(val) {}
libtorrent::aux::strong_typedef<int, libtorrent::aux::file_index_tag, void>::strong_typedef(int)
Line
Count
Source
72
9.94M
    constexpr strong_typedef(UnderlyingType val) : m_val(val) {}
libtorrent::aux::strong_typedef<int, libtorrent::aux::difference_tag<libtorrent::aux::piece_index_tag>, void>::strong_typedef(int)
Line
Count
Source
72
502
    constexpr strong_typedef(UnderlyingType val) : m_val(val) {}
libtorrent::aux::strong_typedef<unsigned int, libtorrent::aux::path_index_tag, void>::strong_typedef(unsigned int)
Line
Count
Source
72
2.87M
    constexpr strong_typedef(UnderlyingType val) : m_val(val) {}
libtorrent::aux::strong_typedef<unsigned int, libtorrent::peer_class_tag, void>::strong_typedef(unsigned int)
Line
Count
Source
72
106k
    constexpr strong_typedef(UnderlyingType val) : m_val(val) {}
libtorrent::aux::strong_typedef<int, libtorrent::torrent_list_tag, void>::strong_typedef(int)
Line
Count
Source
72
437k
    constexpr strong_typedef(UnderlyingType val) : m_val(val) {}
libtorrent::aux::strong_typedef<int, libtorrent::queue_position_tag, void>::strong_typedef(int)
Line
Count
Source
72
16.4k
    constexpr strong_typedef(UnderlyingType val) : m_val(val) {}
libtorrent::aux::strong_typedef<int, libtorrent::port_mapping_tag, void>::strong_typedef(int)
Line
Count
Source
72
10.9k
    constexpr strong_typedef(UnderlyingType val) : m_val(val) {}
libtorrent::aux::strong_typedef<unsigned int, libtorrent::storage_index_tag_t, void>::strong_typedef(unsigned int)
Line
Count
Source
72
3.66k
    constexpr strong_typedef(UnderlyingType val) : m_val(val) {}
libtorrent::aux::strong_typedef<unsigned char, libtorrent::dl_queue_tag, void>::strong_typedef(unsigned char)
Line
Count
Source
72
786k
    constexpr strong_typedef(UnderlyingType val) : m_val(val) {}
libtorrent::aux::strong_typedef<unsigned char, libtorrent::download_priority_tag, void>::strong_typedef(unsigned char)
Line
Count
Source
72
15.6k
    constexpr strong_typedef(UnderlyingType val) : m_val(val) {}
libtorrent::aux::strong_typedef<int, libtorrent::prio_index_tag_t, void>::strong_typedef(int)
Line
Count
Source
72
19.2k
    constexpr strong_typedef(UnderlyingType val) : m_val(val) {}
Unexecuted instantiation: libtorrent::aux::strong_typedef<int, libtorrent::aux::difference_tag<libtorrent::prio_index_tag_t>, void>::strong_typedef(int)
Unexecuted instantiation: libtorrent::aux::strong_typedef<int, libtorrent::piece_extent_tag, void>::strong_typedef(int)
Unexecuted instantiation: libtorrent::aux::strong_typedef<int, libtorrent::slot_index_tag_t, void>::strong_typedef(int)
73
88.2M
    constexpr operator UnderlyingType() const { return m_val; }
libtorrent::aux::strong_typedef<int, libtorrent::aux::piece_index_tag, void>::operator int() const
Line
Count
Source
73
17.6M
    constexpr operator UnderlyingType() const { return m_val; }
libtorrent::aux::strong_typedef<int, libtorrent::aux::file_index_tag, void>::operator int() const
Line
Count
Source
73
55.2M
    constexpr operator UnderlyingType() const { return m_val; }
libtorrent::aux::strong_typedef<int, libtorrent::aux::difference_tag<libtorrent::aux::piece_index_tag>, void>::operator int() const
Line
Count
Source
73
1.28k
    constexpr operator UnderlyingType() const { return m_val; }
libtorrent::aux::strong_typedef<unsigned int, libtorrent::peer_class_tag, void>::operator unsigned int() const
Line
Count
Source
73
242k
    constexpr operator UnderlyingType() const { return m_val; }
libtorrent::aux::strong_typedef<unsigned int, libtorrent::aux::path_index_tag, void>::operator unsigned int() const
Line
Count
Source
73
11.1M
    constexpr operator UnderlyingType() const { return m_val; }
libtorrent::aux::strong_typedef<int, libtorrent::torrent_list_tag, void>::operator int() const
Line
Count
Source
73
1.18M
    constexpr operator UnderlyingType() const { return m_val; }
libtorrent::aux::strong_typedef<unsigned char, libtorrent::dl_queue_tag, void>::operator unsigned char() const
Line
Count
Source
73
2.50M
    constexpr operator UnderlyingType() const { return m_val; }
libtorrent::aux::strong_typedef<unsigned char, libtorrent::download_priority_tag, void>::operator unsigned char() const
Line
Count
Source
73
99.1k
    constexpr operator UnderlyingType() const { return m_val; }
libtorrent::aux::strong_typedef<int, libtorrent::prio_index_tag_t, void>::operator int() const
Line
Count
Source
73
103k
    constexpr operator UnderlyingType() const { return m_val; }
libtorrent::aux::strong_typedef<int, libtorrent::queue_position_tag, void>::operator int() const
Line
Count
Source
73
62.3k
    constexpr operator UnderlyingType() const { return m_val; }
Unexecuted instantiation: libtorrent::aux::strong_typedef<int, libtorrent::port_mapping_tag, void>::operator int() const
libtorrent::aux::strong_typedef<unsigned int, libtorrent::storage_index_tag_t, void>::operator unsigned int() const
Line
Count
Source
73
28
    constexpr operator UnderlyingType() const { return m_val; }
Unexecuted instantiation: libtorrent::aux::strong_typedef<int, libtorrent::slot_index_tag_t, void>::operator int() const
Unexecuted instantiation: libtorrent::aux::strong_typedef<unsigned char, libtorrent::disconnect_severity_tag, void>::operator unsigned char() const
Unexecuted instantiation: libtorrent::aux::strong_typedef<int, libtorrent::aux::difference_tag<libtorrent::prio_index_tag_t>, void>::operator int() const
Unexecuted instantiation: libtorrent::aux::strong_typedef<int, libtorrent::piece_extent_tag, void>::operator int() const
74
#else
75
    constexpr explicit strong_typedef(UnderlyingType val) : m_val(val) {}
76
    constexpr explicit operator UnderlyingType() const { return m_val; }
77
    constexpr bool operator==(strong_typedef const& rhs) const { return m_val == rhs.m_val; }
78
    constexpr bool operator!=(strong_typedef const& rhs) const { return m_val != rhs.m_val; }
79
    constexpr bool operator<(strong_typedef const& rhs) const { return m_val < rhs.m_val; }
80
    constexpr bool operator>(strong_typedef const& rhs) const { return m_val > rhs.m_val; }
81
    constexpr bool operator>=(strong_typedef const& rhs) const { return m_val >= rhs.m_val; }
82
    constexpr bool operator<=(strong_typedef const& rhs) const { return m_val <= rhs.m_val; }
83
#endif
84
3.57M
    strong_typedef& operator++() { ++m_val; return *this; }
libtorrent::aux::strong_typedef<int, libtorrent::aux::piece_index_tag, void>::operator++()
Line
Count
Source
84
2.63M
    strong_typedef& operator++() { ++m_val; return *this; }
libtorrent::aux::strong_typedef<int, libtorrent::aux::file_index_tag, void>::operator++()
Line
Count
Source
84
834k
    strong_typedef& operator++() { ++m_val; return *this; }
libtorrent::aux::strong_typedef<int, libtorrent::aux::difference_tag<libtorrent::aux::piece_index_tag>, void>::operator++()
Line
Count
Source
84
100
    strong_typedef& operator++() { ++m_val; return *this; }
Unexecuted instantiation: libtorrent::aux::strong_typedef<unsigned int, libtorrent::peer_class_tag, void>::operator++()
Unexecuted instantiation: libtorrent::aux::strong_typedef<int, libtorrent::queue_position_tag, void>::operator++()
libtorrent::aux::strong_typedef<int, libtorrent::torrent_list_tag, void>::operator++()
Line
Count
Source
84
29.2k
    strong_typedef& operator++() { ++m_val; return *this; }
Unexecuted instantiation: libtorrent::aux::strong_typedef<int, libtorrent::port_mapping_tag, void>::operator++()
Unexecuted instantiation: libtorrent::aux::strong_typedef<int, libtorrent::prio_index_tag_t, void>::operator++()
libtorrent::aux::strong_typedef<unsigned char, libtorrent::dl_queue_tag, void>::operator++()
Line
Count
Source
84
74.3k
    strong_typedef& operator++() { ++m_val; return *this; }
Unexecuted instantiation: libtorrent::aux::strong_typedef<int, libtorrent::slot_index_tag_t, void>::operator++()
85
24.5k
    strong_typedef& operator--() { --m_val; return *this; }
libtorrent::aux::strong_typedef<int, libtorrent::aux::file_index_tag, void>::operator--()
Line
Count
Source
85
15.2k
    strong_typedef& operator--() { --m_val; return *this; }
Unexecuted instantiation: libtorrent::aux::strong_typedef<int, libtorrent::queue_position_tag, void>::operator--()
libtorrent::aux::strong_typedef<int, libtorrent::aux::piece_index_tag, void>::operator--()
Line
Count
Source
85
9.36k
    strong_typedef& operator--() { --m_val; return *this; }
Unexecuted instantiation: libtorrent::aux::strong_typedef<int, libtorrent::port_mapping_tag, void>::operator--()
Unexecuted instantiation: libtorrent::aux::strong_typedef<int, libtorrent::prio_index_tag_t, void>::operator--()
86
87
0
    strong_typedef operator++(int) & { return strong_typedef{m_val++}; }
Unexecuted instantiation: libtorrent::aux::strong_typedef<int, libtorrent::aux::difference_tag<libtorrent::aux::piece_index_tag>, void>::operator++(int) &
Unexecuted instantiation: libtorrent::aux::strong_typedef<int, libtorrent::prio_index_tag_t, void>::operator++(int) &
88
    strong_typedef operator--(int) & { return strong_typedef{m_val--}; }
89
90
    friend diff_type operator-(strong_typedef lhs, strong_typedef rhs)
91
0
    { return diff_type{lhs.m_val - rhs.m_val}; }
Unexecuted instantiation: libtorrent::aux::operator-(libtorrent::aux::strong_typedef<int, libtorrent::aux::piece_index_tag, void>, libtorrent::aux::strong_typedef<int, libtorrent::aux::piece_index_tag, void>)
Unexecuted instantiation: libtorrent::aux::operator-(libtorrent::aux::strong_typedef<int, libtorrent::prio_index_tag_t, void>, libtorrent::aux::strong_typedef<int, libtorrent::prio_index_tag_t, void>)
92
    friend strong_typedef operator+(strong_typedef lhs, diff_type rhs)
93
0
    { return strong_typedef{lhs.m_val + static_cast<UnderlyingType>(rhs)}; }
Unexecuted instantiation: libtorrent::aux::operator+(libtorrent::aux::strong_typedef<int, libtorrent::aux::piece_index_tag, void>, libtorrent::aux::strong_typedef<int, libtorrent::aux::difference_tag<libtorrent::aux::piece_index_tag>, void>)
Unexecuted instantiation: libtorrent::aux::operator+(libtorrent::aux::strong_typedef<int, libtorrent::prio_index_tag_t, void>, libtorrent::aux::strong_typedef<int, libtorrent::aux::difference_tag<libtorrent::prio_index_tag_t>, void>)
94
    friend strong_typedef operator+(diff_type lhs, strong_typedef rhs)
95
    { return strong_typedef{static_cast<UnderlyingType>(lhs) + rhs.m_val}; }
96
    friend strong_typedef operator-(strong_typedef lhs, diff_type rhs)
97
    { return strong_typedef{lhs.m_val - static_cast<UnderlyingType>(rhs)}; }
98
99
    strong_typedef& operator+=(diff_type rhs) &
100
    { m_val += static_cast<UnderlyingType>(rhs); return *this; }
101
    strong_typedef& operator-=(diff_type rhs) &
102
    { m_val -= static_cast<UnderlyingType>(rhs); return *this; }
103
104
    strong_typedef& operator=(strong_typedef const& rhs) & noexcept = default;
105
    strong_typedef& operator=(strong_typedef&& rhs) & noexcept = default;
106
107
#if TORRENT_USE_IOSTREAM
108
    friend std::ostream& operator<<(std::ostream& os, strong_typedef val)
109
0
    { return os << static_cast<typename type_to_print_as<UnderlyingType>::type>(static_cast<UnderlyingType>(val)); }
110
#endif
111
112
  private:
113
    UnderlyingType m_val;
114
  };
115
116
  // meta function to return the underlying type of a strong_typedef or enumeration
117
  // , or the type itself if it isn't a strong_typedef
118
  template <typename T, typename = void>
119
  struct underlying_index_t { using type = T; };
120
121
  template <typename T>
122
  struct underlying_index_t<T, typename std::enable_if<std::is_enum<T>::value>::type>
123
  { using type = typename std::underlying_type<T>::type; };
124
125
  template <typename U, typename Tag>
126
  struct underlying_index_t<aux::strong_typedef<U, Tag>> { using type = U; };
127
128
  struct piece_index_tag;
129
  struct file_index_tag;
130
131
  template <typename T, typename Tag>
132
  std::string to_string(strong_typedef<T, Tag> const t)
133
0
  { return std::to_string(static_cast<T>(t)); }
134
135
  template <typename T, typename Tag>
136
  strong_typedef<T, Tag> next(strong_typedef<T, Tag> v)
137
19.3k
  { return ++v;}
Unexecuted instantiation: _ZN10libtorrent3aux4nextIiNS_18queue_position_tagEEENS0_14strong_typedefIT_T0_NSt3__19enable_ifIXsr3std11is_integralIS4_EE5valueEvE4typeEEESA_
Unexecuted instantiation: _ZN10libtorrent3aux4nextIiNS_16port_mapping_tagEEENS0_14strong_typedefIT_T0_NSt3__19enable_ifIXsr3std11is_integralIS4_EE5valueEvE4typeEEESA_
_ZN10libtorrent3aux4nextIiNS0_15piece_index_tagEEENS0_14strong_typedefIT_T0_NSt3__19enable_ifIXsr3std11is_integralIS4_EE5valueEvE4typeEEESA_
Line
Count
Source
137
19.3k
  { return ++v;}
Unexecuted instantiation: _ZN10libtorrent3aux4nextIiNS_16slot_index_tag_tEEENS0_14strong_typedefIT_T0_NSt3__19enable_ifIXsr3std11is_integralIS4_EE5valueEvE4typeEEESA_
138
139
  template <typename T, typename Tag>
140
  strong_typedef<T, Tag> prev(strong_typedef<T, Tag> v)
141
9.14k
  { return --v;}
Unexecuted instantiation: _ZN10libtorrent3aux4prevIiNS_18queue_position_tagEEENS0_14strong_typedefIT_T0_NSt3__19enable_ifIXsr3std11is_integralIS4_EE5valueEvE4typeEEESA_
Unexecuted instantiation: _ZN10libtorrent3aux4prevIiNS0_14file_index_tagEEENS0_14strong_typedefIT_T0_NSt3__19enable_ifIXsr3std11is_integralIS4_EE5valueEvE4typeEEESA_
Unexecuted instantiation: _ZN10libtorrent3aux4prevIiNS_16port_mapping_tagEEENS0_14strong_typedefIT_T0_NSt3__19enable_ifIXsr3std11is_integralIS4_EE5valueEvE4typeEEESA_
Unexecuted instantiation: _ZN10libtorrent3aux4prevIiNS_16prio_index_tag_tEEENS0_14strong_typedefIT_T0_NSt3__19enable_ifIXsr3std11is_integralIS4_EE5valueEvE4typeEEESA_
_ZN10libtorrent3aux4prevIiNS0_15piece_index_tagEEENS0_14strong_typedefIT_T0_NSt3__19enable_ifIXsr3std11is_integralIS4_EE5valueEvE4typeEEESA_
Line
Count
Source
141
9.14k
  { return --v;}
142
143
} // namespace libtorrent::aux
144
145
  // this type represents a piece index in a torrent.
146
  using piece_index_t = aux::strong_typedef<std::int32_t, aux::piece_index_tag>;
147
148
  // this type represents an index to a file in a torrent. Any specific torrent
149
  // file has a well defined and immutable file list, and a file index into it
150
  // always refers to the same file.
151
  using file_index_t = aux::strong_typedef<std::int32_t, aux::file_index_tag>;
152
153
} // namespace libtorrent
154
155
namespace std {
156
157
  template<typename UnderlyingType, typename Tag>
158
  class numeric_limits<libtorrent::aux::strong_typedef<UnderlyingType, Tag>> : public std::numeric_limits<UnderlyingType>
159
  {
160
    using type = libtorrent::aux::strong_typedef<UnderlyingType, Tag>;
161
  public:
162
163
    static constexpr type (min)()
164
    { return type((std::numeric_limits<UnderlyingType>::min)()); }
165
166
    static constexpr type (max)()
167
22
    { return type((std::numeric_limits<UnderlyingType>::max)()); }
std::__1::numeric_limits<libtorrent::aux::strong_typedef<int, libtorrent::aux::piece_index_tag, void> >::max()
Line
Count
Source
167
19
    { return type((std::numeric_limits<UnderlyingType>::max)()); }
std::__1::numeric_limits<libtorrent::aux::strong_typedef<int, libtorrent::aux::file_index_tag, void> >::max()
Line
Count
Source
167
3
    { return type((std::numeric_limits<UnderlyingType>::max)()); }
168
  };
169
170
  template<typename UnderlyingType, typename Tag>
171
  struct hash<libtorrent::aux::strong_typedef<UnderlyingType, Tag>> : std::hash<UnderlyingType>
172
  {
173
    using base = std::hash<UnderlyingType>;
174
    using argument_type = libtorrent::aux::strong_typedef<UnderlyingType, Tag>;
175
#if __cplusplus < 201402
176
    // this was deprecated in C++17
177
    using result_type = typename base::result_type;
178
#else
179
    using result_type = std::size_t;
180
#endif
181
    result_type operator()(argument_type const& s) const
182
9.52k
    { return this->base::operator()(static_cast<UnderlyingType>(s)); }
Unexecuted instantiation: std::__1::hash<libtorrent::aux::strong_typedef<unsigned int, libtorrent::storage_index_tag_t, void> >::operator()(libtorrent::aux::strong_typedef<unsigned int, libtorrent::storage_index_tag_t, void> const&) const
std::__1::hash<libtorrent::aux::strong_typedef<int, libtorrent::aux::piece_index_tag, void> >::operator()(libtorrent::aux::strong_typedef<int, libtorrent::aux::piece_index_tag, void> const&) const
Line
Count
Source
182
9.52k
    { return this->base::operator()(static_cast<UnderlyingType>(s)); }
183
  };
184
}
185
186
#endif