Skip to content

Commit

Permalink
Use ObjectDisposedException.ThrowIf throw heleper
Browse files Browse the repository at this point in the history
  • Loading branch information
turbedi committed Feb 18, 2024
1 parent cea4572 commit ffdb229
Show file tree
Hide file tree
Showing 17 changed files with 27 additions and 52 deletions.
3 changes: 1 addition & 2 deletions osu.Framework/Audio/AudioComponent.cs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
6 changes: 2 additions & 4 deletions osu.Framework/Graphics/Containers/CompositeDrawable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ protected internal Task LoadComponentsAsync<TLoadable>(IEnumerable<TLoadable> co

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 void LoadComponents<TLoadable>(IEnumerable<TLoadable> components) wher
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
Original file line number Diff line number Diff line change
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
9 changes: 3 additions & 6 deletions osu.Framework/Graphics/Drawable.cs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,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 @@ -92,17 +91,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 @@ -130,8 +127,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 @@ -145,8 +141,7 @@ public virtual void BindUniformBlock(string blockName, IUniformBuffer buffer)
if (buffer is not IGLUniformBuffer glBuffer)
throw new ArgumentException($"Buffer must be an {nameof(IGLUniformBuffer)}.");

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -830,8 +830,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
Original file line number Diff line number Diff line change
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 void DrawTriangle(this IRenderer renderer, Texture texture, Triang
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
3 changes: 1 addition & 2 deletions osu.Framework/Graphics/Textures/Texture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,7 @@ internal int? MipLevel

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
6 changes: 2 additions & 4 deletions osu.Framework/Graphics/Veldrid/Shaders/VeldridShader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ public VeldridShader(VeldridRenderer 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 @@ -94,8 +93,7 @@ public void BindUniformBlock(string blockName, IUniformBuffer buffer)
if (buffer is not IVeldridUniformBuffer veldridBuffer)
throw new ArgumentException($"Buffer must be an {nameof(IVeldridUniformBuffer)}.");

if (isDisposed)
throw new ObjectDisposedException(ToString(), "Can not retrieve uniforms from a disposed shader.");
ObjectDisposedException.ThrowIf(isDisposed, this);

EnsureShaderInitialised();

Expand Down
3 changes: 1 addition & 2 deletions osu.Framework/Platform/Windows/Native/Imm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,7 @@ private bool tryGetCompositionTargetRange(out int targetStart, out int targetEnd
/// <exception cref="ObjectDisposedException">Thrown if the <see cref="handle"/> was disposed/closed.</exception>
private bool handleInvalidOrClosed()
{
if (handle.IsClosed)
throw new ObjectDisposedException(handle.ToString(), $"Attempted to use a closed {nameof(InputContextHandle)}.");
ObjectDisposedException.ThrowIf(handle.IsClosed, handle);

return handle.IsInvalid;
}
Expand Down

0 comments on commit ffdb229

Please sign in to comment.