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
Closefunction 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
_closeon Windows andcloseon 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 callsCloseon 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
Closecall 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
Invalidto be treated as empty.
-
inline ~ScopedResource()#
Destroys the resource by calling
Close, unless the value equalsInvalid.
-
inline T get() const#
Returns the held resource value without releasing ownership.
- Returns:
The current resource value; equals
Invalidwhen no resource is owned.
-
inline T release()#
Releases ownership and returns the held value.
After the call the internal value is set to
Invalidso the destructor will not invokeClose.- Returns:
The resource value that was held before the call.
-
ScopedResource(const ScopedResource&) = delete#
-
ScopedResource &operator=(const ScopedResource&) = delete#
-
template<typename F>
class ScopeExit# - #include <scoped_resource.h>
Runs a callable unconditionally when the guard goes out of scope.
The callable
Fmust be noexcept-invocable; astatic_assertenforces 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 &>.
-
using ScopedFd = ScopedResource<-1, close_fd>#