From 5ca3a7c2e76bc91356aaa1c6bc3d12bb2a3c6e9c Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Mon, 8 Mar 2021 16:39:43 +0000 Subject: shared_ptr -> unique_ptr in generators --- src/Generating/HeiGen.cpp | 47 ++++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 23 deletions(-) (limited to 'src/Generating/HeiGen.cpp') diff --git a/src/Generating/HeiGen.cpp b/src/Generating/HeiGen.cpp index 4efb89be5..2e4b4fb3c 100644 --- a/src/Generating/HeiGen.cpp +++ b/src/Generating/HeiGen.cpp @@ -106,8 +106,8 @@ void cHeiGenFlat::InitializeHeightGen(cIniFile & a_IniFile) //////////////////////////////////////////////////////////////////////////////// // cHeiGenCache: -cHeiGenCache::cHeiGenCache(cTerrainHeightGenPtr a_HeiGenToCache, size_t a_CacheSize) : - m_HeiGenToCache(std::move(a_HeiGenToCache)), +cHeiGenCache::cHeiGenCache(cTerrainHeightGen & a_HeiGenToCache, size_t a_CacheSize) : + m_HeiGenToCache(a_HeiGenToCache), m_CacheSize(a_CacheSize), m_NumHits(0), m_NumMisses(0), @@ -161,7 +161,7 @@ void cHeiGenCache::GenHeightMap(cChunkCoords a_ChunkCoords, cChunkDef::HeightMap // Not in the cache: m_NumMisses++; - m_HeiGenToCache->GenHeightMap(a_ChunkCoords, a_HeightMap); + m_HeiGenToCache.GenHeightMap(a_ChunkCoords, a_HeightMap); // Insert it as the first item in the MRU order: auto Idx = m_CacheOrder[m_CacheSize - 1]; @@ -219,14 +219,15 @@ bool cHeiGenCache::GetHeightAt(int a_ChunkX, int a_ChunkZ, int a_RelX, int a_Rel //////////////////////////////////////////////////////////////////////////////// // cHeiGenMultiCache: -cHeiGenMultiCache::cHeiGenMultiCache(const cTerrainHeightGenPtr & a_HeiGenToCache, size_t a_SubCacheSize, size_t a_NumSubCaches): - m_NumSubCaches(a_NumSubCaches) +cHeiGenMultiCache::cHeiGenMultiCache(std::unique_ptr a_HeiGenToCache, size_t a_SubCacheSize, size_t a_NumSubCaches): + m_NumSubCaches(a_NumSubCaches), + m_Underlying(std::move(a_HeiGenToCache)) { // Create the individual sub-caches: m_SubCaches.reserve(a_NumSubCaches); for (size_t i = 0; i < a_NumSubCaches; i++) { - m_SubCaches.emplace_back(std::make_shared(a_HeiGenToCache, a_SubCacheSize)); + m_SubCaches.push_back(std::make_unique(*m_Underlying, a_SubCacheSize)); } } @@ -517,7 +518,7 @@ void cHeiGenBiomal::GenHeightMap(cChunkCoords a_ChunkCoords, cChunkDef::HeightMa { for (int x = -1; x <= 1; x++) { - m_BiomeGen->GenBiomes({a_ChunkCoords.m_ChunkX + x, a_ChunkCoords.m_ChunkZ + z}, Biomes[x + 1][z + 1]); + m_BiomeGen.GenBiomes({a_ChunkCoords.m_ChunkX + x, a_ChunkCoords.m_ChunkZ + z}, Biomes[x + 1][z + 1]); } // for x } // for z @@ -636,9 +637,9 @@ class cHeiGenMinMax: public: - cHeiGenMinMax(int a_Seed, cBiomeGenPtr a_BiomeGen): + cHeiGenMinMax(int a_Seed, cBiomeGen & a_BiomeGen): m_Noise(a_Seed), - m_BiomeGen(std::move(a_BiomeGen)), + m_BiomeGen(a_BiomeGen), m_TotalWeight(0) { // Initialize the weights: @@ -664,7 +665,7 @@ public: cChunkDef::BiomeMap neighborBiomes[3][3]; for (int z = 0; z < 3; z++) for (int x = 0; x < 3; x++) { - m_BiomeGen->GenBiomes({a_ChunkCoords.m_ChunkX + x - 1, a_ChunkCoords.m_ChunkZ + z - 1}, neighborBiomes[z][x]); + m_BiomeGen.GenBiomes({a_ChunkCoords.m_ChunkX + x - 1, a_ChunkCoords.m_ChunkZ + z - 1}, neighborBiomes[z][x]); } // Get the min and max heights based on the biomes: @@ -735,7 +736,7 @@ protected: cPerlinNoise m_Perlin; /** The biome generator to query for the underlying biomes. */ - cBiomeGenPtr m_BiomeGen; + cBiomeGen & m_BiomeGen; /** Weights applied to each of the min / max values in the neighborhood of the currently evaluated column. */ double m_Weights[AVERAGING_SIZE * 2 + 1][AVERAGING_SIZE * 2 + 1]; @@ -831,7 +832,7 @@ protected: //////////////////////////////////////////////////////////////////////////////// // cTerrainHeightGen: -cTerrainHeightGenPtr cTerrainHeightGen::CreateHeightGen(cIniFile & a_IniFile, const cBiomeGenPtr & a_BiomeGen, int a_Seed, bool & a_CacheOffByDefault) +std::unique_ptr cTerrainHeightGen::CreateHeightGen(cIniFile & a_IniFile, cBiomeGen & a_BiomeGen, int a_Seed, bool & a_CacheOffByDefault) { AString HeightGenName = a_IniFile.GetValueSet("Generator", "HeightGen", ""); if (HeightGenName.empty()) @@ -841,55 +842,55 @@ cTerrainHeightGenPtr cTerrainHeightGen::CreateHeightGen(cIniFile & a_IniFile, co } a_CacheOffByDefault = false; - cTerrainHeightGenPtr res; + std::unique_ptr res; if (NoCaseCompare(HeightGenName, "Flat") == 0) { - res = std::make_shared(); + res = std::make_unique(); a_CacheOffByDefault = true; // We're generating faster than a cache would retrieve data } else if (NoCaseCompare(HeightGenName, "classic") == 0) { - res = std::make_shared(a_Seed); + res = std::make_unique(a_Seed); } else if (NoCaseCompare(HeightGenName, "DistortedHeightmap") == 0) { // Not a heightmap-based generator, but it used to be accessible via HeightGen, so we need to skip making the default out of it // Return an empty pointer, the caller will create the proper generator: - return cTerrainHeightGenPtr(); + return nullptr; } else if (NoCaseCompare(HeightGenName, "End") == 0) { // Not a heightmap-based generator, but it used to be accessible via HeightGen, so we need to skip making the default out of it // Return an empty pointer, the caller will create the proper generator: - return cTerrainHeightGenPtr(); + return nullptr; } else if (NoCaseCompare(HeightGenName, "MinMax") == 0) { - res = std::make_shared(a_Seed, a_BiomeGen); + res = std::make_unique(a_Seed, a_BiomeGen); } else if (NoCaseCompare(HeightGenName, "Mountains") == 0) { - res = std::make_shared(a_Seed); + res = std::make_unique(a_Seed); } else if (NoCaseCompare(HeightGenName, "BiomalNoise3D") == 0) { // Not a heightmap-based generator, but it used to be accessible via HeightGen, so we need to skip making the default out of it // Return an empty pointer, the caller will create the proper generator: - return cTerrainHeightGenPtr(); + return nullptr; } else if (NoCaseCompare(HeightGenName, "Steppy") == 0) { - res = std::make_shared(a_Seed); + res = std::make_unique(a_Seed); } else if (NoCaseCompare(HeightGenName, "Noise3D") == 0) { // Not a heightmap-based generator, but it used to be accessible via HeightGen, so we need to skip making the default out of it // Return an empty pointer, the caller will create the proper generator: - return cTerrainHeightGenPtr(); + return nullptr; } else if (NoCaseCompare(HeightGenName, "Biomal") == 0) { - res = std::make_shared(a_Seed, a_BiomeGen); + res = std::make_unique(a_Seed, a_BiomeGen); /* // Performance-testing: -- cgit v1.2.3