Use unique_ptr for ZoneLoading

This commit is contained in:
Jan
2021-03-14 12:34:50 +01:00
parent 94230cefb0
commit 301f6e3e7a
14 changed files with 118 additions and 153 deletions

View File

@ -6,7 +6,12 @@
class IZoneLoaderFactory
{
public:
IZoneLoaderFactory() = default;
virtual ~IZoneLoaderFactory() = default;
IZoneLoaderFactory(const IZoneLoaderFactory& other) = default;
IZoneLoaderFactory(IZoneLoaderFactory&& other) noexcept = default;
IZoneLoaderFactory& operator=(const IZoneLoaderFactory& other) = default;
IZoneLoaderFactory& operator=(IZoneLoaderFactory&& other) noexcept = default;
virtual ZoneLoader* CreateLoaderForHeader(ZoneHeader& header, std::string& fileName) = 0;
};

View File

@ -26,7 +26,7 @@ class DBLoadStream
std::condition_variable m_loading_finished;
std::thread m_load_thread;
std::vector<IXChunkProcessor*>& m_processors;
std::vector<std::unique_ptr<IXChunkProcessor>>& m_processors;
void Load()
{
@ -34,7 +34,7 @@ class DBLoadStream
bool firstProcessor = true;
for (auto processor : m_processors)
for (const auto& processor : m_processors)
{
if (!firstProcessor)
{
@ -57,7 +57,7 @@ class DBLoadStream
public:
DBLoadStream(const int streamIndex, const size_t chunkSize,
std::vector<IXChunkProcessor*>& chunkProcessors) : m_processors(chunkProcessors)
std::vector<std::unique_ptr<IXChunkProcessor>>& chunkProcessors) : m_processors(chunkProcessors)
{
m_index = streamIndex;
m_chunk_size = chunkSize;
@ -127,10 +127,10 @@ class ProcessorXChunks::ProcessorXChunksImpl
{
ProcessorXChunks* m_base;
std::vector<DBLoadStream*> m_streams;
std::vector<std::unique_ptr<DBLoadStream>> m_streams;
size_t m_chunk_size;
size_t m_vanilla_buffer_size;
std::vector<IXChunkProcessor*> m_chunk_processors;
std::vector<std::unique_ptr<IXChunkProcessor>> m_chunk_processors;
bool m_initialized_streams;
unsigned int m_current_stream;
@ -175,7 +175,7 @@ class ProcessorXChunks::ProcessorXChunksImpl
throw InvalidChunkSizeException(chunkSize, m_chunk_size);
}
auto* stream = m_streams[streamNum];
const auto& stream = m_streams[streamNum];
const size_t loadedChunkSize = m_base->m_base_stream->Load(stream->GetInputBuffer(), chunkSize);
if (loadedChunkSize != chunkSize)
@ -232,7 +232,7 @@ public:
for (int streamIndex = 0; streamIndex < numStreams; streamIndex++)
{
m_streams.push_back(new DBLoadStream(streamIndex, xChunkSize, m_chunk_processors));
m_streams.emplace_back(std::make_unique<DBLoadStream>(streamIndex, xChunkSize, m_chunk_processors));
}
m_chunk_size = xChunkSize;
@ -255,26 +255,11 @@ public:
m_vanilla_buffer_size = vanillaBufferSize;
}
~ProcessorXChunksImpl()
{
for (auto* stream : m_streams)
{
delete stream;
}
m_streams.clear();
for (auto* processor : m_chunk_processors)
{
delete processor;
}
m_chunk_processors.clear();
}
void AddChunkProcessor(IXChunkProcessor* streamProcessor)
void AddChunkProcessor(std::unique_ptr<IXChunkProcessor> streamProcessor)
{
assert(streamProcessor != nullptr);
m_chunk_processors.push_back(streamProcessor);
m_chunk_processors.emplace_back(std::move(streamProcessor));
}
size_t Load(void* buffer, const size_t length)
@ -339,9 +324,9 @@ ProcessorXChunks::~ProcessorXChunks()
m_impl = nullptr;
}
void ProcessorXChunks::AddChunkProcessor(IXChunkProcessor* chunkProcessor) const
void ProcessorXChunks::AddChunkProcessor(std::unique_ptr<IXChunkProcessor> chunkProcessor) const
{
m_impl->AddChunkProcessor(chunkProcessor);
m_impl->AddChunkProcessor(std::move(chunkProcessor));
}
size_t ProcessorXChunks::Load(void* buffer, const size_t length)

View File

@ -1,4 +1,6 @@
#pragma once
#include <memory>
#include "Loading/StreamProcessor.h"
#include "XChunks/IXChunkProcessor.h"
@ -15,5 +17,5 @@ public:
size_t Load(void* buffer, size_t length) override;
int64_t Pos() override;
void AddChunkProcessor(IXChunkProcessor* chunkProcessor) const;
void AddChunkProcessor(std::unique_ptr<IXChunkProcessor> chunkProcessor) const;
};

