mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-22 23:07:59 +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
|
||||
* 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> 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&);
|
||||
|
@ -783,38 +780,26 @@ void Unserialize(Stream& is, prevector<N, T>& v)
|
|||
/**
|
||||
* vector
|
||||
*/
|
||||
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(Stream& os, const std::vector<T, A>& v)
|
||||
{
|
||||
WriteCompactSize(os, v.size());
|
||||
if (!v.empty())
|
||||
os.write(MakeByteSpan(v));
|
||||
}
|
||||
|
||||
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
|
||||
// std::vector<bool>::const_iterator does not result in a const bool&
|
||||
// due to std::vector's special casing for bool arguments.
|
||||
WriteCompactSize(os, v.size());
|
||||
for (bool elem : v) {
|
||||
::Serialize(os, elem);
|
||||
if constexpr (std::is_same_v<T, unsigned char>) {
|
||||
WriteCompactSize(os, v.size());
|
||||
if (!v.empty())
|
||||
os.write(MakeByteSpan(v));
|
||||
} else if constexpr (std::is_same_v<T, bool>) {
|
||||
// A special case for std::vector<bool>, as dereferencing
|
||||
// std::vector<bool>::const_iterator does not result in a const bool&
|
||||
// due to std::vector's special casing for bool arguments.
|
||||
WriteCompactSize(os, v.size());
|
||||
for (bool elem : v) {
|
||||
::Serialize(os, elem);
|
||||
}
|
||||
} else {
|
||||
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>
|
||||
void Unserialize_impl(Stream& is, std::vector<T, A>& v, const unsigned char&)
|
||||
|
|
Loading…
Add table
Reference in a new issue