IDS peak ICV v1.3.0
Loading...
Searching...
No Matches
ArchiveExtensions.cs
Go to the documentation of this file.
1using System;
2using System.Linq;
4
6
41
45public static class ArchiveExtensions
46{
68 public static void SetValue(this IArchive archive, string key, object value)
69 {
70 if (archive is null)
71 {
72 throw new CommonInvalidParameterException($"The parameter {nameof(archive) } is null!");
73 }
74
75 if (value is null)
76 {
77 throw new CommonInvalidParameterException($"The parameter {nameof(value) } is null!");
78 }
79
80 switch (value)
81 {
82 case bool b:
83 archive.SetBool(key, b);
84 break;
85
86 case int i:
87 archive.SetInt64(key, i);
88 break;
89
90 case long l:
91 archive.SetInt64(key, l);
92 break;
93
94 case float f:
95 archive.SetDouble(key, f);
96 break;
97
98 case double d:
99 archive.SetDouble(key, d);
100 break;
101
102 case string s:
103 archive.SetString(key, s);
104 break;
105
106 case IArchive a:
107 archive.SetArchive(key, a);
108 break;
109
110 case bool[] ba:
111 archive.SetBoolArray(key, ba);
112 break;
113
114 case long[] la:
115 archive.SetInt64Array(key, la);
116 break;
117
118 case int[] ia:
119 archive.SetInt64Array(key, Array.ConvertAll(ia, x => (long)x));
120 break;
121
122 case double[] da:
123 archive.SetDoubleArray(key, da);
124 break;
125
126 case float[] fa:
127 archive.SetDoubleArray(key, Array.ConvertAll(fa, x => (double)x));
128 break;
129
130 case string[] sa:
131 archive.SetStringArray(key, sa);
132 break;
133
134 default:
136 $"Value type '{value.GetType().FullName}' is not supported."
137 );
138 }
139 }
140
164 public static T GetValue<T>(this IArchive archive, string key)
165 {
166 if (archive is null)
167 {
168 throw new CommonInvalidParameterException($"The parameter {nameof(archive) } is null!");
169 }
170
171 Type t = typeof(T);
172
173 if (t == typeof(bool))
174 {
175 return (T)(object)archive.GetBool(key);
176 }
177
178 // TODO: Discuss if we want to allow these (e.g. casting with overflow checks)
179 if (t == typeof(int))
180 {
181 return (T)(object)checked((int)archive.GetInt64(key));
182 }
183
184 if (t == typeof(long))
185 {
186 return (T)(object)archive.GetInt64(key);
187 }
188
189 if (t == typeof(float))
190 {
191 return (T)(object)checked((float)archive.GetDouble(key));
192 }
193
194 if (t == typeof(double))
195 {
196 return (T)(object)archive.GetDouble(key);
197 }
198
199 if (t == typeof(string))
200 {
201 return (T)(object)archive.GetString(key);
202 }
203
204 if (t == typeof(IArchive))
205 {
206 return (T)(object)archive.GetArchive(key);
207 }
208
209 if (t == typeof(bool[]))
210 {
211 return (T)(object)archive.GetBoolArray(key).ToArray();
212 }
213
214 if (t == typeof(long[]))
215 {
216 return (T)(object)archive.GetInt64Array(key).ToArray();
217 }
218
219 if (t == typeof(double[]))
220 {
221 return (T)(object)archive.GetDoubleArray(key).ToArray();
222 }
223
224 if (t == typeof(string[]))
225 {
226 return (T)(object)archive.GetStringArray(key).ToArray();
227 }
228
230 $"Type '{t.FullName}' is not supported."
231 );
232 }
233}
Exception thrown when an invalid parameter is detected.
Exception thrown when an operation is not supported.
Provides convenience extension methods for setting and retrieving values from an IArchive using commo...
static T GetValue< T >(this IArchive archive, string key)
Retrieves a value from the archive and converts it to the requested CLR type.
static void SetValue(this IArchive archive, string key, object value)
Stores a value in the archive using runtime type dispatch.
Represents a hierarchical, strongly-typed key-value archive.
Definition IArchive.cs:79
IArchive GetArchive(string key)
Retrieves a nested archive by key.
void SetBoolArray(string key, IEnumerable< bool > value)
Stores an array of boolean values under the specified key.
double GetDouble(string key)
Retrieves a double-precision floating-point value by key.
void SetBool(string key, bool value)
Stores a boolean value under the specified key.
IReadOnlyList< string > GetStringArray(string key)
Retrieves an array of string values by key.
long GetInt64(string key)
Retrieves a 64-bit integer value by key.
void SetStringArray(string key, IEnumerable< string > value)
Stores an array of string values under the specified key.
IReadOnlyList< long > GetInt64Array(string key)
Retrieves an array of 64-bit integer values by key.
void SetArchive(string key, IArchive value)
Stores a nested archive under the specified key.
void SetInt64(string key, long value)
Stores a 64-bit integer value under the specified key.
bool GetBool(string key)
Retrieves a boolean value by key.
IReadOnlyList< double > GetDoubleArray(string key)
Retrieves an array of double-precision floating-point values by key.
void SetInt64Array(string key, IEnumerable< long > value)
Stores an array of 64-bit integer values under the specified key.
void SetDouble(string key, double value)
Stores a double-precision floating-point value under the specified key.
string GetString(string key)
Retrieves a string value by key.
void SetDoubleArray(string key, IEnumerable< double > value)
Stores an array of double-precision floating-point values under the specified key.
void SetString(string key, string value)
Stores a string value under the specified key.
IReadOnlyList< bool > GetBoolArray(string key)
Retrieves an array of boolean values by key.