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
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_NAMESPACEso 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_protouses hidden visibility by default.Namespace alias so that ONNX C++ code (and consumers such as onnxruntime) that refers to the literal
onnxnamespace — rather than theONNX_NAMESPACEmacro — resolves to the onnx-light namespace. The standard onnx package lives innamespace onnx; onnx-light usesonnx_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 fromonnx.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#
-
using type = int#
-
template<typename Traits>
class ScopedResource# - #include <scoped_resource.h>
RAII wrapper that owns a single OS resource value.
Holds a value of type
Traits::typeand callsTraits::closeon 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()andstatic void close(type). The destructor skips theclosecall when the held value compares equal toinvalid().
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#
-
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<FdTraits>#