Template Class NonOwningResource

Class Documentation

template<typename Resource>
class NonOwningResource

A non-owning wrapper around a handle which can be trivially converted to a reference-counting wrapper

Motivation: When implementing functions that take handles as arguments, but do not take ownership of the object passed by handle, it’s beneficial to have some way of wrapping the handle into a CoreResource but avoid the calls to incRef/decRef. This class bypasses these calls in construction/destruction. Internally this object store the actual resource and can return a reference to it, so it can be seamlessly used with C++ APIs that operate on the resource class reference. The original resource’s interface is not (fully) reexposed.

Example:

void bar(const Image &img)  // takes a reference to the Image shared handle wrapper
{
    doStuff(img);
}

void foo(NVCVImageHandle handle)
{
    NonOwningResource<Image> img(handle);    // no incRef on construction
    bar(img);                                // no incRef/decRef when converting to Image
}                                            // no decRef on destruction

Public Types

using HandleType = typename Resource::HandleType

Type alias for the handle type of the resource.

Public Functions

inline NonOwningResource(HandleType handle)

A constructor that creates a NonOwningResource from a resource handle.

Parameters:

handle – The handle to the resource.

NonOwningResource(const NonOwningResource&) = delete

The copy constructor is deleted to prevent copying.

NonOwningResource(NonOwningResource&&) = default

The move constructor is defaulted.

NonOwningResource &operator=(const NonOwningResource&) = delete

The copy assignment operator is deleted to prevent copying.

NonOwningResource &operator=(NonOwningResource&&) = default

The move assignment operator is defaulted.

inline const HandleType handle() const

Returns the handle to the resource.

Returns:

The handle to the resource.

inline ~NonOwningResource()

The destructor releases the handle to the resource.

inline operator const Resource&() const &

Conversion operator to the underlying resource type.

Returns:

A const reference to the underlying resource.