ICopyable.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2022 Kybernetik //
  2. namespace Animancer
  3. {
  4. /// <summary>Interface for objects that can be copied.</summary>
  5. /// https://kybernetik.com.au/animancer/api/Animancer/ICopyable_1
  6. ///
  7. public interface ICopyable<T>
  8. {
  9. /************************************************************************************************************************/
  10. /// <summary>Copies the contents of `copyFrom` into this object, replacing its previous contents.</summary>
  11. /// <remarks><see cref="AnimancerUtilities.Clone{T}(T)"/> uses this method internally.</remarks>
  12. void CopyFrom(T copyFrom);
  13. /************************************************************************************************************************/
  14. }
  15. /// https://kybernetik.com.au/animancer/api/Animancer/AnimancerUtilities
  16. public static partial class AnimancerUtilities
  17. {
  18. /************************************************************************************************************************/
  19. /// <summary>Creates a new <typeparamref name="T"/> and calls <see cref="ICopyable{T}.CopyFrom(T)"/> on it.</summary>
  20. public static T Clone<T>(this T original) where T : class, ICopyable<T>, new()
  21. {
  22. if (original == null)
  23. return null;
  24. var clone = new T();
  25. clone.CopyFrom(original);
  26. return clone;
  27. }
  28. /************************************************************************************************************************/
  29. }
  30. }