Пример #1
0
        public override CodeWriter WriteField(Schema.Field field)
        {
            var slot = field.slot;

            WriteLine().Write("[global::CapnProto.Field(").Write(slot.offset).Write(")]");

            WriteLine().Write("public ").Write(slot.type).Write(" ").Write(Escape(field.name)).Write(" {get; set; }");

            return(this);
        }
Пример #2
0
        void WriteListImpl(Schema.Field field)
        {
            var elType = field.slot.type.list.elementType;
            int index  = (int)field.slot.offset;

            switch (elType.Union)
            {
            case Schema.Type.Unions.text:
                Write("ctx.Reader.ReadStringList(segment, origin + ").Write(index + 1).Write(", raw[").Write(index).Write("]);");
                break;

            case Schema.Type.Unions.@struct:
                var found = Lookup([email protected]);
                if (!found.IsValid())
                {
                    Write("null; #error type not found: ").Write([email protected]);
                }
                else if (found.Union != Schema.Node.Unions.@struct || found.IsGroup())
                {
                    Write("null; #error invalid type for list: ").Write(found.displayName);
                }
                else
                {
                    Write("ctx.Reader.ReadStructList<").Write(FullyQualifiedName(found)).Write(">(ctx, segment, origin + ").Write(index + 1).Write(", raw[").Write(index).Write("]);");
                }
                break;

            case Schema.Type.Unions.int64:
                Write("ctx.Reader.ReadInt64List(segment, origin, raw[").Write(index).Write("]);");
                break;

            default:
                Write("null;").WriteLine().Write("#warning not yet supported: " + elType.Union);
                break;
            }
        }
