Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
DoubleAttribute.cs
1using System;
2using System.Threading.Tasks;
3using Waher.Content;
4using Waher.Script;
5
7{
11 public class DoubleAttribute
12 {
13 private readonly Expression expression;
14 private readonly string name;
15 private readonly string value;
16 private readonly double parsed;
17 private readonly bool hasEmbeddedScript;
18 private readonly bool isOnlyScript;
19
25 public DoubleAttribute(string Name, string Value)
26 {
27 this.name = Name;
28 this.value = Value;
29 this.hasEmbeddedScript = false;
30 this.isOnlyScript = false;
31 this.expression = null;
32
33 int i = Value.IndexOf('{');
34 if (i >= 0)
35 {
36 int j = Value.IndexOf('}', i + 1);
37 this.hasEmbeddedScript = j > i;
38 this.isOnlyScript = this.hasEmbeddedScript && i == 0 && j == Value.Length - 1;
39 if (this.isOnlyScript)
40 this.expression = new Expression(Value.Substring(1, j - i - 1));
41 }
42
43 if (!this.hasEmbeddedScript && !CommonTypes.TryParse(Value, out this.parsed))
44 throw new Exception("Invalid double value: " + Value);
45 }
46
50 public string Name => this.name;
51
55 public double Value => this.parsed;
56
62 public async Task<double> GetValueAsync(Variables Variables)
63 {
64 if (!this.hasEmbeddedScript)
65 return this.parsed;
66
67 if (this.isOnlyScript)
68 {
69 object Obj = await this.expression.EvaluateAsync(Variables);
70 return Expression.ToDouble(Obj);
71 }
72 else
73 {
74 string s = await Expression.TransformAsync(this.value, "{", "}", Variables);
75
76 if (!CommonTypes.TryParse(s, out double d))
77 throw new Exception("Invalid double value: " + s);
78
79 return d;
80 }
81 }
82
88 public async Task<byte> GetUInt8ValueAsync(Variables Variables)
89 {
90 double d = await this.GetValueAsync(Variables);
91 if (d < byte.MinValue || d > byte.MaxValue)
92 throw new ArgumentOutOfRangeException(this.name, d, "Out of byte range.");
93
94 return (byte)d;
95 }
96
102 public async Task<ushort> GetUInt16ValueAsync(Variables Variables)
103 {
104 double d = await this.GetValueAsync(Variables);
105 if (d < ushort.MinValue || d > ushort.MaxValue)
106 throw new ArgumentOutOfRangeException(this.name, d, "Out of ushort range.");
107
108 return (ushort)d;
109 }
110
116 public async Task<uint> GetUInt32ValueAsync(Variables Variables)
117 {
118 double d = await this.GetValueAsync(Variables);
119 if (d < uint.MinValue || d > uint.MaxValue)
120 throw new ArgumentOutOfRangeException(this.name, d, "Out of uint range.");
121
122 return (uint)d;
123 }
124
130 public async Task<ulong> GetUInt64ValueAsync(Variables Variables)
131 {
132 double d = await this.GetValueAsync(Variables);
133 if (d < ulong.MinValue || d > ulong.MaxValue)
134 throw new ArgumentOutOfRangeException(this.name, d, "Out of ulong range.");
135
136 return (ulong)d;
137 }
138
144 public async Task<sbyte> GetInt8ValueAsync(Variables Variables)
145 {
146 double d = await this.GetValueAsync(Variables);
147 if (d < sbyte.MinValue || d > sbyte.MaxValue)
148 throw new ArgumentOutOfRangeException(this.name, d, "Out of sbyte range.");
149
150 return (sbyte)d;
151 }
152
158 public async Task<short> GetInt16ValueAsync(Variables Variables)
159 {
160 double d = await this.GetValueAsync(Variables);
161 if (d < short.MinValue || d > short.MaxValue)
162 throw new ArgumentOutOfRangeException(this.name, d, "Out of short range.");
163
164 return (short)d;
165 }
166
172 public async Task<int> GetInt32ValueAsync(Variables Variables)
173 {
174 double d = await this.GetValueAsync(Variables);
175 if (d < int.MinValue || d > int.MaxValue)
176 throw new ArgumentOutOfRangeException(this.name, d, "Out of int range.");
177
178 return (int)d;
179 }
180
186 public async Task<long> GetInt64ValueAsync(Variables Variables)
187 {
188 double d = await this.GetValueAsync(Variables);
189 if (d < long.MinValue || d > long.MaxValue)
190 throw new ArgumentOutOfRangeException(this.name, d, "Out of long range.");
191
192 return (long)d;
193 }
194 }
195}
Contains the value of a double attribute, possibly with embedded script.
async Task< ushort > GetUInt16ValueAsync(Variables Variables)
Gets the value of the attribute, as an unsigned 16-bit integer.
async Task< double > GetValueAsync(Variables Variables)
Gets the value of the attribute.
async Task< byte > GetUInt8ValueAsync(Variables Variables)
Gets the value of the attribute, as an unsigned 8-bit integer.
async Task< int > GetInt32ValueAsync(Variables Variables)
Gets the value of the attribute, as a signed 32-bit integer.
DoubleAttribute(string Name, string Value)
Contains the value of a string attribute, possibly with embedded script.
async Task< uint > GetUInt32ValueAsync(Variables Variables)
Gets the value of the attribute, as an unsigned 32-bit integer.
async Task< long > GetInt64ValueAsync(Variables Variables)
Gets the value of the attribute, as a signed 64-bit integer.
async Task< sbyte > GetInt8ValueAsync(Variables Variables)
Gets the value of the attribute, as a signed 8-bit integer.
async Task< ulong > GetUInt64ValueAsync(Variables Variables)
Gets the value of the attribute, as an unsigned 64-bit integer.
double Value
Double value, from definition
async Task< short > GetInt16ValueAsync(Variables Variables)
Gets the value of the attribute, as a signed 16-bit integer.
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static bool TryParse(string s, out double Value)
Tries to decode a string encoded double.
Definition: CommonTypes.cs:46
Class managing a script expression.
Definition: Expression.cs:39
static double ToDouble(object Object)
Converts an object to a double value.
Definition: Expression.cs:4824
static Task< string > TransformAsync(string s, string StartDelimiter, string StopDelimiter, Variables Variables)
Transforms a string by executing embedded script.
Definition: Expression.cs:4441
Collection of variables.
Definition: Variables.cs:25