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_NAMESPACE

Typedefs

using ScopedFd = ScopedResource<-1, close_fd>#

RAII wrapper for a file descriptor; calls close_fd() on destruction.

Functions

inline void close_fd(int fd)#

Closes a POSIX file descriptor (or its Win32 equivalent).

Calls _close on Windows and close on POSIX platforms.

Parameters:

fd – File descriptor to close.

template<auto Invalid, void (*Close)(decltype(Invalid))>
class ScopedResource#
#include <scoped_resource.h>

RAII wrapper that owns a single OS resource value.

Holds a value of type decltype(Invalid) and calls 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.

Template Parameters:
  • Invalid – Sentinel value that represents “no resource”. The destructor skips the Close call when the held value compares equal to this.

  • Close – Function invoked with the held value on destruction.

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 Invalid to be treated as empty.

inline ~ScopedResource()#

Destroys the resource by calling Close, unless the value equals Invalid.

inline T get() const#

Returns the held resource value without releasing ownership.

Returns:

The current resource value; equals 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 Invalid so the destructor will not invoke Close.

Returns:

The resource value that was held before the call.

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

Private Types

using T = decltype(Invalid)#

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_#