Ejemplo n.º 1
0
        void ReadAlignment(BitStreamReader reader, VoidDsdlType t)
        {
            var amount = t.MaxBitlen;

            while (amount > 8)
            {
                reader.ReadByte(8);
                amount -= 8;
            }

            if (amount > 0)
            {
                reader.ReadByte(amount);
            }
        }
Ejemplo n.º 2
0
        void WriteAlignment(BitStreamWriter writer, VoidDsdlType t)
        {
            var amount = t.MaxBitlen;

            while (amount > 8)
            {
                writer.Write(0, 8);
                amount -= 8;
            }

            if (amount > 0)
            {
                writer.Write(0, amount);
            }
        }
Ejemplo n.º 3
0
        void SerializeObjectCore(
            BitStreamWriter writer,
            Func <string, ResolvedProperty?> propertyResolver,
            ContainerContract containerContract,
            DsdlType derivedDsdlType,
            bool tailArrayOptimization)
        {
            var dsdlScheme = GetScheme <CompositeDsdlTypeBase>(containerContract, derivedDsdlType);

            //WriteObjectStart(writer, value, contract, member, collectionContract, containerProperty);

            VoidDsdlType voidDsdlType      = null;
            int          voidDsdlTypeIndex = -1;
            var          isUnion           = dsdlScheme.IsUnion;
            var          unionMemberFound  = false;

            for (int i = 0; i < dsdlScheme.Fields.Count; i++)
            {
                var dsdlMember   = dsdlScheme.Fields[i];
                var isLastMember = i == dsdlScheme.Fields.Count - 1;

                if ((voidDsdlType = (dsdlMember.Type as VoidDsdlType)) != null)
                {
                    voidDsdlTypeIndex = i;
                    if (!isUnion)
                    {
                        WriteAlignment(writer, voidDsdlType);
                    }
                    continue;
                }

                var resolvedProp = propertyResolver(dsdlMember.Name);

                if (isUnion)
                {
                    if (resolvedProp == null)
                    {
                        continue;
                    }
                    if (unionMemberFound)
                    {
                        throw new InvalidOperationException($"Cannot find single union value for type '{containerContract.UnderlyingType.FullName}'.");
                    }
                    unionMemberFound = true;
                    WriteUnionFieldIndex(writer, i, dsdlScheme);
                    var rp = resolvedProp.Value;
                    SerializeValue(writer, rp.MemberValue, rp.MemberContact, dsdlMember.Type);
                }
                else
                {
                    if (resolvedProp == null)
                    {
                        throw new InvalidOperationException($"Cannot resove member '{containerContract.UnderlyingType.FullName}.{dsdlMember.Name}'.");
                    }
                    var rp  = resolvedProp.Value;
                    var tao = tailArrayOptimization &&
                              isLastMember &&
                              dsdlMember.Type is ArrayDsdlType adt;
                    SerializeValue(writer, rp.MemberValue, rp.MemberContact, dsdlMember.Type, tao);
                }
            }

            if (isUnion && !unionMemberFound)
            {
                if (voidDsdlType != null)
                {
                    WriteUnionFieldIndex(writer, voidDsdlTypeIndex, dsdlScheme);
                    WriteAlignment(writer, voidDsdlType);
                }
                else
                {
                    throw new InvalidOperationException($"Cannot find union value for '{containerContract.UnderlyingType.FullName}' type.");
                }
            }
        }