Пример #3
0
        public override CodeWriter WriteFieldAccessor(Schema.Node parent, Schema.Field field, Stack <UnionStub> union)
        {
            if (field.Union == Field.Unions.group)
            {
                var found = Lookup(field.group.typeId);
                if (!found.IsValid())
                {
                    return(WriteLine().Write("#warning no type for: " + field.name));
                }

                return(WriteGroupAccessor(parent, found, field.name.ToString(), union.Count != 0));
            }

            if (!field.slot.type.IsValid())
            {
                return(WriteLine().Write("#warning no type for: " + field.name));
            }
            var ordinal = field.ordinal;
            var slot    = field.slot;
            var type    = slot.type;
            var len     = type.GetFieldLength();

            if (len == 0)
            {
                return(this);
            }

            if (ordinal.Union == Schema.Field.ordinalGroup.Unions.@explicit)
            {
                WriteLine().Write("[").Write(typeof(FieldAttribute)).Write("(").Write(ordinal.@explicit);
                var offset = slot.offset;
                if (len == Schema.Type.LEN_POINTER)
                {
                    Write(", pointer: ").Write(offset);
                }
                else
                {
                    int o = (int)offset * len;
                    Write(", ").Write(o).Write(", ").Write(o + len);
                }
                Write(")]");
            }

            //bool extraNullable = union.Count != 0 && type.Union != Schema.Type.Unions.@struct;
            var grp = (len == Schema.Type.LEN_POINTER && type.Union == Schema.Type.Unions.@struct) ? Lookup([email protected]) : default(Node);

            if (grp.IsValid() && grp.IsGroup())
            {
                return(WriteGroupAccessor(parent, grp, field.name.ToString(), false));
            }
            if (slot.hadExplicitDefault)
            {
                WriteLine().Write("[").Write(typeof(System.ComponentModel.DefaultValueAttribute)).Write("(").Write(type, slot.defaultValue).Write(")]");
            }

            BeginProperty(type, field.name.ToString(), false);
            WriteLine().Write("get");
            Indent();

            if (len == Schema.Type.LEN_POINTER)
            {
                // note: groups already handled
                WriteLine().Write("return (").Write(Format(slot.type)).Write(")this.").Write(PointerName).Write(".GetPointer(");
                WriteFieldOffset(slot.offset, union);
                Write(");");
            }
            else
            {
                switch (type.Union)
                {
                case Schema.Type.Unions.@bool:
                    WriteLine().Write("return ");
                    if (slot.hadExplicitDefault && slot.defaultValue.Union == Schema.Value.Unions.@bool &&
                        slot.defaultValue.@bool)
                    {
                        Write("!");
                    }
                    Write("this.").Write(PointerName).Write(".GetBoolean(");
                    WriteFieldOffset(slot.offset, union).Write(");");
                    break;

                case Schema.Type.Unions.int8:
                case Schema.Type.Unions.uint8:
                case Schema.Type.Unions.int16:
                case Schema.Type.Unions.uint16:
                case Schema.Type.Unions.int32:
                case Schema.Type.Unions.uint32:
                case Schema.Type.Unions.int64:
                case Schema.Type.Unions.uint64:
                case Schema.Type.Unions.float32:
                case Schema.Type.Unions.float64:
                    if (slot.hadExplicitDefault)
                    {
                        WriteLine().Write("return (").Write(Format(slot.type)).Write(")(this.").Write(PointerName).Write(".");
                    }
                    else
                    {
                        WriteLine().Write("return this.").Write(PointerName).Write(".");
                    }
                    switch (type.Union)
                    {
                    case Schema.Type.Unions.int8: Write("GetSByte"); break;

                    case Schema.Type.Unions.uint8: Write("GetByte"); break;

                    case Schema.Type.Unions.int16: Write("GetInt16"); break;

                    case Schema.Type.Unions.uint16: Write("GetUInt16"); break;

                    case Schema.Type.Unions.int32: Write("GetInt32"); break;

                    case Schema.Type.Unions.uint32: Write("GetUInt32"); break;

                    case Schema.Type.Unions.int64: Write("GetInt64"); break;

                    case Schema.Type.Unions.uint64: Write("GetUInt64"); break;

                    case Schema.Type.Unions.float32: Write("GetSingle"); break;

                    case Schema.Type.Unions.float64: Write("GetDouble"); break;
                    }
                    Write("(");
                    WriteFieldOffset(slot.offset, union).Write(")");
                    if (slot.hadExplicitDefault)
                    {
                        WriteXorDefaultValue(field.slot.defaultValue).Write(")");
                    }
                    Write(";");
                    break;

                case Schema.Type.Unions.@enum:

                    var e = Lookup([email protected]);
                    if (!((Pointer)e).IsValid || e.Union != Schema.Node.Unions.@enum || !((Pointer)[email protected]).IsValid)
                    {
                        WriteLine().Write("#error enum not found: ").Write([email protected]);
                    }
                    else
                    {
                        // all enums are Int16; so 4
                        WriteLine().Write("return (").Write(FullyQualifiedName(e)).Write(")this.")
                        .Write(PointerName).Write(".GetUInt16(");
                        WriteFieldOffset(slot.offset, union).Write(");");
                    }
                    break;

                default:
                    WriteLine().Write("throw new global::System.NotImplementedException(); // ").Write(type.Union);
                    break;
                }
            }
            Outdent();


            WriteLine().Write("set");
            Indent();
            if (len == Schema.Type.LEN_POINTER)
            {
                WriteLine().Write("this.").Write(PointerName).Write(".SetPointer(");
                WriteFieldOffset(slot.offset, union).Write(", value);");
            }
            else
            {
                switch (type.Union)
                {
                case Schema.Type.Unions.@bool:
                    WriteLine().Write("this.").Write(PointerName).Write(".SetBoolean(");
                    WriteFieldOffset(slot.offset, union).Write(", ");
                    if (slot.hadExplicitDefault && slot.defaultValue.Union == Schema.Value.Unions.@bool &&
                        slot.defaultValue.@bool)
                    {
                        Write("!");
                    }
                    Write("value);");
                    break;

                case Schema.Type.Unions.int8:
                case Schema.Type.Unions.uint8:
                case Schema.Type.Unions.int16:
                case Schema.Type.Unions.uint16:
                case Schema.Type.Unions.int32:
                case Schema.Type.Unions.uint32:
                case Schema.Type.Unions.int64:
                case Schema.Type.Unions.uint64:
                case Schema.Type.Unions.float32:
                case Schema.Type.Unions.float64:
                    WriteLine().Write("this.").Write(PointerName).Write(".");
                    switch (type.Union)
                    {
                    case Schema.Type.Unions.int8: Write("SetSByte"); break;

                    case Schema.Type.Unions.uint8: Write("SetByte"); break;

                    case Schema.Type.Unions.int16: Write("SetInt16"); break;

                    case Schema.Type.Unions.uint16: Write("SetUInt16"); break;

                    case Schema.Type.Unions.int32: Write("SetInt32"); break;

                    case Schema.Type.Unions.uint32: Write("SetUInt32"); break;

                    case Schema.Type.Unions.int64: Write("SetInt64"); break;

                    case Schema.Type.Unions.uint64: Write("SetUInt64"); break;

                    case Schema.Type.Unions.float32: Write("SetSingle"); break;

                    case Schema.Type.Unions.float64: Write("SetDouble"); break;
                    }
                    Write("(");
                    WriteFieldOffset(slot.offset, union);

                    if (slot.hadExplicitDefault)
                    {
                        Write(", (").Write(Format(slot.type)).Write(")(value");
                        WriteXorDefaultValue(field.slot.defaultValue).Write("));");
                    }
                    else
                    {
                        Write(", value);");
                    }
                    break;

                case Schema.Type.Unions.@enum:

                    var e = Lookup([email protected]);
                    if (!((Pointer)e).IsValid || e.Union != Schema.Node.Unions.@enum || !((Pointer)[email protected]).IsValid)
                    {
                        WriteLine().Write("#error enum not found: ").Write([email protected]);
                    }
                    else
                    {
                        WriteLine().Write("this.").Write(PointerName).Write(".SetUInt16(");
                        WriteFieldOffset(slot.offset, union).Write(", (ushort)value);");
                    }
                    break;

                default:
                    WriteLine().Write("throw new global::System.NotImplementedException(); // ").Write(type.Union);
                    break;
                }
            }
            Outdent();
            return(Outdent());
        }
Пример #4
0
 public abstract CodeWriter WriteFieldAccessor(Schema.Node parent, Schema.Field field, Stack <UnionStub> union);
Пример #5
0
 public abstract CodeWriter WriteField(Schema.Field field);