Repository navigation
Expand file tree
/
Copy pathAttribute.cs
More file actions
56 lines (46 loc) · 2.05 KB
/
Copy pathAttribute.cs
File metadata and controls
56 lines (46 loc) · 2.05 KB
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
// Copyright (C) 2024 - Nordic Space Link
using System.Data;
using System.Diagnostics;
using System.Globalization;
using System.Text.RegularExpressions;
namespace NordicSpaceLink.IIO
{
[DebuggerDisplay("{Name}: {Value}")]
public abstract class Attribute
{
public string Name { get; }
/// <summary>
/// Read/write the current value of the attribute.
/// </summary>
public string Value { get => GetValue(); set => SetValue(value); }
/// <summary>
/// Read/write the current numeric value of the attribute.
/// Reading will try to parse as much as possible of the value if the value starts with a valid number.
/// </summary>
public long Int64 { get => long.Parse(NumberRegex.Match(Value).ToString()); set => Value = value.ToString(); }
/// <summary>
/// Read/write the current floating point value of the attribute.
/// Reading will try to parse as much as possible of the value if the value starts with a valid floating point number.
/// </summary>
public double Float { get => double.Parse(FloatRegex.Match(Value).ToString(), CultureInfo.InvariantCulture); set => Value = value.ToString(CultureInfo.InvariantCulture); }
/// <summary>
/// Read/write the current boolean value of the attribute. A non-zero integer is treated as true, while 0 is false.
/// </summary>
public bool Bool { get => Int64 != 0; set => Value = value ? "1" : "0"; }
public Attribute(string name)
{
Name = name;
}
public override string ToString()
{
return Value;
}
protected virtual void SetValue(string value)
{
throw new ReadOnlyException($"Attribute {Name} is read-only.");
}
protected abstract string GetValue();
private static readonly Regex FloatRegex = new(@"^[+-]?\d+(?:.\d+)?([eE][+-]?\d\d+)?", RegexOptions.Compiled);
private static readonly Regex NumberRegex = new(@"^[+-]?\d+", RegexOptions.Compiled);
}
}