generated from gemstone/gemtem
-
Notifications
You must be signed in to change notification settings - Fork 4
/
PhysicalType.cs
222 lines (192 loc) · 7.21 KB
/
PhysicalType.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//******************************************************************************************************
// PhysicalType.cs - Gbtc
//
// Copyright © 2012, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://www.opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 05/02/2012 - Stephen C. Wills, Grid Protection Alliance
// Generated original version of source code.
// 12/17/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
using System;
using System.Collections.Generic;
using System.Numerics;
namespace Gemstone.PQDIF.Physical
{
#region [ Enumerations ]
/// <summary>
/// Enumeration that defines the types of values stored in
/// <see cref="ScalarElement"/>s and <see cref="VectorElement"/>s.
/// </summary>
public enum PhysicalType : byte
{
/// <summary>
/// 1-byte boolean
/// </summary>
Boolean1 = 1,
/// <summary>
/// 2-byte boolean
/// </summary>
Boolean2 = 2,
/// <summary>
/// 4-byte boolean
/// </summary>
Boolean4 = 3,
/// <summary>
/// 1-byte character (ASCII)
/// </summary>
Char1 = 10,
/// <summary>
/// 2-byte character (UTF-16)
/// </summary>
Char2 = 11,
/// <summary>
/// 8-bit signed integer
/// </summary>
Integer1 = 20,
/// <summary>
/// 16-bit signed integer
/// </summary>
Integer2 = 21,
/// <summary>
/// 32-bit signed integer
/// </summary>
Integer4 = 22,
/// <summary>
/// 8-bit unsigned integer
/// </summary>
UnsignedInteger1 = 30,
/// <summary>
/// 16-bit unsigned integer
/// </summary>
UnsignedInteger2 = 31,
/// <summary>
/// 32-bit unsigned integer
/// </summary>
UnsignedInteger4 = 32,
/// <summary>
/// 32-bit floating point number
/// </summary>
Real4 = 40,
/// <summary>
/// 64-bit floating point number
/// </summary>
Real8 = 41,
/// <summary>
/// 8-byte complex number
/// </summary>
/// <remarks>
/// The first four bytes represent the real part of the complex
/// number, and the last four bytes represent the imaginary part.
/// Both values are 32-bit floating point numbers.
/// </remarks>
Complex8 = 42,
/// <summary>
/// 16-byte complex number
/// </summary>
/// <remarks>
/// The first eight bytes represent the real part of the complex
/// number, and the last eight bytes represent the imaginary part.
/// Both values are 64-bit floating point numbers.
/// </remarks>
Complex16 = 43,
/// <summary>
/// 12-byte timestamp
/// </summary>
/// <remarks>
/// The first four bytes represent the days since January 1, 1900
/// UTC. The last eight bytes represent the number of seconds since
/// midnight. The number of days is an unsigned 32-bit integer, and
/// the number of seconds is a 64-bit floating point number.
/// </remarks>
Timestamp = 50,
/// <summary>
/// 128-bit globally unique identifier
/// </summary>
Guid = 60
}
#endregion
/// <summary>
/// Defines extension methods for <see cref="PhysicalType"/>.
/// </summary>
public static class PhysicalTypeExtensions
{
/// <summary>
/// Gets the size of the physical type, in bytes.
/// </summary>
/// <param name="type">The physical type.</param>
/// <returns>The size of the physical type, in bytes.</returns>
public static int GetByteSize(this PhysicalType type)
{
switch (type)
{
case PhysicalType.Boolean1:
case PhysicalType.Char1:
case PhysicalType.Integer1:
case PhysicalType.UnsignedInteger1:
return 1;
case PhysicalType.Boolean2:
case PhysicalType.Char2:
case PhysicalType.Integer2:
case PhysicalType.UnsignedInteger2:
return 2;
case PhysicalType.Boolean4:
case PhysicalType.Integer4:
case PhysicalType.UnsignedInteger4:
case PhysicalType.Real4:
return 4;
case PhysicalType.Real8:
case PhysicalType.Complex8:
return 8;
case PhysicalType.Timestamp:
return 12;
case PhysicalType.Complex16:
case PhysicalType.Guid:
return 16;
default:
throw new ArgumentOutOfRangeException("type");
}
}
/// <summary>
/// Gets the physical type that most closely maps to the given type.
/// </summary>
/// <param name="type">The type to be converted to a PQDIF physical type.</param>
/// <returns>The physical type that most closely maps to the given type.</returns>
public static PhysicalType GetPhysicalType(Type type)
{
Dictionary<Type, PhysicalType> physicalTypeLookup = new()
{
{ typeof(bool), PhysicalType.Boolean4 },
{ typeof(sbyte), PhysicalType.Integer1 },
{ typeof(short), PhysicalType.Integer2 },
{ typeof(int), PhysicalType.Integer4 },
{ typeof(byte), PhysicalType.UnsignedInteger1 },
{ typeof(ushort), PhysicalType.UnsignedInteger2 },
{ typeof(uint), PhysicalType.UnsignedInteger4 },
{ typeof(float), PhysicalType.Real4 },
{ typeof(double), PhysicalType.Real8 },
{ typeof(Complex), PhysicalType.Complex16 },
{ typeof(DateTime), PhysicalType.Timestamp },
{ typeof(string), PhysicalType.Char1 },
{ typeof(char), PhysicalType.Char1 }
};
if (!physicalTypeLookup.TryGetValue(type, out PhysicalType physicalType))
throw new ArgumentOutOfRangeException("type");
return physicalType;
}
}
}