From cd9645c9dfdb838010e705ba573027e7a5a7b289 Mon Sep 17 00:00:00 2001 From: Daniel Stevens Date: Thu, 7 Mar 2019 18:11:13 +0700 Subject: [PATCH] Add serializer for container of non-trivial types Contains notes about future changes to `enable_if` guards, explaining where this is going. --- src/Stream/Writer.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Stream/Writer.h b/src/Stream/Writer.h index 86e777f3..fd157457 100644 --- a/src/Stream/Writer.h +++ b/src/Stream/Writer.h @@ -49,6 +49,23 @@ namespace OP2Utility::Stream WriteImplementation(container.data(), container.size() * sizeof(typename T::value_type)); } + // Arbitrary container of writeable data types + // For use when the container is non-contiguous, or the element type is not trivially copyable + template + inline + std::enable_if_t< + // The enable_if check needs to change to something like: + // is_iterable && is_writeable + // For now, we leave the old check to avoid ambiguity + !std::is_trivially_copyable::value + > + Write(const T& container) { + // Loop over all elements and call custom serializer for type + for (const auto& element : container) { + Write(element); // Call custom serializer + } + } + // Size prefixed container data types // Ex: Write(vector); // Write 32-bit vector size followed by vector data template