diff --git a/src/Stream/Writer.h b/src/Stream/Writer.h index e9873a62..b4c33d32 100644 --- a/src/Stream/Writer.h +++ b/src/Stream/Writer.h @@ -37,51 +37,33 @@ namespace Stream WriteImplementation(&object, sizeof(object)); } - // Vector of trvially copyable data types - template + // Non-trivial contiguous container of trivially copyable data types + template inline - std::enable_if_t::value> - Write(const std::vector& vector) { - // Size calculation can't possibly overflow since the vector size necessarily fits in memory - WriteImplementation(vector.data(), vector.size() * sizeof(T)); + std::enable_if_t< + !std::is_trivially_copyable::value && + std::is_trivially_copyable::value + > + Write(const T& container) { + // Size calculation can't possibly overflow since the container size necessarily fits in memory + WriteImplementation(container.data(), container.size() * sizeof(typename T::value_type)); } - // Size prefixed vector data types + // Size prefixed container data types // Ex: Write(vector); // Write 32-bit vector size followed by vector data - template - void Write(const std::vector& vector) { - auto vectorSize = vector.size(); - // This check is trivially false if SizeType is larger than max vector size - if (vectorSize > std::numeric_limits::max()) { - throw std::runtime_error("Vector too large to save size field"); - } - // This can't overflow due to check above - auto typedSize = static_cast(vectorSize); - Write(typedSize); - Write(vector); - } - - // String data types - // Does not write null terminator unless specifically included in string - template - void Write(const std::basic_string& string) { - // Size calculation can't possibly overflow since the string size necessarily fits in memory - Write(&string[0], string.size() * sizeof(CharT)); - } - - // Size prefixed string data types - // Ex: Write(string); // Write 32-bit string length followed by string data - // Does not write null terminator unless specifically included in string - template - void Write(const std::basic_string& string) { - auto stringSize = string.size(); - // This check is trivially false if SizeType is larger than max string size - if (stringSize > std::numeric_limits::max()) { - throw std::runtime_error("String's size is too large to write in provided size field"); + template + std::enable_if_t< + true + > + Write(const T& container) { + auto containerSize = container.size(); + // This check is trivially false if SizeType is larger than max container size + if (containerSize > std::numeric_limits::max()) { + throw std::runtime_error("Container too large to save size field"); } - // This can't overflow due to check above - Write(static_cast(stringSize)); - Write(string); + // Cast can't overflow due to check above + Write(static_cast(containerSize)); + Write(container); } // Copy a Reader to a Writer