Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
PropertyKey.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 using System.Diagnostics;
5 using System.Linq;
6 using System.Reflection;
7 using SiliconStudio.Core.Serialization.Serializers;
8 
9 namespace SiliconStudio.Core
10 {
11  /// <summary>
12  /// A class that represents a tag propety.
13  /// </summary>
14  [DataContract]
15  [DataSerializer(typeof(PropertyKeySerializer<>), Mode = DataSerializerGenericMode.Type)]
16  [DebuggerDisplay("{Name}")]
17  public abstract class PropertyKey : IComparable
18  {
19  private DefaultValueMetadata defaultValueMetadata;
20 
21  /// <summary>
22  /// Initializes a new instance of the <see cref="PropertyKey"/> class.
23  /// </summary>
24  /// <param name="name">The name.</param>
25  /// <param name="propertyType">Type of the property.</param>
26  /// <param name="ownerType">Type of the owner.</param>
27  /// <param name="metadatas">The metadatas.</param>
28  protected PropertyKey(string name, Type propertyType, Type ownerType, params PropertyKeyMetadata[] metadatas)
29  {
30  if (name == null) throw new ArgumentNullException("name");
31 
32  Name = name;
33  PropertyType = propertyType;
34  OwnerType = ownerType;
35  Metadatas = metadatas;
36  foreach (PropertyKeyMetadata metadata in metadatas)
37  {
38  if (metadata is DefaultValueMetadata)
39  {
40  DefaultValueMetadata = (DefaultValueMetadata)metadata;
41  }
42  if (metadata is AccessorMetadata)
43  {
44  AccessorMetadata = (AccessorMetadata)metadata;
45  }
46  if (metadata is ValidateValueMetadata)
47  {
48  ValidateValueMetadata = (ValidateValueMetadata)metadata;
49  }
50  if (metadata is ObjectInvalidationMetadata)
51  {
52  ObjectInvalidationMetadata = (ObjectInvalidationMetadata)metadata;
53  }
54  }
55  }
56 
57  /// <summary>
58  /// Gets the name of this key.
59  /// </summary>
60  public string Name { get; protected set; }
61 
62  /// <summary>
63  /// Gets the default value metadata.
64  /// </summary>
66  {
67  get { return defaultValueMetadata;}
68  set
69  {
70  defaultValueMetadata = value;
71  PropertyUpdateCallback = defaultValueMetadata.PropertyUpdateCallback;
72  }
73  }
74 
75  /// <summary>
76  /// Gets the validate value metadata (may be null).
77  /// </summary>
78  /// <value>The validate value metadata.</value>
79  public ValidateValueMetadata ValidateValueMetadata { get; private set; }
80 
81  /// <summary>
82  /// Gets the object invalidation metadata (may be null).
83  /// </summary>
84  /// <value>The object invalidation metadata.</value>
86 
87  /// <summary>
88  /// Gets the accessor metadata (may be null).
89  /// </summary>
90  /// <value>The accessor metadata.</value>
91  public AccessorMetadata AccessorMetadata { get; private set; }
92 
93  /// <summary>Gets or sets the property update callback.</summary>
94  /// <value>The property update callback.</value>
95  internal PropertyContainer.PropertyUpdatedDelegate PropertyUpdateCallback { get; private set; }
96 
97  /// <summary>
98  /// Gets the metadatas.
99  /// </summary>
100  public PropertyKeyMetadata[] Metadatas { get; private set; }
101 
102  /// <summary>
103  /// Gets the type of the owner.
104  /// </summary>
105  /// <value>
106  /// The type of the owner.
107  /// </value>
108  public Type OwnerType { get; protected set; }
109 
110  /// <summary>
111  /// Gets the type of the property.
112  /// </summary>
113  /// <value>
114  /// The type of the property.
115  /// </value>
116  public Type PropertyType { get; protected set; }
117 
118  public abstract bool IsValueType { get; }
119 
120  public int CompareTo(object obj)
121  {
122  var key = obj as PropertyKey;
123  if (key == null)
124  {
125  return 0;
126  }
127 
128  return string.Compare(Name, key.Name, StringComparison.OrdinalIgnoreCase);
129  }
130 
131  internal abstract PropertyContainer.ValueHolder CreateValueHolder(object value);
132  }
133 
134  /// <summary>
135  /// A class that represents a typed tag propety.
136  /// </summary>
137  /// <typeparam name="T">Type of the property</typeparam>
138  public sealed class PropertyKey<T> : PropertyKey
139  {
140  private readonly static bool isValueType = typeof(T).GetTypeInfo().IsValueType;
141 
142  /// <summary>
143  /// Initializes a new instance of the <see cref="PropertyKey{T}"/> class.
144  /// </summary>
145  /// <param name="name">The name.</param>
146  /// <param name="ownerType">Type of the owner.</param>
147  /// <param name="metadatas">The metadatas.</param>
148  public PropertyKey(string name, Type ownerType, params PropertyKeyMetadata[] metadatas)
149  : base(name, typeof(T), ownerType, GenerateDefaultData(metadatas))
150  {
151  }
152 
153  /// <inheritdoc/>
154  public override bool IsValueType
155  {
156  get { return isValueType; }
157  }
158 
159  /// <summary>
160  /// Gets the default value metadata.
161  /// </summary>
162  public DefaultValueMetadata<T> DefaultValueMetadataT { get { return (DefaultValueMetadata<T>)DefaultValueMetadata; } }
163 
164  /// <summary>
165  /// Gets the validate value metadata (may be null).
166  /// </summary>
167  /// <value>The validate value metadata.</value>
168  public ValidateValueMetadata<T> ValidateValueMetadataT { get { return (ValidateValueMetadata<T>)ValidateValueMetadata; } }
169 
170  /// <summary>
171  /// Gets the object invalidation metadata (may be null).
172  /// </summary>
173  /// <value>The object invalidation metadata.</value>
174  public ObjectInvalidationMetadata<T> ObjectInvalidationMetadataT { get { return (ObjectInvalidationMetadata<T>)ObjectInvalidationMetadata; } }
175 
176  private static PropertyKeyMetadata[] GenerateDefaultData(PropertyKeyMetadata[] metadatas)
177  {
178  if (metadatas == null)
179  {
180  return new PropertyKeyMetadata[] { new StaticDefaultValueMetadata<T>(default(T)) };
181  }
182 
183  var defaultMetaData = metadatas.OfType<DefaultValueMetadata>().FirstOrDefault();
184  if (defaultMetaData == null)
185  {
186  var newMetaDatas = new PropertyKeyMetadata[metadatas.Length + 1];
187  metadatas.CopyTo(newMetaDatas, 1);
188  newMetaDatas[0] = new StaticDefaultValueMetadata<T>(default(T));
189  return newMetaDatas;
190  }
191 
192  return metadatas;
193  }
194 
195  internal override PropertyContainer.ValueHolder CreateValueHolder(object value)
196  {
197  return new PropertyContainer.ValueHolder<T>((T)value);
198  }
199  }
200 }
201 
A metadata to allow validation/coercision of a value before storing the value into the PropertyContai...
Abstract class that could be overloaded in order to define how to get default value of an PropertyKey...
DataSerializerGenericMode
Defines what generic parameters to pass to the serializer.
PropertyKey(string name, Type ownerType, params PropertyKeyMetadata[] metadatas)
Initializes a new instance of the PropertyKey{T} class.
Definition: PropertyKey.cs:148
_In_ size_t _In_ const TexMetadata & metadata
Definition: DirectXTexP.h:116
Defines default value of a specific PropertyKey as a parameter value.
The type of the serialized type will be passed as a generic arguments of the serializer. Example: serializer of A becomes instantiated as Serializer{A}.
Specifies metadata for an PropertyKey.
PropertyKey(string name, Type propertyType, Type ownerType, params PropertyKeyMetadata[] metadatas)
Initializes a new instance of the PropertyKey class.
Definition: PropertyKey.cs:28
Metadata used to invalidate an object state after a property value modification.
A class that represents a tag propety.
Definition: PropertyKey.cs:17