UnboundedString.h#

Fully qualified name: carb/cpp/UnboundedString.h

File members: carb/cpp/UnboundedString.h

// SPDX-FileCopyrightText: Copyright (c) 2020-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 "../Defines.h"
#include "TypeTraits.h"

namespace carb::cpp
{
template <class T, class CharT>
constexpr bool is_unbounded_character_sequence_v = (std::is_pointer_v<T> ||
                                                    is_unbounded_array_v<T>)&&std::is_convertible_v<T, const CharT*>;

template <class T, class CharT>
struct is_unbounded_character_sequence : std::bool_constant<is_unbounded_character_sequence_v<T, CharT>>
{
};

template <typename CharT>
class CARB_VIZ basic_unbounded_string
{
    const CharT* m_ptr;

public:
    using value_type = CharT;
    using pointer = CharT*;
    using const_pointer = const CharT*;
    using size_type = std::size_t;

    constexpr basic_unbounded_string() noexcept : m_ptr(nullptr)
    {
    }

    template <typename T, std::enable_if_t<is_unbounded_character_sequence_v<remove_cvref_t<T>, value_type>, bool> = true>
    constexpr basic_unbounded_string(T&& ptr) noexcept : m_ptr(ptr)
    {
    }

    constexpr operator const_pointer() const noexcept
    {
        return m_ptr;
    }

    constexpr const_pointer c_str() const noexcept
    {
        return m_ptr;
    }

    constexpr const_pointer data() const noexcept
    {
        return m_ptr;
    }
};

using unbounded_string = basic_unbounded_string<char>;

using unbounded_wstring = basic_unbounded_string<wchar_t>;

#if CARB_HAS_CPP20 && defined(__cpp_char8_t)
using unbounded_u8string = basic_unbounded_string<char8_t>;
#endif

using unbounded_u16string = basic_unbounded_string<char16_t>;
using unbounded_u32string = basic_unbounded_string<char32_t>;

} // namespace carb::cpp