-
Notifications
You must be signed in to change notification settings - Fork 3
/
PyList.cs
88 lines (76 loc) · 2.34 KB
/
PyList.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
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace eveMarshal
{
public class PyList : PyObject, IEnumerable<PyObject>
{
public List<PyObject> Items { get; private set; }
public PyList()
: base(PyObjectType.List)
{
Items = new List<PyObject>();
}
public PyList(List<PyObject> items)
: base(PyObjectType.List)
{
Items = items;
}
public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
{
int count = -1;
switch (op)
{
case MarshalOpcode.ListEmpty:
count = 0;
break;
case MarshalOpcode.ListOne:
count = 1;
break;
case MarshalOpcode.List:
count = (int)source.ReadSizeEx();
break;
}
if (count >= 0)
{
Items = new List<PyObject>(count);
for (int i = 0; i < count; i++)
Items.Add(context.ReadObject(source));
}
}
protected override void EncodeInternal(BinaryWriter output)
{
if (Items.Count == 0)
output.WriteOpcode(MarshalOpcode.ListEmpty);
else
{
if (Items.Count == 1)
output.WriteOpcode(MarshalOpcode.ListOne);
else
{
output.WriteOpcode(MarshalOpcode.List);
output.WriteSizeEx(Items.Count);
}
foreach (var item in Items)
item.Encode(output);
}
}
public IEnumerator<PyObject> GetEnumerator()
{
return Items.GetEnumerator();
}
public override string ToString()
{
var sb = new StringBuilder("<\n");
foreach (var obj in Items)
sb.AppendLine("\t" + obj);
sb.Append(">");
return sb.ToString();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}