diff options
Diffstat (limited to 'settings.cpp')
| -rw-r--r-- | settings.cpp | 198 |
1 files changed, 178 insertions, 20 deletions
diff --git a/settings.cpp b/settings.cpp index 59394c42..549c7b6a 100644 --- a/settings.cpp +++ b/settings.cpp @@ -179,13 +179,13 @@ string Settings::SerializeSchema() bool Settings::DeserializeSettings(const string& contents, Ref<BinaryView> view, BNSettingsScope scope) { - return BNDeserializeSettings(m_object, contents.c_str(), view ? view->GetObject() : nullptr, scope); + return BNDeserializeSettings(m_object, contents.c_str(), view ? view->GetObject() : nullptr, nullptr, scope); } string Settings::SerializeSettings(Ref<BinaryView> view, BNSettingsScope scope) { - char* settingsStr = BNSerializeSettings(m_object, view ? view->GetObject() : nullptr, scope); + char* settingsStr = BNSerializeSettings(m_object, view ? view->GetObject() : nullptr, nullptr, scope); string settings(settingsStr); BNFreeString(settingsStr); return settings; @@ -194,48 +194,48 @@ string Settings::SerializeSettings(Ref<BinaryView> view, BNSettingsScope scope) bool Settings::Reset(const string& key, Ref<BinaryView> view, BNSettingsScope scope) { - return BNSettingsReset(m_object, key.c_str(), view ? view->GetObject() : nullptr, scope); + return BNSettingsReset(m_object, key.c_str(), view ? view->GetObject() : nullptr, nullptr, scope); } bool Settings::ResetAll(Ref<BinaryView> view, BNSettingsScope scope, bool schemaOnly) { - return BNSettingsResetAll(m_object, view ? view->GetObject() : nullptr, scope, schemaOnly); + return BNSettingsResetAll(m_object, view ? view->GetObject() : nullptr, nullptr, scope, schemaOnly); } template <> bool Settings::Get<bool>(const string& key, Ref<BinaryView> view, BNSettingsScope* scope) { - return BNSettingsGetBool(m_object, key.c_str(), view ? view->GetObject() : nullptr, scope); + return BNSettingsGetBool(m_object, key.c_str(), view ? view->GetObject() : nullptr, nullptr, scope); } template <> double Settings::Get<double>(const string& key, Ref<BinaryView> view, BNSettingsScope* scope) { - return BNSettingsGetDouble(m_object, key.c_str(), view ? view->GetObject() : nullptr, scope); + return BNSettingsGetDouble(m_object, key.c_str(), view ? view->GetObject() : nullptr, nullptr, scope); } template <> int64_t Settings::Get<int64_t>(const string& key, Ref<BinaryView> view, BNSettingsScope* scope) { - return BNSettingsGetInt64(m_object, key.c_str(), view ? view->GetObject() : nullptr, scope); + return BNSettingsGetInt64(m_object, key.c_str(), view ? view->GetObject() : nullptr, nullptr, scope); } template <> uint64_t Settings::Get<uint64_t>(const string& key, Ref<BinaryView> view, BNSettingsScope* scope) { - return BNSettingsGetUInt64(m_object, key.c_str(), view ? view->GetObject() : nullptr, scope); + return BNSettingsGetUInt64(m_object, key.c_str(), view ? view->GetObject() : nullptr, nullptr, scope); } template <> string Settings::Get<string>(const string& key, Ref<BinaryView> view, BNSettingsScope* scope) { - char* tmpStr = BNSettingsGetString(m_object, key.c_str(), view ? view->GetObject() : nullptr, scope); + char* tmpStr = BNSettingsGetString(m_object, key.c_str(), view ? view->GetObject() : nullptr, nullptr, scope); string result(tmpStr); BNFreeString(tmpStr); return result; @@ -247,7 +247,7 @@ vector<string> Settings::Get<vector<string>>(const string& key, Ref<BinaryView> { size_t size = 0; char** outBuffer = - (char**)BNSettingsGetStringList(m_object, key.c_str(), view ? view->GetObject() : nullptr, scope, &size); + (char**)BNSettingsGetStringList(m_object, key.c_str(), view ? view->GetObject() : nullptr, nullptr, scope, &size); vector<string> result; result.reserve(size); @@ -261,7 +261,7 @@ vector<string> Settings::Get<vector<string>>(const string& key, Ref<BinaryView> string Settings::GetJson(const string& key, Ref<BinaryView> view, BNSettingsScope* scope) { - char* tmpStr = BNSettingsGetJson(m_object, key.c_str(), view ? view->GetObject() : nullptr, scope); + char* tmpStr = BNSettingsGetJson(m_object, key.c_str(), view ? view->GetObject() : nullptr, nullptr, scope); string result(tmpStr); BNFreeString(tmpStr); return result; @@ -270,43 +270,43 @@ string Settings::GetJson(const string& key, Ref<BinaryView> view, BNSettingsScop bool Settings::Set(const string& key, bool value, Ref<BinaryView> view, BNSettingsScope scope) { - return BNSettingsSetBool(m_object, view ? view->GetObject() : nullptr, scope, key.c_str(), value); + return BNSettingsSetBool(m_object, view ? view->GetObject() : nullptr, nullptr, scope, key.c_str(), value); } bool Settings::Set(const string& key, double value, Ref<BinaryView> view, BNSettingsScope scope) { - return BNSettingsSetDouble(m_object, view ? view->GetObject() : nullptr, scope, key.c_str(), value); + return BNSettingsSetDouble(m_object, view ? view->GetObject() : nullptr, nullptr, scope, key.c_str(), value); } bool Settings::Set(const string& key, int value, Ref<BinaryView> view, BNSettingsScope scope) { - return BNSettingsSetInt64(m_object, view ? view->GetObject() : nullptr, scope, key.c_str(), value); + return BNSettingsSetInt64(m_object, view ? view->GetObject() : nullptr, nullptr, scope, key.c_str(), value); } bool Settings::Set(const string& key, int64_t value, Ref<BinaryView> view, BNSettingsScope scope) { - return BNSettingsSetInt64(m_object, view ? view->GetObject() : nullptr, scope, key.c_str(), value); + return BNSettingsSetInt64(m_object, view ? view->GetObject() : nullptr, nullptr, scope, key.c_str(), value); } bool Settings::Set(const string& key, uint64_t value, Ref<BinaryView> view, BNSettingsScope scope) { - return BNSettingsSetUInt64(m_object, view ? view->GetObject() : nullptr, scope, key.c_str(), value); + return BNSettingsSetUInt64(m_object, view ? view->GetObject() : nullptr, nullptr, scope, key.c_str(), value); } bool Settings::Set(const string& key, const char* value, Ref<BinaryView> view, BNSettingsScope scope) { - return BNSettingsSetString(m_object, view ? view->GetObject() : nullptr, scope, key.c_str(), value); + return BNSettingsSetString(m_object, view ? view->GetObject() : nullptr, nullptr, scope, key.c_str(), value); } bool Settings::Set(const string& key, const string& value, Ref<BinaryView> view, BNSettingsScope scope) { - return BNSettingsSetString(m_object, view ? view->GetObject() : nullptr, scope, key.c_str(), value.c_str()); + return BNSettingsSetString(m_object, view ? view->GetObject() : nullptr, nullptr, scope, key.c_str(), value.c_str()); } @@ -320,7 +320,7 @@ bool Settings::Set(const string& key, const vector<string>& value, Ref<BinaryVie buffer[i] = BNAllocString(value[i].c_str()); bool result = BNSettingsSetStringList( - m_object, view ? view->GetObject() : nullptr, scope, key.c_str(), (const char**)buffer, value.size()); + m_object, view ? view->GetObject() : nullptr, nullptr, scope, key.c_str(), (const char**)buffer, value.size()); for (size_t i = 0; i < value.size(); i++) BNFreeString(buffer[i]); @@ -331,5 +331,163 @@ bool Settings::Set(const string& key, const vector<string>& value, Ref<BinaryVie bool Settings::SetJson(const string& key, const string& value, Ref<BinaryView> view, BNSettingsScope scope) { - return BNSettingsSetJson(m_object, view ? view->GetObject() : nullptr, scope, key.c_str(), value.c_str()); + return BNSettingsSetJson(m_object, view ? view->GetObject() : nullptr, nullptr, scope, key.c_str(), value.c_str()); +} + + +bool Settings::DeserializeSettings(const string& contents, Ref<Function> func, BNSettingsScope scope) +{ + return BNDeserializeSettings(m_object, contents.c_str(), nullptr, func ? func->GetObject() : nullptr, scope); +} + + +string Settings::SerializeSettings(Ref<Function> func, BNSettingsScope scope) +{ + char* settingsStr = BNSerializeSettings(m_object, nullptr, func ? func->GetObject() : nullptr, scope); + string settings(settingsStr); + BNFreeString(settingsStr); + return settings; +} + + +bool Settings::Reset(const string& key, Ref<Function> func, BNSettingsScope scope) +{ + return BNSettingsReset(m_object, key.c_str(), nullptr, func ? func->GetObject() : nullptr, scope); +} + + +bool Settings::ResetAll(Ref<Function> func, BNSettingsScope scope, bool schemaOnly) +{ + return BNSettingsResetAll(m_object, nullptr, func ? func->GetObject() : nullptr, scope, schemaOnly); +} + + +template <> +bool Settings::Get<bool>(const string& key, Ref<Function> func, BNSettingsScope* scope) +{ + return BNSettingsGetBool(m_object, key.c_str(), nullptr, func ? func->GetObject() : nullptr, scope); +} + + +template <> +double Settings::Get<double>(const string& key, Ref<Function> func, BNSettingsScope* scope) +{ + return BNSettingsGetDouble(m_object, key.c_str(), nullptr, func ? func->GetObject() : nullptr, scope); +} + + +template <> +int64_t Settings::Get<int64_t>(const string& key, Ref<Function> func, BNSettingsScope* scope) +{ + return BNSettingsGetInt64(m_object, key.c_str(), nullptr, func ? func->GetObject() : nullptr, scope); +} + + +template <> +uint64_t Settings::Get<uint64_t>(const string& key, Ref<Function> func, BNSettingsScope* scope) +{ + return BNSettingsGetUInt64(m_object, key.c_str(), nullptr, func ? func->GetObject() : nullptr, scope); +} + + +template <> +string Settings::Get<string>(const string& key, Ref<Function> func, BNSettingsScope* scope) +{ + char* tmpStr = BNSettingsGetString(m_object, key.c_str(), nullptr, func ? func->GetObject() : nullptr, scope); + string result(tmpStr); + BNFreeString(tmpStr); + return result; +} + + +template <> +vector<string> Settings::Get<vector<string>>(const string& key, Ref<Function> func, BNSettingsScope* scope) +{ + size_t size = 0; + char** outBuffer = + (char**)BNSettingsGetStringList(m_object, key.c_str(), nullptr, func ? func->GetObject() : nullptr, scope, &size); + + vector<string> result; + result.reserve(size); + for (size_t i = 0; i < size; i++) + result.emplace_back(outBuffer[i]); + + BNFreeStringList(outBuffer, size); + return result; +} + + +string Settings::GetJson(const string& key, Ref<Function> func, BNSettingsScope* scope) +{ + char* tmpStr = BNSettingsGetJson(m_object, key.c_str(), nullptr, func ? func->GetObject() : nullptr, scope); + string result(tmpStr); + BNFreeString(tmpStr); + return result; +} + + +bool Settings::Set(const string& key, bool value, Ref<Function> func, BNSettingsScope scope) +{ + return BNSettingsSetBool(m_object, nullptr, func ? func->GetObject() : nullptr, scope, key.c_str(), value); +} + + +bool Settings::Set(const string& key, double value, Ref<Function> func, BNSettingsScope scope) +{ + return BNSettingsSetDouble(m_object, nullptr, func ? func->GetObject() : nullptr, scope, key.c_str(), value); +} + + +bool Settings::Set(const string& key, int value, Ref<Function> func, BNSettingsScope scope) +{ + return BNSettingsSetInt64(m_object, nullptr, func ? func->GetObject() : nullptr, scope, key.c_str(), value); +} + + +bool Settings::Set(const string& key, int64_t value, Ref<Function> func, BNSettingsScope scope) +{ + return BNSettingsSetInt64(m_object, nullptr, func ? func->GetObject() : nullptr, scope, key.c_str(), value); +} + + +bool Settings::Set(const string& key, uint64_t value, Ref<Function> func, BNSettingsScope scope) +{ + return BNSettingsSetUInt64(m_object, nullptr, func ? func->GetObject() : nullptr, scope, key.c_str(), value); +} + + +bool Settings::Set(const string& key, const char* value, Ref<Function> func, BNSettingsScope scope) +{ + return BNSettingsSetString(m_object, nullptr, func ? func->GetObject() : nullptr, scope, key.c_str(), value); +} + + +bool Settings::Set(const string& key, const string& value, Ref<Function> func, BNSettingsScope scope) +{ + return BNSettingsSetString(m_object, nullptr, func ? func->GetObject() : nullptr, scope, key.c_str(), value.c_str()); +} + + +bool Settings::Set(const string& key, const vector<string>& value, Ref<Function> func, BNSettingsScope scope) +{ + char** buffer = new char*[value.size()]; + if (!buffer) + return false; + + for (size_t i = 0; i < value.size(); i++) + buffer[i] = BNAllocString(value[i].c_str()); + + bool result = BNSettingsSetStringList( + m_object, nullptr, func ? func->GetObject() : nullptr, scope, key.c_str(), (const char**)buffer, value.size()); + + for (size_t i = 0; i < value.size(); i++) + BNFreeString(buffer[i]); + delete[] buffer; + return result; +} + + +bool Settings::SetJson(const string& key, const string& value, Ref<Function> func, BNSettingsScope scope) +{ + return BNSettingsSetJson(m_object, nullptr, func ? func->GetObject() : nullptr, scope, key.c_str(), value.c_str()); } |
