Ejemplo n.º 1
0
        private static Type GetSchemaType(CapnProto.Pointer parent, System.Type type, out int len)
        {
            if (type == null)
            {
                len = 0;
                return(default(Type));
            }

            // objects and enums
            var idAttrib = (IdAttribute)Attribute.GetCustomAttribute(type, typeof(IdAttribute));

            if (idAttrib != null)
            {
                if (type.IsEnum)
                {
                    len = 16;
                    Schema.Type tmp   = Schema.Type.Create(parent, Type.Unions.@enum);
                    var         @enum = tmp.@enum;
                    @enum.typeId = idAttrib.Id;
                    return(tmp);
                }
                if (type.IsClass || type.IsValueType)
                {
                    len = Type.LEN_POINTER;
                    Schema.Type tmp     = Schema.Type.Create(parent, Type.Unions.@struct);
                    var         @struct = tmp.@struct;
                    @struct.typeId = idAttrib.Id;
                }
            }

            if (type == typeof(byte[]))
            {
                len = 0;
                return(Schema.Type.Create(parent, Type.Unions.data));
            }
            else if (type == typeof(void))
            {
                len = 0;
                return(Schema.Type.Create(parent, Type.Unions.@void));
            }
            else if (type == typeof(object) || type == typeof(System.Collections.IList))
            {
                len = Type.LEN_POINTER;
                return(Schema.Type.Create(parent, Type.Unions.anyPointer));
            }
            switch (System.Type.GetTypeCode(type))
            {
            case TypeCode.Empty: len = 0; return(default(Type));

            case TypeCode.Boolean: len = 1; return(Schema.Type.Create(parent, Type.Unions.@bool));

            case TypeCode.SByte: len = 8; return(Schema.Type.Create(parent, Type.Unions.int8));

            case TypeCode.Byte: len = 8; return(Schema.Type.Create(parent, Type.Unions.uint8));

            case TypeCode.Int16: len = 16; return(Schema.Type.Create(parent, Type.Unions.int16));

            case TypeCode.UInt16: len = 16; return(Schema.Type.Create(parent, Type.Unions.uint16));

            case TypeCode.Int32: len = 32; return(Schema.Type.Create(parent, Type.Unions.int32));

            case TypeCode.UInt32: len = 32; return(Schema.Type.Create(parent, Type.Unions.uint32));

            case TypeCode.Int64: len = 64; return(Schema.Type.Create(parent, Type.Unions.int64));

            case TypeCode.UInt64: len = 64; return(Schema.Type.Create(parent, Type.Unions.uint64));

            case TypeCode.Char: len = 16; return(Schema.Type.Create(parent, Type.Unions.uint16));

            case TypeCode.DBNull: len = 0; return(Schema.Type.Create(parent, Type.Unions.@void));

            case TypeCode.Single: len = 32; return(Schema.Type.Create(parent, Type.Unions.float32));

            case TypeCode.Double: len = 64; return(Schema.Type.Create(parent, Type.Unions.float64));

            case TypeCode.String: len = Type.LEN_POINTER; return(Schema.Type.Create(parent, Type.Unions.text));
            }

            // lists (note this includes recursive)
            var elType = GetSchemaType(parent, GetElementType(type), out len);

            if (elType.IsValid())
            {
                len = Type.LEN_POINTER;
                Schema.Type tmp  = Schema.Type.Create(parent, Type.Unions.list);
                var         list = tmp.list;
                list.elementType = elType;
                return(tmp);
            }
            len = 0;
            return(default(Type));
        }
Ejemplo n.º 2
0
        static Field CreateField(CapnProto.Pointer parent, MemberInfo member, ref ushort discCount, ref uint discOffset)
        {
            if (member == null)
            {
                return(default(Field));
            }
            System.Type type;
            switch (member.MemberType)
            {
            case MemberTypes.Field:
                type = ((FieldInfo)member).FieldType; break;

            case MemberTypes.Property:
                type = ((PropertyInfo)member).PropertyType; break;

            default:
                return(default(Field));
            }
            var fa = (FieldAttribute)Attribute.GetCustomAttribute(member, typeof(FieldAttribute));
            var ua = (UnionAttribute)Attribute.GetCustomAttribute(member, typeof(UnionAttribute));

            if (fa == null && ua == null)
            {
                return(default(Field));
            }

            Field field = new Field();
            var   slot  = field.slot;
            int   len;

            slot.type = GetSchemaType(parent, type, out len);
            if (fa != null)
            {
                if (fa.Start >= 0)
                {
                    slot.offset = len == 0 ? 0 : checked ((uint)(fa.Start / len));
                }
                else if (fa.Pointer >= 0)
                {
                    slot.offset = checked ((uint)fa.Pointer);
                }
                var ordinal = field.ordinal;
                if (fa.Number >= 0)
                {
                    ordinal.Union     = Field.ordinalGroup.Unions.@explicit;
                    ordinal.@explicit = checked ((ushort)fa.Number);
                }
                else
                {
                    ordinal.Union = Field.ordinalGroup.Unions.@implicit;
                }
            }

            if (ua != null)
            {
                discCount++;
                if (ua.Tag == 0)
                {
                    discOffset = checked ((ushort)ua.Start);
                }
                field.discriminantValue = checked ((ushort)ua.Tag);
            }
            else
            {
                field.discriminantValue = Field.noDiscriminant;
            }
            field.name = Text.Create(parent, member.Name);

            return(field);
        }