#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
#if !(XBOX360 || WINDOWS_PHONE)
using System;
using System.Collections.Concurrent;
namespace Nuclex.Support.Cloning {
///
/// Cloning factory which uses expression trees to improve performance when cloning
/// is a high-frequency action.
///
public partial class ExpressionTreeCloner : ICloneFactory {
/// Initializes the static members of the expression tree cloner
static ExpressionTreeCloner() {
shallowFieldBasedCloners = new ConcurrentDictionary>();
deepFieldBasedCloners = new ConcurrentDictionary>();
}
///
/// Creates a deep clone of the specified object, also creating clones of all
/// child objects being referenced
///
/// Type of the object that will be cloned
/// Object that will be cloned
///
/// Whether to clone the object based on its properties only
///
/// A deep clone of the provided object
public static TCloned DeepClone(
TCloned objectToClone, bool usePropertyBasedClone
) {
object objectToCloneAsObject = objectToClone;
if(objectToCloneAsObject == null) {
return default(TCloned);
}
if(usePropertyBasedClone) {
throw new NotImplementedException("Not implemented yet");
} else {
Func cloner = getOrCreateDeepFieldBasedCloner(typeof(TCloned));
return (TCloned)cloner(objectToCloneAsObject);
}
}
///
/// Creates a shallow clone of the specified object, reusing any referenced objects
///
/// Type of the object that will be cloned
/// Object that will be cloned
///
/// Whether to clone the object based on its properties only
///
/// A shallow clone of the provided object
public static TCloned ShallowClone(
TCloned objectToClone, bool usePropertyBasedClone
) {
object objectToCloneAsObject = objectToClone;
if(objectToCloneAsObject == null) {
return default(TCloned);
}
if(usePropertyBasedClone) {
throw new NotImplementedException("Not implemented yet");
} else {
Func cloner = getOrCreateShallowFieldBasedCloner(typeof(TCloned));
return (TCloned)cloner(objectToCloneAsObject);
}
}
///
/// Creates a deep clone of the specified object, also creating clones of all
/// child objects being referenced
///
/// Type of the object that will be cloned
/// Object that will be cloned
///
/// Whether to clone the object based on its properties only
///
/// A deep clone of the provided object
TCloned ICloneFactory.DeepClone(
TCloned objectToClone, bool usePropertyBasedClone
) {
return ExpressionTreeCloner.DeepClone(objectToClone, usePropertyBasedClone);
}
///
/// Creates a shallow clone of the specified object, reusing any referenced objects
///
/// Type of the object that will be cloned
/// Object that will be cloned
///
/// Whether to clone the object based on its properties only
///
/// A shallow clone of the provided object
TCloned ICloneFactory.ShallowClone(
TCloned objectToClone, bool usePropertyBasedClone
) {
return ExpressionTreeCloner.ShallowClone(objectToClone, usePropertyBasedClone);
}
#if false
///
/// Transfers the state of one object into another, creating clones of referenced objects
///
/// Type of the object whose sate will be transferred
/// Original instance the state will be taken from
/// Target instance the state will be written to
/// Whether to perform a property-based state copy
public void DeepCopyState(TState original, TState target, bool propertyBased)
where TState : class {
throw new NotImplementedException();
}
///
/// Transfers the state of one object into another, creating clones of referenced objects
///
/// Type of the object whose sate will be transferred
/// Original instance the state will be taken from
/// Target instance the state will be written to
/// Whether to perform a property-based state copy
public void DeepCopyState(ref TState original, ref TState target, bool propertyBased)
where TState : struct {
throw new NotImplementedException();
}
/// Transfers the state of one object into another
/// Type of the object whose sate will be transferred
/// Original instance the state will be taken from
/// Target instance the state will be written to
/// Whether to perform a property-based state copy
public void ShallowCopyState(TState original, TState target, bool propertyBased)
where TState : class {
throw new NotImplementedException();
}
/// Transfers the state of one object into another
/// Type of the object whose sate will be transferred
/// Original instance the state will be taken from
/// Target instance the state will be written to
/// Whether to perform a property-based state copy
public void ShallowCopyState(ref TState original, ref TState target, bool propertyBased)
where TState : struct {
throw new NotImplementedException();
}
///
/// Compiles a method that copies the state of one object into another object
///
/// Type of object whose state will be copied
/// Whether to create clones of the referenced objects
/// A method that copies the state from one object into another object
public static Action CreateReferenceCopier(bool deepClone)
where TCloned : class {
throw new NotImplementedException();
}
#endif
///
/// Retrieves the existing clone method for the specified type or compiles one if
/// none exists for the type yet
///
/// Type for which a clone method will be retrieved
/// The clone method for the specified type
private static Func getOrCreateShallowFieldBasedCloner(Type clonedType) {
Func cloner;
if(!shallowFieldBasedCloners.TryGetValue(clonedType, out cloner)) {
cloner = createShallowFieldBasedCloner(clonedType);
shallowFieldBasedCloners.TryAdd(clonedType, cloner);
}
return cloner;
}
///
/// Retrieves the existing clone method for the specified type or compiles one if
/// none exists for the type yet
///
/// Type for which a clone method will be retrieved
/// The clone method for the specified type
private static Func getOrCreateDeepFieldBasedCloner(Type clonedType) {
Func cloner;
if(!deepFieldBasedCloners.TryGetValue(clonedType, out cloner)) {
cloner = createDeepFieldBasedCloner(clonedType);
deepFieldBasedCloners.TryAdd(clonedType, cloner);
}
return cloner;
}
/// Compiled cloners that perform shallow clone operations
private static ConcurrentDictionary> shallowFieldBasedCloners;
/// Compiled cloners that perform deep clone operations
private static ConcurrentDictionary> deepFieldBasedCloners;
/// Compiled cloners that perform shallow clone operations
private static ConcurrentDictionary> shallowPropertyBasedCloners;
/// Compiled cloners that perform deep clone operations
private static ConcurrentDictionary> deepPropertyBasedCloners;
}
} // namespace Nuclex.Support.Cloning
#endif // !(XBOX360 || WINDOWS_PHONE)