Replace Memory::Copy with std::copy

This commit is contained in:
Ted John 2018-02-01 12:52:39 +00:00
parent bb8b6a3f68
commit 0c71855769
19 changed files with 72 additions and 85 deletions

View File

@ -14,6 +14,7 @@
*****************************************************************************/
#pragma endregion
#include <algorithm>
#include <exception>
#include <memory>
#include <string>
@ -1006,7 +1007,7 @@ extern "C"
auto resolutions = GetContext()->GetUiContext()->GetFullscreenResolutions();
sint32 count = (sint32)resolutions.size();
*outResolutions = Memory::AllocateArray<Resolution>(count);
Memory::CopyArray(*outResolutions, resolutions.data(), count);
std::copy_n(resolutions.begin(), count, *outResolutions);
return count;
}

View File

@ -16,6 +16,7 @@
#pragma warning(disable : 4611) // interaction between '_setjmp' and C++ object destruction is non-portable
#include <algorithm>
#include <png.h>
#include "core/FileStream.hpp"
#include "core/Guard.hpp"
@ -107,7 +108,7 @@ namespace Imaging
Guard::Assert(rowBytes == pngWidth, GUARD_LINE);
for (png_uint_32 i = 0; i < pngHeight; i++)
{
Memory::Copy(dst, rowPointers[i], rowBytes);
std::copy_n(rowPointers[i], rowBytes, dst);
dst += rowBytes;
}
}
@ -117,7 +118,7 @@ namespace Imaging
Guard::Assert(rowBytes == pngWidth * 4, GUARD_LINE);
for (png_uint_32 i = 0; i < pngHeight; i++)
{
Memory::Copy(dst, rowPointers[i], rowBytes);
std::copy_n(rowPointers[i], rowBytes, dst);
dst += rowBytes;
}
}

View File

@ -14,6 +14,7 @@
*****************************************************************************/
#pragma endregion
#include <algorithm>
#include "../Context.h"
#include "../core/Guard.hpp"
#include "../core/Memory.hpp"
@ -47,7 +48,7 @@ GameActionResult::GameActionResult(GA_ERROR error, rct_string_id title, rct_stri
Error = error;
ErrorTitle = title;
ErrorMessage = message;
Memory::CopyArray(ErrorMessageArgs, args, Util::CountOf(ErrorMessageArgs));
std::copy_n(args, ErrorMessageArgs.size(), ErrorMessageArgs.begin());
}
namespace GameActions
@ -129,7 +130,7 @@ namespace GameActions
{
result->Error = GA_ERROR::INSUFFICIENT_FUNDS;
result->ErrorMessage = STR_NOT_ENOUGH_CASH_REQUIRES;
set_format_arg_on(result->ErrorMessageArgs, 0, uint32, result->Cost);
set_format_arg_on(result->ErrorMessageArgs.data(), 0, uint32, result->Cost);
}
}
return result;
@ -214,7 +215,7 @@ namespace GameActions
if (result->Error != GA_ERROR::OK && !(flags & GAME_COMMAND_FLAG_GHOST))
{
// Show the error box
Memory::Copy(gCommonFormatArgs, result->ErrorMessageArgs, sizeof(result->ErrorMessageArgs));
std::copy(result->ErrorMessageArgs.begin(), result->ErrorMessageArgs.end(), gCommonFormatArgs);
context_show_error(result->ErrorTitle, result->ErrorMessage);
}
return result;

View File

@ -16,6 +16,7 @@
#pragma once
#include <array>
#include <functional>
#include <memory>
@ -71,13 +72,13 @@ class GameActionResult
public:
typedef std::unique_ptr<GameActionResult> Ptr;
GA_ERROR Error = GA_ERROR::OK;
rct_string_id ErrorTitle = STR_NONE;
rct_string_id ErrorMessage = STR_NONE;
uint8 ErrorMessageArgs[12] = { 0 };
LocationXYZ32 Position = { 0 };
money32 Cost = 0;
uint16 ExpenditureType = 0;
GA_ERROR Error = GA_ERROR::OK;
rct_string_id ErrorTitle = STR_NONE;
rct_string_id ErrorMessage = STR_NONE;
std::array<uint8, 12> ErrorMessageArgs;
LocationXYZ32 Position = { 0 };
money32 Cost = 0;
uint16 ExpenditureType = 0;
GameActionResult();
GameActionResult(GA_ERROR error, rct_string_id message);

