carb/ObjectUtils.h

File members: carb/ObjectUtils.h

// Copyright (c) 2020-2024, NVIDIA CORPORATION. All rights reserved.
//
// NVIDIA CORPORATION and its licensors retain all intellectual property
// and proprietary rights in and to this software, related documentation
// and any modifications thereto. Any use, reproduction, disclosure or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA CORPORATION is strictly prohibited.
//

#pragma once

#include "IObject.h"

#include <atomic>

namespace carb
{

template <class T>
void deleteHandler(T* ptr)
{
    delete ptr;
}

} // namespace carb

#define CARB_IOBJECT_IMPL                                                                                              \
public:                                                                                                                \
                                                                                                                \
    size_t addRef() override                                                                                           \
    {                                                                                                                  \
        size_t prev = m_refCount.fetch_add(1, std::memory_order_relaxed);                                              \
        CARB_ASSERT(prev != 0); /* resurrected item if this occurs */                                                  \
        return prev + 1;                                                                                               \
    }                                                                                                                  \
                                                                                                                       \
                                                                                                                \
    size_t release() override                                                                                          \
    {                                                                                                                  \
        size_t prev = m_refCount.fetch_sub(1, std::memory_order_release);                                              \
        CARB_ASSERT(prev != 0); /* double release if this occurs */                                                    \
        if (prev == 1)                                                                                                 \
        {                                                                                                              \
            std::atomic_thread_fence(std::memory_order_acquire);                                                       \
            carb::deleteHandler(this);                                                                                 \
        }                                                                                                              \
        return prev - 1;                                                                                               \
    }                                                                                                                  \
                                                                                                                       \
private:                                                                                                               \
    std::atomic_size_t m_refCount{ 1 };