Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ThrowHelper methods in more places #6184

Merged
merged 8 commits into from Mar 8, 2024
Merged
3 changes: 1 addition & 2 deletions osu.Framework/Audio/AudioComponent.cs
Expand Up @@ -74,8 +74,7 @@ public void Update()
{
ThreadSafety.EnsureNotUpdateThread();

if (IsDisposed)
throw new ObjectDisposedException(ToString(), "Can not update disposed audio components.");
ObjectDisposedException.ThrowIf(IsDisposed, this);

FrameStatistics.Add(StatisticsCounterType.TasksRun, PendingActions.Count);
FrameStatistics.Increment(StatisticsCounterType.Components);
Expand Down
3 changes: 1 addition & 2 deletions osu.Framework/Audio/Sample/Sample.cs
Expand Up @@ -33,8 +33,7 @@ public SampleChannel Play()

public SampleChannel GetChannel()
{
if (IsDisposed)
throw new ObjectDisposedException(ToString(), "Can not get a channel from a disposed sample.");
ObjectDisposedException.ThrowIf(IsDisposed, this);

var channel = CreateChannel();
channel.OnPlay = onPlay;
Expand Down
3 changes: 1 addition & 2 deletions osu.Framework/Audio/Sample/SampleChannel.cs
Expand Up @@ -22,8 +22,7 @@ protected SampleChannel(string name)

public virtual void Play()
{
if (IsDisposed)
throw new ObjectDisposedException(ToString(), "Can not play disposed sample channels.");
ObjectDisposedException.ThrowIf(IsDisposed, this);

Played = true;
OnPlay?.Invoke(this);
Expand Down
2 changes: 1 addition & 1 deletion osu.Framework/Audio/Sample/SampleStore.cs
Expand Up @@ -38,7 +38,7 @@ internal SampleStore([NotNull] IResourceStore<byte[]> store, [NotNull] AudioMixe

public Sample Get(string name)
{
if (IsDisposed) throw new ObjectDisposedException($"Cannot retrieve items for an already disposed {nameof(SampleStore)}");
ObjectDisposedException.ThrowIf(IsDisposed, this);

if (string.IsNullOrEmpty(name)) return null;

Expand Down
3 changes: 1 addition & 2 deletions osu.Framework/Audio/Track/TrackBass.cs
Expand Up @@ -256,8 +256,7 @@ private void setDirection(bool reverse)

public override void Start()
{
if (IsDisposed)
throw new ObjectDisposedException(ToString(), "Can not start disposed tracks.");
ObjectDisposedException.ThrowIf(IsDisposed, this);

StartAsync().WaitSafely();
}
Expand Down
4 changes: 2 additions & 2 deletions osu.Framework/Audio/Track/TrackStore.cs
Expand Up @@ -29,7 +29,7 @@ internal TrackStore([NotNull] IResourceStore<byte[]> store, [NotNull] AudioMixer

public Track GetVirtual(double length = double.PositiveInfinity, string name = "virtual")
{
if (IsDisposed) throw new ObjectDisposedException($"Cannot retrieve items for an already disposed {nameof(TrackStore)}");
ObjectDisposedException.ThrowIf(IsDisposed, this);

var track = new TrackVirtual(length, name);
AddItem(track);
Expand All @@ -38,7 +38,7 @@ public Track GetVirtual(double length = double.PositiveInfinity, string name = "

public Track Get(string name)
{
if (IsDisposed) throw new ObjectDisposedException($"Cannot retrieve items for an already disposed {nameof(TrackStore)}");
ObjectDisposedException.ThrowIf(IsDisposed, this);

if (string.IsNullOrEmpty(name)) return null;

Expand Down
2 changes: 1 addition & 1 deletion osu.Framework/Bindables/BindableBool.cs
Expand Up @@ -14,7 +14,7 @@ public BindableBool(bool value = false)

public override void Parse(object? input, IFormatProvider provider)
{
if (input == null) throw new ArgumentNullException(nameof(input));
ArgumentNullException.ThrowIfNull(input);

if (input is "1")
Value = true;
Expand Down
2 changes: 1 addition & 1 deletion osu.Framework/Bindables/BindableColour4.cs
Expand Up @@ -18,7 +18,7 @@ public BindableColour4(Colour4 value = default)

public override void Parse(object? input, IFormatProvider provider)
{
if (input == null) throw new ArgumentNullException(nameof(input));
ArgumentNullException.ThrowIfNull(input);

switch (input)
{
Expand Down
2 changes: 1 addition & 1 deletion osu.Framework/Bindables/BindableMarginPadding.cs
Expand Up @@ -19,7 +19,7 @@ public BindableMarginPadding(MarginPadding defaultValue = default)

public override void Parse(object? input, IFormatProvider provider)
{
if (input == null) throw new ArgumentNullException(nameof(input));
ArgumentNullException.ThrowIfNull(input);

switch (input)
{
Expand Down
2 changes: 1 addition & 1 deletion osu.Framework/Bindables/BindableSize.cs
Expand Up @@ -23,7 +23,7 @@ public BindableSize(Size defaultValue = default)

public override void Parse(object? input, IFormatProvider provider)
{
if (input == null) throw new ArgumentNullException(nameof(input));
ArgumentNullException.ThrowIfNull(input);

switch (input)
{
Expand Down
Expand Up @@ -119,8 +119,7 @@ public static Color4 Multiply(Color4 first, Color4 second)
/// <param name="scalar">A scalar to multiply with</param>
public static Color4 Multiply(this Color4 colour, float scalar)
{
if (scalar < 0)
throw new ArgumentOutOfRangeException(nameof(scalar), scalar, "Can not multiply colours by negative values.");
ArgumentOutOfRangeException.ThrowIfNegative(scalar);

return new Color4(
Math.Min(1, colour.R * scalar),
Expand Down
3 changes: 1 addition & 2 deletions osu.Framework/Graphics/Audio/WaveformGraph.cs
Expand Up @@ -46,8 +46,7 @@ public float Resolution
get => resolution;
set
{
if (value < 0)
throw new ArgumentOutOfRangeException(nameof(value));
ArgumentOutOfRangeException.ThrowIfNegative(value);

if (resolution == value)
return;
Expand Down
3 changes: 1 addition & 2 deletions osu.Framework/Graphics/BufferedDrawNodeSharedData.cs
Expand Up @@ -67,8 +67,7 @@ public BufferedDrawNodeSharedData(RenderBufferFormat[] formats = null, bool pixe
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="effectBufferCount"/> is less than 0.</exception>
public BufferedDrawNodeSharedData(int effectBufferCount, RenderBufferFormat[] mainBufferFormats = null, bool pixelSnapping = false, bool clipToRootNode = false)
{
if (effectBufferCount < 0)
throw new ArgumentOutOfRangeException(nameof(effectBufferCount), "Must be positive.");
ArgumentOutOfRangeException.ThrowIfNegative(effectBufferCount);

this.mainBufferFormats = mainBufferFormats;
PixelSnapping = pixelSnapping;
Expand Down
9 changes: 3 additions & 6 deletions osu.Framework/Graphics/Colour4.cs
Expand Up @@ -93,8 +93,7 @@ public Colour4(Vector4 vector)
/// <param name="scalar">The value that the existing alpha will be multiplied by.</param>
public Colour4 MultiplyAlpha(float scalar)
{
if (scalar < 0)
throw new ArgumentOutOfRangeException(nameof(scalar), scalar, "Cannot multiply alpha by a negative value.");
ArgumentOutOfRangeException.ThrowIfNegative(scalar);

return new Colour4(R, G, B, Math.Min(1f, A * scalar));
}
Expand Down Expand Up @@ -174,8 +173,7 @@ public Colour4 Darken(float amount)
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="scalar"/> is negative.</exception>
public static Colour4 operator *(Colour4 colour, float scalar)
{
if (scalar < 0)
throw new ArgumentOutOfRangeException(nameof(scalar), scalar, "Cannot multiply colours by negative values.");
ArgumentOutOfRangeException.ThrowIfNegative(scalar);

return new Colour4(Vector4.Min(colour.Vector * scalar, Vector4.One));
}
Expand All @@ -188,8 +186,7 @@ public Colour4 Darken(float amount)
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="scalar"/> is zero or negative.</exception>
public static Colour4 operator /(Colour4 colour, float scalar)
{
if (scalar <= 0)
throw new ArgumentOutOfRangeException(nameof(scalar), scalar, "Cannot divide colours by non-positive values.");
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(scalar);

return colour * (1 / scalar);
}
Expand Down
6 changes: 2 additions & 4 deletions osu.Framework/Graphics/Containers/CompositeDrawable.cs
Expand Up @@ -145,8 +145,7 @@ protected internal Task LoadComponentAsync<TLoadable>([NotNull] TLoadable compon

EnsureMutationAllowed($"load components via {nameof(LoadComponentsAsync)}");

if (IsDisposed)
throw new ObjectDisposedException(ToString());
ObjectDisposedException.ThrowIf(IsDisposed, this);

disposalCancellationSource ??= new CancellationTokenSource();

Expand Down Expand Up @@ -222,8 +221,7 @@ protected internal Task LoadComponentAsync<TLoadable>([NotNull] TLoadable compon
if (LoadState < LoadState.Loading)
throw new InvalidOperationException($"May not invoke {nameof(LoadComponent)} prior to this {nameof(CompositeDrawable)} being loaded.");

if (IsDisposed)
throw new ObjectDisposedException(ToString());
ObjectDisposedException.ThrowIf(IsDisposed, this);

loadComponents(components.ToList(), Dependencies, false);
}
Expand Down
4 changes: 1 addition & 3 deletions osu.Framework/Graphics/Containers/Container.cs
Expand Up @@ -220,9 +220,7 @@ public virtual void Add(T drawable)
throw new InvalidOperationException("Content may not be added to itself.");

ArgumentNullException.ThrowIfNull(drawable);

if (drawable.IsDisposed)
throw new ObjectDisposedException(nameof(drawable));
ObjectDisposedException.ThrowIf(drawable.IsDisposed, drawable);

if (Content == this)
AddInternal(drawable);
Expand Down
7 changes: 2 additions & 5 deletions osu.Framework/Graphics/Containers/GridContainer.cs
Expand Up @@ -420,11 +420,8 @@ public class Dimension
/// <param name="maxSize">The maximum size of this row or column.</param>
public Dimension(GridSizeMode mode = GridSizeMode.Distributed, float size = 0, float minSize = 0, float maxSize = float.MaxValue)
{
if (minSize < 0)
throw new ArgumentOutOfRangeException(nameof(minSize), "Must be greater than 0.");

if (minSize > maxSize)
throw new ArgumentOutOfRangeException(nameof(minSize), $"Must be less than {nameof(maxSize)}.");
ArgumentOutOfRangeException.ThrowIfNegative(minSize);
ArgumentOutOfRangeException.ThrowIfGreaterThan(minSize, maxSize);
Comment on lines -426 to +424
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noting that maxSize no will longer appear in the error message.

master:

System.ArgumentOutOfRangeException : Must be less than maxSize. (Parameter 'minSize')

This PR:

System.ArgumentOutOfRangeException : minSize ('20') must be less than or equal to '10'. (Parameter 'minSize')
Actual value was 20.


Mode = mode;
Size = size;
Expand Down
9 changes: 3 additions & 6 deletions osu.Framework/Graphics/Drawable.cs
Expand Up @@ -243,8 +243,7 @@ internal void Load(IFrameBasedClock clock, IReadOnlyDependencyContainer dependen
$"Tried to load long-running drawable type {GetType().ReadableName()} in a non-direct async context. See https://git.io/Je1YF for more details.");
}

if (IsDisposed)
throw new ObjectDisposedException(ToString(), "Attempting to load an already disposed drawable.");
ObjectDisposedException.ThrowIf(IsDisposed, this);

if (loadState == LoadState.NotLoaded)
{
Expand Down Expand Up @@ -466,8 +465,7 @@ protected internal Scheduler Scheduler
/// <returns>False if the drawable should not be updated.</returns>
public virtual bool UpdateSubTree()
{
if (IsDisposed)
throw new ObjectDisposedException(ToString(), "Disposed Drawables may never be in the scene graph.");
ObjectDisposedException.ThrowIf(IsDisposed, this);

if (ProcessCustomClock)
customClock?.ProcessFrame();
Expand Down Expand Up @@ -1484,8 +1482,7 @@ public CompositeDrawable Parent
get => parent;
internal set
{
if (IsDisposed)
throw new ObjectDisposedException(ToString(), "Disposed Drawables may never get a parent and return to the scene graph.");
ObjectDisposedException.ThrowIf(IsDisposed, this);

if (value == null)
ChildID = 0;
Expand Down
3 changes: 1 addition & 2 deletions osu.Framework/Graphics/OpenGL/Buffers/GLVertexBuffer.cs
Expand Up @@ -97,8 +97,7 @@ protected virtual void Dispose(bool disposing)

public void Bind(bool forRendering)
{
if (IsDisposed)
throw new ObjectDisposedException(ToString(), "Can not bind disposed vertex buffers.");
ObjectDisposedException.ThrowIf(IsDisposed, this);

if (!isInitialised)
{
Expand Down
15 changes: 5 additions & 10 deletions osu.Framework/Graphics/OpenGL/Shaders/GLShader.cs
Expand Up @@ -72,8 +72,7 @@ internal GLShader(GLRenderer renderer, string name, GLShaderPart[] parts, Shader

private void compile()
{
if (IsDisposed)
throw new ObjectDisposedException(ToString(), "Can not compile a disposed shader.");
ObjectDisposedException.ThrowIf(IsDisposed, this);

if (IsLoaded)
throw new InvalidOperationException("Attempting to compile an already-compiled shader.");
Expand All @@ -91,17 +90,15 @@ private void compile()

internal void EnsureShaderCompiled()
{
if (IsDisposed)
throw new ObjectDisposedException(ToString(), "Can not compile a disposed shader.");
ObjectDisposedException.ThrowIf(IsDisposed, this);

if (shaderCompileDelegate.State == RunState.Waiting)
shaderCompileDelegate.RunTask();
}

public void Bind()
{
if (IsDisposed)
throw new ObjectDisposedException(ToString(), "Can not bind a disposed shader.");
ObjectDisposedException.ThrowIf(IsDisposed, this);

if (IsBound)
return;
Expand Down Expand Up @@ -129,8 +126,7 @@ public void Unbind()
public Uniform<T> GetUniform<T>(string name)
where T : unmanaged, IEquatable<T>
{
if (IsDisposed)
throw new ObjectDisposedException(ToString(), "Can not retrieve uniforms from a disposed shader.");
ObjectDisposedException.ThrowIf(IsDisposed, this);

EnsureShaderCompiled();

Expand All @@ -141,8 +137,7 @@ public Uniform<T> GetUniform<T>(string name)

public virtual void BindUniformBlock(string blockName, IUniformBuffer buffer)
{
if (IsDisposed)
throw new ObjectDisposedException(ToString(), "Can not retrieve uniforms from a disposed shader.");
ObjectDisposedException.ThrowIf(IsDisposed, this);

EnsureShaderCompiled();

Expand Down
3 changes: 1 addition & 2 deletions osu.Framework/Graphics/OpenGL/Textures/GLTexture.cs
Expand Up @@ -172,8 +172,7 @@ public virtual int TextureId
{
get
{
if (!Available)
throw new ObjectDisposedException(ToString(), "Can not obtain ID of a disposed texture.");
ObjectDisposedException.ThrowIf(!Available, this);

return textureId;
}
Expand Down
3 changes: 1 addition & 2 deletions osu.Framework/Graphics/Rendering/Renderer.cs
Expand Up @@ -789,8 +789,7 @@ private void freeUnusedVertexBuffers()

public bool BindTexture(Texture texture, int unit, WrapMode? wrapModeS, WrapMode? wrapModeT)
{
if (!texture.Available)
throw new ObjectDisposedException(nameof(texture), "Can not bind a disposed texture.");
ObjectDisposedException.ThrowIf(!texture.Available, texture);

if (texture is TextureWhitePixel && lastBoundTextureIsAtlas[unit])
{
Expand Down
6 changes: 2 additions & 4 deletions osu.Framework/Graphics/Rendering/RendererExtensions.cs
Expand Up @@ -30,8 +30,7 @@ public static class RendererExtensions
public static void DrawTriangle(this IRenderer renderer, Texture texture, Triangle vertexTriangle, ColourInfo drawColour, RectangleF? textureRect = null,
Action<TexturedVertex2D>? vertexAction = null, Vector2? inflationPercentage = null, RectangleF? textureCoords = null)
{
if (!texture.Available)
throw new ObjectDisposedException(texture.ToString(), "Can not draw a triangle with a disposed texture.");
ObjectDisposedException.ThrowIf(!texture.Available, texture);

if (!renderer.BindTexture(texture))
return;
Expand Down Expand Up @@ -121,8 +120,7 @@ public static class RendererExtensions
public static void DrawQuad(this IRenderer renderer, Texture texture, Quad vertexQuad, ColourInfo drawColour, RectangleF? textureRect = null, Action<TexturedVertex2D>? vertexAction = null,
Vector2? inflationPercentage = null, Vector2? blendRangeOverride = null, RectangleF? textureCoords = null)
{
if (!texture.Available)
throw new ObjectDisposedException(texture.ToString(), "Can not draw a quad with a disposed texture.");
ObjectDisposedException.ThrowIf(!texture.Available, texture);

if (!renderer.BindTexture(texture))
return;
Expand Down
Expand Up @@ -64,8 +64,8 @@ public class ShaderStorageBufferObjectStack<TData> : IDisposable
/// <param name="ssboSize">Must be at least 2. See <see cref="IRenderer.CreateShaderStorageBufferObject{TData}"/></param>
public ShaderStorageBufferObjectStack(IRenderer renderer, int uboSize, int ssboSize)
{
if (uboSize < 2) throw new ArgumentOutOfRangeException(nameof(uboSize), "The size of the buffer must be at least 2.");
if (ssboSize < 2) throw new ArgumentOutOfRangeException(nameof(ssboSize), "The size of the buffer must be at least 2.");
ArgumentOutOfRangeException.ThrowIfLessThan(uboSize, 2);
ArgumentOutOfRangeException.ThrowIfLessThan(ssboSize, 2);

this.renderer = renderer;
this.uboSize = uboSize;
Expand Down
3 changes: 1 addition & 2 deletions osu.Framework/Graphics/Textures/Texture.cs
Expand Up @@ -192,8 +192,7 @@ public virtual int Height

internal virtual void SetData(ITextureUpload upload, WrapMode wrapModeS, WrapMode wrapModeT, Opacity? opacity)
{
if (!Available)
throw new ObjectDisposedException(ToString(), "Can not set data of a disposed texture.");
ObjectDisposedException.ThrowIf(!Available, this);

if (upload.Bounds.Width > NativeTexture.MaxSize || upload.Bounds.Height > NativeTexture.MaxSize)
throw new TextureTooLargeForGLException();
Expand Down
3 changes: 1 addition & 2 deletions osu.Framework/Graphics/TransformableExtensions.cs
Expand Up @@ -146,8 +146,7 @@ public static class TransformableExtensions
if (!isFinite(newValue))
throw new ArgumentException($"{nameof(newValue)} must be finite, but is {newValue}.", nameof(newValue));

if (duration < 0)
throw new ArgumentOutOfRangeException(nameof(duration), $"{nameof(duration)} must be positive.");
ArgumentOutOfRangeException.ThrowIfNegative(duration);

if (transform.Target != null)
throw new InvalidOperationException($"May not {nameof(PopulateTransform)} the same {nameof(Transform<TValue, TThis>)} more than once.");
Expand Down
3 changes: 1 addition & 2 deletions osu.Framework/Graphics/UserInterface/Dropdown.cs
Expand Up @@ -87,8 +87,7 @@ public IBindableList<T> ItemSource
get => itemSource;
set
{
if (value == null)
throw new ArgumentNullException(nameof(value));
ArgumentNullException.ThrowIfNull(value);

if (boundItemSource != null) itemSource.UnbindFrom(boundItemSource);
itemSource.BindTo(boundItemSource = value);
Expand Down
6 changes: 2 additions & 4 deletions osu.Framework/Graphics/Veldrid/Shaders/VeldridShader.cs
Expand Up @@ -58,8 +58,7 @@ public VeldridShader(IVeldridRenderer renderer, string name, VeldridShaderPart[]

internal void EnsureShaderInitialised()
{
if (isDisposed)
throw new ObjectDisposedException(ToString(), "Can not compile a disposed shader.");
ObjectDisposedException.ThrowIf(isDisposed, this);

if (shaderInitialiseDelegate.State == RunState.Waiting)
shaderInitialiseDelegate.RunTask();
Expand Down Expand Up @@ -90,8 +89,7 @@ public void Unbind()

public void BindUniformBlock(string blockName, IUniformBuffer buffer)
{
if (isDisposed)
throw new ObjectDisposedException(ToString(), "Can not retrieve uniforms from a disposed shader.");
ObjectDisposedException.ThrowIf(isDisposed, this);

EnsureShaderInitialised();

Expand Down