View File

@ -63,8 +63,8 @@ public:
GameActionResult::Ptr res = std::make_unique<GameActionResult>();
Ride *ride = get_ride(_rideIndex);
res->ErrorTitle = _StatusErrorTitles[_status];
set_format_arg_on(res->ErrorMessageArgs, 6, rct_string_id, ride->name);
set_format_arg_on(res->ErrorMessageArgs, 8, uint32, ride->name_arguments);
set_format_arg_on(res->ErrorMessageArgs.data(), 6, rct_string_id, ride->name);
set_format_arg_on(res->ErrorMessageArgs.data(), 8, uint32, ride->name_arguments);
if (_rideIndex >= MAX_RIDES || _rideIndex < 0)
{
@ -104,8 +104,8 @@ public:
Ride *ride = get_ride(_rideIndex);
res->ErrorTitle = _StatusErrorTitles[_status];
set_format_arg_on(res->ErrorMessageArgs, 6, rct_string_id, ride->name);
set_format_arg_on(res->ErrorMessageArgs, 8,uint32, ride->name_arguments);
set_format_arg_on(res->ErrorMessageArgs.data(), 6, rct_string_id, ride->name);
set_format_arg_on(res->ErrorMessageArgs.data(), 8,uint32, ride->name_arguments);
if (ride->type == RIDE_TYPE_NULL)
{

View File

@ -31,7 +31,7 @@ utf8 * IStream::ReadString()
result.push_back(0);
utf8 * resultString = Memory::AllocateArray<utf8>(result.size());
Memory::CopyArray(resultString, result.data(), result.size());
std::copy(result.begin(), result.end(), resultString);
return resultString;
}

View File

@ -88,45 +88,18 @@ namespace Memory
free((void*)ptr);
}
template<typename T>
static T * Copy(T * dst, const T * src, size_t size)
{
if (size == 0) return (T*)dst;
#ifdef DEBUG
uintptr_t srcBegin = (uintptr_t)src;
uintptr_t srcEnd = srcBegin + size;
uintptr_t dstBegin = (uintptr_t)dst;
uintptr_t dstEnd = dstBegin + size;
Guard::Assert(srcEnd <= dstBegin || srcBegin >= dstEnd, "Source overlaps destination, try using Memory::Move.");
#endif
return (T*)memcpy((void*)dst, (const void*)src, size);
}
template<typename T>
static T * Duplicate(const T * src, size_t size)
{
T *result = Allocate<T>(size);
return Copy(result, src, size);
}
template<typename T>
static T * CopyArray(T * dst, const T * src, size_t count)
{
// Use a loop so that copy constructors are called
// compiler should optimise to memcpy if possible
T * result = dst;
for (; count > 0; count--)
{
*dst++ = *src++;
}
return result;
return (T *)memcpy(result, src, size);
}
template<typename T>
static T * DuplicateArray(const T * src, size_t count)
{
T * result = AllocateArray<T>(count);
return CopyArray(result, src, count);
return (T *)memcpy(result, src, count);
}
template<typename T>

View File

@ -14,6 +14,7 @@
*****************************************************************************/
#pragma endregion
#include <algorithm>
#include "Math.hpp"
#include "Memory.hpp"
#include "MemoryStream.h"
@ -135,7 +136,7 @@ void MemoryStream::Read(void * buffer, uint64 length)
throw IOException("Attempted to read past end of stream.");
}
Memory::Copy(buffer, _position, (size_t)length);
std::copy_n((const uint8 *)_position, length, (uint8 *)buffer);
_position = (void*)((uintptr_t)_position + length);
}
@ -163,7 +164,7 @@ void MemoryStream::Write(const void * buffer, uint64 length)
}
}
Memory::Copy(_position, buffer, (size_t)length);
std::copy_n((const uint8 *)buffer, length, (uint8 *)_position);
_position = (void*)((uintptr_t)_position + length);
_dataSize = Math::Max<size_t>(_dataSize, (size_t)nextPosition);
}

