Example #1
0
        public override void CSharpMarshalToManaged(MarshalContext ctx)
        {
            TemplateSpecializationType templateType = (TemplateSpecializationType) this.Type;
            QualifiedType type = templateType.Arguments[0].Type;

            TextGenerator supportBefore = ctx.SupportBefore;
            supportBefore.WriteLine("var __qlistData = new QListData({0}._0.d);", ctx.ReturnVarName);
            supportBefore.WriteLine("var __size = __qlistData.Size;");
            supportBefore.WriteLine("var __list = new System.Collections.Generic.List<{0}>(__size);", type);
            supportBefore.WriteLine("for (int i = 0; i < __size; i++)");
            supportBefore.WriteStartBraceIndent();
            // TODO: handle pointers to primitives, they cannot be used as a placeholder type and use IntPtr instead
            if (type.Type.IsPrimitiveType() || type.Type.IsEnumType())
            {
                supportBefore.WriteLine("__list.Add(*({0}*) __qlistData.At(i));", type);
            }
            else
            {
                Class @class;
                Type pointee;
                if ((type.Type.IsTagDecl(out @class) ||
                     (type.Type.IsPointerTo(out pointee) && pointee.IsTagDecl(out @class))) && @class.IsAbstract)
                {
                    supportBefore.WriteLine("__list.Add(new {0}Internal(new global::System.IntPtr(__qlistData.At(i))));", type, ctx.ReturnVarName);
                }
                else
                {
                    supportBefore.WriteLine("__list.Add(new {0}(new global::System.IntPtr(__qlistData.At(i))));", type, ctx.ReturnVarName);
                }
            }
            supportBefore.WriteCloseBraceIndent();
            ctx.Return.Write("__list");
        }
Example #2
0
 public override void CSharpMarshalToManaged(MarshalContext ctx)
 {
     if (ctx.ReturnType.Type.Desugar().IsAddress())
     {
         var finalType = ctx.ReturnType.Type.GetFinalPointee() ?? ctx.ReturnType.Type;
         var enumType = GetEnumType(finalType);
         ctx.Return.Write("*({0}*) {1}", enumType, ctx.ReturnVarName);
     }
     else
     {
         ctx.Return.Write(ctx.ReturnVarName);
     }
 }
Example #3
0
 public override void CSharpMarshalCopyCtorToManaged(MarshalContext ctx)
 {
     ctx.SupportBefore.WriteLine("var __instance = new {0}.Internal();", ctx.ReturnType);
     ctx.SupportBefore.WriteLine("QListData.Data.Internal* qListData = (QListData.Data.Internal*) __instance._0.d;");
     ctx.SupportBefore.WriteLine("QListData.Data.Internal* result = (QListData.Data.Internal*) __ret._0.d;");
     ctx.SupportBefore.WriteLine("qListData->begin = result->begin;");
     ctx.SupportBefore.WriteLine("qListData->end = result->end;");
     ctx.SupportBefore.WriteLine("qListData->alloc = result->alloc;");
     ctx.SupportBefore.WriteLine("fixed (void** v = new void*[qListData->end])");
     ctx.SupportBefore.WriteStartBraceIndent();
     ctx.SupportBefore.WriteLine("for (int i = 0; i < qListData->end; i++)");
     ctx.SupportBefore.WriteStartBraceIndent();
     ctx.SupportBefore.WriteLine("v[i] = *(result->array + i);");
     ctx.SupportBefore.WriteCloseBraceIndent();
     ctx.SupportBefore.WriteLine("qListData->array = v;");
     ctx.SupportBefore.WriteCloseBraceIndent();
     ctx.SupportBefore.WriteLine("__instance._0.d = (IntPtr) qListData;");
 }
Example #4
0
 public override void CSharpMarshalToNative(MarshalContext ctx)
 {
     ctx.SupportBefore.WriteLine("var __qstring{0} = QString.FromUtf16((ushort*) Marshal.StringToHGlobalUni({1}).ToPointer(), {1}.Length);",
                                 ctx.ParameterIndex, ctx.Parameter.Name);
     Type type = ctx.Parameter.Type.Desugar();
     if (type.IsAddress())
     {
         ctx.Return.Write("ReferenceEquals(__qstring{0}, null) ? global::System.IntPtr.Zero : __qstring{0}.{1}",
                          ctx.ParameterIndex, Helpers.InstanceIdentifier);
         return;
     }
     Class @class;
     type.IsTagDecl(out @class);
     if (@class == null)
     {
         Type.IsTagDecl(out @class);
     }
     var qualifiedIdentifier = CSharpMarshalNativeToManagedPrinter.QualifiedIdentifier(@class.OriginalClass ?? @class);
     ctx.Return.Write("ReferenceEquals(__qstring{0}, null) ? new {1}.Internal() : *({1}.Internal*) (__qstring{0}.{2})",
                      ctx.ParameterIndex, qualifiedIdentifier, Helpers.InstanceIdentifier);
 }
