4 Commits

Author SHA1 Message Date
b2f0ac3a3b system: add note to deprecate sstring
Some checks failed
Doxygen Action / build (push) Has been cancelled
Signed-off-by: Simon Rozman <simon@rozman.si>
2025-08-27 12:49:37 +02:00
de592cebb9 stream: resolve code analysis warnings
Some checks failed
Doxygen Action / build (push) Has been cancelled
Signed-off-by: Simon Rozman <simon@rozman.si>
2025-08-11 17:21:34 +02:00
9588b602a9 system: add to_sstring()
Some checks failed
Doxygen Action / build (push) Has been cancelled
Signed-off-by: Simon Rozman <simon@rozman.si>
2025-07-08 11:17:40 +02:00
b99b2fc19e Common: Enforce explicit handle validation
Some invalid handle values are -1 or INVALID_HANDLE_VALUE, hence our
handle template has a specific invalid handle value parameter we should
always test against. Time and again, I find my code simply comparing
handle against zero.

Signed-off-by: Simon Rozman <simon@rozman.si>
2025-07-08 11:17:16 +02:00
2 changed files with 37 additions and 4 deletions

View File

@@ -2383,6 +2383,9 @@ namespace stdex
socket(_In_ const socket& other); socket(_In_ const socket& other);
socket& operator =(_In_ const socket& other); socket& operator =(_In_ const socket& other);
// Force use of valid() method when testing handle.
operator bool() const;
public: public:
socket(_Inout_ socket&& other) noexcept : m_h(other.m_h) socket(_Inout_ socket&& other) noexcept : m_h(other.m_h)
{ {
@@ -3582,7 +3585,7 @@ namespace stdex
#endif #endif
size_t available = m_size - m_offset; size_t available = m_size - m_offset;
if (length <= available) { if (length <= available) {
stdex_assert(m_data || !length); stdex_assert(&m_data[m_offset] || !length);
memcpy(data, &m_data[m_offset], length); memcpy(data, &m_data[m_offset], length);
m_offset += length; m_offset += length;
m_state = state_t::ok; m_state = state_t::ok;
@@ -3592,7 +3595,7 @@ namespace stdex
m_state = state_t::eof; m_state = state_t::eof;
return 0; return 0;
} }
stdex_assert(m_data || !available); stdex_assert(&m_data[m_offset] || !available);
memcpy(data, &m_data[m_offset], available); memcpy(data, &m_data[m_offset], available);
m_offset += available; m_offset += available;
m_state = state_t::ok; m_state = state_t::ok;
@@ -3700,7 +3703,7 @@ namespace stdex
if (!ok()) _Unlikely_ if (!ok()) _Unlikely_
return 0; return 0;
} }
stdex_assert(m_data || !length); stdex_assert(&m_data[m_offset] || !length);
memcpy(&m_data[m_offset], data, length); memcpy(&m_data[m_offset], data, length);
m_offset = end_offset; m_offset = end_offset;
if (m_offset > m_size) if (m_offset > m_size)
@@ -3723,7 +3726,7 @@ namespace stdex
if (!ok()) _Unlikely_ if (!ok()) _Unlikely_
return; return;
} }
stdex_assert(m_data || !amount); stdex_assert(&m_data[m_offset] || !amount);
memset(&m_data[m_offset], byte, amount); memset(&m_data[m_offset], byte, amount);
m_offset = end_offset; m_offset = end_offset;
if (m_offset > m_size) if (m_offset > m_size)

View File

@@ -58,6 +58,8 @@ namespace stdex
/// ///
/// Character type for system functions /// Character type for system functions
/// ///
/// \note TODO: Should migrate to std::filesystem::path::value_type. Research UNICODE/_UNICODE behavior on Windows.
///
#if defined(_WIN32) #if defined(_WIN32)
using schar_t = TCHAR; using schar_t = TCHAR;
#else #else
@@ -73,8 +75,32 @@ namespace stdex
/// ///
/// String for system functions /// String for system functions
/// ///
/// \note TODO: Should migrate to std::filesystem::path. Research UNICODE/_UNICODE behavior on Windows.
///
using sstring = std::basic_string<stdex::schar_t>; using sstring = std::basic_string<stdex::schar_t>;
#ifdef UNICODE
inline sstring to_sstring(int value) { return std::to_wstring(value); }
inline sstring to_sstring(long value) { return std::to_wstring(value); }
inline sstring to_sstring(long long value) { return std::to_wstring(value); }
inline sstring to_sstring(unsigned value) { return std::to_wstring(value); }
inline sstring to_sstring(unsigned long value) { return std::to_wstring(value); }
inline sstring to_sstring(unsigned long long value) { return std::to_wstring(value); }
inline sstring to_sstring(float value) { return std::to_wstring(value); }
inline sstring to_sstring(double value) { return std::to_wstring(value); }
inline sstring to_sstring(long double value) { return std::to_wstring(value); }
#else
inline sstring to_sstring(int value) { return std::to_string(value); }
inline sstring to_sstring(long value) { return std::to_string(value); }
inline sstring to_sstring(long long value) { return std::to_string(value); }
inline sstring to_sstring(unsigned value) { return std::to_string(value); }
inline sstring to_sstring(unsigned long value) { return std::to_string(value); }
inline sstring to_sstring(unsigned long long value) { return std::to_string(value); }
inline sstring to_sstring(float value) { return std::to_string(value); }
inline sstring to_sstring(double value) { return std::to_string(value); }
inline sstring to_sstring(long double value) { return std::to_string(value); }
#endif
/// ///
/// String for system functions for backward compatibility /// String for system functions for backward compatibility
/// Use stdex::sstring /// Use stdex::sstring
@@ -200,6 +226,10 @@ namespace stdex
/// ///
operator T() const noexcept { return m_h; } operator T() const noexcept { return m_h; }
private:
// Force use of valid() method when testing handle.
operator bool() const;
protected: protected:
T m_h; T m_h;
}; };