Files
GhostEngine/Ghost.Core/Result.cs
Misaki 6f786a0698 Refactor namespaces and improve resource handling
- Updated namespaces from `Ghost.UnitTest` to `Ghost.Graphics.Test` across multiple files.
- Refactored `GraphicsTestWindow` to use a new `RenderSystem` configuration.
- Removed deprecated `Logger` and `SerializationTest` classes.
- Improved memory management in D3D12 components, including resource allocation and cleanup.
- Added `[SupportedOSPlatform]` attributes to specify Windows version compatibility.
- Updated `.editorconfig` settings and project references for consistency.
- Enabled `nativeDebugging` in `launchSettings.json`.
2025-11-11 21:30:47 +09:00

99 lines
2.3 KiB
C#

namespace Ghost.Core;
public readonly struct Result
{
public readonly bool success;
public readonly string? message;
public Result(bool success, string? message = null)
{
this.success = success;
this.message = message;
}
public static Result Success()
{
return new Result(true);
}
public static Result Fail(string? message)
{
return new Result(false, message);
}
public override string ToString() => success ? "OK" : $"Error: {message}";
public static implicit operator bool(Result result) => result.success;
}
public readonly struct Result<T>
{
public readonly bool success;
public readonly T value;
public readonly string? message;
public Result(bool success, T data, string? message = null)
{
this.success = success;
this.value = data;
this.message = message;
}
public static Result<T> Success(T data)
{
return new Result<T>(true, data);
}
public static Result<T> Fail(string? message)
{
return new Result<T>(false, default!, message);
}
public override string ToString() => success ? $"OK: {value}" : $"Error: {message}";
public static implicit operator Result<T>(T? data) => data is not null ? Success(data) : Fail(null);
public static implicit operator Result<T>(Result result) => result.success ? Success(default!) : Fail(result.message);
}
public readonly struct Result<T, S>
{
public readonly T value;
public readonly S status;
public Result(T value, S status)
{
this.value = value;
this.status = status;
}
public static Result<T, S> Create(T value, S status)
{
return new Result<T, S>(value, status);
}
public override string ToString() => $"Value: {value}, Status: {status}";
}
public static class ResultExtensions
{
public static void ThrowIfFailed(this Result result)
{
if (!result.success)
{
throw new InvalidOperationException($"Operation failed: {result.message}");
}
}
public static T GetValueOrThrow<T>(this Result<T> result)
{
if (!result.success)
{
throw new InvalidOperationException($"Operation failed: {result.message}");
}
return result.value;
}
}