~ruther/NosSmooth

ref: 7f45f96e9a0ff9e0c0110b6184eb41aee91667af NosSmooth/Core/NosSmooth.PacketSerializersGenerator/Data/Parameters.cs -rw-r--r-- 2.1 KiB
7f45f96e — František Boháček refactor: split string converters 3 years ago
                                                                                
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
//
//  Parameters.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

using NosSmooth.PacketSerializersGenerator.AttributeGenerators;
using NosSmooth.PacketSerializersGenerator.Extensions;

namespace NosSmooth.PacketSerializersGenerator.Data;

/// <summary>
/// Contains set of parameters of a packet.
/// </summary>
public class Parameters
{
    /// <summary>
    /// Initializes a new instance of the <see cref="Parameters"/> class.
    /// </summary>
    /// <param name="parameters">The list of the parameters.</param>
    public Parameters(IReadOnlyList<ParameterInfo> parameters)
    {
        List = parameters;
    }

    /// <summary>
    /// Gets the list of the parameters.
    /// </summary>
    public IReadOnlyList<ParameterInfo> List { get; }

    /// <summary>
    /// Gets the current index of the parameter.
    /// </summary>
    public int CurrentIndex { get; set; }

    /// <summary>
    /// Gets the currently processing parameter.
    /// </summary>
    public ParameterInfo Current => List[CurrentIndex];

    /// <summary>
    /// Gets the next processing parameter.
    /// </summary>
    public ParameterInfo? Next => CurrentIndex < List.Count - 1 ? List[CurrentIndex + 1] : null;

    /// <summary>
    /// Gets the previous processing parameter.
    /// </summary>
    public ParameterInfo? Previous => CurrentIndex > 0 ? List[CurrentIndex - 1] : null;

    /// <summary>
    /// Gets whether the current parameter is the last one.
    /// </summary>
    public bool IsLast => CurrentIndex == List.Count - 1 || IsRestOptionals();

    /// <summary>
    /// Gets whether the current parameter is the first one.
    /// </summary>
    public bool IsFirst => CurrentIndex == 0;

    private bool IsRestOptionals()
    {
        for (int i = CurrentIndex + 1; i < List.Count; i++)
        {
            if (!List[i].IsOptional() && List[i].Attributes.All(x => x.FullName != PacketConditionalIndexAttributeGenerator.PacketConditionalIndexAttributeFullName))
            {
                return false;
            }
        }

        return true;
    }
}
Do not follow this link