mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 12:58:33 +01:00
Marked CopyExtensions as obsolete
This commit is contained in:
parent
f3e6df6862
commit
c7f021e62d
4 changed files with 9 additions and 37 deletions
|
@ -48,6 +48,9 @@ Improvements
|
||||||
- Rethrow exceptions when no RawContentManager readers could be constructed
|
- Rethrow exceptions when no RawContentManager readers could be constructed
|
||||||
- Make Newtonsoft.Json dependency optional
|
- Make Newtonsoft.Json dependency optional
|
||||||
|
|
||||||
|
Removals
|
||||||
|
- Marked CopyExtensions as obsolete
|
||||||
|
|
||||||
## 5.2.0
|
## 5.2.0
|
||||||
### MLEM
|
### MLEM
|
||||||
Additions
|
Additions
|
||||||
|
|
|
@ -7,6 +7,7 @@ namespace MLEM.Data {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A set of extensions for dealing with copying objects.
|
/// A set of extensions for dealing with copying objects.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[Obsolete("CopyExtensions has major flaws and insufficient speed compared to other libraries specifically designed for copying objects.")]
|
||||||
public static class CopyExtensions {
|
public static class CopyExtensions {
|
||||||
|
|
||||||
private const BindingFlags DefaultFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
|
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>
|
/// <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>
|
/// <typeparam name="T">The type of the object to copy</typeparam>
|
||||||
/// <returns>A shallow copy of the object</returns>
|
/// <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) {
|
public static T Copy<T>(this T obj, BindingFlags flags = DefaultFlags, Predicate<FieldInfo> fieldInclusion = null) {
|
||||||
var copy = (T) Construct(typeof(T), flags);
|
var copy = (T) Construct(typeof(T), flags);
|
||||||
obj.CopyInto(copy, flags, fieldInclusion);
|
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>
|
/// <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>
|
/// <typeparam name="T">The type of the object to copy</typeparam>
|
||||||
/// <returns>A deep copy of the object</returns>
|
/// <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) {
|
public static T DeepCopy<T>(this T obj, BindingFlags flags = DefaultFlags, Predicate<FieldInfo> fieldInclusion = null) {
|
||||||
var copy = (T) Construct(typeof(T), flags);
|
var copy = (T) Construct(typeof(T), flags);
|
||||||
obj.DeepCopyInto(copy, flags, fieldInclusion);
|
obj.DeepCopyInto(copy, flags, fieldInclusion);
|
||||||
|
@ -50,6 +53,7 @@ namespace MLEM.Data {
|
||||||
/// <param name="flags">The binding flags for field searching</param>
|
/// <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>
|
/// <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>
|
/// <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) {
|
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)) {
|
foreach (var field in typeof(T).GetFields(flags)) {
|
||||||
if (fieldInclusion == null || fieldInclusion(field))
|
if (fieldInclusion == null || fieldInclusion(field))
|
||||||
|
@ -66,6 +70,7 @@ namespace MLEM.Data {
|
||||||
/// <param name="flags">The binding flags for field searching</param>
|
/// <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>
|
/// <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>
|
/// <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) {
|
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)) {
|
foreach (var field in obj.GetType().GetFields(flags)) {
|
||||||
if (fieldInclusion != null && !fieldInclusion(field))
|
if (fieldInclusion != null && !fieldInclusion(field))
|
||||||
|
@ -109,6 +114,6 @@ namespace MLEM.Data {
|
||||||
/// <summary>
|
/// <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}"/>.
|
/// 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>
|
/// </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 {}
|
public class CopyConstructorAttribute : Attribute {}
|
||||||
}
|
}
|
|
@ -119,13 +119,6 @@ namespace Sandbox {
|
||||||
Console.WriteLine(vec + " -> " + dir);
|
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();
|
var writer = new StringWriter();
|
||||||
this.Content.GetJsonSerializer().Serialize(writer, obj);
|
this.Content.GetJsonSerializer().Serialize(writer, obj);
|
||||||
//Console.WriteLine(writer.ToString());
|
//Console.WriteLine(writer.ToString());
|
||||||
|
@ -345,7 +338,6 @@ namespace Sandbox {
|
||||||
public Direction2 Dir { get; set; }
|
public Direction2 Dir { get; set; }
|
||||||
public Test OtherTest;
|
public Test OtherTest;
|
||||||
|
|
||||||
[CopyConstructor]
|
|
||||||
public Test(Vector2 test, string test2) {
|
public Test(Vector2 test, string test2) {
|
||||||
Console.WriteLine("Constructed with " + test + ", " + test2);
|
Console.WriteLine("Constructed with " + test + ", " + test2);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
|
@ -38,33 +37,6 @@ namespace Tests {
|
||||||
Assert.AreEqual(this.testObject, read);
|
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]
|
[Test]
|
||||||
public void TestDynamicEnum() {
|
public void TestDynamicEnum() {
|
||||||
var flags = new TestEnum[100];
|
var flags = new TestEnum[100];
|
||||||
|
|
Loading…
Reference in a new issue