View File

@ -14,6 +14,7 @@
*****************************************************************************/
#pragma endregion
#include <algorithm>
#ifndef _WIN32
#include <dirent.h>
#endif
@ -74,7 +75,7 @@ namespace Path
}
size_t copyLength = Math::Min(lastPathSepIndex, static_cast<ptrdiff_t>(bufferSize - 1));
Memory::Copy(buffer, path, copyLength);
std::copy_n(path, copyLength, buffer);
buffer[copyLength] = '\0';
return buffer;
}
@ -148,7 +149,7 @@ namespace Path
}
size_t truncatedLength = Math::Min<size_t>(bufferSize - 1, lastDot - path);
Memory::Copy(buffer, path, truncatedLength);
std::copy_n(path, truncatedLength, buffer);
buffer[truncatedLength] = '\0';
return buffer;
}

View File

@ -14,6 +14,7 @@
*****************************************************************************/
#pragma endregion
#include <algorithm>
#include <cwctype>
#include <stdexcept>
#include <vector>
@ -376,7 +377,7 @@ namespace String
}
utf8 * result = Memory::Allocate<utf8>(size + 1);
Memory::Copy(result, buffer + index, size);
std::copy_n(buffer + index, size, result);
result[size] = '\0';
return result;
}

View File

@ -16,6 +16,7 @@
#pragma once
#include <algorithm>
#include "../common.h"
#include "Math.hpp"
@ -75,7 +76,7 @@ public:
void Append(const utf8 * text, size_t textLength)
{
EnsureCapacity(_length + textLength + 1);
Memory::Copy(_buffer + _length, text, textLength);
std::copy_n(text, textLength, _buffer + _length);
_length += textLength;
_buffer[_length] = 0;
}
@ -133,7 +134,7 @@ public:
{
// If buffer is null, length should be 0 which will create a new one byte memory block containing a null terminator
utf8 * result = Memory::AllocateArray<utf8>(_length + 1);
Memory::CopyArray(result, _buffer, _length);
std::copy_n(_buffer, _length, result);
result[_length] = 0;
return result;
}

View File

