Skip to content

Commit

Permalink
[Breaking] Scoping generic extension methods (#1959)
Browse files Browse the repository at this point in the history
This was causing every object to have additional methods appearing in the IDE and on the documentation, which can be very confusing for new developers and irritating for experienced ones.

This means that these extension methods no longer work with IntPtr and "IReferencable" types, but this feature was unused.

Seperate extension methods for "IReferencable" types could be created without "Dispose" in the name and instead use the "Release" naming.

* Adding ReleaseBy and RemoveReleaseBy and fixed documentation comments

Adding ReleaseBy and RemoveReleaseBy to replace the lost functionality of DisposeBy.

Fixed typos and overly-aggressive copy pasting in documentation comments.
  • Loading branch information
Fydar committed Oct 30, 2023
1 parent a4985e6 commit 680f493
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
40 changes: 36 additions & 4 deletions sources/core/Stride.Core/ComponentBaseExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,57 @@ namespace Stride.Core
public static class ComponentBaseExtensions
{
/// <summary>
/// Keeps a component alive by adding it to a container.
/// Keeps a disposable object alive by adding it to a container.
/// </summary>
/// <typeparam name="T">A component</typeparam>
/// <param name="thisArg">The component to keep alive.</param>
/// <param name="container">The container that will keep a reference to the component.</param>
/// <returns>The same component instance</returns>
public static T DisposeBy<T>(this T thisArg, ICollectorHolder container)
where T : IDisposable
{
if (ReferenceEquals(thisArg, null))
return default(T);
return default;
return container.Collector.Add(thisArg);
}

/// <summary>
/// Removes a component that ws being kept alive from a container.
/// Removes a disposable object that was being kept alive from a container.
/// </summary>
/// <typeparam name="T">A component</typeparam>
/// <param name="thisArg">The component to remove.</param>
/// <param name="container">The container that kept a reference to the component.</param>
public static void RemoveDisposeBy<T>(this T thisArg, ICollectorHolder container)
where T : IDisposable
{
if (ReferenceEquals(thisArg, null))
return;
container.Collector.Remove(thisArg);
}

/// <summary>
/// Keeps a referencable object alive by adding it to a container.
/// </summary>
/// <typeparam name="T">A component</typeparam>
/// <param name="thisArg">The component to keep alive.</param>
/// <param name="container">The container that will keep a reference to the component.</param>
/// <returns>The same component instance</returns>
public static T ReleaseBy<T>(this T thisArg, ICollectorHolder container)
where T : IReferencable
{
if (ReferenceEquals(thisArg, null))
return default;
return container.Collector.Add(thisArg);
}

/// <summary>
/// Removes a referencable object that was being kept alive from a container.
/// </summary>
/// <typeparam name="T">A component</typeparam>
/// <param name="thisArg">The component to remove.</param>
/// <param name="container">The container that kept a reference to the component.</param>
public static void RemoveReleaseBy<T>(this T thisArg, ICollectorHolder container)
where T : IReferencable
{
if (ReferenceEquals(thisArg, null))
return;
Expand All @@ -44,7 +75,8 @@ public static void RemoveDisposeBy<T>(this T thisArg, ICollectorHolder container
/// <param name="thisArg">The component to add a reference to.</param>
/// <returns>This component.</returns>
/// <remarks>This method is equivalent to call <see cref="IReferencable.AddReference"/> and return this instance.</remarks>
public static T KeepReference<T>(this T thisArg) where T : IReferencable
public static T KeepReference<T>(this T thisArg)
where T : IReferencable
{
if (ReferenceEquals(thisArg, null))
return default(T);
Expand Down
2 changes: 1 addition & 1 deletion sources/core/Stride.Core/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static bool IsMemoryAligned(nint memoryPtr, int align = 16)
: throw new ArgumentException("Alignment is not a power of 2.", nameof(align));

/// <summary>
/// Allocate an aligned memory buffer.
/// Free an aligned memory buffer.
/// </summary>
/// <remarks>
/// The buffer must have been allocated with <see cref="AllocateMemory"/>
Expand Down

0 comments on commit 680f493

Please sign in to comment.