Example #5
0
 public override void CSharpMarshalToNative(MarshalContext ctx)
 {
     ctx.SupportBefore.WriteLine("var __stringPtr{0} = (ushort*) Marshal.StringToHGlobalUni({1}).ToPointer();",
                                 ctx.ParameterIndex, ctx.Parameter.Name);
     ctx.SupportBefore.WriteLine("var __qstring{0} = QtCore.QString.FromUtf16(ref *__stringPtr{0}, {1}.Length);",
                                 ctx.ParameterIndex, ctx.Parameter.Name);
     Type type = ctx.Parameter.Type.Desugar();
     if (type.IsAddress())
     {
         ctx.Return.Write("ReferenceEquals(__qstring{0}, null) ? global::System.IntPtr.Zero : __qstring{0}.{1}",
                          ctx.ParameterIndex, Helpers.InstanceIdentifier);
         return;
     }
     Class @class;
     type.TryGetClass(out @class);
     if (@class == null)
     {
         Type.TryGetClass(out @class);
     }
     typePrinter = typePrinter ?? (typePrinter = new CSharpTypePrinter(ctx.Driver));
     var qualifiedIdentifier = (@class.OriginalClass ?? @class).Visit(typePrinter);
     ctx.Return.Write("ReferenceEquals(__qstring{0}, null) ? new {1}.Internal() : *({1}.Internal*) (__qstring{0}.{2})",
                      ctx.ParameterIndex, qualifiedIdentifier, Helpers.InstanceIdentifier);
 }
Example #6
0
 public override void CSharpMarshalToNative(MarshalContext ctx)
 {
     if (ctx.Parameter.Type.Desugar().IsAddress())
         ctx.Return.Write("new global::System.IntPtr(&{0})", ctx.Parameter.Name);
     else
         ctx.Return.Write(ctx.Parameter.Name);
 }
Example #7
0
 public override void CSharpMarshalToNative(MarshalContext ctx)
 {
     ctx.Return.Write("new Std.WString()");
 }
Example #8
0
 public override void CSharpMarshalToManaged(MarshalContext ctx)
 {
     ctx.Return.Write("Marshal.PtrToStringUni(new IntPtr(new QString({0}).Utf16))", ctx.ReturnVarName);
 }
Example #9
0
 public virtual void CSharpMarshalCopyCtorToManaged(MarshalContext ctx)
 {
 }
Example #10
0
        public override void CSharpMarshalToManaged(MarshalContext ctx)
        {
            var templateType = Type as TemplateSpecializationType;
            var type = templateType.Arguments[0].Type;

            ctx.Return.Write("new Std.Vector<{0}>({1})", type,
                ctx.ReturnVarName);
        }
Example #11
0
 public override void CSharpMarshalToNative(MarshalContext ctx)
 {
 }
Example #12
0
 public override void CSharpMarshalToNative(MarshalContext ctx)
 {
     ctx.Return.Write("IntPtr.Zero");
 }
Example #13
0
 public override void CLIMarshalToNative(MarshalContext ctx)
 {
     var marshal = (CLIMarshalManagedToNativePrinter) ctx.MarshalToNative;
     if (!ctx.Parameter.Type.Desugar().IsPointer())
         marshal.ArgumentPrefix.Write("*");
     var marshalCtxName = string.Format("ctx_{0}", ctx.Parameter.Name);
     ctx.SupportBefore.WriteLine("msclr::interop::marshal_context {0};", marshalCtxName);
     ctx.Return.Write("{0}.marshal_as<std::ostream*>({1})",
         marshalCtxName, ctx.Parameter.Name);
 }
Example #14
0
 public override void CSharpMarshalToManaged(MarshalContext ctx)
 {
     ctx.Return.Write("Marshal.PtrToStringUni(new IntPtr(QtCore.QString.{0}({1}).Utf16))",
         Helpers.CreateInstanceIdentifier, ctx.ReturnVarName);
 }
Example #15
0
 protected MarshalPrinter(MarshalContext ctx)
 {
     Context = ctx;
 }
Example #16
0
 public override void CLIMarshalToNative(MarshalContext ctx)
 {
     ctx.Return.Write("clix::marshalString<clix::E_UTF8>({0})",
         ctx.Parameter.Name);
 }
Example #17
0
 public override void CSharpMarshalToNative(MarshalContext ctx)
 {
     // pointless, put just so that the generated code compiles
     ctx.Return.Write("new global::System.IntPtr()");
 }
