cauterize 0.0.1.pre1 → 0.0.1.pre5
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/.rspec +1 -1
- data/Gemfile +1 -1
- data/README.md +0 -2
- data/Rakefile +19 -3
- data/bin/cauterize +14 -6
- data/example/Cauterize +22 -10
- data/example/build.sh +15 -2
- data/lib/cauterize/base_type.rb +2 -5
- data/lib/cauterize/builders.rb +1 -3
- data/lib/cauterize/builders/c/buildable.rb +3 -2
- data/lib/cauterize/builders/c/builtin.rb +46 -0
- data/lib/cauterize/builders/c/enumeration.rb +23 -2
- data/lib/cauterize/builders/c/fixed_array.rb +16 -7
- data/lib/cauterize/builders/c/group.rb +32 -3
- data/lib/cauterize/builders/c/scalar.rb +5 -0
- data/lib/cauterize/builders/c/variable_array.rb +1 -1
- data/lib/cauterize/builders/cs/buildable.rb +59 -0
- data/lib/cauterize/builders/cs/builtin.rb +23 -0
- data/lib/cauterize/builders/cs/composite.rb +21 -0
- data/lib/cauterize/builders/cs/csarray.rb +32 -0
- data/lib/cauterize/builders/cs/enumeration.rb +21 -0
- data/lib/cauterize/builders/cs/fixed_array.rb +25 -0
- data/lib/cauterize/builders/cs/group.rb +33 -0
- data/lib/cauterize/builders/cs/scalar.rb +10 -0
- data/lib/cauterize/builders/cs/variable_array.rb +34 -0
- data/lib/cauterize/builtin.rb +52 -0
- data/lib/cauterize/c_builder.rb +15 -1
- data/lib/cauterize/cauterize.rb +44 -13
- data/lib/cauterize/composite.rb +3 -3
- data/lib/cauterize/cs_builder.rb +53 -0
- data/lib/cauterize/enumeration.rb +20 -3
- data/lib/cauterize/fixed_array.rb +3 -3
- data/lib/cauterize/formatter.rb +7 -3
- data/lib/cauterize/group.rb +5 -8
- data/lib/cauterize/scalar.rb +16 -7
- data/lib/cauterize/variable_array.rb +6 -6
- data/lib/cauterize/version.rb +1 -1
- data/spec/base_type_spec.rb +133 -125
- data/spec/builders/c/buildable_spec.rb +18 -18
- data/spec/builders/c/builtin_spec.rb +22 -0
- data/spec/builders/c/composite_spec.rb +37 -33
- data/spec/builders/c/enumeration_spec.rb +84 -21
- data/spec/builders/c/fixed_array_spec.rb +6 -6
- data/spec/builders/c/group_spec.rb +97 -90
- data/spec/builders/c/scalar_spec.rb +24 -6
- data/spec/builders/c/variable_array_spec.rb +37 -37
- data/spec/builders/cs/buildable_spec.rb +8 -0
- data/spec/builders/cs/composite_spec.rb +32 -0
- data/spec/builders/cs/enumeration_spec.rb +33 -0
- data/spec/builders/cs/fixed_array_spec.rb +40 -0
- data/spec/builders/cs/group_spec.rb +56 -0
- data/spec/builders/cs/scalar_spec.rb +7 -0
- data/spec/builders/cs/variable_array_spec.rb +46 -0
- data/spec/builders_spec.rb +38 -38
- data/spec/builtin_spec.rb +46 -0
- data/spec/c_builder_spec.rb +116 -102
- data/spec/cauterize_spec.rb +8 -1
- data/spec/composite_spec.rb +52 -48
- data/spec/cs_builder_spec.rb +113 -0
- data/spec/enumeration_spec.rb +55 -16
- data/spec/fixed_array_spec.rb +7 -9
- data/spec/group_spec.rb +81 -76
- data/spec/scalar_spec.rb +20 -10
- data/spec/spec_helper.rb +103 -94
- data/spec/support/shared_examples_for_c_buildables.rb +68 -64
- data/spec/variable_array_spec.rb +12 -17
- data/{c → support/c}/src/cauterize.c +8 -7
- data/support/c/src/cauterize.h +59 -0
- data/{c → support/c}/src/cauterize_debug.h +0 -0
- data/support/c/src/cauterize_util.h +49 -0
- data/{c → support/c}/test/greatest.h +0 -0
- data/{c → support/c}/test/test.c +0 -0
- data/support/cs/src/CauterizeCompositeFormatter.cs +34 -0
- data/support/cs/src/CauterizeContainerFormatter.cs +18 -0
- data/support/cs/src/CauterizeEnumFormatter.cs +67 -0
- data/support/cs/src/CauterizeException.cs +15 -0
- data/support/cs/src/CauterizeFixedArrayFormatter.cs +39 -0
- data/support/cs/src/CauterizeFormatter.cs +40 -0
- data/support/cs/src/CauterizeGroupFormatter.cs +46 -0
- data/support/cs/src/CauterizePrimitiveFormatter.cs +33 -0
- data/support/cs/src/CauterizeTypeFormatterFactory.cs +39 -0
- data/support/cs/src/CauterizeTypes.cs +107 -0
- data/support/cs/src/CauterizeVariableArrayFormatter.cs +49 -0
- data/support/cs/src/ICauterizeTypeFormatter.cs +12 -0
- data/support/cs/src/OrderAttribute.cs +50 -0
- data/support/cs/src/PrimitiveSupport.cs +134 -0
- data/support/cs/src/SerializedRepresentationAttribute.cs +24 -0
- data/support/cs/test/CauterizeCompositeFormatterTest.cs +59 -0
- data/support/cs/test/CauterizeEnumFormatterTest.cs +110 -0
- data/support/cs/test/CauterizeFixedArrayFormatterTest.cs +91 -0
- data/support/cs/test/CauterizeFormatterTest.cs +40 -0
- data/support/cs/test/CauterizeGroupFormatterTest.cs +147 -0
- data/support/cs/test/CauterizeIntegrationTest.cs +129 -0
- data/support/cs/test/CauterizePrimitiveFormatterTest.cs +98 -0
- data/support/cs/test/CauterizeTypeFormatterFactoryTest.cs +73 -0
- data/support/cs/test/CauterizeVariableArrayFormatterTest.cs +130 -0
- data/support/cs/test/OrderAttributeTest.cs +39 -0
- data/support/cs/test/SerializedRepresentationAttributeTest.cs +39 -0
- metadata +68 -10
- data/c/src/cauterize.h +0 -46
- data/c/src/cauterize_util.h +0 -7
@@ -0,0 +1,18 @@
|
|
1
|
+
using System;
|
2
|
+
using System.IO;
|
3
|
+
|
4
|
+
namespace Cauterize
|
5
|
+
{
|
6
|
+
public abstract class CauterizeContainerFormatter : ICauterizeTypeFormatter
|
7
|
+
{
|
8
|
+
protected CauterizeTypeFormatterFactory _typeFormatterFactory;
|
9
|
+
|
10
|
+
public CauterizeContainerFormatter(CauterizeTypeFormatterFactory factory)
|
11
|
+
{
|
12
|
+
_typeFormatterFactory = factory;
|
13
|
+
}
|
14
|
+
|
15
|
+
public abstract object Deserialize(Stream serializationStream, Type t);
|
16
|
+
public abstract void Serialize(Stream serializationStream, object obj);
|
17
|
+
}
|
18
|
+
}
|
@@ -0,0 +1,67 @@
|
|
1
|
+
using System;
|
2
|
+
using System.Collections.Generic;
|
3
|
+
using System.IO;
|
4
|
+
using System.Text;
|
5
|
+
|
6
|
+
namespace Cauterize
|
7
|
+
{
|
8
|
+
public class CauterizeEnumFormatter : ICauterizeTypeFormatter
|
9
|
+
{
|
10
|
+
public object Deserialize(Stream serializationStream, Type t)
|
11
|
+
{
|
12
|
+
var numBytes = GetNumBytesForEnum(t);
|
13
|
+
var bytes = new byte[numBytes];
|
14
|
+
serializationStream.Read(bytes, 0, numBytes);
|
15
|
+
if (!BitConverter.IsLittleEndian)
|
16
|
+
{
|
17
|
+
Array.Reverse(bytes);
|
18
|
+
}
|
19
|
+
Int64 intValue = 0;
|
20
|
+
switch (numBytes)
|
21
|
+
{
|
22
|
+
case 8: intValue = BitConverter.ToInt64(bytes, 0);
|
23
|
+
break;
|
24
|
+
case 4: intValue = BitConverter.ToInt32(bytes, 0);
|
25
|
+
break;
|
26
|
+
case 2: intValue = BitConverter.ToInt16(bytes, 0);
|
27
|
+
break;
|
28
|
+
case 1: intValue = (Int32)bytes[0];
|
29
|
+
break;
|
30
|
+
}
|
31
|
+
return Enum.ToObject(t, intValue);
|
32
|
+
}
|
33
|
+
|
34
|
+
public void Serialize(Stream serializationStream, object obj)
|
35
|
+
{
|
36
|
+
var longValue = Convert.ToInt64(obj);
|
37
|
+
var bytes = BitConverter.GetBytes(longValue);
|
38
|
+
if (!BitConverter.IsLittleEndian)
|
39
|
+
{
|
40
|
+
Array.Reverse(bytes);
|
41
|
+
}
|
42
|
+
serializationStream.Write(bytes, 0, GetNumBytesForEnum(obj.GetType()));
|
43
|
+
}
|
44
|
+
|
45
|
+
private int GetNumBytesForEnum(Type t)
|
46
|
+
{
|
47
|
+
var type = SerializedRepresentationAttribute.GetRepresentation(t);
|
48
|
+
if (type == typeof(SByte) || type == typeof(Byte))
|
49
|
+
{
|
50
|
+
return 1;
|
51
|
+
}
|
52
|
+
else if (type == typeof(Int16) || type == typeof(UInt16))
|
53
|
+
{
|
54
|
+
return 2;
|
55
|
+
}
|
56
|
+
else if (type == typeof(Int32) || type == typeof(UInt32))
|
57
|
+
{
|
58
|
+
return 4;
|
59
|
+
}
|
60
|
+
else if (type == typeof(Int64) || type == typeof(UInt64))
|
61
|
+
{
|
62
|
+
return 8;
|
63
|
+
}
|
64
|
+
return 0;
|
65
|
+
}
|
66
|
+
}
|
67
|
+
}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
using System;
|
2
|
+
|
3
|
+
namespace Cauterize
|
4
|
+
{
|
5
|
+
public class CauterizeException : Exception
|
6
|
+
{
|
7
|
+
public CauterizeException(string message) : base(message)
|
8
|
+
{
|
9
|
+
}
|
10
|
+
|
11
|
+
public CauterizeException(string message, Exception innerException) : base(message, innerException)
|
12
|
+
{
|
13
|
+
}
|
14
|
+
}
|
15
|
+
}
|
@@ -0,0 +1,39 @@
|
|
1
|
+
using System;
|
2
|
+
using System.IO;
|
3
|
+
using System.Reflection;
|
4
|
+
|
5
|
+
namespace Cauterize
|
6
|
+
{
|
7
|
+
public class CauterizeFixedArrayFormatter : CauterizeContainerFormatter
|
8
|
+
{
|
9
|
+
public CauterizeFixedArrayFormatter(CauterizeTypeFormatterFactory factory) : base(factory)
|
10
|
+
{
|
11
|
+
}
|
12
|
+
public override object Deserialize(Stream serializationStream, Type t)
|
13
|
+
{
|
14
|
+
var ret = t.GetConstructor(new Type[] {}).Invoke(new object[] {});
|
15
|
+
var arrayField = t.BaseType.GetField("_data", BindingFlags.NonPublic | BindingFlags.Instance);
|
16
|
+
var array = (Array)arrayField.GetValue(ret);
|
17
|
+
var arrayType = arrayField.FieldType.GetElementType();
|
18
|
+
var subFormatter = _typeFormatterFactory.GetFormatter(arrayType);
|
19
|
+
for (var i = 0; i < array.Length; i++)
|
20
|
+
{
|
21
|
+
array.SetValue(subFormatter.Deserialize(serializationStream, arrayType), i);
|
22
|
+
}
|
23
|
+
return ret;
|
24
|
+
}
|
25
|
+
|
26
|
+
public override void Serialize(Stream serializationStream, object obj)
|
27
|
+
{
|
28
|
+
var t = obj.GetType();
|
29
|
+
var arrayField = t.BaseType.GetField("_data", BindingFlags.NonPublic | BindingFlags.Instance);
|
30
|
+
var array = (Array)arrayField.GetValue(obj);
|
31
|
+
var arrayType = arrayField.FieldType.GetElementType();
|
32
|
+
var subFormatter = _typeFormatterFactory.GetFormatter(arrayType);
|
33
|
+
for (var i = 0; i < array.Length; i++)
|
34
|
+
{
|
35
|
+
subFormatter.Serialize(serializationStream, array.GetValue(i));
|
36
|
+
}
|
37
|
+
}
|
38
|
+
}
|
39
|
+
}
|
@@ -0,0 +1,40 @@
|
|
1
|
+
using System;
|
2
|
+
using System.Collections.Generic;
|
3
|
+
using System.IO;
|
4
|
+
using System.Reflection;
|
5
|
+
using System.Runtime.Serialization;
|
6
|
+
using System.Text;
|
7
|
+
|
8
|
+
namespace Cauterize
|
9
|
+
{
|
10
|
+
public class CauterizeFormatter
|
11
|
+
{
|
12
|
+
public SerializationBinder Binder { get; set; }
|
13
|
+
public StreamingContext Context { get; set; }
|
14
|
+
public ISurrogateSelector SurrogateSelector { get; set; }
|
15
|
+
|
16
|
+
private readonly CauterizeTypeFormatterFactory _formatterFactory;
|
17
|
+
public CauterizeFormatter() // only needed for deserialization
|
18
|
+
{
|
19
|
+
_formatterFactory = new CauterizeTypeFormatterFactory();
|
20
|
+
}
|
21
|
+
|
22
|
+
public CauterizeFormatter(CauterizeTypeFormatterFactory factory)
|
23
|
+
{
|
24
|
+
_formatterFactory = factory;
|
25
|
+
}
|
26
|
+
|
27
|
+
public virtual T Deserialize<T>(Stream serializationStream)
|
28
|
+
{
|
29
|
+
var formatter = _formatterFactory.GetFormatter(typeof(T));
|
30
|
+
return (T)formatter.Deserialize(serializationStream, typeof(T));
|
31
|
+
}
|
32
|
+
|
33
|
+
public virtual void Serialize(Stream serializationStream, object obj)
|
34
|
+
{
|
35
|
+
var formatter = _formatterFactory.GetFormatter(obj.GetType());
|
36
|
+
formatter.Serialize(serializationStream, obj);
|
37
|
+
}
|
38
|
+
}
|
39
|
+
}
|
40
|
+
|
@@ -0,0 +1,46 @@
|
|
1
|
+
using System;
|
2
|
+
using System.IO;
|
3
|
+
|
4
|
+
namespace Cauterize
|
5
|
+
{
|
6
|
+
public class CauterizeGroupFormatter : CauterizeContainerFormatter
|
7
|
+
{
|
8
|
+
public CauterizeGroupFormatter(CauterizeTypeFormatterFactory factory) : base(factory)
|
9
|
+
{
|
10
|
+
}
|
11
|
+
public override object Deserialize(Stream serializationStream, Type t)
|
12
|
+
{
|
13
|
+
var ret = t.GetConstructor(new Type[] {}).Invoke(new object[] {});
|
14
|
+
var typeProp = OrderAttribute.GetPropertyByOrder(t, 0);
|
15
|
+
var enumFormatter = _typeFormatterFactory.GetFormatter(typeProp.PropertyType);
|
16
|
+
var enumValue = enumFormatter.Deserialize(serializationStream, typeProp.PropertyType);
|
17
|
+
typeProp.SetValue(ret, enumValue, null);
|
18
|
+
var index = ((int) enumValue) + 1;
|
19
|
+
var prop = OrderAttribute.GetPropertyByOrder(t, index);
|
20
|
+
if (prop != null)
|
21
|
+
{
|
22
|
+
var subType = prop.PropertyType;
|
23
|
+
var subFormatter = _typeFormatterFactory.GetFormatter(subType);
|
24
|
+
var subObj = subFormatter.Deserialize(serializationStream, subType);
|
25
|
+
prop.SetValue(ret, subObj, null);
|
26
|
+
}
|
27
|
+
return ret;
|
28
|
+
}
|
29
|
+
|
30
|
+
public override void Serialize(Stream serializationStream, object obj)
|
31
|
+
{
|
32
|
+
var typeProp = OrderAttribute.GetPropertyByOrder(obj.GetType(), 0);
|
33
|
+
var enumFormatter = _typeFormatterFactory.GetFormatter(typeProp.PropertyType);
|
34
|
+
var enumValue = typeProp.GetValue(obj, null);
|
35
|
+
enumFormatter.Serialize(serializationStream, enumValue);
|
36
|
+
var index = ((int) enumValue) + 1;
|
37
|
+
var subProp = OrderAttribute.GetPropertyByOrder(obj.GetType(), index);
|
38
|
+
if (subProp != null)
|
39
|
+
{
|
40
|
+
var subType = subProp.PropertyType;
|
41
|
+
var subFormatter = _typeFormatterFactory.GetFormatter(subType);
|
42
|
+
subFormatter.Serialize(serializationStream, subProp.GetValue(obj, null));
|
43
|
+
}
|
44
|
+
}
|
45
|
+
}
|
46
|
+
}
|
@@ -0,0 +1,33 @@
|
|
1
|
+
using System;
|
2
|
+
using System.ComponentModel;
|
3
|
+
using System.IO;
|
4
|
+
using System.Runtime.Serialization;
|
5
|
+
using System.Text;
|
6
|
+
|
7
|
+
namespace Cauterize
|
8
|
+
{
|
9
|
+
public class CauterizePrimitiveFormatter : ICauterizeTypeFormatter
|
10
|
+
{
|
11
|
+
public object Deserialize(Stream serializationStream, Type t)
|
12
|
+
{
|
13
|
+
var numBytes = PrimitiveSupport.TypeToByteSize(t);
|
14
|
+
var bytes = new byte[numBytes];
|
15
|
+
serializationStream.Read(bytes, 0, numBytes);
|
16
|
+
if (!BitConverter.IsLittleEndian)
|
17
|
+
{
|
18
|
+
Array.Reverse(bytes);
|
19
|
+
}
|
20
|
+
return PrimitiveSupport.TypeFromBytes(t, bytes);
|
21
|
+
}
|
22
|
+
|
23
|
+
public void Serialize(Stream serializationStream, object obj)
|
24
|
+
{
|
25
|
+
var bytes = PrimitiveSupport.BytesFromValue(obj);
|
26
|
+
if (!BitConverter.IsLittleEndian)
|
27
|
+
{
|
28
|
+
Array.Reverse(bytes);
|
29
|
+
}
|
30
|
+
serializationStream.Write(bytes, 0, bytes.Length);
|
31
|
+
}
|
32
|
+
}
|
33
|
+
}
|
@@ -0,0 +1,39 @@
|
|
1
|
+
using System;
|
2
|
+
using System.Collections.Generic;
|
3
|
+
using System.Text;
|
4
|
+
|
5
|
+
namespace Cauterize
|
6
|
+
{
|
7
|
+
public class CauterizeTypeFormatterFactory
|
8
|
+
{
|
9
|
+
public virtual ICauterizeTypeFormatter GetFormatter(Type t)
|
10
|
+
{
|
11
|
+
ICauterizeTypeFormatter formatter;
|
12
|
+
if (t.IsSubclassOf(typeof (CauterizeComposite)))
|
13
|
+
{
|
14
|
+
formatter = new CauterizeCompositeFormatter(this);
|
15
|
+
}
|
16
|
+
else if (t.IsSubclassOf(typeof (CauterizeGroup)))
|
17
|
+
{
|
18
|
+
formatter = new CauterizeGroupFormatter(this);
|
19
|
+
}
|
20
|
+
else if (t.IsSubclassOf(typeof (CauterizeFixedArray)))
|
21
|
+
{
|
22
|
+
formatter = new CauterizeFixedArrayFormatter(this);
|
23
|
+
}
|
24
|
+
else if (t.IsSubclassOf(typeof (CauterizeVariableArray)))
|
25
|
+
{
|
26
|
+
formatter = new CauterizeVariableArrayFormatter(this);
|
27
|
+
}
|
28
|
+
else if (t.IsSubclassOf(typeof (Enum)))
|
29
|
+
{
|
30
|
+
formatter = new CauterizeEnumFormatter();
|
31
|
+
}
|
32
|
+
else
|
33
|
+
{
|
34
|
+
formatter = new CauterizePrimitiveFormatter();
|
35
|
+
}
|
36
|
+
return formatter;
|
37
|
+
}
|
38
|
+
}
|
39
|
+
}
|
@@ -0,0 +1,107 @@
|
|
1
|
+
using System;
|
2
|
+
using System.Collections.Generic;
|
3
|
+
using System.Text;
|
4
|
+
|
5
|
+
namespace Cauterize
|
6
|
+
{
|
7
|
+
public class CauterizeInfo {
|
8
|
+
public static string Name;
|
9
|
+
public static string GeneratedVersion;
|
10
|
+
public static string GeneratedDate;
|
11
|
+
}
|
12
|
+
|
13
|
+
public class CauterizeComposite
|
14
|
+
{
|
15
|
+
}
|
16
|
+
|
17
|
+
public class CauterizeGroup
|
18
|
+
{
|
19
|
+
}
|
20
|
+
|
21
|
+
public abstract class CauterizeFixedArray
|
22
|
+
{
|
23
|
+
}
|
24
|
+
|
25
|
+
public abstract class CauterizeFixedArrayTyped<T> : CauterizeFixedArray
|
26
|
+
{
|
27
|
+
private T[] _data;
|
28
|
+
|
29
|
+
protected abstract int Size { get; }
|
30
|
+
|
31
|
+
protected void Allocate(T[] data)
|
32
|
+
{
|
33
|
+
Allocate(data.Length);
|
34
|
+
Array.Copy(data, _data, data.Length);
|
35
|
+
}
|
36
|
+
|
37
|
+
protected void Allocate(int size)
|
38
|
+
{
|
39
|
+
if (size != Size)
|
40
|
+
{
|
41
|
+
throw new CauterizeException("arrays for " + GetType() + " must be size " + Size);
|
42
|
+
}
|
43
|
+
_data = new T[size];
|
44
|
+
}
|
45
|
+
|
46
|
+
public static implicit operator T[](CauterizeFixedArrayTyped<T> array)
|
47
|
+
{
|
48
|
+
return array._data;
|
49
|
+
}
|
50
|
+
|
51
|
+
// implicit conversion won't cover slicing
|
52
|
+
public T this[int i]
|
53
|
+
{
|
54
|
+
get { return _data[i]; }
|
55
|
+
set { _data[i] = value; }
|
56
|
+
}
|
57
|
+
|
58
|
+
public T[] GetArray()
|
59
|
+
{
|
60
|
+
return _data;
|
61
|
+
}
|
62
|
+
}
|
63
|
+
|
64
|
+
public abstract class CauterizeVariableArray
|
65
|
+
{
|
66
|
+
}
|
67
|
+
|
68
|
+
public abstract class CauterizeVariableArrayTyped<T> : CauterizeVariableArray
|
69
|
+
{
|
70
|
+
private T[] _data;
|
71
|
+
|
72
|
+
protected abstract int MaxSize { get; }
|
73
|
+
|
74
|
+
protected void Allocate(T[] data)
|
75
|
+
{
|
76
|
+
Allocate(data.Length);
|
77
|
+
Array.Copy(data, _data, data.Length);
|
78
|
+
}
|
79
|
+
|
80
|
+
protected void Allocate(int size)
|
81
|
+
{
|
82
|
+
if (size > MaxSize)
|
83
|
+
{
|
84
|
+
throw new CauterizeException("arrays for " + GetType() + " must be smaller than " + MaxSize);
|
85
|
+
}
|
86
|
+
_data = new T[size];
|
87
|
+
}
|
88
|
+
|
89
|
+
public static implicit operator T[](CauterizeVariableArrayTyped<T> array)
|
90
|
+
{
|
91
|
+
return array._data;
|
92
|
+
}
|
93
|
+
|
94
|
+
// implicit conversion won't cover slicing
|
95
|
+
public T this[int i]
|
96
|
+
{
|
97
|
+
get { return _data[i]; }
|
98
|
+
set { _data[i] = value; }
|
99
|
+
}
|
100
|
+
|
101
|
+
public T[] GetArray()
|
102
|
+
{
|
103
|
+
return _data;
|
104
|
+
}
|
105
|
+
}
|
106
|
+
}
|
107
|
+
|
@@ -0,0 +1,49 @@
|
|
1
|
+
using System;
|
2
|
+
using System.Collections.Generic;
|
3
|
+
using System.IO;
|
4
|
+
using System.Reflection;
|
5
|
+
|
6
|
+
namespace Cauterize
|
7
|
+
{
|
8
|
+
public class CauterizeVariableArrayFormatter : CauterizeContainerFormatter
|
9
|
+
{
|
10
|
+
public CauterizeVariableArrayFormatter(CauterizeTypeFormatterFactory factory) : base(factory)
|
11
|
+
{
|
12
|
+
}
|
13
|
+
|
14
|
+
|
15
|
+
public override object Deserialize(Stream serializationStream, Type t)
|
16
|
+
{
|
17
|
+
var sizeType = (Type) t.GetField("SizeType").GetValue(null);
|
18
|
+
var sizeFormatter = _typeFormatterFactory.GetFormatter(sizeType);
|
19
|
+
var rawSize = sizeFormatter.Deserialize(serializationStream, sizeType);
|
20
|
+
var arraySize = PrimitiveSupport.TypeToInt(rawSize);
|
21
|
+
var ret = t.GetConstructor(new Type[] {typeof (int)}).Invoke(new object[] {arraySize});
|
22
|
+
var arrayField = t.BaseType.GetField("_data", BindingFlags.NonPublic | BindingFlags.Instance);
|
23
|
+
var array = (Array)arrayField.GetValue(ret);
|
24
|
+
var arrayType = arrayField.FieldType.GetElementType();
|
25
|
+
var subFormatter = _typeFormatterFactory.GetFormatter(arrayType);
|
26
|
+
for (var i = 0; i < arraySize; i++)
|
27
|
+
{
|
28
|
+
array.SetValue(subFormatter.Deserialize(serializationStream, arrayType), i);
|
29
|
+
}
|
30
|
+
return ret;
|
31
|
+
}
|
32
|
+
|
33
|
+
public override void Serialize(Stream serializationStream, object obj)
|
34
|
+
{
|
35
|
+
var t = obj.GetType();
|
36
|
+
var arrayField = t.BaseType.GetField("_data", BindingFlags.NonPublic | BindingFlags.Instance);
|
37
|
+
var array = (Array)arrayField.GetValue(obj);
|
38
|
+
var sizeType = (Type) t.GetField("SizeType").GetValue(null);
|
39
|
+
var sizeFormatter = _typeFormatterFactory.GetFormatter(sizeType);
|
40
|
+
sizeFormatter.Serialize(serializationStream, PrimitiveSupport.IntToType(sizeType, array.Length));
|
41
|
+
var arrayType = arrayField.FieldType.GetElementType();
|
42
|
+
var subFormatter = _typeFormatterFactory.GetFormatter(arrayType);
|
43
|
+
for (var i = 0; i < array.Length; i++)
|
44
|
+
{
|
45
|
+
subFormatter.Serialize(serializationStream, array.GetValue(i));
|
46
|
+
}
|
47
|
+
}
|
48
|
+
}
|
49
|
+
}
|