DictHelpers.h#

Fully qualified name: omni/extras/DictHelpers.h

File members: omni/extras/DictHelpers.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/dictionary/DictionaryUtils.h"

#include <string>
#include <vector>

namespace carb::dictionary
{

template <typename T CARB_NO_DOC(, std::enable_if_t<!cpp::is_std_string_view_like_v<T>, bool> = true)>
T getValueOrDefault(const Item* baseItem, cpp::string_view path, T defaultValue)
{
    auto d = getCachedDictionaryInterface();
    if (auto item = detail::getItem(d, baseItem, path))
        return d->get<T>(item);
    return defaultValue;
}

inline cpp::zstring_view getValueOrDefault(const Item* baseItem, cpp::string_view path, cpp::zstring_view defaultValue)
{
    auto d = getCachedDictionaryInterface();
    if (auto item = detail::getItem(d, baseItem, path))
    {
        if (auto val = detail::getStringView(d, item))
            return val.value();
    }
    return defaultValue;
}

inline Item* makeStringArrayAtPath(Item* baseItem, const char* path, const std::vector<std::string>& arr)
{
    cpp::string_view pathView(cpp::unsafe_length, path);
    IDictionary* dict = getCachedDictionaryInterface();
    ScopedWrite g(*dict, baseItem);
    Item* item = detail::getItem(dict, baseItem, pathView);
    if (!item)
    {
        item = detail::createItem(dict, baseItem, pathView, ItemType::eDictionary);
    }

    for (size_t i = 0, count = arr.size(); i < count; ++i)
    {
        detail::setStringAt(dict, item, i, arr[i]);
    }
    return item;
}

} // namespace carb::dictionary