Example #18
0
 public override void CLIMarshalToNative(MarshalContext ctx)
 {
     ctx.Return.Write("::TypeMappedIndex()");
 }
Example #19
0
 public override void CSharpMarshalCopyCtorToManaged(MarshalContext ctx)
 {
     ctx.SupportBefore.WriteLine("var __instance = new {0}.Internal();", ctx.ReturnType);
     ctx.SupportBefore.WriteLine("__instance.i = {0}.i;", ctx.ReturnVarName);
 }
Example #20
0
        public override void CLIMarshalToNative(MarshalContext ctx)
        {
            var templateType = Type as TemplateSpecializationType;
            var type = templateType.Arguments[0].Type;
            var isPointerToPrimitive = type.Type.IsPointerToPrimitiveType();
            var managedType = isPointerToPrimitive
                ? new CILType(typeof(System.IntPtr))
                : type.Type;

            var entryString = (ctx.Parameter != null) ? ctx.Parameter.Name
                : ctx.ArgName;

            var tmpVarName = "_tmp" + entryString;

            var cppTypePrinter = new CppTypePrinter();
            var nativeType = type.Type.Visit(cppTypePrinter);

            ctx.SupportBefore.WriteLine("auto {0} = std::vector<{1}>();",
                tmpVarName, nativeType);
            ctx.SupportBefore.WriteLine("for each({0} _element in {1})",
                managedType, entryString);
            ctx.SupportBefore.WriteStartBraceIndent();
            {
                var param = new Parameter
                {
                    Name = "_element",
                    QualifiedType = type
                };

                var elementCtx = new MarshalContext(ctx.Context)
                                     {
                                         Parameter = param,
                                         ArgName = param.Name,
                                     };

                var marshal = new CLIMarshalManagedToNativePrinter(elementCtx);
                type.Type.Visit(marshal);

                if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
                    ctx.SupportBefore.Write(marshal.Context.SupportBefore);

                if (isPointerToPrimitive)
                    ctx.SupportBefore.WriteLine("auto _marshalElement = {0}.ToPointer();",
                        marshal.Context.Return);
                else
                    ctx.SupportBefore.WriteLine("auto _marshalElement = {0};",
                    marshal.Context.Return);

                ctx.SupportBefore.WriteLine("{0}.push_back(_marshalElement);",
                    tmpVarName);
            }
            
            ctx.SupportBefore.WriteCloseBraceIndent();

            ctx.Return.Write(tmpVarName);
        }
Example #21
0
 public override void CSharpMarshalToManaged(MarshalContext ctx)
 {
     ctx.Return.Write(ctx.ReturnVarName);
 }
Example #22
0
        public override void CLIMarshalToManaged(MarshalContext ctx)
        {
            var templateType = Type as TemplateSpecializationType;
            var type = templateType.Arguments[0].Type;
            var isPointerToPrimitive = type.Type.IsPointerToPrimitiveType();
            var managedType = isPointerToPrimitive
                ? new CILType(typeof(System.IntPtr))
                : type.Type;
            var tmpVarName = "_tmp" + ctx.ArgName;
            
            ctx.SupportBefore.WriteLine(
                "auto {0} = gcnew System::Collections::Generic::List<{1}>();",
                tmpVarName, managedType);
            ctx.SupportBefore.WriteLine("for(auto _element : {0})",
                ctx.ReturnVarName);
            ctx.SupportBefore.WriteStartBraceIndent();
            {
                var elementCtx = new MarshalContext(ctx.Context)
                                     {
                                         ReturnVarName = "_element",
                                         ReturnType = type
                                     };

                var marshal = new CLIMarshalNativeToManagedPrinter(elementCtx);
                type.Type.Visit(marshal);

                if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
                    ctx.SupportBefore.Write(marshal.Context.SupportBefore);

                ctx.SupportBefore.WriteLine("auto _marshalElement = {0};",
                    marshal.Context.Return);

                if (isPointerToPrimitive)
                    ctx.SupportBefore.WriteLine("{0}->Add({1}(_marshalElement));",
                        tmpVarName, managedType);
                else
                    ctx.SupportBefore.WriteLine("{0}->Add(_marshalElement);",
                        tmpVarName);
            }
            ctx.SupportBefore.WriteCloseBraceIndent();

            ctx.Return.Write(tmpVarName);
        }
Example #23
0
 public override void CSharpMarshalToManaged(MarshalContext ctx)
 {
 }
Example #24
0
 public override void CLIMarshalToManaged(MarshalContext ctx)
 {
     throw new System.NotImplementedException();
 }
