aboutsummaryrefslogtreecommitdiff
path: root/src/serialize.h
diff options
context:
space:
mode:
authorMartin Leitner-Ankerl <martin.ankerl@gmail.com>2023-08-02 19:54:11 +0200
committerMartin Leitner-Ankerl <martin.ankerl@gmail.com>2023-08-03 10:34:42 +0200
commit088caa68fb8efd8624709d643913b8a7e1218f8a (patch)
treed96ecc29a2794bd42fa758e5d917aa35fa661fc6 /src/serialize.h
parent0fafaca4d3bbf0c0b5bfe1ec617ab15252ea51e6 (diff)
refactor: use "if constexpr" in std::vector's Serialize()
This gets rid of unnecessarily creating a temporary object T() to call the right function.
Diffstat (limited to 'src/serialize.h')
-rw-r--r--src/serialize.h47
1 files changed, 16 insertions, 31 deletions
diff --git a/src/serialize.h b/src/serialize.h
index 8dd14cd0e3..72672b459e 100644
--- a/src/serialize.h
+++ b/src/serialize.h
@@ -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&)
-{
- 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&)
+template <typename Stream, typename T, typename A>
+void Serialize(Stream& os, const std::vector<T, A>& v)
{
- // 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&)