-
Notifications
You must be signed in to change notification settings - Fork 4
/
None.cs
90 lines (82 loc) · 4.16 KB
/
None.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
using System.Runtime.CompilerServices;
using System.Xml.Linq;
namespace DSPtoVCXPROJ;
/// <summary>
/// The base class for files, meant to contain the filename as well as a filter. Because it is also used as the base for project-wide
/// objects, the filename and filter are optional so they can be unused on project-wide objects. It is also meant for any files in the
/// project that don't have a specific type.
/// </summary>
class None
{
public string? Include { get; }
public Filter? Filter { get; set; }
public None(string? include = null) => this.Include = include;
/// <summary>
/// Gets the block as an <see cref="XElement" />.
/// </summary>
/// <returns>The block as an <see cref="XElement" />.</returns>
public virtual XElement GetBlock()
{
var block = Program.CreateElement(nameof(None));
block.AddAttributeIfNotBlank(this.Include);
if (this.Filter is not null)
block.Add(this.Filter.GetBlock(false));
return block;
}
/// <summary>
/// Normally calls <see cref="Program.CreateElement" /> to create a generic <see cref="XElement "/> block, but can be overridden in
/// order to do more than that.
/// </summary>
/// <param name="name">The name of the element.</param>
/// <param name="elements">The child elements to add to the block.</param>
/// <returns>The generic <see cref="XElement" /> block.</returns>
public virtual XElement CreateElement(string name, object value) => Program.CreateElement(name, value);
/// <summary>
/// Adds an <see cref="XElement" /> child to an existing <see cref="XElement" />, with the given string value and the name coming from
/// the call to the method, but only if the value is not null or whitespace.
/// </summary>
/// <param name="block">The <see cref="XElement" /> to add the child to.</param>
/// <param name="value">The value to add, if it isn't null or whitespace.</param>
/// <param name="name">
/// The name for the element, which comes from the expression for <paramref name="value" /> and should not need to be supplied.
/// </param>
public void AddIfNotBlank(XElement block, string? value, [CallerArgumentExpression(nameof(value))] string name = null!)
{
// The range on name skips 'this.' in the expression for 'name'
if (!string.IsNullOrWhiteSpace(value))
block.Add(this.CreateElement(name[5..], value));
}
/// <summary>
/// Adds an <see cref="XElement" /> child to an existing <see cref="XElement" />, with the given <see cref="HashSet{T}" /> of string
/// values and the name coming from the call to the method, but only if the set is not empty. The values in the set will be joined by a
/// separator before the element is added.
/// </summary>
/// <param name="block">The <see cref="XElement" /> to add the child to.</param>
/// <param name="set">The set to add, if it isn't null.</param>
/// <param name="separator">The separator to use between elements of the set.</param>
/// <param name="name">
/// The name for the element, which comes from the expression for <paramref name="set" /> and should not need to be supplied.
/// </param>
public void AddIfNotEmpty(XElement block, HashSet<string> set, char separator,
[CallerArgumentExpression(nameof(set))] string name = null!)
{
// The range on name skips 'this.' in the expression for 'name'
if (set.Count > 0)
block.Add(this.CreateElement(name[5..], string.Join(separator, set.Append($"%({name[5..]})"))));
}
/// <summary>
/// Adds an <see cref="XElement" /> child to an existing <see cref="XElement" />, with the given boolean value and the name coming from
/// the call to the method, but only if the value is not null.
/// </summary>
/// <param name="block">The <see cref="XElement" /> to add the child to.</param>
/// <param name="value">The value to add, if it isn't null.</param>
/// <param name="name">
/// The name for the element, which comes from the expression for <paramref name="value" /> and should not need to be supplied.
/// </param>
public void AddIfNotNull(XElement block, bool? value, [CallerArgumentExpression(nameof(value))] string name = null!)
{
// The range on name skips 'this.' in the expression for 'name'
if (value is not null)
block.Add(this.CreateElement(name[5..], value == true ? "true" : "false"));
}
}