Example #25
0
 public override void CSharpMarshalToNative(MarshalContext ctx)
 {
     TextGenerator supportBefore = ctx.SupportBefore;
     string suffix = ctx.ParameterIndex > 0 ? ctx.ParameterIndex.ToString(CultureInfo.InvariantCulture) : string.Empty;
     string qList = string.Format("__qList{0}", suffix);
     supportBefore.WriteLine(string.Format("var {0} = new QtCore.QList.Internal();", qList));
     string qListDataData = string.Format("__qlistDataData{0}", suffix);
     supportBefore.WriteLine("var {0} = (QListData.Data.Internal*) {1}._0.d;", qListDataData, qList);
     // TODO: tests with Qt shows that while alloc is not smaller than end, it's not equal, it reserves more space actually
     supportBefore.WriteLine("{0}->alloc = {1}.Count;", qListDataData, ctx.Parameter.Name);
     supportBefore.WriteLine("{0}->begin = 0;", qListDataData, ctx.Parameter.Name);
     supportBefore.WriteLine("{0}->end = {1}.Count;", qListDataData, ctx.Parameter.Name);
     supportBefore.WriteLine("fixed (void** __v = new void*[{0}.Count])", ctx.Parameter.Name);
     supportBefore.WriteStartBraceIndent();
     supportBefore.WriteLine("{0}->array = __v;", qListDataData);
     supportBefore.WriteCloseBraceIndent();
     supportBefore.WriteLine("", qListDataData, ctx.Parameter.Name);
     var parameterType = ctx.Parameter.Type.Desugar();
     TemplateSpecializationType type = parameterType as TemplateSpecializationType;
     if (type == null)
     {
         TypedefType typedef;
         if (parameterType.IsPointerTo(out typedef))
         {
             type = (TemplateSpecializationType) typedef.Desugar();
         }
         else
         {
             parameterType.IsPointerTo(out type);
         }
     }
     Type elementType = type.Arguments[0].Type.Type.Desugar();
     string instance = string.Empty;
     if (!elementType.IsPointerToPrimitiveType())
     {
         instance = string.Format(".{0}", Helpers.InstanceIdentifier);
     }
     supportBefore.WriteLine("for (int i = 0; i < {0}.Count; i++)", ctx.Parameter.Name);
     supportBefore.WriteStartBraceIndent();
     Type desugared = ctx.Parameter.Type.Desugar();
     TemplateSpecializationType templateSpecializationType = desugared as TemplateSpecializationType;
     if (templateSpecializationType == null)
     {
         Type paramType;
         desugared.IsPointerTo(out paramType);
         templateSpecializationType = (TemplateSpecializationType) paramType.Desugar();
     }
     TemplateArgument templateArgument = templateSpecializationType.Arguments[0];
     if (templateArgument.Type.ToString() == "string")
     {
         supportBefore.WriteLine("{0}->array[i] = Marshal.StringToHGlobalUni({1}[i]).ToPointer();", qListDataData, ctx.Parameter.Name, instance);
     }
     else
     {
         Class @class;
         if (templateArgument.Type.Type.IsTagDecl(out @class) && @class.IsValueType)
         {
             supportBefore.WriteLine("{0}.Internal __value = {1}[i].ToInternal();", @class.Name, ctx.Parameter.Name, instance);
             supportBefore.WriteLine("{0}->array[i] = &__value;", qListDataData, ctx.Parameter.Name, instance);
         }
         else
         {
             supportBefore.WriteLine("{0}->array[i] = (void*) {1}[i]{2};", qListDataData, ctx.Parameter.Name, instance);
         }
     }
     supportBefore.WriteCloseBraceIndent();
     ctx.Return.Write(qList);
 }
Example #26
0
 public override void CLIMarshalToManaged(MarshalContext ctx)
 {
     ctx.Return.Write("clix::marshalString<clix::E_UTF8>({0})",
         ctx.ReturnVarName);
 }
Example #27
0
 public virtual void CSharpMarshalToManaged(MarshalContext ctx)
 {
     throw new NotImplementedException();
 }
Example #28
0
 public override void CSharpMarshalToNative(MarshalContext ctx)
 {
     // pointless, put just so that the generated code compiles
     ctx.Return.Write("new QList.Internal()");
 }
Example #29
0
 public virtual void CLIMarshalToNative(MarshalContext ctx)
 {
     throw new NotImplementedException();
 }
Example #30
0
 public override void CSharpMarshalToNative(MarshalContext ctx)
 {
     ctx.Return.Write(ctx.Parameter.Name);
 }
Example #31
0
 protected MarshalPrinter(MarshalContext ctx)
 {
     Context = ctx;
 }