scoped_resource.h#

RAII helpers for OS-level resources and scope-exit guards.

Provides three utilities:

  • ScopedResource – a non-copyable RAII wrapper that calls a user-supplied Close function when the held value leaves scope.

  • ScopedFd / ScopedHandle – ready-made specialisations for POSIX file descriptors and Win32 HANDLE values, respectively.

  • ScopeExit – a non-copyable guard that invokes a callable when it is destroyed, regardless of how the enclosing scope is exited.

namespace onnx_light

Alias that makes onnx-light headers compatible with code that references ONNX_LIGHT_NAMESPACE (the macro used in the standard onnx package).

Set to ONNX_LIGHT_NAMESPACE so both names resolve to the same namespace.

Symbol-visibility attribute for the public onnx-light C++ API.

Maps the upstream compatibility macro to onnx-light’s explicit proto ABI annotation. This keeps declarations from vendored ONNX headers visible when lib_onnx_proto uses hidden visibility by default.

Namespace alias so that ONNX C++ code (and consumers such as onnxruntime) that refers to the literal onnx namespace — rather than the ONNX_NAMESPACE macro — resolves to the onnx-light namespace. The standard onnx package lives in namespace onnx; onnx-light uses onnx_light (via ONNX_LIGHT_NAMESPACE), so this alias keeps onnx-light a true drop-in. It is only introduced when the onnx-light namespace differs from onnx.

Typedefs

using ScopedFd = ScopedResource<FdTraits>#

RAII wrapper for a file descriptor; calls FdTraits::close() on destruction.

struct FdTraits#
#include <scoped_resource.h>

Traits describing how to close a file descriptor and its invalid sentinel.

Public Types

using type = int#

Public Static Functions

static inline constexpr int invalid()#
static inline void close(int fd)#
template<typename Traits>
class ScopedResource#
#include <scoped_resource.h>

RAII wrapper that owns a single OS resource value.

Holds a value of type Traits::type and calls Traits::close on it when the wrapper is destroyed, unless ownership has been transferred via release(). Copying is disabled; move semantics are intentionally omitted to keep the API minimal.

The sentinel is obtained through Traits::invalid() rather than a non-type template argument because some platform sentinels (e.g. Windows’ INVALID_HANDLE_VALUE, which reinterpret-casts -1 to a pointer) are not valid converted constant expressions for a pointer-typed non-type template parameter, even though permissive compilers accept them.

Template Parameters:

Traits – Type supplying using type, static type invalid() and static void close(type). The destructor skips the close call when the held value compares equal to invalid().

Public Functions

inline explicit ScopedResource(T v)#

Constructs a ScopedResource that takes ownership of v.

Parameters:

v – Resource value to manage; must compare equal to Traits::invalid() to be treated as empty.

inline ~ScopedResource()#

Destroys the resource by calling Traits::close, unless the value equals Traits::invalid().

inline T get() const#

Returns the held resource value without releasing ownership.

Returns:

The current resource value; equals Traits::invalid() when no resource is owned.

inline T release()#

Releases ownership and returns the held value.

After the call the internal value is set to Traits::invalid() so the destructor will not invoke Traits::close.

Returns:

The resource value that was held before the call.

ScopedResource(const ScopedResource&) = delete#
ScopedResource &operator=(const ScopedResource&) = delete#

Private Types

using T = typename Traits::type#

Private Members

T val_#
template<typename F>
class ScopeExit#
#include <scoped_resource.h>

Runs a callable unconditionally when the guard goes out of scope.

The callable F must be noexcept-invocable; a static_assert enforces this at instantiation time. Copying is disabled so that the guard fires exactly once.

Template Parameters:

F – Callable type. Must satisfy std::is_nothrow_invocable_v<F &>.

Public Functions

inline explicit ScopeExit(F fn)#

Constructs a ScopeExit that will invoke fn on destruction.

Parameters:

fn – Callable to invoke when the guard is destroyed; must be noexcept-invocable.

inline ~ScopeExit() noexcept#

Invokes the stored callable.

ScopeExit(const ScopeExit&) = delete#
ScopeExit &operator=(const ScopeExit&) = delete#

Private Members

F fn_#