View File

@ -1,15 +1,9 @@
#include "StepAddProcessor.h"
#include <cassert>
StepAddProcessor::StepAddProcessor(StreamProcessor* streamProcessor)
StepAddProcessor::StepAddProcessor(std::unique_ptr<StreamProcessor> streamProcessor)
: m_stream_processor(std::move(streamProcessor))
{
m_stream_processor = streamProcessor;
}
StepAddProcessor::~StepAddProcessor()
{
delete m_stream_processor;
m_stream_processor = nullptr;
}
void StepAddProcessor::PerformStep(ZoneLoader* zoneLoader, ILoadingStream* stream)
@ -17,6 +11,6 @@ void StepAddProcessor::PerformStep(ZoneLoader* zoneLoader, ILoadingStream* strea
assert(zoneLoader != nullptr);
assert(m_stream_processor != nullptr);
zoneLoader->AddStreamProcessor(m_stream_processor);
zoneLoader->AddStreamProcessor(std::move(m_stream_processor));
m_stream_processor = nullptr;
}

View File

@ -1,14 +1,15 @@
#pragma once
#include <memory>
#include "Loading/ILoadingStep.h"
class StepAddProcessor final : public ILoadingStep
{
StreamProcessor* m_stream_processor;
std::unique_ptr<StreamProcessor> m_stream_processor;
public:
explicit StepAddProcessor(StreamProcessor* streamProcessor);
~StepAddProcessor() override;
explicit StepAddProcessor(std::unique_ptr<StreamProcessor> streamProcessor);
void PerformStep(ZoneLoader* zoneLoader, ILoadingStream* stream) override;
};

View File

@ -2,18 +2,12 @@
#include "Zone/Stream/Impl/XBlockInputStream.h"
#include <cassert>
StepLoadZoneContent::StepLoadZoneContent(IContentLoadingEntryPoint* entryPoint, Zone* zone, const int offsetBlockBitCount, const block_t insertBlock)
StepLoadZoneContent::StepLoadZoneContent(std::unique_ptr<IContentLoadingEntryPoint> entryPoint, Zone* zone, const int offsetBlockBitCount, const block_t insertBlock)
: m_content_loader(std::move(entryPoint)),
m_zone(zone),
m_offset_block_bit_count(offsetBlockBitCount),
m_insert_block(insertBlock)
{
m_content_loader = entryPoint;
m_zone = zone;
m_offset_block_bit_count = offsetBlockBitCount;
m_insert_block = insertBlock;
}
StepLoadZoneContent::~StepLoadZoneContent()
{
delete m_content_loader;
m_content_loader = nullptr;
}
void StepLoadZoneContent::PerformStep(ZoneLoader* zoneLoader, ILoadingStream* stream)
@ -23,4 +17,4 @@ void StepLoadZoneContent::PerformStep(ZoneLoader* zoneLoader, ILoadingStream* st
m_content_loader->Load(m_zone, inputStream);
delete inputStream;
}
}

View File

@ -1,18 +1,19 @@
#pragma once
#include <memory>
#include "Loading/ILoadingStep.h"
#include "Loading/IContentLoadingEntryPoint.h"
class StepLoadZoneContent final : public ILoadingStep
{
IContentLoadingEntryPoint* m_content_loader;
std::unique_ptr<IContentLoadingEntryPoint> m_content_loader;
Zone* m_zone;
int m_offset_block_bit_count;
block_t m_insert_block;
public:
StepLoadZoneContent(IContentLoadingEntryPoint* entryPoint, Zone* zone, int offsetBlockBitCount, block_t insertBlock);
~StepLoadZoneContent();
StepLoadZoneContent(std::unique_ptr<IContentLoadingEntryPoint> entryPoint, Zone* zone, int offsetBlockBitCount, block_t insertBlock);
void PerformStep(ZoneLoader* zoneLoader, ILoadingStream* stream) override;
};

