Added more comments and and done some minor refactoring

git-svn-id: file:///srv/devel/repo-conversion/nusu@238 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2012-02-06 12:30:59 +00:00
parent 28998dbdcf
commit b2e484673d

View File

@ -208,21 +208,30 @@ namespace Nuclex.Support.Cloning {
);
}
// Create a new (empty) array with the same dimensions and lengths as the original
transferExpressions.Add(
Expression.Assign(
clone, Expression.NewArrayBounds(elementType, lengths)
)
);
Expression innerLoop = null;
// Initialize the indexer of the outer loop (indexers are initialized one up
// in the loops (ie. before the loop using it begins), so we have to set this
// one outside of the loop building code.
transferExpressions.Add(
Expression.Assign(indexes[0], Expression.Constant(0))
);
// We use a temporary variable to store the element
ParameterExpression element = Expression.Variable(elementType);
variables.Add(element);
// Build the nested loops (one for each dimension) from the inside out
Expression innerLoop = null;
for(int index = dimensionCount - 1; index >= 0; --index) {
var loopExpressions = new List<Expression>();
// If we reached the end of the current array dimension, break the loop
loopExpressions.Add(
Expression.IfThen(
Expression.GreaterThanOrEqual(indexes[index], lengths[index]),
@ -231,6 +240,9 @@ namespace Nuclex.Support.Cloning {
);
if(innerLoop == null) {
// The innermost loop clones an actual array element
loopExpressions.Add(
Expression.Assign(
Expression.ArrayAccess(clone, indexes),
@ -238,20 +250,26 @@ namespace Nuclex.Support.Cloning {
)
);
} else {
// Outer loops of any level just reset the inner loop's indexer and execute
// the inner loop
loopExpressions.Add(
Expression.Assign(indexes[index + 1], Expression.Constant(0))
);
loopExpressions.Add(innerLoop);
}
// Each time we executed the loop instructions, increment the indexer
loopExpressions.Add(Expression.PreIncrementAssign(indexes[index]));
// Build the loop using the expressions recorded above
innerLoop = Expression.Loop(
Expression.Block(loopExpressions),
labels[index]
);
}
// After the loop builder has finished, the innerLoop variable contains
// the entire hierarchy of nested loops, so add this to the clone expressions.
transferExpressions.Add(innerLoop);
return clone;
@ -298,16 +316,40 @@ namespace Nuclex.Support.Cloning {
transferExpressions
);
} else {
generateReferenceTypeTransferExpressions(
original, clone, transferExpressions, fieldInfo, fieldType
);
}
}
}
/// <summary>
/// Generates the expressions to transfer a reference type (array or class)
/// </summary>
/// <param name="original">Original value that will be cloned</param>
/// <param name="clone">Variable that will receive the cloned value</param>
/// <param name="transferExpressions">
/// Receives the expression generated to transfer the values
/// </param>
/// <param name="fieldInfo">Reflection informations about the field being cloned</param>
/// <param name="fieldType">Type of the field being cloned</param>
private static void generateReferenceTypeTransferExpressions(
Expression original,
Expression clone,
ICollection<Expression> transferExpressions,
FieldInfo fieldInfo,
Type fieldType
) {
// Reference types and arrays require special care because they can be null,
// so gather the transfer expressions in a separate block for the null check
Expression fieldClone;
var fieldTransferExpressions = new List<Expression>();
var fieldVariables = new List<ParameterExpression>();
if(fieldType.IsArray) {
// Arrays need to be cloned element-by-element
Type elementType = fieldType.GetElementType();
Expression fieldClone;
Type elementType = fieldType.GetElementType();
if(elementType.IsPrimitive || (elementType == typeof(string))) {
// For primitive arrays, the Array.Clone() method is sufficient
fieldClone = generatePrimitiveArrayTransferExpressions(
@ -378,8 +420,6 @@ namespace Nuclex.Support.Cloning {
)
);
}
}
}
/// <summary>Compiles a method that creates a clone of an object</summary>
/// <param name="clonedType">Type for which a clone method will be created</param>