glfw: rework error handling system to prevent footguns
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
cf8922cc33
commit
abb1077052
12 changed files with 1136 additions and 1531 deletions
|
|
@ -12,21 +12,16 @@ const internal_debug = @import("internal_debug.zig");
|
|||
///
|
||||
/// @param[in] string A UTF-8 encoded string.
|
||||
///
|
||||
/// Possible errors include glfw.Error.NotInitialized and glfw.Error.PlatformError.
|
||||
/// Possible errors include glfw.Error.PlatformError.
|
||||
///
|
||||
/// @pointer_lifetime The specified string is copied before this function returns.
|
||||
///
|
||||
/// @thread_safety This function must only be called from the main thread.
|
||||
///
|
||||
/// see also: clipboard, glfwGetClipboardString
|
||||
pub inline fn setClipboardString(value: [*:0]const u8) error{PlatformError}!void {
|
||||
pub inline fn setClipboardString(value: [*:0]const u8) void {
|
||||
internal_debug.assertInitialized();
|
||||
c.glfwSetClipboardString(null, value);
|
||||
getError() catch |err| return switch (err) {
|
||||
Error.NotInitialized => unreachable,
|
||||
Error.PlatformError => |e| e,
|
||||
else => unreachable,
|
||||
};
|
||||
}
|
||||
|
||||
/// Returns the contents of the clipboard as a string.
|
||||
|
|
@ -37,7 +32,8 @@ pub inline fn setClipboardString(value: [*:0]const u8) error{PlatformError}!void
|
|||
///
|
||||
/// @return The contents of the clipboard as a UTF-8 encoded string.
|
||||
///
|
||||
/// Possible errors include glfw.Error.NotInitialized, glfw.Error.FormatUnavailable and glfw.Error.PlatformError.
|
||||
/// Possible errors include glfw.Error.FormatUnavailable and glfw.Error.PlatformError.
|
||||
/// null is returned in the event of an error.
|
||||
///
|
||||
/// @pointer_lifetime The returned string is allocated and freed by GLFW. You should not free it
|
||||
/// yourself. It is valid until the next call to glfw.getClipboardString or glfw.setClipboardString
|
||||
|
|
@ -46,30 +42,32 @@ pub inline fn setClipboardString(value: [*:0]const u8) error{PlatformError}!void
|
|||
/// @thread_safety This function must only be called from the main thread.
|
||||
///
|
||||
/// see also: clipboard, glfwSetClipboardString
|
||||
pub inline fn getClipboardString() error{ FormatUnavailable, PlatformError }![:0]const u8 {
|
||||
pub inline fn getClipboardString() ?[:0]const u8 {
|
||||
internal_debug.assertInitialized();
|
||||
if (c.glfwGetClipboardString(null)) |c_str| return std.mem.span(@ptrCast([*:0]const u8, c_str));
|
||||
getError() catch |err| return switch (err) {
|
||||
Error.NotInitialized => unreachable,
|
||||
Error.FormatUnavailable, Error.PlatformError => |e| e,
|
||||
else => unreachable,
|
||||
};
|
||||
// `glfwGetClipboardString` returns `null` only for errors
|
||||
unreachable;
|
||||
return null;
|
||||
}
|
||||
|
||||
test "setClipboardString" {
|
||||
const glfw = @import("main.zig");
|
||||
try glfw.init(.{});
|
||||
defer glfw.getError() catch {}; // clear any error we generate
|
||||
if (!glfw.init(.{})) {
|
||||
std.log.err("failed to initialize GLFW: {?s}", .{glfw.getErrorString()});
|
||||
std.process.exit(1);
|
||||
}
|
||||
defer glfw.terminate();
|
||||
|
||||
try glfw.setClipboardString("hello mach");
|
||||
glfw.setClipboardString("hello mach");
|
||||
}
|
||||
|
||||
test "getClipboardString" {
|
||||
const glfw = @import("main.zig");
|
||||
try glfw.init(.{});
|
||||
defer glfw.getError() catch {}; // clear any error we generate
|
||||
if (!glfw.init(.{})) {
|
||||
std.log.err("failed to initialize GLFW: {?s}", .{glfw.getErrorString()});
|
||||
std.process.exit(1);
|
||||
}
|
||||
defer glfw.terminate();
|
||||
|
||||
_ = glfw.getClipboardString() catch |err| std.debug.print("can't get clipboard, not supported by OS? error={}\n", .{err});
|
||||
_ = glfw.getClipboardString();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue