Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
DefaultValueMetadata.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 namespace SiliconStudio.Core
4 {
5  /// <summary>
6  /// Abstract class that could be overloaded in order to define how to get default value of an <see cref="PropertyKey"/>.
7  /// </summary>
8  public abstract class DefaultValueMetadata : PropertyKeyMetadata
9  {
10  /// <summary>
11  /// Gets the default value of an external property, and specify if this default value should be kept.
12  /// It could be usefull with properties with default values depending of its container, especially if they are long to generate.
13  /// An example would be collision data, which should be generated only once.
14  /// </summary>
15  /// <param name="obj">Returns a default value as well as a boolean which specifies if the generated default value should be kept.</param>
16  /// <returns></returns>
17  public abstract object GetDefaultValue(ref PropertyContainer obj);
18 
19  /// <summary>
20  /// Gets a value indicating whether this value is kept.
21  /// </summary>
22  /// <value>
23  /// <c>true</c> if this value is kept; otherwise, <c>false</c>.
24  /// </value>
25  public virtual bool KeepValue
26  {
27  get { return false; }
28  }
29 
30  /// <summary>Gets or sets the property update callback.</summary>
31  /// <value>The property update callback.</value>
32  public PropertyContainer.PropertyUpdatedDelegate PropertyUpdateCallback { get; set; }
33 
34  public static StaticDefaultValueMetadata<T> Static<T>(T defaultValue, bool keepDefaultValue = false)
35  {
36  return new StaticDefaultValueMetadata<T>(defaultValue, keepDefaultValue);
37  }
38 
39  public static DelegateDefaultValueMetadata<T> Delegate<T>(DelegateDefaultValueMetadata<T>.DefaultValueCallback callback)
40  {
41  return new DelegateDefaultValueMetadata<T>(callback);
42  }
43  }
44 
45  public abstract class DefaultValueMetadata<T> : DefaultValueMetadata
46  {
47  /// <summary>
48  /// Gets the default value of an external property, and specify if this default value should be kept.
49  /// It could be usefull with properties with default values depending of its container, especially if they are long to generate.
50  /// An example would be collision data, which should be generated only once.
51  /// </summary>
52  /// <param name="obj">Returns a default value as well as a boolean which specifies if the generated default value should be kept.</param>
53  /// <returns></returns>
54  public abstract T GetDefaultValueT(ref PropertyContainer obj);
55 
56  public override object GetDefaultValue(ref PropertyContainer obj)
57  {
58  return GetDefaultValueT(ref obj);
59  }
60  }
61 
62  /// <summary>
63  /// Defines default value of a specific <see cref="PropertyKey"/> as a parameter value.
64  /// </summary>
65  public class StaticDefaultValueMetadata<T> : DefaultValueMetadata<T>
66  {
67  private readonly T defaultValue;
68  private readonly bool keepDefaultValue;
69 
70  /// <summary>
71  /// Initializes a new instance of the <see cref="StaticDefaultValueMetadata"/> class.
72  /// </summary>
73  /// <param name="defaultValue">The default value.</param>
74  /// <param name="keepDefaultValue">if set to <c>true</c> [keep default value].</param>
75  public StaticDefaultValueMetadata(T defaultValue, bool keepDefaultValue = false)
76  {
77  this.defaultValue = defaultValue;
78  this.keepDefaultValue = keepDefaultValue;
79  }
80 
81  /// <inheritdoc/>
82  public override T GetDefaultValueT(ref PropertyContainer obj)
83  {
84  return defaultValue;
85  }
86 
87  /// <inheritdoc/>
88  public override bool KeepValue
89  {
90  get
91  {
92  return keepDefaultValue;
93  }
94  }
95  }
96 
97  /// <summary>
98  /// Specifies a delegate to fetch the default value of an <see cref="PropertyKey"/>.
99  /// </summary>
100  public class DelegateDefaultValueMetadata<T> : DefaultValueMetadata<T>
101  {
102  private readonly DefaultValueCallback callback;
103 
104  /// <summary>
105  /// Callback used to initialiwe the tag value.
106  /// </summary>
107  /// <param name="container">The tag property container.</param>
108  /// <returns>Value of the tag.</returns>
109  public delegate T DefaultValueCallback(ref PropertyContainer container);
110 
111  /// <summary>
112  /// Initializes a new instance of the <see cref="DelegateDefaultValueMetadata"/> class.
113  /// </summary>
114  /// <param name="callback">The callback.</param>
115  public DelegateDefaultValueMetadata(DefaultValueCallback callback)
116  {
117  this.callback = callback;
118  }
119 
120  public override T GetDefaultValueT(ref PropertyContainer obj)
121  {
122  return callback(ref obj);
123  }
124 
125  public override bool KeepValue
126  {
127  get { return true; }
128  }
129  }
130 }
Abstract class that could be overloaded in order to define how to get default value of an PropertyKey...
override object GetDefaultValue(ref PropertyContainer obj)
Gets the default value of an external property, and specify if this default value should be kept...
Represents a container that can hold properties, lightweight to embed (lazy initialized).
StaticDefaultValueMetadata(T defaultValue, bool keepDefaultValue=false)
Initializes a new instance of the StaticDefaultValueMetadata class.
Defines default value of a specific PropertyKey as a parameter value.
override T GetDefaultValueT(ref PropertyContainer obj)
Gets the default value of an external property, and specify if this default value should be kept...
Specifies metadata for an PropertyKey.
Specifies a delegate to fetch the default value of an PropertyKey.
override T GetDefaultValueT(ref PropertyContainer obj)
Gets the default value of an external property, and specify if this default value should be kept...
DelegateDefaultValueMetadata(DefaultValueCallback callback)
Initializes a new instance of the DelegateDefaultValueMetadata class.