@ -320,7 +320,7 @@ void X8DrawingEngine::ConfigureBits(uint32 width, uint32 height, uint32 pitch)
{
if (_pitch == pitch)
{
Memory::Copy(newBits, _bits, Math::Min(_bitsSize, newBitsSize));
std::copy_n(_bits, std::min(_bitsSize, newBitsSize), newBits);
}
else
{
@ -331,7 +331,7 @@ void X8DrawingEngine::ConfigureBits(uint32 width, uint32 height, uint32 pitch)
uint32 minHeight = Math::Min(_height, height);
for (uint32 y = 0; y < minHeight; y++)
{
Memory::Copy(dst, src, minWidth);
std::copy_n(src, minWidth, dst);
if (pitch - minWidth > 0)
{
std::fill_n(dst + minWidth, pitch - minWidth, 0);

View File

@ -14,6 +14,7 @@
*****************************************************************************/
#pragma endregion
#include <algorithm>
#include <string>
#include <vector>
@ -407,7 +408,7 @@ private:
_objectOverrides.push_back(ObjectOverride());
_currentObjectOverride = &_objectOverrides[_objectOverrides.size() - 1];
Memory::Copy(_currentObjectOverride->name, _currentGroup.c_str(), 8);
std::copy_n(_currentGroup.c_str(), 8, _currentObjectOverride->name);
}
}
}

View File

@ -27,7 +27,7 @@ Object::Object(const rct_object_entry &entry)
_objectEntry = entry;
char name[DAT_NAME_LENGTH + 1] = { 0 };
Memory::Copy(name, entry.name, DAT_NAME_LENGTH);
std::copy_n(entry.name, DAT_NAME_LENGTH, name);
_identifier = String::Duplicate(name);
if (IsRCT1Object())
@ -90,7 +90,7 @@ rct_object_entry Object::CreateHeader(const char name[DAT_NAME_LENGTH + 1], uint
{
rct_object_entry header = { 0 };
header.flags = flags;
Memory::Copy(header.name, name, DAT_NAME_LENGTH);
std::copy_n(name, DAT_NAME_LENGTH, header.name);
header.checksum = checksum;
return header;
}

View File

@ -592,14 +592,14 @@ private:
static void ReportMissingObject(const rct_object_entry * entry)
{
utf8 objName[DAT_NAME_LENGTH + 1] = { 0 };
Memory::Copy(objName, entry->name, DAT_NAME_LENGTH);
std::copy_n(entry->name, DAT_NAME_LENGTH, objName);
Console::Error::WriteLine("[%s] Object not found.", objName);
}
void ReportObjectLoadProblem(const rct_object_entry * entry)
{
utf8 objName[DAT_NAME_LENGTH + 1] = { 0 };
Memory::Copy(objName, entry->name, DAT_NAME_LENGTH);
std::copy_n(entry->name, DAT_NAME_LENGTH, objName);
Console::Error::WriteLine("[%s] Object could not be loaded.", objName);
}

View File

@ -236,7 +236,7 @@ public:
rct_object_entry entry = { 0 };
utf8 entryName[9] = { ' ' };
String::Set(entryName, sizeof(entryName), name);
Memory::Copy<void>(entry.name, entryName, 8);
std::copy_n(entryName, 8, entry.name);
auto kvp = _itemMap.find(entry);
if (kvp != _itemMap.end())
@ -464,10 +464,10 @@ private:
// Create new data blob with appended bytes
size_t newDataSize = dataSize + extraBytesCount;
void * newData = Memory::Allocate<void>(newDataSize);
void * newDataSaltOffset = (void *)((uintptr_t)newData + dataSize);
Memory::Copy(newData, data, dataSize);
Memory::Copy(newDataSaltOffset, extraBytes, extraBytesCount);
uint8 * newData = Memory::Allocate<uint8>(newDataSize);
uint8 * newDataSaltOffset = newData + dataSize;
std::copy_n((const uint8 *)data, dataSize, newData);
std::copy_n((const uint8 *)extraBytes, extraBytesCount, newDataSaltOffset);
try
{

View File

@ -184,7 +184,7 @@ public:
if (decodedSize == sizeof(rct1_s4))
{
Memory::Copy<void>(&_s4, decodedData.get(), sizeof(rct1_s4));
std::memcpy(&_s4, decodedData.get(), sizeof(rct1_s4));
if (_s4Path)
{
Memory::Free(_s4Path);
@ -1353,7 +1353,7 @@ private:
std::fill(std::begin(gStaffModes), std::end(gStaffModes), 0);
std::fill(std::begin(gStaffPatrolAreas), std::end(gStaffPatrolAreas), 0);
Memory::Copy(gStaffModes, _s4.staff_modes, sizeof(_s4.staff_modes));
std::copy(std::begin(_s4.staff_modes), std::end(_s4.staff_modes), gStaffModes);
FOR_ALL_STAFF(i, peep)
{
@ -1856,7 +1856,7 @@ private:
{
rct_object_entry entry;
entry.flags = 0x00008000 + objectType;
Memory::Copy(entry.name, objectName, 8);
std::copy_n(objectName, 8, entry.name);
entry.checksum = 0;
Object * object = objectManager->LoadObject(&entry);
@ -1903,7 +1903,7 @@ private:
{
rct_object_entry entry;
entry.flags = 0x00008000 + objectType;
Memory::Copy(entry.name, objectName, 8);
std::copy_n(objectName, 8, entry.name);
entry.checksum = 0;
const ObjectRepositoryItem * ori = objectRepository->FindObject(&entry);
@ -1927,7 +1927,10 @@ private:
void ImportTileElements()
{
Memory::Copy(gTileElements, _s4.tile_elements, RCT1_MAX_TILE_ELEMENTS * sizeof(rct_tile_element));
std::copy(
std::begin(_s4.tile_elements),
std::end(_s4.tile_elements),
gTileElements);
ClearExtraTileEntries();
FixSceneryColours();
FixTileElementZ();
@ -2172,7 +2175,7 @@ private:
dst->Ticks = src->Ticks;
dst->MonthYear = src->MonthYear;
dst->Day = src->Day;
Memory::Copy(dst->Text, src->Text, sizeof(src->Text));
std::copy(std::begin(src->Text), std::end(src->Text), dst->Text);
if (dst->Type == NEWS_ITEM_RESEARCH)
{

View File

@ -107,19 +107,19 @@ std::shared_ptr<SawyerChunk> SawyerChunkReader::ReadChunk()
void SawyerChunkReader::ReadChunk(void * dst, size_t length)
{
auto chunk = ReadChunk();
const void * chunkData = chunk->GetData();
size_t chunkLength = chunk->GetLength();
auto chunkData = (const uint8 *)chunk->GetData();
auto chunkLength = chunk->GetLength();
if (chunkLength > length)
{
Memory::Copy(dst, chunkData, length);
std::copy_n(chunkData, length, (uint8 *)dst);
}
else
{
Memory::Copy(dst, chunkData, chunkLength);
size_t remainingLength = length - chunkLength;
std::copy_n(chunkData, length, (uint8 *)dst);
auto remainingLength = length - chunkLength;
if (remainingLength > 0)
{
uint8 * offset = (uint8 *)((uintptr_t)dst + chunkLength);
auto offset = (uint8 *)dst + chunkLength;
std::fill_n(offset, remainingLength, 0);
}
}
@ -135,7 +135,7 @@ size_t SawyerChunkReader::DecodeChunk(void * dst, size_t dstCapacity, const void
{
throw SawyerChunkException(EXCEPTION_MSG_DESTINATION_TOO_SMALL);
}
Memory::Copy(dst, src, header.length);
std::copy_n((const uint8 *)src, header.length, (uint8 *)dst);
resultLength = header.length;
break;
case CHUNK_ENCODING_RLE:
@ -197,7 +197,7 @@ size_t SawyerChunkReader::DecodeChunkRLE(void * dst, size_t dstCapacity, const v
throw SawyerChunkException(EXCEPTION_MSG_DESTINATION_TOO_SMALL);
}
Memory::Copy(dst8, src8 + i + 1, rleCodeByte + 1);
std::copy_n(src8 + i + 1, rleCodeByte + 1, dst8);
dst8 += rleCodeByte + 1;
i += rleCodeByte + 1;
}
@ -226,7 +226,7 @@ size_t SawyerChunkReader::DecodeChunkRepeat(void * dst, size_t dstCapacity, cons
throw SawyerChunkException(EXCEPTION_MSG_DESTINATION_TOO_SMALL);
}
Memory::Copy(dst8, copySrc, count);
std::copy_n(copySrc, count, dst8);
dst8 += count;
}
}

View File

@ -14,6 +14,8 @@
*****************************************************************************/
#pragma endregion
#include <algorithm>
#include <cstring>
#include <limits>
#include "../core/Math.hpp"
@ -306,10 +308,10 @@ static money32 BannerSetName(uint8 bannerIndex,
return MONEY32_UNDEFINED;
}
size_t nameChunkOffset = Math::Min<size_t>(indexToOffset[nameChunkIndex], Util::CountOf(newName) - 12);
Memory::Copy<uint32>((uint32*)(&newName[0 + nameChunkOffset]), &nameChunk1, sizeof(uint32));
Memory::Copy<uint32>((uint32*)(&newName[4 + nameChunkOffset]), &nameChunk2, sizeof(uint32));
Memory::Copy<uint32>((uint32*)(&newName[8 + nameChunkOffset]), &nameChunk3, sizeof(uint32));
size_t nameChunkOffset = std::min<size_t>(indexToOffset[nameChunkIndex], Util::CountOf(newName) - 12);
std::memcpy(&newName[0 + nameChunkOffset], &nameChunk1, sizeof(uint32));
std::memcpy(&newName[4 + nameChunkOffset], &nameChunk2, sizeof(uint32));
std::memcpy(&newName[8 + nameChunkOffset], &nameChunk3, sizeof(uint32));
if (nameChunkIndex != 0)
{