Skip to content

Commit 3a1db36

Browse files
committed
refactor: make iniConf store as shared_ptr
1 parent 7134bd8 commit 3a1db36

4 files changed

Lines changed: 13 additions & 14 deletions

File tree

src/legacy/api/DataAPI.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,13 @@ bool ConfJsonClass::reload() {
269269
ConfIniClass::ConfIniClass(const Local<Object>& scriptObj, const string& path, const string& defContent)
270270
: ScriptClass(scriptObj),
271271
ConfBaseClass(path) {
272-
iniConf = SimpleIni::create(path, defContent).release();
272+
iniConf = SimpleIni::create(path, defContent);
273273
}
274274

275275
ConfIniClass::ConfIniClass(const string& path, const string& defContent)
276276
: ScriptClass(ScriptClass::ConstructFromCpp<ConfIniClass>{}),
277277
ConfBaseClass(path) {
278-
iniConf = SimpleIni::create(path, defContent).release();
278+
iniConf = SimpleIni::create(path, defContent);
279279
}
280280

281281
ConfIniClass::~ConfIniClass() { close(); }
@@ -300,16 +300,15 @@ bool ConfIniClass::flush() { return iniConf->SaveFile(iniConf->filePath.c_str(),
300300
bool ConfIniClass::close() {
301301
if (isValid()) {
302302
reload();
303-
delete iniConf;
304-
iniConf = nullptr;
303+
iniConf.reset();
305304
}
306305
return true;
307306
}
308307
bool ConfIniClass::reload() {
309308
if (!isValid()) return false;
310309

311-
delete iniConf;
312-
iniConf = SimpleIni::create(confPath).release();
310+
iniConf.reset();
311+
iniConf = SimpleIni::create(confPath);
313312
return true;
314313
}
315314

src/legacy/api/DataAPI.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ extern ClassDefine<ConfJsonClass> ConfJsonClassBuilder;
9797

9898
class ConfIniClass : public ScriptClass, public ConfBaseClass {
9999
private:
100-
SimpleIni* iniConf;
101-
bool flush() override;
102-
bool close() override;
103-
bool reload() override;
100+
std::shared_ptr<SimpleIni> iniConf;
101+
bool flush() override;
102+
bool close() override;
103+
bool reload() override;
104104

105105
public:
106106
explicit ConfIniClass(const Local<Object>& scriptObj, const std::string& path, const std::string& defContent);

src/legacy/utils/IniHelper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include <filesystem>
77

8-
std::unique_ptr<SimpleIni> SimpleIni::create(const std::string& path, const std::string& defContent) {
8+
std::shared_ptr<SimpleIni> SimpleIni::create(const std::string& path, const std::string& defContent) {
99
namespace fs = std::filesystem;
1010
auto fpath = fs::path(ll::string_utils::str2wstr(path));
1111
if (!fpath.empty() && !fs::exists(fpath)) { // Create new file
@@ -18,7 +18,7 @@ std::unique_ptr<SimpleIni> SimpleIni::create(const std::string& path, const std:
1818
}
1919

2020
// Exist
21-
auto root = std::make_unique<SimpleIni>();
21+
auto root = std::make_shared<SimpleIni>();
2222
root->SetUnicode(true);
2323
auto res = root->LoadFile(path.c_str());
2424
if (res < 0) {

src/legacy/utils/IniHelper.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ class SimpleIni : public CSimpleIniA {
88
public:
99
std::string filePath;
1010

11-
static inline std::unique_ptr<SimpleIni> create(const std::string& path) { return create(path, ""); }
12-
static std::unique_ptr<SimpleIni> create(const std::string& path, const std::string& defContent);
11+
static inline std::shared_ptr<SimpleIni> create(const std::string& path) { return create(path, ""); }
12+
static std::shared_ptr<SimpleIni> create(const std::string& path, const std::string& defContent);
1313

1414
bool setInt(const std::string& sec, const std::string& key, int value);
1515
bool setFloat(const std::string& sec, const std::string& key, float value);

0 commit comments

Comments
 (0)