improve span container constructor

This commit is contained in:
arvidn 2017-04-27 23:44:08 -04:00 committed by Arvid Norberg
parent 4ea318faa0
commit 2dca174785
4 changed files with 152 additions and 3 deletions

View File

@ -39,12 +39,35 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent {
namespace aux {
template <typename From, typename To>
struct compatible_type
{
// conversions that are OK
// int -> int
// int const -> int const
// int -> int const
// int -> int volatile
// int -> int const volatile
// int volatile -> int const volatile
// int const -> int const volatile
static const bool value = std::is_same<From, To>::value
|| std::is_same<From, typename std::remove_const<To>::type>::value
|| std::is_same<From, typename std::remove_volatile<To>::type>::value
|| std::is_same<From, typename std::remove_cv<To>::type>::value
;
};
}
template <typename T>
struct span
{
span() : m_ptr(nullptr), m_len(0) {}
template <typename U>
template <typename U, typename
= typename std::enable_if<aux::compatible_type<U, T>::value>::type>
span(span<U> const& v) // NOLINT
: m_ptr(v.data()), m_len(v.size()) {}
@ -60,7 +83,10 @@ namespace libtorrent {
: m_ptr(&arr[0]), m_len(N) {}
// anything with a .data() member function is considered a container
template <typename Cont, typename = decltype(std::declval<Cont>().data())>
// but only if the value type is compatible with T
template <typename Cont
, typename U = typename std::remove_reference<decltype(*std::declval<Cont>().data())>::type
, typename = typename std::enable_if<aux::compatible_type<U, T>::value>::type>
span(Cont& c) // NOLINT
: m_ptr(c.data()), m_len(c.size()) {}
@ -114,6 +140,14 @@ namespace libtorrent {
T* m_ptr;
std::size_t m_len;
};
template <class T, class U>
inline bool operator==(span<T> const& lhs, span<U> const& rhs)
{
return lhs.size() == rhs.size()
&& (lhs.begin() == rhs.begin() || std::equal(lhs.begin(), lhs.end(), rhs.begin()));
}
}
#endif // TORRENT_SPAN_HPP_INCLUDED

View File

@ -158,6 +158,7 @@ test-suite libtorrent :
]
[ run test_sha1_hash.cpp ]
[ run test_span.cpp ]
[ run test_bitfield.cpp ]
[ run test_crc32.cpp ]
[ run test_ffs.cpp ]

View File

@ -47,7 +47,8 @@ test_programs = \
test_linked_list \
test_direct_dht \
test_ffs \
test_session_params
test_session_params \
test_span
if ENABLE_TESTS
check_PROGRAMS = $(test_programs)
@ -241,6 +242,7 @@ test_linked_list_SOURCES = test_linked_list.cpp
test_direct_dht_SOURCES = test_direct_dht.cpp
test_ffs_SOURCES = test_ffs.cpp
test_session_params_SOURCES = test_session_params.cpp
test_span_SOURCES = test_span.cpp
LDADD = libtest.la $(top_builddir)/src/libtorrent-rasterbar.la

112
test/test_span.cpp Normal file
View File

@ -0,0 +1,112 @@
/*
Copyright (c) 2017, Arvid Norberg
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "libtorrent/config.hpp"
#include "libtorrent/span.hpp"
#include "test.hpp"
#include "setup_transfer.hpp"
#include <vector>
#include <array>
using namespace lt;
span<char const> f(span<char const> x) { return x; }
span<span<char>> g(span<span<char>> x) { return x; }
TORRENT_TEST(span_vector)
{
std::vector<char> v1 = {1,2,3,4};
span<char> a(v1);
TEST_CHECK(a == f(v1));
TEST_CHECK(a.size() == 4);
}
TORRENT_TEST(span_std_array)
{
std::array<char, 4> v1{{1,2,3,4}};
span<char> a(v1);
TEST_CHECK(a == f(v1));
TEST_CHECK(a.size() == 4);
}
TORRENT_TEST(span_const_std_array)
{
std::array<char const, 4> v1{{1,2,3,4}};
span<char const> a(v1);
TEST_CHECK(a == f(v1));
TEST_CHECK(a.size() == 4);
}
TORRENT_TEST(span_array)
{
char v1[] = {1,2,3,4};
span<char> a(v1);
TEST_CHECK(a == f(v1));
TEST_CHECK(a.size() == 4);
}
TORRENT_TEST(span_string)
{
std::string v1 = "test";
span<char const> a(v1);
TEST_CHECK(a == f(v1));
TEST_CHECK(a.size() == 4);
}
TORRENT_TEST(span_const_array)
{
char const v1[] = {1,2,3,4};
span<char const> a(v1);
TEST_CHECK(a == f(v1));
TEST_CHECK(a.size() == 4);
}
TORRENT_TEST(span_single_element)
{
char const v1 = 1;
span<char const> a(v1);
TEST_CHECK(a == f(v1));
TEST_CHECK(a.size() == 1);
}
TORRENT_TEST(span_of_spans)
{
std::vector<char> v1 = {1,2,3,4};
span<char> s1(v1);
span<span<char> > a(s1);
TEST_CHECK(a == g(s1));
TEST_CHECK(a.size() == 1);
TEST_CHECK(a[0].size() == 4);
}