示例#1
0
            private void SetFields(object fields)
            {
                lock (this) {
                    IList <object> list       = GetFieldsList(fields);
                    IList <object> anonFields = StructType.GetAnonymousFields(this);

                    int          size = 0, alignment = 1;
                    List <Field> allFields = new List <Field>();//GetBaseSizeAlignmentAndFields(out size, out alignment);
                    int?         bitCount;
                    for (int fieldIndex = 0; fieldIndex < list.Count; fieldIndex++)
                    {
                        object      o = list[fieldIndex];
                        string      fieldName;
                        INativeType cdata;
                        GetFieldInfo(this, o, out fieldName, out cdata, out bitCount);
                        alignment = Math.Max(alignment, cdata.Alignment);
                        size      = Math.Max(size, cdata.Size);

                        Field newField = new Field(fieldName, cdata, 0, allFields.Count);
                        allFields.Add(newField);
                        AddSlot(fieldName, newField);

                        if (anonFields != null && anonFields.Contains(fieldName))
                        {
                            StructType.AddAnonymousFields(this, allFields, cdata, newField);
                        }
                    }

                    StructType.CheckAnonymousFields(allFields, anonFields);

                    _fields    = allFields.ToArray();
                    _size      = PythonStruct.Align(size, alignment);
                    _alignment = alignment;
                }
            }
示例#2
0
            private int UpdateSizeAndAlignment(INativeType cdata, int?bitCount, INativeType lastType, ref int size, ref int alignment, ref int?totalBitCount)
            {
                int prevSize = size;

                if (bitCount != null)
                {
                    if (lastType != null && lastType.Size != cdata.Size)
                    {
                        totalBitCount = null;
                        prevSize      = size += lastType.Size;
                    }

                    size = PythonStruct.Align(size, cdata.Alignment);

                    if (totalBitCount != null)
                    {
                        if ((bitCount + totalBitCount + 7) / 8 <= cdata.Size)
                        {
                            totalBitCount = bitCount + totalBitCount;
                        }
                        else
                        {
                            size         += lastType.Size;
                            prevSize      = size;
                            totalBitCount = bitCount;
                        }
                    }
                    else
                    {
                        totalBitCount = bitCount;
                    }
                }
                else
                {
                    if (totalBitCount != null)
                    {
                        size         += lastType.Size;
                        prevSize      = size;
                        totalBitCount = null;
                    }

                    if (_pack != null)
                    {
                        alignment = _pack.Value;
                        prevSize  = size = PythonStruct.Align(size, _pack.Value);

                        size += cdata.Size;
                    }
                    else
                    {
                        alignment = Math.Max(alignment, cdata.Alignment);
                        prevSize  = size = PythonStruct.Align(size, cdata.Alignment);
                        size     += cdata.Size;
                    }
                }

                return(prevSize);
            }
示例#3
0
            private void SetFields(object fields)
            {
                lock (this) {
                    IList <object> list = GetFieldsList(fields);

                    int          size;
                    int          alignment;
                    int?         bitCount    = null;
                    int?         curBitCount = null;
                    INativeType  lastType    = null;
                    List <Field> allFields   = GetBaseSizeAlignmentAndFields(out size, out alignment);

                    IList <object> anonFields = GetAnonymousFields(this);

                    for (int fieldIndex = 0; fieldIndex < list.Count; fieldIndex++)
                    {
                        object      o = list[fieldIndex];
                        string      fieldName;
                        INativeType cdata;
                        GetFieldInfo(this, o, out fieldName, out cdata, out bitCount);

                        int prevSize = UpdateSizeAndAlignment(cdata, bitCount, lastType, ref size, ref alignment, ref curBitCount);

                        Field newField = new Field(fieldName, cdata, prevSize, allFields.Count, bitCount, curBitCount - bitCount);
                        allFields.Add(newField);
                        AddSlot(fieldName, newField);

                        if (anonFields != null && anonFields.Contains(fieldName))
                        {
                            AddAnonymousFields(this, allFields, cdata, newField);
                        }

                        lastType = cdata;
                    }

                    CheckAnonymousFields(allFields, anonFields);

                    if (bitCount != null)
                    {
                        size += lastType.Size;
                    }

                    _fields    = allFields.ToArray();
                    _size      = PythonStruct.Align(size, alignment);
                    _alignment = alignment;
                }
            }