IAssert.h#
Fully qualified name: carb/assert/IAssert.h
File members: carb/assert/IAssert.h
// SPDX-FileCopyrightText: Copyright (c) 2019-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 "../Interface.h"
#include <cstdarg>
#define carb_assert_IAssert_latest CARB_HEXVERSION(1, 1)
#ifndef carb_assert_IAssert
#    define carb_assert_IAssert CARB_HEXVERSION(1, 0)
#endif
namespace carb::assert
{
struct IAssert;
}
CARB_WEAKLINK CARB_HIDDEN carb::assert::IAssert* g_carbAssert;
namespace carb
{
namespace assert
{
enum class Type : uint16_t
{
    eAssert,
    eCheck,
    eReleaseAssert,
    eFatal
};
enum class Result
{
    eCallNext,
    eContinueExecution,
    eHalt,
};
struct AssertEntry
{
    uint16_t sizeOf{ uint16_t(sizeof(AssertEntry)) };
    Type type;
    int32_t line;
    const char* condition;
    const char* file;
    const char* func;
    constexpr AssertEntry(Type type, const char* condition, const char* func, const char* file, int32_t line) noexcept
        : type(type), line(line), condition(condition), file(file), func(func)
    {
    }
};
using VectoredAssertionHandler = Result (*)(const AssertEntry& entry, const char* extraInfo, void* userData);
enum class [[nodiscard]] Handle : uintptr_t{
    kInvalid = 0
};
using AssertFlags = uint32_t;
constexpr AssertFlags fAssertSkipDialog = 0x00000001;
constexpr AssertFlags fAssertSkipBreakpoint = 0x00000002;
constexpr AssertFlags fAssertNoConsole = 0x00000004;
constexpr AssertFlags fAssertNoMetadata = 0x00000008;
struct IAssert
{
    CARB_PLUGIN_INTERFACE_EX("carb::assert::IAssert", carb_assert_IAssert_latest, carb_assert_IAssert)
    AssertFlags(CARB_ABI* setAssertionFlags)(AssertFlags set, AssertFlags clear);
    uint64_t(CARB_ABI* getAssertionFailureCount)();
    bool(CARB_ABI* reportFailedAssertionV)(
        const char* condition, const char* file, const char* func, int32_t line, const char* fmt, ...);
#if CARB_VERSION_ATLEAST(carb_assert_IAssert, 1, 1)
    bool(CARB_ABI* reportFailedAssertion2)(const AssertEntry& entry, const char* fmt, std::va_list* ap);
    Handle(CARB_ABI* registerAssertionHandler)(bool first, VectoredAssertionHandler handler, void* userData);
    bool(CARB_ABI* unregisterAssertionHandler)(Handle h);
#endif
    // ^^^ API functions
    // vvv Inline helper functions
    bool reportFailedAssertion(
        const char* condition, const char* file, const char* func, int32_t line, const char* fmt = nullptr, ...)
    {
        std::va_list args;
        bool result;
        va_start(args, fmt);
        result = reportFailedAssertionV(condition, file, func, line, fmt, &args);
        va_end(args);
        return result;
    }
};
namespace detail
{
inline CARB_NOINLINE bool dispatchAssert(const ::carb::assert::AssertEntry& entry, const char* fmt = nullptr, ...) noexcept
{
    std::va_list ap;
    va_start(ap, fmt);
    bool breakpoint = true;
    if (g_carbAssert)
    {
#if CARB_VERSION_ATLEAST(carb_assert_IAssert, 1, 1)
        breakpoint = g_carbAssert->reportFailedAssertion2(entry, fmt, &ap);
#else
        breakpoint = g_carbAssert->reportFailedAssertionV(entry.condition, entry.file, entry.func, entry.line, fmt, &ap);
#endif
    }
    else
    {
        char* buf = nullptr;
        if (fmt)
        {
            // measure string
            std::va_list ap2;
            va_copy(ap2, ap);
            auto icount = ptrdiff_t(std::vsnprintf(nullptr, 0, fmt, ap2));
            va_end(ap2);
            if (icount >= 0)
            {
                size_t count = size_t(icount);
                buf = CARB_STACK_ALLOC(char, count + 1);
                if (std::vsnprintf(buf, count + 1, fmt, ap) < 0)
                    buf = nullptr; // an error occurred, so ignore the buffer
            }
        }
        fprintf(stderr, "%s:%" PRId32 " (%s):: Assertion (%s) failed%c %s\n", entry.file, entry.line, entry.func,
                entry.condition, buf ? ':' : '.', buf ? buf : "");
    }
    va_end(ap);
    return breakpoint;
}
} // namespace detail
} // namespace assert
} // namespace carb