SettingsHelpers.h#
Fully qualified name: omni/extras/SettingsHelpers.h
File members: omni/extras/SettingsHelpers.h
// SPDX-FileCopyrightText: Copyright (c) 2018-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: LicenseRef-NvidiaProprietary
//
// NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
// property and proprietary rights in and to this material, related
// documentation and any modifications thereto. Any use, reproduction,
// disclosure or distribution of this material and related documentation
// without an express license agreement from NVIDIA CORPORATION or
// its affiliates is strictly prohibited.
#pragma once
#include "../../carb/settings/SettingsUtils.h"
#include <string>
#include <vector>
namespace carb::settings
{
#if CARB_VERSION_ATLEAST(carb_settings_ISettings, 2, 0)
template <typename T, std::enable_if_t<!cpp::is_std_string_view_like_v<T>, bool> = false>
T getSettingOrDefault(const cpp::string_view path, T defaultValue = {})
{
ISettings* s = getCachedInterface<ISettings>();
return detail::get<T>(s, path).value_or(defaultValue);
}
inline cpp::zstring_view getSettingOrDefault(const cpp::string_view path, cpp::zstring_view defaultValue = {})
{
auto s = getCachedInterface<ISettings>();
return detail::get<cpp::zstring_view>(s, path).value_or(defaultValue);
}
template <typename T, std::enable_if_t<!cpp::is_std_string_view_like_v<T>, bool> = false>
T setDefaultAndGetSetting(const cpp::string_view path, T defaultValue = {})
{
ISettings* s = getCachedInterface<ISettings>();
detail::setDefault(s, path, defaultValue);
return *detail::get<T>(s, path);
}
inline cpp::zstring_view setDefaultAndGetSetting(const cpp::string_view path, const cpp::zstring_view defaultValue = {})
{
ISettings* s = getCachedInterface<ISettings>();
detail::setDefault(s, path, defaultValue);
return *detail::get<cpp::zstring_view>(s, path);
}
inline void appendToStringArray(const cpp::string_view path, const std::vector<std::string>& values)
{
if (values.empty())
return;
ISettings* s = getCachedInterface<ISettings>();
ScopedWrite writeLock; // Hold a write lock while the modification takes place
// TODO: This could be implemented a lot more efficiently than fully copying everything twice
auto v = settings::detail::getStringArray<std::vector<std::string>>(s, path);
v.insert(v.end(), values.begin(), values.end());
detail::setStringArray(s, path, v);
}
#else
template <typename T>
T getSettingOrDefault(const char* path, T defaultValue = {})
{
ISettings* s = getCachedInterface<ISettings>();
return detail::get<T>(s, cpp::string_view(cpp::unsafe_length, path)).value_or(defaultValue);
}
template <typename T>
T setDefaultAndGetSetting(const char* path, T defaultValue = {})
{
ISettings* s = getCachedInterface<ISettings>();
cpp::string_view pathView(cpp::unsafe_length, path);
detail::setDefault(s, pathView, defaultValue);
return *detail::get<T>(s, pathView);
}
inline void appendToStringArray(const char* path_, const std::vector<std::string>& values)
{
if (values.empty())
return;
ISettings* s = getCachedInterface<ISettings>();
cpp::string_view path(cpp::unsafe_length, path_);
ScopedWrite writeLock; // Hold a write lock while the modification takes place
// TODO: This could be implemented a lot more efficiently than fully copying everything twice
auto v = settings::detail::getStringArray<std::vector<std::string>>(s, path);
v.insert(v.end(), values.begin(), values.end());
detail::setStringArray(s, path, v);
}
#endif
} // namespace carb::settings