Skip to content

Commit

Permalink
Make boolean packing optional through PackAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Davipb committed Mar 18, 2017
1 parent 5d6fa44 commit f2af2ac
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ public class ConstantSizePackedBooleanClass
[Ignore] public const int LengthConstraint = 2;

[FieldCount(CountConstraint)]
[FieldOrder(0)]
[FieldOrder(0), Pack]
public bool[] ConstantCountArray { get; set; }

[FieldLength(2)]
[FieldOrder(1)]
[FieldLength(LengthConstraint)]
[FieldOrder(1), Pack]
public bool[] ConstantLengthArray { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ namespace BinarySerialization.Test.PackedBoolean
public class EndianAwarePackedBooleanClass
{
[FieldEndianness(BinarySerialization.Endianness.Little)]
[FieldOrder(0)]
[FieldOrder(0), Pack]
public bool[] LittleEndianArray { get; set; }

[FieldEndianness(BinarySerialization.Endianness.Big)]
[FieldOrder(1)]
[FieldOrder(1), Pack]
public bool[] BigEndianArray { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class ValidPackedBooleanClass
[FieldOrder(1)] public long BooleanArrayLength { get; set; }

[FieldCount(nameof(BooleanArrayCount)), FieldLength(nameof(BooleanArrayLength))]
[FieldOrder(2)]
[FieldOrder(2), Pack]
public bool[] BooleanArray { get; set; }
}
}
17 changes: 13 additions & 4 deletions BinarySerializer/Graph/TypeGraph/ContainerTypeNode.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

namespace BinarySerialization.Graph.TypeGraph
Expand Down Expand Up @@ -45,7 +47,7 @@ protected TypeNode GenerateChild(Type parentType, MemberInfo memberInfo)
{
ThrowOnBadType(memberType);

var nodeType = GetNodeType(memberType);
var nodeType = GetNodeType(memberType, memberInfo.GetCustomAttributes());

return (TypeNode)Activator.CreateInstance(nodeType, this, parentType, memberInfo);
}
Expand All @@ -64,7 +66,8 @@ private static void ThrowOnBadType(Type type)
}
// ReSharper restore UnusedParameter.Local

private static Type GetNodeType(Type type)
private static Type GetNodeType(Type type) => GetNodeType(type, Enumerable.Empty<Attribute>());
private static Type GetNodeType(Type type, IEnumerable<Attribute> attributes)
{
var nullableType = Nullable.GetUnderlyingType(type);

Expand All @@ -76,8 +79,14 @@ private static Type GetNodeType(Type type)
if (IsValueType(effectiveType))
return typeof (ValueTypeNode);

if (type == typeof(bool[]))
return typeof(PackedBooleanArrayTypeNode);
if (attributes.OfType<PackAttribute>().Any())
{
if (type == typeof(bool[]))
return typeof(PackedBooleanArrayTypeNode);

throw new InvalidOperationException($"Cannot use the Pack attribute on member of type {type.Name}.");
}

if (type.IsArray)
return typeof(ArrayTypeNode);
if (typeof(IList).IsAssignableFrom(type))
Expand Down
14 changes: 14 additions & 0 deletions BinarySerializer/PackAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace BinarySerialization
{
/// <summary> Tells the <see cref="BinarySerializer"/> to pack the decorated member. </summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
public sealed class PackAttribute : Attribute
{
/// <summary> Initializes a new instance of the <see cref="PackAttribute"/> class. </summary>
public PackAttribute() { }
}
}

0 comments on commit f2af2ac

Please sign in to comment.