-
Notifications
You must be signed in to change notification settings - Fork 6
/
FlagParameters.cs
63 lines (55 loc) · 1.76 KB
/
FlagParameters.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
using GotaSoundIO.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GotaSoundIO {
/// <summary>
/// Has optional parameters that are enabled by bit flags.
/// </summary>
public class FlagParameters : IReadable, IWriteable {
/// <summary>
/// Parameters.
/// </summary>
private uint?[] Parameters = new uint?[32];
/// <summary>
/// Get parameter.
/// </summary>
/// <param name="bit">Bit index.</param>
/// <returns>Parameter at bit index.</returns>
public uint? this[int bit] { get { return Parameters[bit]; } set { Parameters[bit] = value; } }
/// <summary>
/// Read the item.
/// </summary>
/// <param name="r">The reader.</param>
public void Read(FileReader r) {
uint mask = r.ReadUInt32();
for (int i = 0; i < 32; i++) {
if ((mask & (0b1 << i)) > 0) {
Parameters[i] = r.ReadUInt32();
} else {
Parameters[i] = null;
}
}
}
/// <summary>
/// Write the item.
/// </summary>
/// <param name="w">The writer.</param>
public void Write(FileWriter w) {
uint mask = 0;
for (int i = 0; i < 32; i++) {
if (Parameters[i] != null) {
mask |= (uint)(0b1 << i);
}
}
w.Write(mask);
foreach (var p in Parameters) {
if (p != null) {
w.Write(p.Value);
}
}
}
}
}