diff --git a/chrono/datetime.cpp b/chrono/datetime.cpp index b75820f..3929385 100644 --- a/chrono/datetime.cpp +++ b/chrono/datetime.cpp @@ -416,29 +416,29 @@ std::uint64_t DateTime::timeToTicks(int hour, int minute, int second, double mil */ int DateTime::getDatePart(DatePart part) const { - const int fullDays = m_ticks / TimeSpan::ticksPerDay; - const int full400YearBlocks = fullDays / m_daysPer400Years; - const int daysMinusFull400YearBlocks = fullDays - full400YearBlocks * m_daysPer400Years; - int full100YearBlocks = daysMinusFull400YearBlocks / m_daysPer100Years; + const auto fullDays = static_cast(m_ticks / TimeSpan::ticksPerDay); + const auto full400YearBlocks = fullDays / m_daysPer400Years; + const auto daysMinusFull400YearBlocks = fullDays - full400YearBlocks * m_daysPer400Years; + auto full100YearBlocks = daysMinusFull400YearBlocks / m_daysPer100Years; if (full100YearBlocks == 4) { full100YearBlocks = 3; } - const int daysMinusFull100YearBlocks = daysMinusFull400YearBlocks - full100YearBlocks * m_daysPer100Years; - const int full4YearBlocks = daysMinusFull100YearBlocks / m_daysPer4Years; - const int daysMinusFull4YearBlocks = daysMinusFull100YearBlocks - full4YearBlocks * m_daysPer4Years; - int full1YearBlocks = daysMinusFull4YearBlocks / m_daysPerYear; + const auto daysMinusFull100YearBlocks = daysMinusFull400YearBlocks - full100YearBlocks * m_daysPer100Years; + const auto full4YearBlocks = daysMinusFull100YearBlocks / m_daysPer4Years; + const auto daysMinusFull4YearBlocks = daysMinusFull100YearBlocks - full4YearBlocks * m_daysPer4Years; + auto full1YearBlocks = daysMinusFull4YearBlocks / m_daysPerYear; if (full1YearBlocks == 4) { full1YearBlocks = 3; } if (part == DatePart::Year) { return full400YearBlocks * 400 + full100YearBlocks * 100 + full4YearBlocks * 4 + full1YearBlocks + 1; } - const int restDays = daysMinusFull4YearBlocks - full1YearBlocks * m_daysPerYear; + const auto restDays = daysMinusFull4YearBlocks - full1YearBlocks * m_daysPerYear; if (part == DatePart::DayOfYear) { // day return restDays + 1; } - const int *const daysToMonth = (full1YearBlocks == 3 && (full4YearBlocks != 24 || full100YearBlocks == 3)) ? m_daysToMonth366 : m_daysToMonth365; - int month = 1; + const auto *const daysToMonth = (full1YearBlocks == 3 && (full4YearBlocks != 24 || full100YearBlocks == 3)) ? m_daysToMonth366 : m_daysToMonth365; + auto month = 1; while (restDays >= daysToMonth[month]) { ++month; } diff --git a/chrono/datetime.h b/chrono/datetime.h index e3173f6..45c280a 100644 --- a/chrono/datetime.h +++ b/chrono/datetime.h @@ -306,7 +306,7 @@ constexpr inline DayOfWeek DateTime::dayOfWeek() const */ constexpr inline int DateTime::hour() const { - return m_ticks / TimeSpan::ticksPerHour % 24ul; + return static_cast(m_ticks / TimeSpan::ticksPerHour % 24ul); } /*! @@ -314,7 +314,7 @@ constexpr inline int DateTime::hour() const */ constexpr inline int DateTime::minute() const { - return m_ticks / TimeSpan::ticksPerMinute % 60ul; + return static_cast(m_ticks / TimeSpan::ticksPerMinute % 60ul); } /*! @@ -322,7 +322,7 @@ constexpr inline int DateTime::minute() const */ constexpr inline int DateTime::second() const { - return m_ticks / TimeSpan::ticksPerSecond % 60ul; + return static_cast(m_ticks / TimeSpan::ticksPerSecond % 60ul); } /*! @@ -330,7 +330,7 @@ constexpr inline int DateTime::second() const */ constexpr inline int DateTime::millisecond() const { - return m_ticks / TimeSpan::ticksPerMillisecond % 1000ul; + return static_cast(m_ticks / TimeSpan::ticksPerMillisecond % 1000ul); } /*! @@ -338,7 +338,7 @@ constexpr inline int DateTime::millisecond() const */ constexpr inline int DateTime::microsecond() const { - return m_ticks / TimeSpan::ticksPerMicrosecond % 1000ul; + return static_cast(m_ticks / TimeSpan::ticksPerMicrosecond % 1000ul); } /*! @@ -348,7 +348,7 @@ constexpr inline int DateTime::microsecond() const */ constexpr inline int DateTime::nanosecond() const { - return m_ticks % 10ul * TimeSpan::nanosecondsPerTick; + return static_cast(m_ticks % 10ul * TimeSpan::nanosecondsPerTick); } /*! @@ -365,7 +365,7 @@ constexpr inline bool DateTime::isNull() const */ constexpr inline TimeSpan DateTime::timeOfDay() const { - return TimeSpan(m_ticks % TimeSpan::ticksPerDay); + return TimeSpan(static_cast(m_ticks % TimeSpan::ticksPerDay)); } /*! @@ -425,7 +425,7 @@ inline std::string DateTime::toString(DateTimeOutputFormat format, bool noMillis */ constexpr std::time_t DateTime::toTimeStamp() const { - return (totalTicks() - DateTime::unixEpochStart().totalTicks()) / TimeSpan::ticksPerSecond; + return static_cast((totalTicks() - DateTime::unixEpochStart().totalTicks()) / TimeSpan::ticksPerSecond); } /*! @@ -516,7 +516,7 @@ constexpr inline bool DateTime::operator>=(const DateTime &other) const */ constexpr inline DateTime DateTime::operator+(const TimeSpan &timeSpan) const { - return DateTime(m_ticks + timeSpan.m_ticks); + return DateTime(m_ticks + static_cast(timeSpan.m_ticks)); } /*! @@ -525,7 +525,7 @@ constexpr inline DateTime DateTime::operator+(const TimeSpan &timeSpan) const */ constexpr inline DateTime DateTime::operator-(const TimeSpan &timeSpan) const { - return DateTime(m_ticks - timeSpan.m_ticks); + return DateTime(m_ticks - static_cast(timeSpan.m_ticks)); } /*! @@ -534,7 +534,7 @@ constexpr inline DateTime DateTime::operator-(const TimeSpan &timeSpan) const */ constexpr inline TimeSpan DateTime::operator+(const DateTime &other) const { - return TimeSpan(m_ticks + other.m_ticks); + return TimeSpan(static_cast(m_ticks + other.m_ticks)); } /*! @@ -545,7 +545,7 @@ constexpr inline TimeSpan DateTime::operator+(const DateTime &other) const */ constexpr inline TimeSpan DateTime::operator-(const DateTime &other) const { - return TimeSpan(m_ticks - other.m_ticks); + return TimeSpan(static_cast(m_ticks - other.m_ticks)); } /*! @@ -553,7 +553,7 @@ constexpr inline TimeSpan DateTime::operator-(const DateTime &other) const */ inline DateTime &DateTime::operator+=(const TimeSpan &timeSpan) { - m_ticks += timeSpan.m_ticks; + m_ticks += static_cast(timeSpan.m_ticks); return *this; } @@ -562,7 +562,7 @@ inline DateTime &DateTime::operator+=(const TimeSpan &timeSpan) */ inline DateTime &DateTime::operator-=(const TimeSpan &timeSpan) { - m_ticks -= timeSpan.m_ticks; + m_ticks += static_cast(timeSpan.m_ticks); return *this; } } // namespace CppUtilities diff --git a/chrono/timespan.h b/chrono/timespan.h index 9e4b1b7..9c245fa 100644 --- a/chrono/timespan.h +++ b/chrono/timespan.h @@ -247,7 +247,7 @@ constexpr inline double TimeSpan::totalDays() const */ constexpr int TimeSpan::nanoseconds() const { - return m_ticks % 10l * TimeSpan::nanosecondsPerTick; + return static_cast(m_ticks % 10l * TimeSpan::nanosecondsPerTick); } /*! @@ -255,7 +255,7 @@ constexpr int TimeSpan::nanoseconds() const */ constexpr int TimeSpan::microseconds() const { - return (m_ticks / ticksPerMicrosecond) % 1000l; + return static_cast((m_ticks / ticksPerMicrosecond) % 1000l); } /*! @@ -263,7 +263,7 @@ constexpr int TimeSpan::microseconds() const */ constexpr inline int TimeSpan::milliseconds() const { - return (m_ticks / ticksPerMillisecond) % 1000l; + return static_cast((m_ticks / ticksPerMillisecond) % 1000l); } /*! @@ -271,7 +271,7 @@ constexpr inline int TimeSpan::milliseconds() const */ constexpr inline int TimeSpan::seconds() const { - return (m_ticks / ticksPerSecond) % 60l; + return static_cast((m_ticks / ticksPerSecond) % 60l); } /*! @@ -279,7 +279,7 @@ constexpr inline int TimeSpan::seconds() const */ constexpr inline int TimeSpan::minutes() const { - return (m_ticks / ticksPerMinute) % 60l; + return static_cast((m_ticks / ticksPerMinute) % 60l); } /*! @@ -287,7 +287,7 @@ constexpr inline int TimeSpan::minutes() const */ constexpr inline int TimeSpan::hours() const { - return (m_ticks / ticksPerHour) % 24l; + return static_cast((m_ticks / ticksPerHour) % 24l); } /*! @@ -295,7 +295,7 @@ constexpr inline int TimeSpan::hours() const */ constexpr inline int TimeSpan::days() const { - return (m_ticks / ticksPerDay); + return static_cast((m_ticks / ticksPerDay)); } /*! @@ -367,7 +367,7 @@ constexpr inline TimeSpan TimeSpan::operator-(const TimeSpan &other) const */ constexpr inline TimeSpan TimeSpan::operator*(double factor) const { - return TimeSpan(m_ticks * factor); + return TimeSpan(static_cast(static_cast(m_ticks) * factor)); } /*! @@ -375,7 +375,7 @@ constexpr inline TimeSpan TimeSpan::operator*(double factor) const */ constexpr inline TimeSpan TimeSpan::operator/(double factor) const { - return TimeSpan(m_ticks / factor); + return TimeSpan(static_cast(static_cast(m_ticks) / factor)); } /*! @@ -409,7 +409,7 @@ inline TimeSpan &TimeSpan::operator-=(const TimeSpan &other) */ inline TimeSpan &TimeSpan::operator*=(double factor) { - m_ticks = m_ticks * factor; + m_ticks = static_cast(static_cast(m_ticks) * factor); return *this; } @@ -418,7 +418,7 @@ inline TimeSpan &TimeSpan::operator*=(double factor) */ inline TimeSpan &TimeSpan::operator/=(double factor) { - m_ticks = m_ticks / factor; + m_ticks = static_cast(static_cast(m_ticks) / factor); return *this; } diff --git a/conversion/binaryconversion.h b/conversion/binaryconversion.h index 6c48cbb..ba71249 100644 --- a/conversion/binaryconversion.h +++ b/conversion/binaryconversion.h @@ -153,7 +153,7 @@ CPP_UTILITIES_EXPORT constexpr std::uint32_t toNormalInt(std::uint32_t synchsafe */ CPP_UTILITIES_EXPORT constexpr std::uint16_t swapOrder(std::uint16_t value) { - return ((value >> 8) & 0x00FF) | ((value << 8) & 0xFF00); + return static_cast(((value >> 8) & 0x00FF) | ((value << 8) & 0xFF00)); } /*! diff --git a/conversion/binaryconversionprivate.h b/conversion/binaryconversionprivate.h index 1be6e9f..989c128 100644 --- a/conversion/binaryconversionprivate.h +++ b/conversion/binaryconversionprivate.h @@ -6,15 +6,21 @@ #include +// disable warnings about sign conversions when using GCC or Clang +#ifdef __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wsign-conversion" +#endif + /*! * \brief Returns a 16-bit signed integer converted from two bytes at a specified position in a char array. */ CPP_UTILITIES_EXPORT constexpr std::int16_t toInt16(const char *value) { #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0 - return (static_cast(value[0]) << 8 & 0xFF00) | (static_cast(value[1]) & 0x00FF); + return static_cast((static_cast(value[0]) << 8 & 0xFF00) | (static_cast(value[1]) & 0x00FF)); #else - return (static_cast(value[1]) << 8 & 0xFF00) | (static_cast(value[0]) & 0x00FF); + return static_cast((static_cast(value[1]) << 8 & 0xFF00) | (static_cast(value[0]) & 0x00FF)); #endif } @@ -24,9 +30,9 @@ CPP_UTILITIES_EXPORT constexpr std::int16_t toInt16(const char *value) CPP_UTILITIES_EXPORT constexpr std::uint16_t toUInt16(const char *value) { #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0 - return (static_cast(value[0]) << 8 & 0xFF00) | (static_cast(value[1]) & 0x00FF); + return static_cast((static_cast(value[0]) << 8 & 0xFF00) | (static_cast(value[1]) & 0x00FF)); #else - return (static_cast(value[1]) << 8 & 0xFF00) | (static_cast(value[0]) & 0x00FF); + return static_cast((static_cast(value[1]) << 8 & 0xFF00) | (static_cast(value[0]) & 0x00FF)); #endif } @@ -36,11 +42,13 @@ CPP_UTILITIES_EXPORT constexpr std::uint16_t toUInt16(const char *value) CPP_UTILITIES_EXPORT constexpr std::int32_t toInt32(const char *value) { #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0 - return (static_cast(value[0]) << 24 & 0xFF000000) | (static_cast(value[1]) << 16 & 0x00FF0000) - | (static_cast(value[2]) << 8 & 0x0000FF00) | (static_cast(value[3]) & 0x000000FF); + return static_cast((static_cast(value[0]) << 24 & 0xFF000000) + | (static_cast(value[1]) << 16 & 0x00FF0000) | (static_cast(value[2]) << 8 & 0x0000FF00) + | (static_cast(value[3]) & 0x000000FF)); #else - return (static_cast(value[3]) << 24 & 0xFF000000) | (static_cast(value[2]) << 16 & 0x00FF0000) - | (static_cast(value[1]) << 8 & 0x0000FF00) | (static_cast(value[0]) & 0x000000FF); + return static_cast((static_cast(value[3]) << 24 & 0xFF000000) + | (static_cast(value[2]) << 16 & 0x00FF0000) | (static_cast(value[1]) << 8 & 0x0000FF00) + | (static_cast(value[0]) & 0x000000FF)); #endif } @@ -125,7 +133,7 @@ CPP_UTILITIES_EXPORT inline double toFloat64(const char *value) { const auto val = toInt64(value); const auto *const c = reinterpret_cast(&val); - return *reinterpret_cast(c); + return *reinterpret_cast(c); } /*! @@ -281,4 +289,8 @@ CPP_UTILITIES_EXPORT inline void getBytes(double value, char *outputbuffer) getBytes(i, outputbuffer); } +#ifdef __GNUC__ +#pragma GCC diagnostic pop +#endif + #endif // CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL diff --git a/conversion/stringbuilder.h b/conversion/stringbuilder.h index d9f67e6..a1f4dc2 100644 --- a/conversion/stringbuilder.h +++ b/conversion/stringbuilder.h @@ -101,9 +101,9 @@ constexpr std::size_t computeTupleElementSize(CharType) template >, std::is_integral, std::is_unsigned> * = nullptr> -constexpr std::size_t computeTupleElementSize(IntegralType number, typename StringType::value_type base = 10) +constexpr std::size_t computeTupleElementSize(IntegralType number, IntegralType base = 10) { - std::size_t size = 0; + auto size = std::size_t(0u); for (auto n = number; n; n /= base, ++size) ; return size; @@ -112,9 +112,9 @@ constexpr std::size_t computeTupleElementSize(IntegralType number, typename Stri template >, std::is_integral, std::is_signed> * = nullptr> -constexpr std::size_t computeTupleElementSize(IntegralType number, typename StringType::value_type base = 10) +constexpr std::size_t computeTupleElementSize(IntegralType number, IntegralType base = 10) { - std::size_t size = number < 0 ? 1 : 0; + auto size = std::size_t(number < 0 ? 1u : 0u); for (auto n = number; n; n /= base, ++size) ; return size; @@ -176,11 +176,11 @@ inline void append(StringType &target, CharType c) template >, std::is_integral, std::is_unsigned> * = nullptr> -inline void append(StringType &target, IntegralType number, typename StringType::value_type base = 10) +inline void append(StringType &target, IntegralType number, IntegralType base = 10) { - const auto start = target.begin() + target.size(); + const auto start = target.begin() + static_cast(target.size()); do { - target.insert(start, digitToChar(number % base)); + target.insert(start, static_cast(digitToChar(number % base))); number /= base; } while (number); } @@ -188,15 +188,15 @@ inline void append(StringType &target, IntegralType number, typename StringType: template >, std::is_integral, std::is_signed> * = nullptr> -inline void append(StringType &target, IntegralType number, typename StringType::value_type base = 10) +inline void append(StringType &target, IntegralType number, IntegralType base = 10) { if (number < 0) { target += '-'; number = -number; } - const auto start = target.begin() + target.size(); + const auto start = target.begin() + static_cast(target.size()); do { - target.insert(start, digitToChar(number % base)); + target.insert(start, static_cast(digitToChar(number % base))); number /= base; } while (number); } @@ -232,12 +232,14 @@ template struct TupleToString, std::tuple>> *> constexpr std::size_t computeTupleElementSize(TupleType &&tuple, typename StringType::value_type base) { + CPP_UTILITIES_UNUSED(base) return TupleToString>>::precomputeSize(std::forward(tuple)); } template , std::tuple>> *> constexpr void append(StringType &target, TupleType &&tuple, typename StringType::value_type base) { + CPP_UTILITIES_UNUSED(base) return TupleToString>>::append(std::forward(tuple), target); } diff --git a/conversion/stringconversion.cpp b/conversion/stringconversion.cpp index 80e81f7..caef14f 100644 --- a/conversion/stringconversion.cpp +++ b/conversion/stringconversion.cpp @@ -49,7 +49,7 @@ struct Factor { : factor(factor){}; size_t operator()(size_t value) { - return static_cast(value * factor); + return static_cast(static_cast(value) * factor); } float factor; }; @@ -340,13 +340,13 @@ const char base64Pad = '='; */ string encodeBase64(const std::uint8_t *data, std::uint32_t dataSize) { - string encoded; - std::uint8_t mod = dataSize % 3; + auto encoded = std::string(); + auto mod = static_cast(dataSize % 3); + auto temp = std::uint32_t(); encoded.reserve(((dataSize / 3) + (mod > 0)) * 4); - std::uint32_t temp; for (const std::uint8_t *end = --data + dataSize - mod; data != end;) { - temp = *++data << 16; - temp |= *++data << 8; + temp = static_cast(*++data << 16); + temp |= static_cast(*++data << 8); temp |= *++data; encoded.push_back(base64Chars[(temp & 0x00FC0000) >> 18]); encoded.push_back(base64Chars[(temp & 0x0003F000) >> 12]); @@ -355,15 +355,15 @@ string encodeBase64(const std::uint8_t *data, std::uint32_t dataSize) } switch (mod) { case 1: - temp = *++data << 16; + temp = static_cast(*++data << 16); encoded.push_back(base64Chars[(temp & 0x00FC0000) >> 18]); encoded.push_back(base64Chars[(temp & 0x0003F000) >> 12]); encoded.push_back(base64Pad); encoded.push_back(base64Pad); break; case 2: - temp = *++data << 16; - temp |= *++data << 8; + temp = static_cast(*++data << 16); + temp |= static_cast(*++data << 8); encoded.push_back(base64Chars[(temp & 0x00FC0000) >> 18]); encoded.push_back(base64Chars[(temp & 0x0003F000) >> 12]); encoded.push_back(base64Chars[(temp & 0x00000FC0) >> 6]); @@ -412,11 +412,11 @@ pair, std::uint32_t> decodeBase64(const char *encoded } else if (*encodedStr == base64Pad) { switch (end - encodedStr) { case 1: - *++iter = (temp >> 16) & 0xFF; - *++iter = (temp >> 8) & 0xFF; + *++iter = static_cast((temp >> 16) & 0xFF); + *++iter = static_cast((temp >> 8) & 0xFF); return make_pair(move(buffer), decodedSize); case 2: - *++iter = (temp >> 10) & 0xFF; + *++iter = static_cast((temp >> 10) & 0xFF); return make_pair(move(buffer), decodedSize); default: throw ConversionException("invalid padding in base64"); @@ -425,9 +425,9 @@ pair, std::uint32_t> decodeBase64(const char *encoded throw ConversionException("invalid character in base64"); } } - *++iter = (temp >> 16) & 0xFF; - *++iter = (temp >> 8) & 0xFF; - *++iter = (temp)&0xFF; + *++iter = static_cast((temp >> 16) & 0xFF); + *++iter = static_cast((temp >> 8) & 0xFF); + *++iter = static_cast(temp & 0xFF); } return make_pair(move(buffer), decodedSize); } diff --git a/conversion/stringconversion.h b/conversion/stringconversion.h index e8b2fa1..be91b3d 100644 --- a/conversion/stringconversion.h +++ b/conversion/stringconversion.h @@ -281,7 +281,8 @@ template bool endsWith(const StringType &str, const String if (str.size() < phrase.size()) { return false; } - for (auto stri = str.cend() - phrase.size(), strend = str.cend(), phrasei = phrase.cbegin(); stri != strend; ++stri, ++phrasei) { + for (auto stri = str.cend() - static_cast(phrase.size()), strend = str.cend(), phrasei = phrase.cbegin(); + stri != strend; ++stri, ++phrasei) { if (*stri != *phrasei) { return false; } @@ -298,7 +299,8 @@ template bool endsWith(const StringType &str, const typena if (str.size() < phraseSize) { return false; } - for (auto stri = str.cend() - phraseSize, strend = str.cend(); stri != strend; ++stri, ++phrase) { + for (auto stri = str.cend() - static_cast(phraseSize), strend = str.cend(); stri != strend; + ++stri, ++phrase) { if (*stri != *phrase) { return false; } @@ -397,7 +399,7 @@ template constexpr CharType digitToChar(CharType digit) */ template , std::is_unsigned> * = nullptr> -StringType numberToString(IntegralType number, typename StringType::value_type base = 10) +StringType numberToString(IntegralType number, IntegralType base = 10) { std::size_t resSize = 0; for (auto n = number; n; n /= base, ++resSize) @@ -405,7 +407,7 @@ StringType numberToString(IntegralType number, typename StringType::value_type b StringType res; res.reserve(resSize); do { - res.insert(res.begin(), digitToChar(number % base)); + res.insert(res.begin(), digitToChar(static_cast(number % base))); number /= base; } while (number); return res; @@ -419,7 +421,7 @@ StringType numberToString(IntegralType number, typename StringType::value_type b */ template , std::is_signed> * = nullptr> -StringType numberToString(IntegralType number, typename StringType::value_type base = 10) +StringType numberToString(IntegralType number, IntegralType base = 10) { const bool negative = number < 0; std::size_t resSize; @@ -433,7 +435,7 @@ StringType numberToString(IntegralType number, typename StringType::value_type b StringType res; res.reserve(resSize); do { - res.insert(res.begin(), digitToChar(number % base)); + res.insert(res.begin(), digitToChar(static_cast(number % base))); number /= base; } while (number); if (negative) { @@ -451,7 +453,7 @@ StringType numberToString(IntegralType number, typename StringType::value_type b * \sa stringToNumber(), bufferToNumber() */ template > * = nullptr> -StringType numberToString(FloatingType number, typename StringType::value_type base = 10) +StringType numberToString(FloatingType number, int base = 10) { std::basic_stringstream ss; ss << std::setbase(base) << number; @@ -479,7 +481,7 @@ template CharType charToDigit(CharType character, CharType b std::string errorMsg; errorMsg.reserve(36); errorMsg += "The character \""; - errorMsg += character; + errorMsg += character >= ' ' && character <= '~' ? static_cast(character) : '?'; errorMsg += "\" is no valid digit."; throw ConversionException(std::move(errorMsg)); } @@ -493,7 +495,7 @@ template CharType charToDigit(CharType character, CharType b * \todo Provide an alternative using std::expected (when switching to C++17). */ template , std::is_unsigned> * = nullptr> -IntegralType stringToNumber(const StringType &string, typename StringType::value_type base = 10) +IntegralType stringToNumber(const StringType &string, IntegralType base = 10) { IntegralType result = 0; for (const auto &c : string) { @@ -501,7 +503,7 @@ IntegralType stringToNumber(const StringType &string, typename StringType::value continue; } result *= base; - result += charToDigit(c, base); + result += static_cast(charToDigit(c, static_cast(base))); } return result; } @@ -515,7 +517,7 @@ IntegralType stringToNumber(const StringType &string, typename StringType::value * \todo Provide an alternative using std::expected (when switching to C++17). */ template , std::is_signed> * = nullptr> -IntegralType stringToNumber(const StringType &string, typename StringType::value_type base = 10) +IntegralType stringToNumber(const StringType &string, IntegralType base = 10) { auto i = string.begin(); auto end = string.end(); @@ -534,7 +536,7 @@ IntegralType stringToNumber(const StringType &string, typename StringType::value continue; } result *= base; - result += charToDigit(*i, base); + result += static_cast(charToDigit(*i, static_cast(base))); } return negative ? -result : result; } @@ -550,7 +552,7 @@ IntegralType stringToNumber(const StringType &string, typename StringType::value * \todo Provide an alternative using std::expected (when switching to C++17). */ template > * = nullptr> -FloatingType stringToNumber(const StringType &string, typename StringType::value_type base = 10) +FloatingType stringToNumber(const StringType &string, int base = 10) { std::basic_stringstream ss; ss << std::setbase(base) << string; @@ -575,7 +577,7 @@ FloatingType stringToNumber(const StringType &string, typename StringType::value * \todo Provide an alternative using std::expected (when switching to C++17). */ template , std::is_unsigned> * = nullptr> -IntegralType stringToNumber(const CharType *string, unsigned char base = 10) +IntegralType stringToNumber(const CharType *string, IntegralType base = 10) { IntegralType result = 0; for (; *string; ++string) { @@ -583,7 +585,7 @@ IntegralType stringToNumber(const CharType *string, unsigned char base = 10) continue; } result *= base; - result += charToDigit(*string, base); + result += static_cast(charToDigit(*string, static_cast(base))); } return result; } @@ -599,7 +601,7 @@ IntegralType stringToNumber(const CharType *string, unsigned char base = 10) * \todo Provide an alternative using std::expected (when switching to C++17). */ template > * = nullptr> -FloatingType stringToNumber(const CharType *string, unsigned char base = 10) +FloatingType stringToNumber(const CharType *string, int base = 10) { std::basic_stringstream ss; ss << std::setbase(base) << string; @@ -623,7 +625,7 @@ FloatingType stringToNumber(const CharType *string, unsigned char base = 10) * \sa numberToString(), stringToNumber() */ template , std::is_unsigned> * = nullptr> -IntegralType bufferToNumber(const CharType *string, std::size_t size, unsigned char base = 10) +IntegralType bufferToNumber(const CharType *string, std::size_t size, IntegralType base = 10) { IntegralType result = 0; for (const CharType *end = string + size; string != end; ++string) { @@ -631,7 +633,7 @@ IntegralType bufferToNumber(const CharType *string, std::size_t size, unsigned c continue; } result *= base; - result += charToDigit(*string, base); + result += static_cast(charToDigit(*string, static_cast(base))); } return result; } @@ -644,7 +646,7 @@ IntegralType bufferToNumber(const CharType *string, std::size_t size, unsigned c * \sa numberToString(), bufferToNumber() */ template , std::is_signed> * = nullptr> -IntegralType stringToNumber(const CharType *string, unsigned char base = 10) +IntegralType stringToNumber(const CharType *string, IntegralType base = 10) { if (!*string) { return 0; @@ -664,7 +666,7 @@ IntegralType stringToNumber(const CharType *string, unsigned char base = 10) continue; } result *= base; - result += charToDigit(*string, base); + result += static_cast(charToDigit(*string, static_cast(base))); } return negative ? -result : result; } @@ -677,7 +679,7 @@ IntegralType stringToNumber(const CharType *string, unsigned char base = 10) * \sa numberToString(), stringToNumber() */ template , std::is_signed> * = nullptr> -IntegralType bufferToNumber(const CharType *string, std::size_t size, unsigned char base = 10) +IntegralType bufferToNumber(const CharType *string, std::size_t size, IntegralType base = 10) { if (!size) { return 0; @@ -698,7 +700,7 @@ IntegralType bufferToNumber(const CharType *string, std::size_t size, unsigned c continue; } result *= base; - result += charToDigit(*string, base); + result += static_cast(charToDigit(*string, static_cast(base))); } return negative ? -result : result; } @@ -716,7 +718,7 @@ template std::string interpretIntegerAsString(T integer, int startO { char buffer[sizeof(T)]; BE::getBytes(integer, buffer); - return std::string(buffer + startOffset, sizeof(T) - startOffset); + return std::string(buffer + startOffset, sizeof(T) - static_cast(startOffset)); } CPP_UTILITIES_EXPORT std::string dataSizeToString(std::uint64_t sizeInByte, bool includeByte = false); diff --git a/io/binaryreader.cpp b/io/binaryreader.cpp index e111d1b..02d6cfb 100644 --- a/io/binaryreader.cpp +++ b/io/binaryreader.cpp @@ -87,7 +87,7 @@ void BinaryReader::bufferVariableLengthInteger() } memset(m_buffer, 0, maxPrefixLength); m_stream->read(m_buffer + (maxPrefixLength - prefixLength), prefixLength); - *(m_buffer + (maxPrefixLength - prefixLength)) ^= mask; + *(m_buffer + (maxPrefixLength - prefixLength)) ^= static_cast(mask); } /*! diff --git a/io/binarywriter.h b/io/binarywriter.h index 8912c25..29f9cf6 100644 --- a/io/binarywriter.h +++ b/io/binarywriter.h @@ -534,7 +534,7 @@ inline void BinaryWriter::writeFloat64LE(double value) */ inline void BinaryWriter::writeString(const std::string &value) { - m_stream->write(value.c_str(), value.length()); + m_stream->write(value.data(), static_cast(value.size())); } /*! @@ -542,7 +542,7 @@ inline void BinaryWriter::writeString(const std::string &value) */ inline void BinaryWriter::writeTerminatedString(const std::string &value) { - m_stream->write(value.c_str(), value.length() + 1); + m_stream->write(value.data(), static_cast(value.size() + 1)); } /*! diff --git a/io/bitreader.cpp b/io/bitreader.cpp index 8d147a7..584a01b 100644 --- a/io/bitreader.cpp +++ b/io/bitreader.cpp @@ -18,7 +18,7 @@ namespace CppUtilities { void BitReader::skipBits(std::size_t bitCount) { if (bitCount <= m_bitsAvail) { - m_bitsAvail -= bitCount; + m_bitsAvail -= static_cast(bitCount); } else { if ((m_buffer += 1 + (bitCount -= m_bitsAvail) / 8) >= m_end) { throw ios_base::failure("end of buffer exceeded"); diff --git a/io/bitreader.h b/io/bitreader.h index 913ad66..348a66c 100644 --- a/io/bitreader.h +++ b/io/bitreader.h @@ -75,7 +75,8 @@ template intType BitReader::readBits(std::uint8_t bitCount) m_bitsAvail = 8; } readAtOnce = std::min(bitCount, m_bitsAvail); - val = static_cast((val << readAtOnce) | (((*m_buffer) >> (m_bitsAvail -= readAtOnce)) & (0xFF >> (0x08 - readAtOnce)))); + val = static_cast( + (val << readAtOnce) | static_cast(((*m_buffer) >> (m_bitsAvail -= readAtOnce)) & (0xFF >> (0x08 - readAtOnce)))); } return val; } @@ -104,7 +105,7 @@ template intType BitReader::readUnsignedExpGolombCodedBits() while (!readBit()) { ++count; } - return count ? (((1 << count) | readBits(count)) - 1) : 0; + return count ? static_cast(((1 << count) | readBits(count)) - 1) : 0; } /*! diff --git a/io/copy.h b/io/copy.h index 5c7b309..05c00b7 100644 --- a/io/copy.h +++ b/io/copy.h @@ -47,8 +47,8 @@ template void CopyHelper::copy(std::istream output.write(m_buffer, bufferSize); count -= bufferSize; } - input.read(m_buffer, count); - output.write(m_buffer, count); + input.read(m_buffer, static_cast(count)); + output.write(m_buffer, static_cast(count)); } /*! @@ -65,7 +65,7 @@ template void CopyHelper::callbackCopy(std::istream &input, std::ostream &output, std::size_t count, const std::function &isAborted, const std::function &callback) { - const std::size_t totalBytes = count; + const auto totalBytes = count; while (count > bufferSize) { input.read(m_buffer, bufferSize); output.write(m_buffer, bufferSize); @@ -73,10 +73,10 @@ void CopyHelper::callbackCopy(std::istream &input, std::ostream &out if (isAborted()) { return; } - callback(static_cast(totalBytes - count) / totalBytes); + callback(static_cast(totalBytes - count) / static_cast(totalBytes)); } - input.read(m_buffer, count); - output.write(m_buffer, count); + input.read(m_buffer, static_cast(count)); + output.write(m_buffer, static_cast(count)); callback(1.0); } diff --git a/misc/multiarray.h b/misc/multiarray.h index a370c08..5998348 100644 --- a/misc/multiarray.h +++ b/misc/multiarray.h @@ -12,22 +12,22 @@ namespace Detail { template struct DimensionsHelper { static std::size_t requiredSize(const Tuple &dimensionSizes) { - return DimensionsHelper::requiredSize(dimensionSizes) * std::get(dimensionSizes); + return DimensionsHelper::requiredSize(dimensionSizes) * static_cast(std::get(dimensionSizes)); } static std::size_t offset(const Tuple &dimensions, const Tuple &indices, std::size_t factor) { - return DimensionsHelper::offset(dimensions, indices, factor * std::get(dimensions)) - + (factor * std::get(indices)); + return DimensionsHelper::offset(dimensions, indices, factor * static_cast(std::get(dimensions))) + + (factor * static_cast(std::get(indices))); } }; template struct DimensionsHelper { static std::size_t requiredSize(const Tuple &dimensionSizes) { - return std::get<0>(dimensionSizes); + return static_cast(std::get<0>(dimensionSizes)); } static std::size_t offset(const Tuple &, const Tuple &indices, std::size_t factor) { - return factor * std::get<0>(indices); + return factor * static_cast(std::get<0>(indices)); } }; } // namespace Detail @@ -70,6 +70,9 @@ struct NoneOwningMultiArray { }; /// \brief The MultiArray class provides an *N*-dimensional array. +/// \tparam T Specifies the type of the data the MultiArray is supposed to contain. +/// \tparam UnderlyingContainer Specifies the type of the underlying container to use. +/// \tparam Dimentions Specifies the types used to store the limit/size of the dimentions. Must be safely castable to std::size_t. template class MultiArray { public: MultiArray(Dimensions... dimensionSizes); @@ -89,7 +92,7 @@ private: typename UnderlyingContainer::template Type m_buff; }; -/// \brief Constructs a new *N*-dimensional array. The sizes for the dimensions are passed as arguments. +/// \brief Constructs a new *N*-dimensional array. The sizes for the dimensions are passed as arguments and must be greather than zero. /// \remarks The number of dimensions *N* is deduced from the number of \a dimensionSizes. /// \sa makeMultiArray(), makeFixedSizeMultiArray() and makeNoneOwningMultiArray() for more convenient construction template @@ -119,7 +122,7 @@ template template std::size_t MultiArray::dimensionSize() const { - return std::get(m_dims); + return static_cast(std::get(m_dims)); } /// \brief Returns the element at the position specified via \a indices. diff --git a/tests/conversiontests.cpp b/tests/conversiontests.cpp index 0f92417..2c82cba 100644 --- a/tests/conversiontests.cpp +++ b/tests/conversiontests.cpp @@ -25,8 +25,8 @@ using namespace CPPUNIT_NS; static_assert(toSynchsafeInt(255) == 383, "toSynchsafeInt()"); static_assert(toNormalInt(383) == 255, "toNormalInt()"); static_assert(swapOrder(static_cast(0xABCD)) == 0xCDAB, "swapOrder(uint16)"); -static_assert(swapOrder(static_cast(0xABCDEF12)) == 0x12EFCDAB, "swapOrder(uint32)"); -static_assert(swapOrder(static_cast(0xABCDEF1234567890)) == 0x9078563412EFCDAB, "swapOrder(uint64)"); +static_assert(swapOrder(0xABCDEF12u) == 0x12EFCDABu, "swapOrder(uint32)"); +static_assert(swapOrder(0xABCDEF1234567890ul) == 0x9078563412EFCDABul, "swapOrder(uint64)"); /*! * \brief The ConversionTests class tests classes and functions provided by the files inside the conversion directory. @@ -256,16 +256,16 @@ void ConversionTests::testStringConversions() auto signedRandom = randomDistSigned(m_randomEngine); auto unsignedRandom = randomDistUnsigned(m_randomEngine); for (const auto base : initializer_list{ 2, 8, 10, 16 }) { - const auto asString = numberToString(unsignedRandom, static_cast(base)); + const auto asString = numberToString(unsignedRandom, base); const auto asWideString = numberToString(unsignedRandom, base); - CPPUNIT_ASSERT_EQUAL_MESSAGE(stringMsg, unsignedRandom, stringToNumber(asString, static_cast(base))); + CPPUNIT_ASSERT_EQUAL_MESSAGE(stringMsg, unsignedRandom, stringToNumber(asString, base)); CPPUNIT_ASSERT_EQUAL_MESSAGE(wideStringMsg, unsignedRandom, stringToNumber(asWideString, base)); CPPUNIT_ASSERT_EQUAL_MESSAGE(bufferMsg, unsignedRandom, bufferToNumber(asString.data(), asString.size(), base)); } for (const auto base : initializer_list{ 10 }) { const auto asString = numberToString(signedRandom, static_cast(base)); const auto asWideString = numberToString(signedRandom, base); - CPPUNIT_ASSERT_EQUAL_MESSAGE(stringMsg, signedRandom, stringToNumber(asString, static_cast(base))); + CPPUNIT_ASSERT_EQUAL_MESSAGE(stringMsg, signedRandom, stringToNumber(asString, base)); CPPUNIT_ASSERT_EQUAL_MESSAGE(wideStringMsg, signedRandom, stringToNumber(asWideString, base)); CPPUNIT_ASSERT_EQUAL_MESSAGE(bufferMsg, signedRandom, bufferToNumber(asString.data(), asString.size(), base)); } diff --git a/tests/testutils.cpp b/tests/testutils.cpp index c9ec063..a13bd55 100644 --- a/tests/testutils.cpp +++ b/tests/testutils.cpp @@ -506,13 +506,13 @@ int TestApplication::execApp(const char *const *args, string &output, string &er // determine the path of the application to be tested const char *appPath = m_applicationPathArg.firstValue(); - string fallbackAppPath; + auto fallbackAppPath = string(); if (!appPath || !*appPath) { // try to find the path by removing "_tests"-suffix from own executable path // (the own executable path is the path of the test application and its name is usually the name of the application // to be tested with "_tests"-suffix) const char *const testAppPath = m_parser.executable(); - const size_t testAppPathLength = strlen(testAppPath); + const auto testAppPathLength = strlen(testAppPath); if (testAppPathLength > 6 && !strcmp(testAppPath + testAppPathLength - 6, "_tests")) { fallbackAppPath.assign(testAppPath, testAppPathLength - 6); appPath = fallbackAppPath.data(); @@ -524,27 +524,27 @@ int TestApplication::execApp(const char *const *args, string &output, string &er // determine new path for profiling output (to not override profiling output of parent and previous invocations) const auto newProfilingPath = [appPath] { - string newProfilingPath; + auto path = string(); const char *const llvmProfileFile = getenv("LLVM_PROFILE_FILE"); if (!llvmProfileFile) { - return newProfilingPath; + return path; } // replace eg. "/some/path/tageditor_tests.profraw" with "/some/path/tageditor0.profraw" const char *const llvmProfileFileEnd = strstr(llvmProfileFile, ".profraw"); if (!llvmProfileFileEnd) { - return newProfilingPath; + return path; } - const string llvmProfileFileWithoutExtension(llvmProfileFile, llvmProfileFileEnd); + const auto llvmProfileFileWithoutExtension = string(llvmProfileFile, llvmProfileFileEnd); // extract application name from path const char *appName = strrchr(appPath, '/'); appName = appName ? appName + 1 : appPath; // concat new path - newProfilingPath = argsToString(llvmProfileFileWithoutExtension, '_', appName, invocationCount, ".profraw"); + path = argsToString(llvmProfileFileWithoutExtension, '_', appName, invocationCount, ".profraw"); // append path to profiling list file if (const char *const profrawListFile = getenv("LLVM_PROFILE_LIST_FILE")) { - ofstream(profrawListFile, ios_base::app) << newProfilingPath << endl; + ofstream(profrawListFile, ios_base::app) << path << endl; } - return newProfilingPath; + return path; }(); return execAppInternal(appPath, args, output, errors, suppressLogging, timeout, newProfilingPath);