Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ParameterCollectionExtensions.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 
5 namespace SiliconStudio.Paradox.Effects
6 {
7  /// <summary>
8  /// Extensions for <see cref="ParameterCollection"/>.
9  /// </summary>
10  public static class ParameterCollectionExtensions
11  {
12  /// <summary>
13  /// Clones the specified <see cref="ParameterCollection"/>.
14  /// </summary>
15  /// <typeparam name="T">Type of the parameter collection</typeparam>
16  /// <param name="parameterCollection">The parameter collection.</param>
17  /// <returns>A clone of the parameter collection. Values are not cloned.</returns>
18  public static T Clone<T>(this T parameterCollection) where T : ParameterCollection, new()
19  {
20  var newParams = new T();
21  parameterCollection.CopyTo(newParams);
22  return newParams;
23  }
24 
25  /// <summary>
26  /// Copies the automatic.
27  /// </summary>
28  /// <typeparam name="T"></typeparam>
29  /// <param name="parameters">The parameters.</param>
30  /// <param name="parametersTo">The parameters automatic.</param>
31  public static void CopyTo<T>(this T parameters, ParameterCollection parametersTo) where T : ParameterCollection
32  {
33  if (parametersTo == null) throw new ArgumentNullException("parametersTo");
34  foreach (var parameter in parameters)
35  {
36  parametersTo.SetObject(parameter.Key, parameter.Value);
37  }
38  }
39 
40  /// <summary>
41  /// Determines whether this instance container is the subset of another container.
42  /// </summary>
43  /// <param name="subset">The container to test as a subset of the 'against' container.</param>
44  /// <param name="against">The container superset.</param>
45  /// <returns><c>true</c> if the specified against is subset; otherwise, <c>false</c>.</returns>
46  public static bool IsSubsetOf(this ParameterCollection subset, ParameterCollection against)
47  {
48  foreach (var keyValuePair in subset.InternalValues)
49  {
50  object value = against.GetObject(keyValuePair.Key);
51 
52  var innerFrom = keyValuePair.Value.Object as ParameterCollection;
53  if (innerFrom != null)
54  {
55  var innerTo = value as ParameterCollection;
56  if (innerTo == null)
57  {
58  return false;
59  }
60 
61  if (ReferenceEquals(innerFrom, innerTo))
62  {
63  continue;
64  }
65 
66  if (!innerFrom.IsSubsetOf(innerTo))
67  {
68  return false;
69  }
70  }
71  else
72  {
73  var innerFromArray = keyValuePair.Value.Object as ParameterCollection[];
74  if (innerFromArray != null)
75  {
76  var innerToArray = value as ParameterCollection[];
77  if (innerToArray == null)
78  {
79  return false;
80  }
81 
82  if (innerFromArray.Length != innerToArray.Length)
83  {
84  return false;
85  }
86 
87  for (int i = 0; i < innerFromArray.Length; i++)
88  {
89  if (ReferenceEquals(innerFromArray[i], innerToArray[i]))
90  {
91  continue;
92  }
93 
94  if (innerFromArray[i] == null || innerToArray[i] == null)
95  {
96  return false;
97  }
98 
99  if (!innerFromArray[i].IsSubsetOf(innerToArray[i]))
100  {
101  return false;
102  }
103  }
104  }
105  else if (!Equals(keyValuePair.Value.Object, value))
106  {
107  return false;
108  }
109  }
110  }
111 
112  // If we are here, then 'subset' container is included in 'against'
113  return true;
114  }
115  }
116 }
static bool IsSubsetOf(this ParameterCollection subset, ParameterCollection against)
Determines whether this instance container is the subset of another container.
A container to handle a hierarchical collection of effect variables.