|
1 | 1 | using System;
|
2 | 2 |
|
3 |
| -namespace Kermalis.EndianBinaryIO |
| 3 | +namespace Kermalis.EndianBinaryIO; |
| 4 | + |
| 5 | +public interface IBinaryAttribute<T> |
| 6 | +{ |
| 7 | + T Value { get; } |
| 8 | +} |
| 9 | + |
| 10 | +[AttributeUsage(AttributeTargets.Property)] |
| 11 | +public sealed class BinaryIgnoreAttribute : Attribute, IBinaryAttribute<bool> |
4 | 12 | {
|
5 |
| - public interface IBinaryAttribute<T> |
| 13 | + public bool Value { get; } |
| 14 | + |
| 15 | + public BinaryIgnoreAttribute(bool ignore = true) |
6 | 16 | {
|
7 |
| - T Value { get; } |
| 17 | + Value = ignore; |
8 | 18 | }
|
| 19 | +} |
| 20 | +[AttributeUsage(AttributeTargets.Property)] |
| 21 | +public sealed class BinaryBooleanSizeAttribute : Attribute, IBinaryAttribute<BooleanSize> |
| 22 | +{ |
| 23 | + public BooleanSize Value { get; } |
9 | 24 |
|
10 |
| - [AttributeUsage(AttributeTargets.Property)] |
11 |
| - public sealed class BinaryIgnoreAttribute : Attribute, IBinaryAttribute<bool> |
| 25 | + public BinaryBooleanSizeAttribute(BooleanSize booleanSize) |
12 | 26 | {
|
13 |
| - public bool Value { get; } |
14 |
| - |
15 |
| - public BinaryIgnoreAttribute(bool ignore = true) |
| 27 | + if (booleanSize >= BooleanSize.MAX) |
16 | 28 | {
|
17 |
| - Value = ignore; |
| 29 | + throw new ArgumentOutOfRangeException(nameof(booleanSize), $"{nameof(BinaryBooleanSizeAttribute)} cannot be created with a size of {booleanSize}."); |
18 | 30 | }
|
| 31 | + Value = booleanSize; |
19 | 32 | }
|
20 |
| - [AttributeUsage(AttributeTargets.Property)] |
21 |
| - public sealed class BinaryBooleanSizeAttribute : Attribute, IBinaryAttribute<BooleanSize> |
22 |
| - { |
23 |
| - public BooleanSize Value { get; } |
| 33 | +} |
| 34 | +[AttributeUsage(AttributeTargets.Property)] |
| 35 | +public sealed class BinaryASCIIAttribute : Attribute, IBinaryAttribute<bool> |
| 36 | +{ |
| 37 | + public bool Value { get; } |
24 | 38 |
|
25 |
| - public BinaryBooleanSizeAttribute(BooleanSize booleanSize) |
26 |
| - { |
27 |
| - if (booleanSize >= BooleanSize.MAX) |
28 |
| - { |
29 |
| - throw new ArgumentOutOfRangeException(nameof(booleanSize), $"{nameof(BinaryBooleanSizeAttribute)} cannot be created with a size of {booleanSize}."); |
30 |
| - } |
31 |
| - Value = booleanSize; |
32 |
| - } |
33 |
| - } |
34 |
| - [AttributeUsage(AttributeTargets.Property)] |
35 |
| - public sealed class BinaryASCIIAttribute : Attribute, IBinaryAttribute<bool> |
| 39 | + public BinaryASCIIAttribute(bool ascii = true) |
36 | 40 | {
|
37 |
| - public bool Value { get; } |
38 |
| - |
39 |
| - public BinaryASCIIAttribute(bool ascii = true) |
40 |
| - { |
41 |
| - Value = ascii; |
42 |
| - } |
| 41 | + Value = ascii; |
43 | 42 | }
|
44 |
| - [AttributeUsage(AttributeTargets.Property)] |
45 |
| - public sealed class BinaryStringNullTerminatedAttribute : Attribute, IBinaryAttribute<bool> |
46 |
| - { |
47 |
| - public bool Value { get; } |
| 43 | +} |
| 44 | +[AttributeUsage(AttributeTargets.Property)] |
| 45 | +public sealed class BinaryStringNullTerminatedAttribute : Attribute, IBinaryAttribute<bool> |
| 46 | +{ |
| 47 | + public bool Value { get; } |
48 | 48 |
|
49 |
| - public BinaryStringNullTerminatedAttribute(bool nullTerminated = true) |
50 |
| - { |
51 |
| - Value = nullTerminated; |
52 |
| - } |
53 |
| - } |
54 |
| - [AttributeUsage(AttributeTargets.Property)] |
55 |
| - public sealed class BinaryArrayFixedLengthAttribute : Attribute, IBinaryAttribute<int> |
| 49 | + public BinaryStringNullTerminatedAttribute(bool nullTerminated = true) |
56 | 50 | {
|
57 |
| - public int Value { get; } |
58 |
| - |
59 |
| - public BinaryArrayFixedLengthAttribute(int length) |
60 |
| - { |
61 |
| - if (length < 0) |
62 |
| - { |
63 |
| - throw new ArgumentOutOfRangeException(nameof(length), $"{nameof(BinaryArrayFixedLengthAttribute)} cannot be created with a length of {length}. Length must be 0 or greater."); |
64 |
| - } |
65 |
| - Value = length; |
66 |
| - } |
| 51 | + Value = nullTerminated; |
67 | 52 | }
|
68 |
| - [AttributeUsage(AttributeTargets.Property)] |
69 |
| - public sealed class BinaryArrayVariableLengthAttribute : Attribute, IBinaryAttribute<string> |
70 |
| - { |
71 |
| - public string Value { get; } |
| 53 | +} |
| 54 | +[AttributeUsage(AttributeTargets.Property)] |
| 55 | +public sealed class BinaryArrayFixedLengthAttribute : Attribute, IBinaryAttribute<int> |
| 56 | +{ |
| 57 | + public int Value { get; } |
72 | 58 |
|
73 |
| - public BinaryArrayVariableLengthAttribute(string anchor) |
74 |
| - { |
75 |
| - Value = anchor; |
76 |
| - } |
77 |
| - } |
78 |
| - [AttributeUsage(AttributeTargets.Property)] |
79 |
| - public sealed class BinaryStringFixedLengthAttribute : Attribute, IBinaryAttribute<int> |
| 59 | + public BinaryArrayFixedLengthAttribute(int length) |
80 | 60 | {
|
81 |
| - public int Value { get; } |
82 |
| - |
83 |
| - public BinaryStringFixedLengthAttribute(int length) |
| 61 | + if (length < 0) |
84 | 62 | {
|
85 |
| - if (length < 0) |
86 |
| - { |
87 |
| - throw new ArgumentOutOfRangeException(nameof(length), $"{nameof(BinaryStringFixedLengthAttribute)} cannot be created with a length of {length}. Length must be 0 or greater."); |
88 |
| - } |
89 |
| - Value = length; |
| 63 | + throw new ArgumentOutOfRangeException(nameof(length), $"{nameof(BinaryArrayFixedLengthAttribute)} cannot be created with a length of {length}. Length must be 0 or greater."); |
90 | 64 | }
|
| 65 | + Value = length; |
91 | 66 | }
|
92 |
| - [AttributeUsage(AttributeTargets.Property)] |
93 |
| - public sealed class BinaryStringVariableLengthAttribute : Attribute, IBinaryAttribute<string> |
| 67 | +} |
| 68 | +[AttributeUsage(AttributeTargets.Property)] |
| 69 | +public sealed class BinaryArrayVariableLengthAttribute : Attribute, IBinaryAttribute<string> |
| 70 | +{ |
| 71 | + public string Value { get; } |
| 72 | + |
| 73 | + public BinaryArrayVariableLengthAttribute(string anchor) |
94 | 74 | {
|
95 |
| - public string Value { get; } |
| 75 | + Value = anchor; |
| 76 | + } |
| 77 | +} |
| 78 | +[AttributeUsage(AttributeTargets.Property)] |
| 79 | +public sealed class BinaryStringFixedLengthAttribute : Attribute, IBinaryAttribute<int> |
| 80 | +{ |
| 81 | + public int Value { get; } |
96 | 82 |
|
97 |
| - public BinaryStringVariableLengthAttribute(string anchor) |
| 83 | + public BinaryStringFixedLengthAttribute(int length) |
| 84 | + { |
| 85 | + if (length < 0) |
98 | 86 | {
|
99 |
| - Value = anchor; |
| 87 | + throw new ArgumentOutOfRangeException(nameof(length), $"{nameof(BinaryStringFixedLengthAttribute)} cannot be created with a length of {length}. Length must be 0 or greater."); |
100 | 88 | }
|
| 89 | + Value = length; |
101 | 90 | }
|
102 |
| - [AttributeUsage(AttributeTargets.Property)] |
103 |
| - public sealed class BinaryStringTrimNullTerminatorsAttribute : Attribute, IBinaryAttribute<bool> |
| 91 | +} |
| 92 | +[AttributeUsage(AttributeTargets.Property)] |
| 93 | +public sealed class BinaryStringVariableLengthAttribute : Attribute, IBinaryAttribute<string> |
| 94 | +{ |
| 95 | + public string Value { get; } |
| 96 | + |
| 97 | + public BinaryStringVariableLengthAttribute(string anchor) |
104 | 98 | {
|
105 |
| - public bool Value { get; } |
| 99 | + Value = anchor; |
| 100 | + } |
| 101 | +} |
| 102 | +[AttributeUsage(AttributeTargets.Property)] |
| 103 | +public sealed class BinaryStringTrimNullTerminatorsAttribute : Attribute, IBinaryAttribute<bool> |
| 104 | +{ |
| 105 | + public bool Value { get; } |
106 | 106 |
|
107 |
| - public BinaryStringTrimNullTerminatorsAttribute(bool trim = true) |
108 |
| - { |
109 |
| - Value = trim; |
110 |
| - } |
| 107 | + public BinaryStringTrimNullTerminatorsAttribute(bool trim = true) |
| 108 | + { |
| 109 | + Value = trim; |
111 | 110 | }
|
112 | 111 | }
|
0 commit comments