Integer casts. Part 2. (#41)

This commit is contained in:
tower120
2020-04-21 08:27:18 +03:00
committed by Mindaugas Vinkelis
parent a544879b22
commit 3dc5940c16
16 changed files with 66 additions and 51 deletions

View File

@@ -48,7 +48,7 @@ namespace bitsery {
};
InputBufferAdapter(TIterator beginIt, TIterator endIt)
:InputBufferAdapter(beginIt, std::distance(beginIt, endIt)) {
:InputBufferAdapter(beginIt, static_cast<size_t>(std::distance(beginIt, endIt))) {
}
InputBufferAdapter(const InputBufferAdapter&) = delete;
@@ -103,6 +103,7 @@ namespace bitsery {
}
private:
using diff_t = typename std::iterator_traits<TIterator>::difference_type;
template <size_t SIZE>
void readInternalValue(TValue *data) {
@@ -115,17 +116,17 @@ namespace bitsery {
template <size_t SIZE>
void readInternalValueChecked(TValue *data, std::false_type) {
const auto newOffset = _currOffset + SIZE;
const size_t newOffset = _currOffset + SIZE;
assert(newOffset <= _endReadOffset);
std::copy_n(_beginIt + _currOffset, SIZE, data);
std::copy_n(_beginIt + static_cast<diff_t>(_currOffset), SIZE, data);
_currOffset = newOffset;
}
template <size_t SIZE>
void readInternalValueChecked(TValue *data, std::true_type) {
const auto newOffset = _currOffset + SIZE;
const size_t newOffset = _currOffset + SIZE;
if (newOffset <= _endReadOffset) {
std::copy_n(_beginIt + _currOffset, SIZE, data);
std::copy_n(_beginIt + static_cast<diff_t>(_currOffset), SIZE, data);
_currOffset = newOffset;
} else {
//set everything to zeros
@@ -136,16 +137,16 @@ namespace bitsery {
}
void readInternalBufferChecked(TValue *data, size_t size, std::false_type) {
const auto newOffset = _currOffset + size;
const size_t newOffset = _currOffset + size;
assert(newOffset <= _endReadOffset);
std::copy_n(_beginIt + _currOffset, size, data);
std::copy_n(_beginIt + static_cast<diff_t>(_currOffset), size, data);
_currOffset = newOffset;
}
void readInternalBufferChecked(TValue *data, size_t size, std::true_type) {
const auto newOffset = _currOffset + size;
const size_t newOffset = _currOffset + size;
if (newOffset <= _endReadOffset) {
std::copy_n(_beginIt + _currOffset, size, data);
std::copy_n(_beginIt + static_cast<diff_t>(_currOffset), size, data);
_currOffset = newOffset;
} else {
//set everything to zeros
@@ -229,6 +230,7 @@ namespace bitsery {
private:
using TResizable = std::integral_constant<bool, traits::ContainerTraits<Buffer>::isResizable>;
using diff_t = typename std::iterator_traits<TIterator>::difference_type;
template <size_t SIZE>
void writeInternalValue(const TValue *data) {
@@ -259,9 +261,9 @@ namespace bitsery {
template <size_t SIZE>
void writeInternalValueImpl(const TValue *data, std::true_type) {
const auto newOffset = _currOffset + SIZE;
const size_t newOffset = _currOffset + SIZE;
if (newOffset <= _bufferSize) {
std::copy_n(data, SIZE, _beginIt + _currOffset);
std::copy_n(data, SIZE, _beginIt + static_cast<diff_t>(_currOffset));
_currOffset = newOffset;
} else {
traits::BufferAdapterTraits<Buffer>::increaseBufferSize(*_buffer);
@@ -271,9 +273,9 @@ namespace bitsery {
}
void writeInternalBufferImpl(const TValue *data, const size_t size, std::true_type) {
const auto newOffset = _currOffset + size;
const size_t newOffset = _currOffset + size;
if (newOffset <= _bufferSize) {
std::copy_n(data, size, _beginIt + _currOffset);
std::copy_n(data, size, _beginIt + static_cast<diff_t>(_currOffset));
_currOffset = newOffset;
} else {
traits::BufferAdapterTraits<Buffer>::increaseBufferSize(*_buffer);
@@ -301,16 +303,16 @@ namespace bitsery {
template <size_t SIZE>
void writeInternalValueImpl(const TValue *data, std::false_type) {
const auto newOffset = _currOffset + SIZE;
const size_t newOffset = _currOffset + SIZE;
assert(newOffset <= _bufferSize);
std::copy_n(data, SIZE, _beginIt + _currOffset);
std::copy_n(data, SIZE, _beginIt + static_cast<diff_t>(_currOffset));
_currOffset = newOffset;
}
void writeInternalBufferImpl(const TValue *data, size_t size, std::false_type) {
const auto newOffset = _currOffset + size;
const size_t newOffset = _currOffset + size;
assert(newOffset <= _bufferSize);
std::copy_n(data, size, _beginIt + _currOffset);
std::copy_n(data, size, _beginIt + static_cast<diff_t>(_currOffset));
_currOffset = newOffset;
}

View File

@@ -93,7 +93,7 @@ namespace bitsery {
}
void readChecked(TValue* data, size_t size, std::true_type) {
if (size - static_cast<size_t>(_ios->rdbuf()->sgetn(data, size)) != _zeroIfNoErrors) {
if (size - static_cast<size_t>(_ios->rdbuf()->sgetn(data, static_cast<std::streamsize>(size))) != _zeroIfNoErrors) {
*data = {};
if (_zeroIfNoErrors == 0) {
error(_ios->rdstate() == std::ios_base::badbit
@@ -104,7 +104,7 @@ namespace bitsery {
}
void readChecked(TValue* data, size_t size, std::false_type) {
_ios->rdbuf()->sgetn(data , size);
_ios->rdbuf()->sgetn(data , static_cast<std::streamsize>(size));
}
std::basic_ios<TChar, CharTraits>* _ios;
@@ -223,6 +223,7 @@ namespace bitsery {
private:
using TResizable = std::integral_constant<bool, traits::ContainerTraits<TBuffer>::isResizable>;
using diff_t = typename std::iterator_traits<BufferIt>::difference_type;
template <size_t SIZE>
void writeInternalValue(const TValue* data) {
@@ -231,14 +232,14 @@ namespace bitsery {
writeBufferToStream();
newOffset = SIZE;
}
std::copy_n(data, SIZE, _beginIt + _currOffset);
std::copy_n(data, SIZE, _beginIt + static_cast<diff_t>(_currOffset));
_currOffset = newOffset;
}
void writeInternalBuffer(const TValue* data, size_t size) {
const auto newOffset = _currOffset + size;
if (newOffset <= _bufferSize) {
std::copy_n(data, size, _beginIt + _currOffset);
std::copy_n(data, size, _beginIt + static_cast<diff_t>(_currOffset));
_currOffset = newOffset;
} else {
writeBufferToStream();
@@ -248,7 +249,7 @@ namespace bitsery {
}
void writeBufferToStream() {
_ostream->rdbuf()->sputn(std::addressof(*_beginIt), _currOffset);
_ostream->rdbuf()->sputn(std::addressof(*_beginIt), static_cast<std::streamsize>(_currOffset));
_currOffset = 0;
}

View File

@@ -141,7 +141,7 @@ namespace bitsery {
}
auto shiftedRes =
static_cast<T>(m_scratch & ((static_cast<ScratchType>(1) << bits) - 1)) << (size - bitsLeft);
res |= shiftedRes;
res = static_cast<T>(res | shiftedRes);
m_scratch >>= bits;
m_scratchBits -= bits;
bitsLeft -= bits;
@@ -443,8 +443,11 @@ namespace bitsery {
void procContainer(It first, It last, std::true_type) {
using TValue = typename std::decay<decltype(*first)>::type;
using TIntegral = typename details::IntegralFromFundamental<TValue>::TValue;
if (first != last)
this->_adapter.template readBuffer<VSIZE>(reinterpret_cast<TIntegral*>(&(*first)), std::distance(first, last));
if (first != last){
const auto distance = std::distance(first, last);
assert(distance>=0);
this->_adapter.template readBuffer<VSIZE>(reinterpret_cast<TIntegral*>(&(*first)), static_cast<size_t>(distance));
}
}
//process by calling functions
@@ -465,7 +468,8 @@ namespace bitsery {
void procText(T& str, size_t length) {
auto begin = std::begin(str);
//end of string, not end of container
auto end = std::next(begin, length);
using diff_t = typename std::iterator_traits<decltype(begin)>::difference_type;
auto end = std::next(begin, static_cast<diff_t>(length));
procContainer<VSIZE>(begin, end, std::integral_constant<bool, traits::ContainerTraits<T>::isContiguous>{});
//null terminated character at the end
if (traits::TextTraits<T>::addNUL)

View File

@@ -139,7 +139,7 @@ namespace bitsery {
using UT = typename std::conditional<TSize == 1, uint8_t,
typename std::conditional<TSize == 2, uint16_t,
typename std::conditional<TSize == 4, uint32_t, uint64_t>::type>::type>::type;
return SwapImpl::exec(static_cast<UT>(value));
return static_cast<TValue>(SwapImpl::exec(static_cast<UT>(value)));
}
/**
@@ -176,7 +176,7 @@ namespace bitsery {
template<>
struct ScratchType<uint8_t> {
using type = uint16_t;
using type = uint_fast16_t;
};
/**
@@ -290,7 +290,8 @@ namespace bitsery {
template<typename T>
void swapDataBits(T *v, size_t count, std::true_type) {
std::for_each(v, std::next(v, count), [](T &x) { x = details::swap(x); });
using diff_t = typename std::iterator_traits<T*>::difference_type;
std::for_each(v, std::next(v, static_cast<diff_t>(count)), [](T &x) { x = details::swap(x); });
}
template<typename T>

View File

@@ -71,7 +71,7 @@ namespace bitsery {
StdPolyAlloc<PointerSharedStateBase> alloc{_memResource};
alloc.deallocate(data, 1);
}
MemResourceBase* _memResource;
MemResourceBase* _memResource = nullptr;
};
//PLC info is internal classes for serializer, and deserializer

View File

@@ -121,8 +121,8 @@ namespace bitsery {
details::SameSizeUnsigned<T> getRangeValue(const T &v, const RangeSpec<T> &r) {
using VT = details::SameSizeUnsigned<T>;
const VT maxUint = (static_cast<VT>(1) << r.bitsRequired) - 1;
const auto ratio = (v - r.min) / (r.max - r.min);
return static_cast<VT>(ratio * maxUint);
const T ratio = (v - r.min) / (r.max - r.min);
return static_cast<VT>(ratio * static_cast<T>(maxUint));
}
template<typename T, typename std::enable_if<std::is_integral<T>::value>::type * = nullptr>
@@ -141,7 +141,7 @@ namespace bitsery {
using UIT = details::SameSizeUnsigned<T>;
const auto intRep = reinterpret_cast<UIT &>(v);
const UIT maxUint = (static_cast<UIT>(1) << r.bitsRequired) - 1;
v = r.min + (static_cast<T>(intRep) / maxUint) * (r.max - r.min);
v = r.min + (static_cast<T>(intRep) / static_cast<T>(maxUint)) * (r.max - r.min);
}
template<typename T, typename std::enable_if<std::is_arithmetic<T>::value>::type * = nullptr>

View File

@@ -120,8 +120,8 @@ namespace bitsery {
template<typename T>
void writeBitsInternal(const T &v, size_t size) {
constexpr size_t valueSize = details::BitsSize<UnsignedType>::value;
auto value = v;
auto bitsLeft = size;
T value = v;
size_t bitsLeft = size;
while (bitsLeft > 0) {
auto bits = (std::min)(bitsLeft, valueSize);
_scratch |= static_cast<ScratchType>( value ) << _scratchBits;
@@ -132,7 +132,7 @@ namespace bitsery {
_scratch >>= valueSize;
_scratchBits -= valueSize;
value >>= valueSize;
value = static_cast<T>(value >> valueSize);
}
bitsLeft -= bits;
}
@@ -452,11 +452,12 @@ namespace bitsery {
//process text,
template<size_t VSIZE, typename T>
void procText(const T& str, size_t maxSize) {
auto length = traits::TextTraits<T>::length(str);
const size_t length = traits::TextTraits<T>::length(str);
assert((length + (traits::TextTraits<T>::addNUL ? 1u : 0u)) <= maxSize);
details::writeSize(this->_adapter, length);
auto begin = std::begin(str);
procContainer<VSIZE>(begin, std::next(begin, length), std::integral_constant<bool, traits::ContainerTraits<T>::isContiguous>{});
using diff_t = typename std::iterator_traits<decltype(begin)>::difference_type;
procContainer<VSIZE>(begin, std::next(begin, static_cast<diff_t>(length)), std::integral_constant<bool, traits::ContainerTraits<T>::isContiguous>{});
}
//process object types

View File

@@ -56,6 +56,8 @@ namespace bitsery {
resizeImpl(container, size, std::is_default_constructible<TValue>{});
}
private:
using diff_t = typename T::difference_type;
static void resizeImpl(T& container, size_t size, std::true_type) {
container.resize(size);
}
@@ -65,7 +67,7 @@ namespace bitsery {
container.push_back(::bitsery::Access::create<TValue>());
}
if (oldSize > newSize) {
container.erase(std::next(std::begin(container), newSize), std::end(container));
container.erase(std::next(std::begin(container), static_cast<diff_t>(newSize)), std::end(container));
}
}

View File

@@ -44,6 +44,7 @@ namespace bitsery {
resizeImpl(container, size, std::is_default_constructible<TValue>{});
}
private:
using diff_t = typename std::forward_list<T, Allocator>::difference_type;
static void resizeImpl(std::forward_list<T, Allocator>& container, size_t size, std::true_type) {
container.resize(size);
}
@@ -55,7 +56,7 @@ namespace bitsery {
if (oldSize > newSize) {
//erase_after must have atleast one element to work
if (newSize > 0)
container.erase_after(std::next(std::begin(container), newSize-1));
container.erase_after(std::next(std::begin(container), static_cast<diff_t>(newSize-1)));
else
container.clear();
}

View File

@@ -39,7 +39,10 @@ foreach (TestFile ${TestSourceFiles})
add_executable(${TestName} ${TestFile})
target_link_libraries(${TestName} PRIVATE GTest::Main Bitsery::bitsery)
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
target_compile_options(${TestName} PRIVATE -Wextra -Wno-missing-braces -Wpedantic -Weffc++ -Wno-c++14-extensions)
target_compile_options(${TestName} PRIVATE -Wextra -Wconversion -Wno-missing-braces -Wpedantic -Weffc++ -Werror)
endif()
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
target_compile_options(${TestName} PRIVATE -Wno-c++14-extensions)
endif()
gtest_discover_tests(${TestName})

View File

@@ -39,11 +39,11 @@
#include <bitsery/brief_syntax/tuple.h>
#include <bitsery/brief_syntax/variant.h>
#else
#if defined(_MSC_VER)
/*#if defined(_MSC_VER)
#pragma message("tuple and variant only works with c++17")
#else
#warning "tuple and variant only works with c++17"
#endif
#endif*/
#endif
#include <gmock/gmock.h>

View File

@@ -125,7 +125,7 @@ TYPED_TEST(SerializeContainerDynamicSizeArthmeticTypes, CustomFunctionIncrements
});
//decrement result by 1, before comparing for eq
for (auto &v:this->res)
v -= 1;
v = static_cast<TValue>(v-1);
EXPECT_THAT(ctx.getBufferSize(), Eq(this->getExpectedBufSize(ctx)));
EXPECT_THAT(this->res, ContainerEq(this->src));

View File

@@ -121,9 +121,9 @@ TEST(SerializeExtensionStdOptional, NoAlignAfterStateWriteRead) {
}
#else
#if defined(_MSC_VER)
/*#if defined(_MSC_VER)
#pragma message("Tests for StdOptional requires C++17")
#else
#warning "Tests for StdOptional requires C++17"
#endif
#endif*/
#endif

View File

@@ -71,7 +71,7 @@ void serialize(S& s, Derived& o) {
}
struct MoreDerived : Derived {
int8_t z{};
uint8_t z{};
MoreDerived() = default;

View File

@@ -119,9 +119,9 @@ TEST(SerializeExtensionStdTuple, NonDefaultConstructable) {
}
#else
#if defined(_MSC_VER)
/*#if defined(_MSC_VER)
#pragma message("Tests for StdTuple requires C++17")
#else
#warning "Tests for StdTuple requires C++17"
#endif
#endif*/
#endif

View File

@@ -168,9 +168,9 @@ TEST(SerializeExtensionStdVariant, CorrectlyHandleMonoState) {
}
#else
#if defined(_MSC_VER)
/*#if defined(_MSC_VER)
#pragma message("Tests for StdVariant requires C++17")
#else
#warning "Tests for StdVariant requires C++17"
#endif
#endif*/
#endif