/src/libtorrent/include/libtorrent/link.hpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | |
3 | | Copyright (c) 2010, 2013-2019, Arvid Norberg |
4 | | Copyright (c) 2017, Alden Torres |
5 | | All rights reserved. |
6 | | |
7 | | Redistribution and use in source and binary forms, with or without |
8 | | modification, are permitted provided that the following conditions |
9 | | are met: |
10 | | |
11 | | * Redistributions of source code must retain the above copyright |
12 | | notice, this list of conditions and the following disclaimer. |
13 | | * Redistributions in binary form must reproduce the above copyright |
14 | | notice, this list of conditions and the following disclaimer in |
15 | | the documentation and/or other materials provided with the distribution. |
16 | | * Neither the name of the author nor the names of its |
17 | | contributors may be used to endorse or promote products derived |
18 | | from this software without specific prior written permission. |
19 | | |
20 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
21 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
22 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
23 | | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
24 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
25 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
26 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
27 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
28 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
29 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
30 | | POSSIBILITY OF SUCH DAMAGE. |
31 | | |
32 | | */ |
33 | | |
34 | | #ifndef TORRENT_LINK_HPP_INCLUDED |
35 | | #define TORRENT_LINK_HPP_INCLUDED |
36 | | |
37 | | #include "libtorrent/aux_/vector.hpp" |
38 | | #include "libtorrent/units.hpp" |
39 | | |
40 | | namespace libtorrent { |
41 | | |
42 | | using torrent_list_index_t = aux::strong_typedef<int, struct torrent_list_tag>; |
43 | | |
44 | | struct link |
45 | | { |
46 | 13.5k | link() : index(-1) {} |
47 | | // this is either -1 (not in the list) |
48 | | // or the index of where in the list this |
49 | | // element is found |
50 | | int index; |
51 | | |
52 | 89.6k | bool in_list() const { return index >= 0; } |
53 | | |
54 | 0 | void clear() { index = -1; } |
55 | | |
56 | | template <class T> |
57 | | void unlink(aux::vector<T*>& list |
58 | | , torrent_list_index_t const link_index) |
59 | 2.37k | { |
60 | 2.37k | if (index == -1) return; |
61 | 2.37k | TORRENT_ASSERT(index >= 0 && index < int(list.size())); |
62 | 2.37k | int const last = int(list.size()) - 1; |
63 | 2.37k | if (index < last) |
64 | 0 | { |
65 | 0 | list[last]->m_links[link_index].index = index; |
66 | 0 | list[index] = list[last]; |
67 | 0 | } |
68 | 2.37k | list.resize(last); |
69 | 2.37k | index = -1; |
70 | 2.37k | } |
71 | | |
72 | | template <class T> |
73 | | void insert(aux::vector<T*>& list, T* self) |
74 | 2.37k | { |
75 | 2.37k | if (index >= 0) return; |
76 | 2.37k | TORRENT_ASSERT(index == -1); |
77 | 2.37k | list.push_back(self); |
78 | 2.37k | index = int(list.size()) - 1; |
79 | 2.37k | } |
80 | | }; |
81 | | } |
82 | | |
83 | | #endif |