1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-12-24 17:29:23 +01:00

Marked CopyExtensions as obsolete

This commit is contained in:
Ell 2022-01-23 21:18:13 +01:00
parent f3e6df6862
commit c7f021e62d
4 changed files with 9 additions and 37 deletions

View file

@ -48,6 +48,9 @@ Improvements
- Rethrow exceptions when no RawContentManager readers could be constructed
- Make Newtonsoft.Json dependency optional
Removals
- Marked CopyExtensions as obsolete
## 5.2.0
### MLEM
Additions

View file

@ -7,6 +7,7 @@ namespace MLEM.Data {
/// <summary>
/// A set of extensions for dealing with copying objects.
/// </summary>
[Obsolete("CopyExtensions has major flaws and insufficient speed compared to other libraries specifically designed for copying objects.")]
public static class CopyExtensions {
private const BindingFlags DefaultFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
@ -21,6 +22,7 @@ namespace MLEM.Data {
/// <param name="fieldInclusion">A predicate that determines whether or not the given field should be copied. If null, all fields will be copied.</param>
/// <typeparam name="T">The type of the object to copy</typeparam>
/// <returns>A shallow copy of the object</returns>
[Obsolete("CopyExtensions has major flaws and insufficient speed compared to other libraries specifically designed for copying objects.")]
public static T Copy<T>(this T obj, BindingFlags flags = DefaultFlags, Predicate<FieldInfo> fieldInclusion = null) {
var copy = (T) Construct(typeof(T), flags);
obj.CopyInto(copy, flags, fieldInclusion);
@ -36,6 +38,7 @@ namespace MLEM.Data {
/// <param name="fieldInclusion">A predicate that determines whether or not the given field should be copied. If null, all fields will be copied.</param>
/// <typeparam name="T">The type of the object to copy</typeparam>
/// <returns>A deep copy of the object</returns>
[Obsolete("CopyExtensions has major flaws and insufficient speed compared to other libraries specifically designed for copying objects.")]
public static T DeepCopy<T>(this T obj, BindingFlags flags = DefaultFlags, Predicate<FieldInfo> fieldInclusion = null) {
var copy = (T) Construct(typeof(T), flags);
obj.DeepCopyInto(copy, flags, fieldInclusion);
@ -50,6 +53,7 @@ namespace MLEM.Data {
/// <param name="flags">The binding flags for field searching</param>
/// <param name="fieldInclusion">A predicate that determines whether or not the given field should be copied. If null, all fields will be copied.</param>
/// <typeparam name="T">The type of the object to copy</typeparam>
[Obsolete("CopyExtensions has major flaws and insufficient speed compared to other libraries specifically designed for copying objects.")]
public static void CopyInto<T>(this T obj, T otherObj, BindingFlags flags = DefaultFlags, Predicate<FieldInfo> fieldInclusion = null) {
foreach (var field in typeof(T).GetFields(flags)) {
if (fieldInclusion == null || fieldInclusion(field))
@ -66,6 +70,7 @@ namespace MLEM.Data {
/// <param name="flags">The binding flags for field searching</param>
/// <param name="fieldInclusion">A predicate that determines whether or not the given field should be copied. If null, all fields will be copied.</param>
/// <typeparam name="T">The type of the object to copy</typeparam>
[Obsolete("CopyExtensions has major flaws and insufficient speed compared to other libraries specifically designed for copying objects.")]
public static void DeepCopyInto<T>(this T obj, T otherObj, BindingFlags flags = DefaultFlags, Predicate<FieldInfo> fieldInclusion = null) {
foreach (var field in obj.GetType().GetFields(flags)) {
if (fieldInclusion != null && !fieldInclusion(field))
@ -109,6 +114,6 @@ namespace MLEM.Data {
/// <summary>
/// An attribute that, when added to a constructor, will make that constructor the one used by <see cref="CopyExtensions.Copy{T}"/>, <see cref="CopyExtensions.DeepCopy{T}"/> and <see cref="CopyExtensions.DeepCopyInto{T}"/>.
/// </summary>
[AttributeUsage(AttributeTargets.Constructor)]
[AttributeUsage(AttributeTargets.Constructor), Obsolete("CopyExtensions has major flaws and insufficient speed compared to other libraries specifically designed for copying objects.")]
public class CopyConstructorAttribute : Attribute {}
}

View file

@ -119,13 +119,6 @@ namespace Sandbox {
Console.WriteLine(vec + " -> " + dir);
}
var copy = obj.DeepCopy();
Console.WriteLine(copy);
var intoCopy = new Test(Vector2.One, "test") {OtherTest = new Test(Vector2.One, "other")};
obj.DeepCopyInto(intoCopy);
Console.WriteLine(intoCopy);
var writer = new StringWriter();
this.Content.GetJsonSerializer().Serialize(writer, obj);
//Console.WriteLine(writer.ToString());
@ -345,7 +338,6 @@ namespace Sandbox {
public Direction2 Dir { get; set; }
public Test OtherTest;
[CopyConstructor]
public Test(Vector2 test, string test2) {
Console.WriteLine("Constructed with " + test + ", " + test2);
}

View file

@ -1,5 +1,4 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Numerics;
using Microsoft.Xna.Framework;
@ -38,33 +37,6 @@ namespace Tests {
Assert.AreEqual(this.testObject, read);
}
[Test]
public void TestCopy() {
var copy = this.testObject.Copy();
Assert.AreEqual(this.testObject, copy);
Assert.AreSame(this.testObject.OtherTest, copy.OtherTest);
var deepCopy = this.testObject.DeepCopy();
Assert.AreEqual(this.testObject, deepCopy);
Assert.AreNotSame(this.testObject.OtherTest, deepCopy.OtherTest);
}
[Test]
public void TestCopySpeed() {
const int count = 1000000;
var stopwatch = Stopwatch.StartNew();
for (var i = 0; i < count; i++)
this.testObject.Copy();
stopwatch.Stop();
TestContext.WriteLine($"Copy took {stopwatch.Elapsed.TotalMilliseconds / count * 1000000}ns on average");
stopwatch.Restart();
for (var i = 0; i < count; i++)
this.testObject.DeepCopy();
stopwatch.Stop();
TestContext.WriteLine($"DeepCopy took {stopwatch.Elapsed.TotalMilliseconds / count * 1000000}ns on average");
}
[Test]
public void TestDynamicEnum() {
var flags = new TestEnum[100];