Skip to content

Commit aa3bd39

Browse files
committed
hide std::enable_shared_from_this constructors
1 parent abca13b commit aa3bd39

7 files changed

Lines changed: 38 additions & 15 deletions

File tree

libsave/include/SatisfactorySave/Pak/AbstractPakFile.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ namespace SatisfactorySave {
1212
class PakManager;
1313

1414
class SATISFACTORYSAVE_API AbstractPakFile : public std::enable_shared_from_this<AbstractPakFile> {
15+
protected:
16+
struct Private {
17+
explicit Private() = default;
18+
};
19+
1520
public:
16-
explicit AbstractPakFile(std::shared_ptr<PakManager> pakManager) : pakManager_(std::move(pakManager)) {}
1721
virtual ~AbstractPakFile() = default;
1822

1923
[[nodiscard]] virtual std::vector<std::string> getAllAssetFilenames() const = 0;
@@ -27,6 +31,9 @@ namespace SatisfactorySave {
2731
AssetFile readAsset(const std::string& filename);
2832

2933
protected:
34+
explicit AbstractPakFile(Private, std::shared_ptr<PakManager> pakManager)
35+
: pakManager_(std::move(pakManager)) {}
36+
3037
std::shared_ptr<PakManager> pakManager_;
3138
};
3239
} // namespace SatisfactorySave

libsave/include/SatisfactorySave/Pak/IoStoreFile.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ namespace SatisfactorySave {
3434

3535
class SATISFACTORYSAVE_API IoStoreFile : public AbstractPakFile {
3636
public:
37-
IoStoreFile(std::shared_ptr<PakManager> pakManager, const std::filesystem::path& path);
37+
static std::shared_ptr<IoStoreFile> create(std::shared_ptr<PakManager> pakManager,
38+
const std::filesystem::path& path) {
39+
return std::make_shared<IoStoreFile>(Private(), std::move(pakManager), path);
40+
}
41+
42+
IoStoreFile(Private p, std::shared_ptr<PakManager> pakManager, const std::filesystem::path& path);
3843

3944
[[nodiscard]] std::vector<std::string> getAllAssetFilenames() const override;
4045

libsave/include/SatisfactorySave/Pak/PakFile.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,12 @@ namespace SatisfactorySave {
105105

106106
class SATISFACTORYSAVE_API PakFile : public AbstractPakFile {
107107
public:
108-
PakFile(std::shared_ptr<PakManager> pakManager, const std::filesystem::path& pakPath);
108+
static std::shared_ptr<PakFile> create(std::shared_ptr<PakManager> pakManager,
109+
const std::filesystem::path& path) {
110+
return std::make_shared<PakFile>(Private(), std::move(pakManager), path);
111+
}
112+
113+
PakFile(Private p, std::shared_ptr<PakManager> pakManager, const std::filesystem::path& pakPath);
109114

110115
[[nodiscard]] std::vector<std::string> getAllAssetFilenames() const override;
111116

libsave/include/SatisfactorySave/Pak/PakManager.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
namespace SatisfactorySave {
1616

1717
class SATISFACTORYSAVE_API PakManager : public std::enable_shared_from_this<PakManager> {
18+
struct Private {
19+
explicit Private() = default;
20+
};
21+
1822
public:
1923
struct FScriptObjectDesc {
2024
std::string Name;
@@ -46,13 +50,13 @@ namespace SatisfactorySave {
4650
}
4751

4852
static std::shared_ptr<PakManager> create(const std::filesystem::path& gameDir) {
49-
auto pakManager = std::make_shared<PakManager>();
53+
auto pakManager = std::make_shared<PakManager>(Private());
5054
// Do two-phase init because shared_from_this() is required.
5155
pakManager->init(gameDir);
5256
return pakManager;
53-
};
57+
}
5458

55-
PakManager() = default;
59+
PakManager(Private) {};
5660
~PakManager() = default;
5761

5862
[[nodiscard]] std::vector<std::string> getAllAssetFilenames() const;

libsave/src/Pak/IoStoreFile.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ void SatisfactorySave::DirectoryIndexReader::parseFile(uint32_t file_idx, const
3939
parseFile(entry.NextFileEntry, path);
4040
}
4141

42-
SatisfactorySave::IoStoreFile::IoStoreFile(std::shared_ptr<PakManager> pakManager, const std::filesystem::path& path)
43-
: AbstractPakFile(std::move(pakManager)) {
42+
SatisfactorySave::IoStoreFile::IoStoreFile(Private p, std::shared_ptr<PakManager> pakManager,
43+
const std::filesystem::path& path)
44+
: AbstractPakFile(p, std::move(pakManager)) {
4445
if (!std::filesystem::is_regular_file(path)) {
4546
throw std::runtime_error("IoStore utoc file invalid: " + path.string());
4647
}

libsave/src/Pak/PakFile.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
#include "IO/ZlibUtils.h"
77
#include "Utils/StringUtils.h"
88

9-
SatisfactorySave::PakFile::PakFile(std::shared_ptr<PakManager> pakManager, const std::filesystem::path& pakPath)
10-
: AbstractPakFile(std::move(pakManager)),
9+
SatisfactorySave::PakFile::PakFile(Private p, std::shared_ptr<PakManager> pakManager,
10+
const std::filesystem::path& pakPath)
11+
: AbstractPakFile(p, std::move(pakManager)),
1112
NumEntries(0),
1213
PathHashSeed(0) {
1314
if (!std::filesystem::is_regular_file(pakPath)) {

libsave/src/Pak/PakManager.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void SatisfactorySave::PakManager::init(const std::filesystem::path& gameDir) {
1818
if (!std::filesystem::is_regular_file(globalUtocPath)) {
1919
throw std::runtime_error("Global utoc file not found!");
2020
}
21-
auto globalUtoc = std::make_shared<IoStoreFile>(shared_from_this(), globalUtocPath);
21+
auto globalUtoc = IoStoreFile::create(shared_from_this(), globalUtocPath);
2222
const auto globalBuf = globalUtoc->readChunkContent(0);
2323
IStreamArchive globalAr(globalBuf);
2424
globalAr << GlobalNameMap;
@@ -31,13 +31,13 @@ void SatisfactorySave::PakManager::init(const std::filesystem::path& gameDir) {
3131
if (!std::filesystem::is_regular_file(mainPakPath)) {
3232
throw std::runtime_error("Main pak file not found!");
3333
}
34-
pakFiles_.push_back(std::make_shared<PakFile>(shared_from_this(), mainPakPath));
34+
pakFiles_.push_back(PakFile::create(shared_from_this(), mainPakPath));
3535
cacheLatestPakNames();
3636

3737
if (!std::filesystem::is_regular_file(mainUtocPath)) {
3838
throw std::runtime_error("Main utoc file not found!");
3939
}
40-
pakFiles_.push_back(std::make_shared<IoStoreFile>(shared_from_this(), mainUtocPath));
40+
pakFiles_.push_back(IoStoreFile::create(shared_from_this(), mainUtocPath));
4141
cacheLatestPakNames();
4242

4343
// Search for Mod pak/utoc files.
@@ -76,9 +76,9 @@ void SatisfactorySave::PakManager::init(const std::filesystem::path& gameDir) {
7676
std::shared_ptr<AbstractPakFile> pak;
7777
try {
7878
if (is_pak) {
79-
pak = std::make_shared<PakFile>(shared_from_this(), filePath);
79+
pak = PakFile::create(shared_from_this(), filePath);
8080
} else if (is_utoc) {
81-
pak = std::make_shared<IoStoreFile>(shared_from_this(), filePath);
81+
pak = IoStoreFile::create(shared_from_this(), filePath);
8282
}
8383
} catch (const std::exception& ex) {
8484
spdlog::error("Error reading pak/utoc file: {}", ex.what());

0 commit comments

Comments
 (0)