From 831b2ffdb6de6c9df48917cf640d3383cad7dd5e Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 15 Aug 2019 11:26:00 -0400 Subject: [PATCH] IStreamReader/IStreamWriter: Simplify buffer functions where applicable We can simply use std::make_unique instead of raw new + memset. For fill(), we can just use std::vector, given one of its constructors allows for an initial value to be specified. --- include/athena/IStreamReader.hpp | 14 ++++++-------- include/athena/IStreamWriter.hpp | 5 ++--- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/include/athena/IStreamReader.hpp b/include/athena/IStreamReader.hpp index 787367b..752576e 100644 --- a/include/athena/IStreamReader.hpp +++ b/include/athena/IStreamReader.hpp @@ -103,10 +103,9 @@ public: * @return The buffer at the current position from the given length. */ std::unique_ptr readBytes(atUint64 length) { - atInt8* buf = new atInt8[length]; - memset(buf, 0, length); - readUBytesToBuf(buf, length); - return std::unique_ptr(buf); + auto buf = std::make_unique(length); + readUBytesToBuf(buf.get(), length); + return buf; } /** @brief Reads a byte at the current position and advances the current position. @@ -114,10 +113,9 @@ public: * @return The buffer at the current position from the given length. */ std::unique_ptr readUBytes(atUint64 length) { - atUint8* buf = new atUint8[length]; - memset(buf, 0, length); - readUBytesToBuf(buf, length); - return std::unique_ptr(buf); + auto buf = std::make_unique(length); + readUBytesToBuf(buf.get(), length); + return buf; } /** @brief Attempts to read a fixed length of data into a pre-allocated buffer. diff --git a/include/athena/IStreamWriter.hpp b/include/athena/IStreamWriter.hpp index e10beaf..edfd34e 100644 --- a/include/athena/IStreamWriter.hpp +++ b/include/athena/IStreamWriter.hpp @@ -977,9 +977,8 @@ public: if (length == 0) return; - std::unique_ptr tmp(new atUint8[length]); - memset(tmp.get(), val, length); - writeUBytes(tmp.get(), length); + const std::vector tmp(length, val); + writeUBytes(tmp.data(), length); } void fill(atInt8 val, atUint64 length) { fill((atUint8)val, length); }