View File

@ -10,67 +10,54 @@ ZoneLoader::ZoneLoader(Zone* zone)
m_processor_chain_dirty = false;
}
ZoneLoader::~ZoneLoader()
{
for(auto* step : m_steps)
{
delete step;
}
m_steps.clear();
for(auto* processor : m_processors)
{
delete processor;
}
m_processors.clear();
}
ILoadingStream* ZoneLoader::BuildLoadingChain(ILoadingStream* rootStream)
{
auto* currentStream = rootStream;
for(auto* processor : m_processors)
for(const auto& processor : m_processors)
{
processor->SetBaseStream(currentStream);
currentStream = processor;
currentStream = processor.get();
}
m_processor_chain_dirty = false;
return currentStream;
}
void ZoneLoader::AddXBlock(XBlock* block)
void ZoneLoader::AddXBlock(std::unique_ptr<XBlock> block)
{
m_blocks.push_back(block);
m_blocks.push_back(block.get());
std::sort(m_blocks.begin(), m_blocks.end(), [](XBlock* b1, XBlock* b2) -> bool
{
return b1->m_index < b2->m_index;
});
m_zone->GetMemory()->AddBlock(block);
m_zone->GetMemory()->AddBlock(std::move(block));
}
void ZoneLoader::AddLoadingStep(ILoadingStep* step)
void ZoneLoader::AddLoadingStep(std::unique_ptr<ILoadingStep> step)
{
m_steps.push_back(step);
m_steps.emplace_back(std::move(step));
}
void ZoneLoader::AddStreamProcessor(StreamProcessor* streamProcessor)
void ZoneLoader::AddStreamProcessor(std::unique_ptr<StreamProcessor> streamProcessor)
{
m_processors.push_back(streamProcessor);
m_processors.push_back(std::move(streamProcessor));
m_processor_chain_dirty = true;
}
void ZoneLoader::RemoveStreamProcessor(StreamProcessor* streamProcessor)
{
const auto foundEntry = std::find(m_processors.begin(), m_processors.end(), streamProcessor);
if(foundEntry != m_processors.end())
for(auto i = m_processors.begin(); i < m_processors.end(); ++i)
{
m_processors.erase(foundEntry);
m_processor_chain_dirty = true;
if(i->get() == streamProcessor)
{
m_processors.erase(i);
m_processor_chain_dirty = true;
break;
}
}
}
@ -81,7 +68,7 @@ Zone* ZoneLoader::LoadZone(std::istream& stream)
try
{
for(auto* step : m_steps)
for(const auto& step : m_steps)
{
step->PerformStep(this, endStream);
@ -93,7 +80,7 @@ Zone* ZoneLoader::LoadZone(std::istream& stream)
}
catch (LoadingException& e)
{
const std::string detailedMessage = e.DetailedMessage();
const auto detailedMessage = e.DetailedMessage();
printf("Loading fastfile failed: %s\n", detailedMessage.c_str());
delete m_zone;

View File

@ -12,8 +12,8 @@ class ILoadingStep;
class ZoneLoader
{
std::vector<ILoadingStep*> m_steps;
std::vector<StreamProcessor*> m_processors;
std::vector<std::unique_ptr<ILoadingStep>> m_steps;
std::vector<std::unique_ptr<StreamProcessor>> m_processors;
bool m_processor_chain_dirty;
@ -25,11 +25,10 @@ public:
std::vector<XBlock*> m_blocks;
explicit ZoneLoader(Zone* zone);
~ZoneLoader();
void AddXBlock(XBlock* block);
void AddLoadingStep(ILoadingStep* step);
void AddStreamProcessor(StreamProcessor* streamProcessor);
void AddXBlock(std::unique_ptr<XBlock> block);
void AddLoadingStep(std::unique_ptr<ILoadingStep> step);
void AddStreamProcessor(std::unique_ptr<StreamProcessor> streamProcessor);
void RemoveStreamProcessor(StreamProcessor* streamProcessor);