mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-23 07:15:29 +01:00
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:
parent
0fafaca4d3
commit
088caa68fb
1 changed files with 16 additions and 31 deletions
|
@ -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,38 +780,26 @@ 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)
|
||||||
{
|
{
|
||||||
WriteCompactSize(os, v.size());
|
if constexpr (std::is_same_v<T, unsigned char>) {
|
||||||
if (!v.empty())
|
WriteCompactSize(os, v.size());
|
||||||
os.write(MakeByteSpan(v));
|
if (!v.empty())
|
||||||
}
|
os.write(MakeByteSpan(v));
|
||||||
|
} else if constexpr (std::is_same_v<T, bool>) {
|
||||||
template<typename Stream, typename T, typename A>
|
// A special case for std::vector<bool>, as dereferencing
|
||||||
void Serialize_impl(Stream& os, const std::vector<T, A>& v, const bool&)
|
// std::vector<bool>::const_iterator does not result in a const bool&
|
||||||
{
|
// due to std::vector's special casing for bool arguments.
|
||||||
// A special case for std::vector<bool>, as dereferencing
|
WriteCompactSize(os, v.size());
|
||||||
// std::vector<bool>::const_iterator does not result in a const bool&
|
for (bool elem : v) {
|
||||||
// due to std::vector's special casing for bool arguments.
|
::Serialize(os, elem);
|
||||||
WriteCompactSize(os, v.size());
|
}
|
||||||
for (bool elem : v) {
|
} else {
|
||||||
::Serialize(os, elem);
|
Serialize(os, Using<VectorFormatter<DefaultFormatter>>(v));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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));
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Stream, typename T, typename A>
|
|
||||||
inline void Serialize(Stream& os, const std::vector<T, A>& v)
|
|
||||||
{
|
|
||||||
Serialize_impl(os, v, T());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
template<typename Stream, typename T, typename A>
|
template<typename Stream, typename T, typename A>
|
||||||
void Unserialize_impl(Stream& is, std::vector<T, A>& v, const unsigned char&)
|
void Unserialize_impl(Stream& is, std::vector<T, A>& v, const unsigned char&)
|
||||||
|
|
Loading…
Add table
Reference in a new issue