Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
SettingsValueKey.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.Generic;
5 using System.ComponentModel;
6 
7 using SiliconStudio.Core.IO;
8 using SiliconStudio.Presentation.Extensions;
9 
10 namespace SiliconStudio.Presentation.Settings
11 {
12  /// <summary>
13  /// This class represents a <see cref="SettingsKey"/> containing a single value of the specified type <see cref="T"/>.
14  /// </summary>
15  /// <typeparam name="T">The type of value contained in this settings key.</typeparam>
16  public class SettingsValueKey<T> : SettingsKey
17  {
18  /// <summary>
19  /// Initializes a new instance of the <see cref="SettingsValueKey{T}"/> class.
20  /// </summary>
21  /// <param name="name">The name of the settings key. Must be unique amongst an application.</param>
22  /// <param name="defaultValue">The default value for this settings key.</param>
23  public SettingsValueKey(UFile name, T defaultValue)
24  : base(name, defaultValue)
25  {
26  }
27 
28  /// <inheritdoc/>
29  public override Type Type { get { return typeof(T); } }
30 
31  /// <summary>
32  /// Gets the default value of this settings key.
33  /// </summary>
34  public T DefaultValue { get { return (T)DefaultObjectValue; } }
35 
36  /// <summary>
37  /// Gets or sets a function that returns an enumation of acceptable values for this <see cref="SettingsValueKey{T}"/>.
38  /// </summary>
39  public Func<IEnumerable<T>> GetAcceptableValues { get; set; }
40 
41  /// <summary>
42  /// Gets the value of this settings key in the given profile.
43  /// </summary>
44  /// <param name="searchInParentProfile">If true, the settings service will look in the parent profile of the given profile if the settings key is not defined into it.</param>
45  /// <param name="profile">The profile in which to look for the value. If <c>null</c>, it will look in the <see cref="SettingsService.CurrentProfile"/>.</param>
46  /// <param name="createInCurrentProfile">If true, the list will be created in the current profile, from the value of its parent profile.</param>
47  /// <returns>The value of this settings key.</returns>
48  /// <exception cref="KeyNotFoundException">No value can be found in the given profile matching this settings key.</exception>
49  public T GetValue(bool searchInParentProfile = true, SettingsProfile profile = null, bool createInCurrentProfile = false)
50  {
51  object value;
52  profile = profile ?? SettingsService.CurrentProfile;
53  if (profile.GetValue(Name, out value, searchInParentProfile, createInCurrentProfile))
54  {
55  return (T)value;
56  }
57  throw new KeyNotFoundException("Settings key not found");
58  }
59 
60  /// <summary>
61  /// Sets the value of this settings key in the given profile.
62  /// </summary>
63  /// <param name="value">The new value to set.</param>
64  /// <param name="profile">The profile in which to set the value. If <c>null</c>, it will look in the <see cref="SettingsService.CurrentProfile"/>.</param>
65  public void SetValue(T value, SettingsProfile profile = null)
66  {
67  profile = profile ?? SettingsService.CurrentProfile;
68  profile.SetValue(Name, value);
69  }
70 
71  /// <summary>
72  /// Tries to gets the value of this settings key in the given profile, if it exists.
73  /// </summary>
74  /// <param name="value">The resulting value, if found</param>
75  /// <param name="searchInParentProfile">If true, the settings service will look in the parent profile of the given profile if the settings key is not defined into it.</param>
76  /// <param name="profile">The profile in which to look for the value. If <c>null</c>, it will look in the <see cref="SettingsService.CurrentProfile"/>.</param>
77  /// <returns><c>true</c> if the value was found, <c>false</c> otherwise.</returns>
78  public bool TryGetValue(out T value, bool searchInParentProfile = true, SettingsProfile profile = null)
79  {
80  object obj;
81  profile = profile ?? SettingsService.CurrentProfile;
82  if (profile.GetValue(Name, out obj, searchInParentProfile, false))
83  {
84  try
85  {
86  value = (T)obj;
87  return true;
88  }
89  catch (Exception e)
90  {
91  // The cast exception will fallback to the value unfound result.
92  e.Ignore();
93  }
94  }
95  value = default(T);
96  return false;
97  }
98 
99  /// <inheritdoc/>
100  internal override object ConvertValue(object value)
101  {
102  T convertedValue;
103  try
104  {
105  var converter = TypeDescriptor.GetConverter(typeof(T));
106  if (converter.CanConvertFrom(value != null ? value.GetType() : typeof(object)))
107  {
108  convertedValue = (T)converter.ConvertFrom(value);
109  }
110  else
111  {
112  convertedValue = (T)Convert.ChangeType(value, typeof(T));
113  }
114  }
115  catch (Exception)
116  {
117  convertedValue = DefaultValue;
118  }
119  return convertedValue;
120  }
121  }
122 }
SettingsValueKey(UFile name, T defaultValue)
Initializes a new instance of the SettingsValueKey{T} class.
This class represents a collection of values for all registered SettingsKey. It may also contains val...
void SetValue(T value, SettingsProfile profile=null)
Sets the value of this settings key in the given profile.
This class represents property to store in the settings that is identified by a key.
Definition: SettingsKey.cs:37
T GetValue(bool searchInParentProfile=true, SettingsProfile profile=null, bool createInCurrentProfile=false)
Gets the value of this settings key in the given profile.
HRESULT Convert(_In_ const Image &srcImage, _In_ DXGI_FORMAT format, _In_ DWORD filter, _In_ float threshold, _Out_ ScratchImage &image)
bool TryGetValue(out T value, bool searchInParentProfile=true, SettingsProfile profile=null)
Tries to gets the value of this settings key in the given profile, if it exists.
Defines a normalized file path. See UPath for details. This class cannot be inherited.
Definition: UFile.cs:13