Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
PropertyCollection.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.Collections.Concurrent;
5 using System.Collections.Generic;
6 using SiliconStudio.Core;
7 
8 namespace SiliconStudio.Assets
9 {
10  /// <summary>
11  /// A collection of properties.
12  /// </summary>
13  [DataContract("PropertyCollection")]
14  public sealed class PropertyCollection : ConcurrentDictionary<PropertyKey, object>
15  {
16  /// <summary>
17  /// Initializes a new instance of the <see cref="PropertyCollection"/> class.
18  /// </summary>
20  {
21  }
22 
23  /// <summary>
24  /// Initializes a new instance of the <see cref="PropertyCollection"/> class.
25  /// </summary>
26  /// <param name="dictionary">The dictionary.</param>
27  public PropertyCollection(IEnumerable<KeyValuePair<PropertyKey, object>> dictionary)
28  : base(dictionary)
29  {
30  }
31 
32  /// <summary>
33  /// Gets a value for the specified key, null if not found.
34  /// </summary>
35  /// <param name="key">The key.</param>
36  /// <returns>A value for the specified key, null if not found.</returns>
37  public object Get(PropertyKey key)
38  {
39  object value;
40  TryGetValue(key, out value);
41  return value;
42  }
43 
44  /// <summary>
45  /// Gets a value for the specified key, null if not found.
46  /// </summary>
47  /// <typeparam name="T">Type of the value</typeparam>
48  /// <param name="key">The key.</param>
49  /// <returns>a value for the specified key, null if not found.</returns>
50  public T Get<T>(PropertyKey<T> key)
51  {
52  var value = Get((PropertyKey)key);
53  return value == null ? default(T) : (T)value;
54  }
55 
56  /// <summary>
57  /// Sets a value for the specified key.
58  /// </summary>
59  /// <param name="key">The key.</param>
60  /// <param name="value">The value.</param>
61  public void Set(PropertyKey key, object value)
62  {
63  this[key] = value;
64  }
65 
66  /// <summary>
67  /// Sets a value for the specified key.
68  /// </summary>
69  /// <typeparam name="T">Type of the value.</typeparam>
70  /// <param name="key">The key.</param>
71  /// <param name="value">The value.</param>
72  public void Set<T>(PropertyKey<T> key, T value)
73  {
74  this[key] = value;
75  }
76 
77  /// <summary>
78  /// Copies this properties to a output dictionary.
79  /// </summary>
80  /// <param name="properties">The dictionary to receive a copy of the properties of this instance.</param>
81  /// <param name="overrideValues">if set to <c>true</c> [override values].</param>
82  /// <exception cref="System.ArgumentNullException">properties</exception>
83  public void CopyTo(IDictionary<PropertyKey, object> properties, bool overrideValues)
84  {
85  if (properties == null) throw new ArgumentNullException("properties");
86  foreach (var propKeyValue in this)
87  {
88  if (!overrideValues && properties.ContainsKey(propKeyValue.Key))
89  {
90  continue;
91  }
92  properties[propKeyValue.Key] = propKeyValue.Value;
93  }
94  }
95  }
96 }
PropertyCollection(IEnumerable< KeyValuePair< PropertyKey, object >> dictionary)
Initializes a new instance of the PropertyCollection class.
PropertyCollection()
Initializes a new instance of the PropertyCollection class.
object Get(PropertyKey key)
Gets a value for the specified key, null if not found.
void Set(PropertyKey key, object value)
Sets a value for the specified key.
A class that represents a typed tag propety.
Definition: PropertyKey.cs:138
void CopyTo(IDictionary< PropertyKey, object > properties, bool overrideValues)
Copies this properties to a output dictionary.
A class that represents a tag propety.
Definition: PropertyKey.cs:17