refactor: use "if constexpr" in std::vector's Serialize()

This gets rid of unnecessarily creating a temporary object T() to call
the right function.
This commit is contained in:
Martin Leitner-Ankerl 2023-08-02 19:54:11 +02:00
parent 0fafaca4d3
commit 088caa68fb

View file

@ -649,9 +649,6 @@ template<typename Stream, unsigned int N, typename T> inline void Unserialize(St
* vector * vector
* vectors of unsigned char are a special case and are intended to be serialized as a single opaque blob. * vectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
*/ */
template<typename Stream, typename T, typename A> void Serialize_impl(Stream& os, const std::vector<T, A>& v, const unsigned char&);
template<typename Stream, typename T, typename A> void Serialize_impl(Stream& os, const std::vector<T, A>& v, const bool&);
template<typename Stream, typename T, typename A, typename V> void Serialize_impl(Stream& os, const std::vector<T, A>& v, const V&);
template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v); template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v);
template<typename Stream, typename T, typename A> void Unserialize_impl(Stream& is, std::vector<T, A>& v, const unsigned char&); template<typename Stream, typename T, typename A> void Unserialize_impl(Stream& is, std::vector<T, A>& v, const unsigned char&);
template<typename Stream, typename T, typename A, typename V> void Unserialize_impl(Stream& is, std::vector<T, A>& v, const V&); template<typename Stream, typename T, typename A, typename V> void Unserialize_impl(Stream& is, std::vector<T, A>& v, const V&);
@ -783,17 +780,14 @@ void Unserialize(Stream& is, prevector<N, T>& v)
/** /**
* vector * vector
*/ */
template<typename Stream, typename T, typename A> template <typename Stream, typename T, typename A>
void Serialize_impl(Stream& os, const std::vector<T, A>& v, const unsigned char&) void Serialize(Stream& os, const std::vector<T, A>& v)
{ {
if constexpr (std::is_same_v<T, unsigned char>) {
WriteCompactSize(os, v.size()); WriteCompactSize(os, v.size());
if (!v.empty()) if (!v.empty())
os.write(MakeByteSpan(v)); os.write(MakeByteSpan(v));
} } else if constexpr (std::is_same_v<T, bool>) {
template<typename Stream, typename T, typename A>
void Serialize_impl(Stream& os, const std::vector<T, A>& v, const bool&)
{
// A special case for std::vector<bool>, as dereferencing // A special case for std::vector<bool>, as dereferencing
// std::vector<bool>::const_iterator does not result in a const bool& // std::vector<bool>::const_iterator does not result in a const bool&
// due to std::vector's special casing for bool arguments. // due to std::vector's special casing for bool arguments.
@ -801,18 +795,9 @@ void Serialize_impl(Stream& os, const std::vector<T, A>& v, const bool&)
for (bool elem : v) { for (bool elem : v) {
::Serialize(os, elem); ::Serialize(os, elem);
} }
} } else {
template<typename Stream, typename T, typename A, typename V>
void Serialize_impl(Stream& os, const std::vector<T, A>& v, const V&)
{
Serialize(os, Using<VectorFormatter<DefaultFormatter>>(v)); Serialize(os, Using<VectorFormatter<DefaultFormatter>>(v));
} }
template<typename Stream, typename T, typename A>
inline void Serialize(Stream& os, const std::vector<T, A>& v)
{
Serialize_impl(os, v, T());
} }