2012-02-01 22:45:15 +00:00
|
|
|
|
#region CPL License
|
|
|
|
|
/*
|
|
|
|
|
Nuclex Framework
|
|
|
|
|
Copyright (C) 2002-2010 Nuclex Development Labs
|
|
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the IBM Common Public License as
|
|
|
|
|
published by the IBM Corporation; either version 1.0 of the
|
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
IBM Common Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the IBM Common Public
|
|
|
|
|
License along with this library
|
|
|
|
|
*/
|
|
|
|
|
#endregion
|
|
|
|
|
|
2012-02-03 14:49:31 +00:00
|
|
|
|
#if !(XBOX360 || WINDOWS_PHONE)
|
|
|
|
|
|
2012-02-01 22:45:15 +00:00
|
|
|
|
using System;
|
2012-02-03 14:49:31 +00:00
|
|
|
|
using System.Collections.Concurrent;
|
2012-02-01 22:45:15 +00:00
|
|
|
|
|
|
|
|
|
namespace Nuclex.Support.Cloning {
|
|
|
|
|
|
|
|
|
|
/// <summary>An action that takes its arguments as references to a structure</summary>
|
|
|
|
|
/// <typeparam name="TFirst">Type of the first argument to the method</typeparam>
|
|
|
|
|
/// <typeparam name="TSecond">Type of the second argument to the method</typeparam>
|
|
|
|
|
/// <param name="first">First argument to the method</param>
|
|
|
|
|
/// <param name="second">Second argument to the method</param>
|
|
|
|
|
public delegate void ReferenceAction<TFirst, TSecond>(ref TFirst first, ref TSecond second)
|
|
|
|
|
where TFirst : struct
|
|
|
|
|
where TSecond : struct;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Cloning factory which uses expression trees to improve performance when cloning
|
|
|
|
|
/// is a high-frequency action.
|
|
|
|
|
/// </summary>
|
2012-02-03 14:49:31 +00:00
|
|
|
|
public class ExpressionTreeCloner : ICloneFactory {
|
|
|
|
|
|
|
|
|
|
/// <summary>Initializes the static members of the expression tree cloner</summary>
|
|
|
|
|
static ExpressionTreeCloner() {
|
|
|
|
|
shallowCloners = new ConcurrentDictionary<Type, Func<object, object>>();
|
|
|
|
|
deepCloners = new ConcurrentDictionary<Type, Func<object, object>>();
|
|
|
|
|
}
|
2012-02-01 22:45:15 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a deep clone of the specified object, also creating clones of all
|
|
|
|
|
/// child objects being referenced
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TCloned">Type of the object that will be cloned</typeparam>
|
|
|
|
|
/// <param name="objectToClone">Object that will be cloned</param>
|
|
|
|
|
/// <param name="usePropertyBasedClone">
|
|
|
|
|
/// Whether to clone the object based on its properties only
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <returns>A deep clone of the provided object</returns>
|
2012-02-03 14:49:31 +00:00
|
|
|
|
public static TCloned DeepClone<TCloned>(
|
|
|
|
|
TCloned objectToClone, bool usePropertyBasedClone
|
|
|
|
|
) {
|
|
|
|
|
throw new NotImplementedException("Not implemented yet");
|
2012-02-01 22:45:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a shallow clone of the specified object, reusing any referenced objects
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TCloned">Type of the object that will be cloned</typeparam>
|
|
|
|
|
/// <param name="objectToClone">Object that will be cloned</param>
|
|
|
|
|
/// <param name="usePropertyBasedClone">
|
|
|
|
|
/// Whether to clone the object based on its properties only
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <returns>A shallow clone of the provided object</returns>
|
2012-02-03 14:49:31 +00:00
|
|
|
|
public static TCloned ShallowClone<TCloned>(
|
|
|
|
|
TCloned objectToClone, bool usePropertyBasedClone
|
|
|
|
|
) {
|
|
|
|
|
throw new NotImplementedException("Not implemented yet");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a deep clone of the specified object, also creating clones of all
|
|
|
|
|
/// child objects being referenced
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TCloned">Type of the object that will be cloned</typeparam>
|
|
|
|
|
/// <param name="objectToClone">Object that will be cloned</param>
|
|
|
|
|
/// <param name="usePropertyBasedClone">
|
|
|
|
|
/// Whether to clone the object based on its properties only
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <returns>A deep clone of the provided object</returns>
|
|
|
|
|
TCloned ICloneFactory.DeepClone<TCloned>(
|
|
|
|
|
TCloned objectToClone, bool usePropertyBasedClone
|
|
|
|
|
) {
|
|
|
|
|
return ExpressionTreeCloner.DeepClone<TCloned>(objectToClone, usePropertyBasedClone);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a shallow clone of the specified object, reusing any referenced objects
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TCloned">Type of the object that will be cloned</typeparam>
|
|
|
|
|
/// <param name="objectToClone">Object that will be cloned</param>
|
|
|
|
|
/// <param name="usePropertyBasedClone">
|
|
|
|
|
/// Whether to clone the object based on its properties only
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <returns>A shallow clone of the provided object</returns>
|
|
|
|
|
TCloned ICloneFactory.ShallowClone<TCloned>(
|
|
|
|
|
TCloned objectToClone, bool usePropertyBasedClone
|
|
|
|
|
) {
|
|
|
|
|
return ExpressionTreeCloner.ShallowClone<TCloned>(objectToClone, usePropertyBasedClone);
|
2012-02-01 22:45:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-02-03 14:49:31 +00:00
|
|
|
|
#if false
|
2012-02-01 22:45:15 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Transfers the state of one object into another, creating clones of referenced objects
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TState">Type of the object whose sate will be transferred</typeparam>
|
|
|
|
|
/// <param name="original">Original instance the state will be taken from</param>
|
|
|
|
|
/// <param name="target">Target instance the state will be written to</param>
|
|
|
|
|
/// <param name="propertyBased">Whether to perform a property-based state copy</param>
|
|
|
|
|
public void DeepCopyState<TState>(TState original, TState target, bool propertyBased)
|
|
|
|
|
where TState : class {
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Transfers the state of one object into another, creating clones of referenced objects
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TState">Type of the object whose sate will be transferred</typeparam>
|
|
|
|
|
/// <param name="original">Original instance the state will be taken from</param>
|
|
|
|
|
/// <param name="target">Target instance the state will be written to</param>
|
|
|
|
|
/// <param name="propertyBased">Whether to perform a property-based state copy</param>
|
|
|
|
|
public void DeepCopyState<TState>(ref TState original, ref TState target, bool propertyBased)
|
|
|
|
|
where TState : struct {
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Transfers the state of one object into another</summary>
|
|
|
|
|
/// <typeparam name="TState">Type of the object whose sate will be transferred</typeparam>
|
|
|
|
|
/// <param name="original">Original instance the state will be taken from</param>
|
|
|
|
|
/// <param name="target">Target instance the state will be written to</param>
|
|
|
|
|
/// <param name="propertyBased">Whether to perform a property-based state copy</param>
|
|
|
|
|
public void ShallowCopyState<TState>(TState original, TState target, bool propertyBased)
|
|
|
|
|
where TState : class {
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Transfers the state of one object into another</summary>
|
|
|
|
|
/// <typeparam name="TState">Type of the object whose sate will be transferred</typeparam>
|
|
|
|
|
/// <param name="original">Original instance the state will be taken from</param>
|
|
|
|
|
/// <param name="target">Target instance the state will be written to</param>
|
|
|
|
|
/// <param name="propertyBased">Whether to perform a property-based state copy</param>
|
|
|
|
|
public void ShallowCopyState<TState>(ref TState original, ref TState target, bool propertyBased)
|
|
|
|
|
where TState : struct {
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Compiles a method that copies the state of one object into another object
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TCloned">Type of object whose state will be copied</typeparam>
|
|
|
|
|
/// <param name="deepClone">Whether to create clones of the referenced objects</param>
|
|
|
|
|
/// <returns>A method that copies the state from one object into another object</returns>
|
|
|
|
|
public static Action<TCloned, TCloned> CreateReferenceCopier<TCloned>(bool deepClone)
|
|
|
|
|
where TCloned : class {
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Compiles a method that copies the state of one object into another object
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TCloned">Type of object whose state will be copied</typeparam>
|
|
|
|
|
/// <param name="deepClone">Whether to create clones of the referenced objects</param>
|
|
|
|
|
/// <returns>A method that copies the state from one object into another object</returns>
|
|
|
|
|
public static ReferenceAction<TCloned, TCloned> CreateValueCopier<TCloned>(bool deepClone)
|
|
|
|
|
where TCloned : struct {
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Compiles a method that creates a clone of an object</summary>
|
|
|
|
|
/// <typeparam name="TCloned">Type of object that will be cloned</typeparam>
|
|
|
|
|
/// <param name="deepClone">Whether to create clones of the referenced objects</param>
|
|
|
|
|
/// <returns>A method that clones an object of the provided type</returns>
|
|
|
|
|
public static Func<TCloned, TCloned> CreateCloner<TCloned>(bool deepClone)
|
|
|
|
|
where TCloned : class, new() {
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
2012-02-03 14:49:31 +00:00
|
|
|
|
#endif
|
2012-02-01 22:45:15 +00:00
|
|
|
|
|
2012-02-03 14:49:31 +00:00
|
|
|
|
/// <summary>Compiled cloners that perform shallow clone operations</summary>
|
|
|
|
|
private static ConcurrentDictionary<Type, Func<object, object>> shallowCloners;
|
|
|
|
|
/// <summary>Compiled cloners that perform deep clone operations</summary>
|
|
|
|
|
private static ConcurrentDictionary<Type, Func<object, object>> deepCloners;
|
2012-02-01 22:45:15 +00:00
|
|
|
|
|
2012-02-03 14:49:31 +00:00
|
|
|
|
}
|
2012-02-01 22:45:15 +00:00
|
|
|
|
|
|
|
|
|
} // namespace Nuclex.Support.Cloning
|
2012-02-03 14:49:31 +00:00
|
|
|
|
|
|
|
|
|
#endif // !(XBOX360 || WINDOWS_PHONE)
|