Пример #1
0
 /// <summary>
 /// Gets the code for declaring the specified model as a parameter to a managed method.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <returns>The code.</returns>
 public string GetFormalParameterCode(ParameterModel model)
 {
     if (model.Flags.HasFlag(ParameterModelFlags.IsOutput))
         return string.Format("out {0} {1}", model.Type.Name, model.Name);
     else
         return string.Format("{0} {1}", model.Type.Name, model.Name);
 }
Пример #2
0
 /// <summary>
 /// Gets the code for passing the specified model as parameter to a trampoline method.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <returns>The code.</returns>
 public string GetTrampolineParameterCode(ParameterModel model)
 {
     if (model.Flags.HasFlag(ParameterModelFlags.IsOutput))
         return string.Format("ref {0}", model.Name);
     else
         return string.Format("{0}", model.Name);
 }
Пример #3
0
        /// <summary>
        /// Gets the code for setup of local variables related to the specified parameter.
        /// </summary>
        /// <param name="marshaller">The marshalling service interface.</param>
        /// <param name="model">The model.</param>
        /// <returns>The code.</returns>
        public string GetLocalVariableSetupCode(MarshallingService marshaller, ParameterModel model)
        {
            if (model.Flags.HasFlag(ParameterModelFlags.IsOutput))
                return string.Empty;

            var builder = new StringBuilder();
            switch (marshaller.ResolveBehavior(model.Type))
            {
                case MarshalBehavior.Structure:
                    builder.AppendLine(string.Format("{0}Marshaller* _{1} = stackalloc {0}Marshaller[{1}.Length];", model.Type.Name, model.Name));
                    builder.AppendLine(string.Format("for(int i = 0; i < {0}.Length; ++i)", model.Name));
                    builder.AppendLine(string.Format("\t_{0}[i] = {1}.ToMarshaller({0}[i]);", model.Name, model.Type.Name));
                    break;
                case MarshalBehavior.Interface:
                    builder.AppendLine(string.Format("System.IntPtr* _{1} = stackalloc System.IntPtr[{1}.Length];", model.Type.Name, model.Name));
                    builder.AppendLine(string.Format("for(int i = 0; i < {0}.Length; ++i)", model.Name));
                    builder.AppendLine(string.Format("\t_{0}[i] = {0}[i].NativePointer;", model.Name, model.Type.Name));
                    break;
                default:
                    builder.AppendLine(string.Format("{0}* _{1} = stackalloc {0}[{1}.Length];", model.Type.Name, model.Name));
                    builder.AppendLine(string.Format("for(int i = 0; i < {0}.Length; ++i)", model.Name));
                    builder.AppendLine(string.Format("\t_{0}[i] = {0}[i];", model.Name, model.Type.Name));
                    break;
            }

            return builder.ToString();
        }
Пример #4
0
 /// <summary>
 /// Gets the code for declaring the specified model as a parameter to a managed method.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <returns>The code.</returns>
 public string GetFormalParameterCode(ParameterModel model)
 {
     if (model.Flags.HasFlag(ParameterModelFlags.IsOutput))
         return string.Format("out System.IntPtr {0}", model.Name);
     else
         return string.Format("System.IntPtr {0}", model.Name);
 }
 /// <summary>
 /// Gets the code for cleanup of local variables related to the specified parameter.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <returns>The code.</returns>
 public string GetLocalVariableCleanupCode(ParameterModel model)
 {
     if (model.Flags.HasFlag(ParameterModelFlags.IsOutput))
     {
         return(string.Format("{0} = {1}.FromMarshaller(_{0});", model.Name, model.Type.Name));
     }
     return(string.Format("_{0}.Release();", model.Name));
 }
 /// <summary>
 /// Gets the code for setup of local variables related to the specified parameter.
 /// </summary>
 /// <param name="marshaller">The marshalling service interface.</param>
 /// <param name="model">The model.</param>
 /// <returns>The code.</returns>
 public string GetLocalVariableSetupCode(MarshallingService marshaller, ParameterModel model)
 {
     if (model.Flags.HasFlag(ParameterModelFlags.IsOutput))
     {
         return(string.Format("{0}Marshaller _{1} = default({0}Marshaller);", model.Type.Name, model.Name));
     }
     return(string.Format("{0}Marshaller _{1} = {0}.ToMarshaller({1});", model.Type.Name, model.Name));
 }
Пример #7
0
 /// <summary>
 /// Gets the code for passing the specified model as parameter to a trampoline method.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <returns>The code.</returns>
 public string GetTrampolineParameterCode(ParameterModel model)
 {
     if (model.Flags.HasFlag(ParameterModelFlags.IsOutput))
     {
         return(string.Format("ref _{0}", model.Name));
     }
     return(string.Format("{0} != null ? {0}.NativePointer : System.IntPtr.Zero", model.Name));
 }
 /// <summary>
 /// Gets the code for declaring the specified model as a parameter to a managed method.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <returns>The code.</returns>
 public string GetFormalParameterCode(ParameterModel model)
 {
     if (model.Flags.HasFlag(ParameterModelFlags.IsOutput))
     {
         return(string.Format("out {0} {1}", model.Type.Name, model.Name));
     }
     return(string.Format("{0} {1}", model.Type.Name, model.Name));
 }
Пример #9
0
 /// <summary>
 /// Gets the code for passing the specified model as parameter to a trampoline method.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <returns>The code.</returns>
 public string GetTrampolineParameterCode(ParameterModel model)
 {
     if (model.Flags.HasFlag(ParameterModelFlags.IsOutput))
     {
         return("System.IntPtr.Zero");
     }
     return(string.Format("new System.IntPtr(_{0})", model.Name));
 }
Пример #10
0
 /// <summary>
 /// Gets the code for setup of local variables related to the specified parameter.
 /// </summary>
 /// <param name="marshaller">The marshalling service interface.</param>
 /// <param name="model">The model.</param>
 /// <returns>The code.</returns>
 public string GetLocalVariableSetupCode(MarshallingService marshaller, ParameterModel model)
 {
     if (model.Flags.HasFlag(ParameterModelFlags.IsOutput))
     {
         return(string.Format("System.IntPtr _{0} = default(System.IntPtr);", model.Name));
     }
     return(string.Empty);
 }
Пример #11
0
 /// <summary>
 /// Gets the code for cleanup of local variables related to the specified parameter.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <returns>The code.</returns>
 public string GetLocalVariableCleanupCode(ParameterModel model)
 {
     if (model.Flags.HasFlag(ParameterModelFlags.IsOutput))
     {
         return(string.Format("{0} = _{0};", model.Name));
     }
     return(string.Empty);
 }
Пример #12
0
 /// <summary>
 /// Gets the code for declaring the specified model as a parameter to a managed method.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <returns>The code.</returns>
 public string GetFormalParameterCode(ParameterModel model)
 {
     if (model.Flags.HasFlag(ParameterModelFlags.IsOutput))
     {
         return(string.Format("out System.IntPtr {0}", model.Name));
     }
     else
     {
         return(string.Format("System.IntPtr {0}", model.Name));
     }
 }
Пример #13
0
 /// <summary>
 /// Gets the code for passing the specified model as parameter to a trampoline method.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <returns>The code.</returns>
 public string GetTrampolineParameterCode(ParameterModel model)
 {
     if (model.Flags.HasFlag(ParameterModelFlags.IsOutput))
     {
         return(string.Format("ref {0}", model.Name));
     }
     else
     {
         return(string.Format("{0}", model.Name));
     }
 }
Пример #14
0
        public Type ResolveType(ParameterModel model)
        {
            if (model == null)
            {
                return(null);
            }

            var behavior = new MarshallingService().ResolveBehavior(model);

            if (behavior == MarshalBehavior.Array || behavior == MarshalBehavior.Indirect)
            {
                return(typeof(IntPtr));
            }
            return(ResolveType(model.Type));
        }
Пример #15
0
        /// <summary>
        /// Gets the code for passing the specified model as parameter to a trampoline method.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns>The code.</returns>
        public string GetTrampolineParameterCode(ParameterModel model)
        {
            if (model.Flags.HasFlag(ParameterModelFlags.IsOutput))
                return string.Format("ref _{0}", model.Name);

            TranslationModel translationModel = model.Type as TranslationModel;
            if (translationModel != null)
            {
                var type = Type.GetType(translationModel.TargetType);
                if (Marshal.SizeOf(type) > sizeof(long))
                    return string.Format("new System.IntPtr(&{0})", model.Name);
            }

            return model.Name;
        }
Пример #16
0
        public MarshalBehavior ResolveBehavior(ParameterModel model)
        {
            if (model.SizeParameter != null)
            {
                if (model.Flags.HasFlag(ParameterModelFlags.HasElementCount))
                    return MarshalBehavior.Array;
                else
                    return MarshalBehavior.Indirect;
            }

            var fallback = ResolveBehavior(model.Type);
            if (fallback == MarshalBehavior.Direct && model.IndirectionLevel > 0)
                return MarshalBehavior.Indirect;
            return fallback;
        }
Пример #17
0
        /// <summary>
        /// Gets the code for cleanup of local variables related to the specified parameter.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns>The code.</returns>
        public string GetLocalVariableCleanupCode(ParameterModel model)
        {
            if (model.Flags.HasFlag(ParameterModelFlags.IsOutput))
            {
                //TODO: This check against IUnknown is a hack for DXGISwapChain::GetBuffer. To remove the
                //      hack, it must be made possible to mark a function for removal -- GetBuffer must be
                //      hand-written in terms of the trampoline that would be generated for it, because it
                //      does not make sense for the model layer to understand the relationship between the
                //      output parameter in question, the IID parameter ("riid"), and the object factory.
                if (model.Type.Key == "IUnknown")
                    return string.Format("{0} = SlimDX.ObjectFactory.Create(_{0}, riid);", model.Name);
                return string.Format("{0} = _{0} != System.IntPtr.Zero ? new {1}(_{0}) : null;", model.Name, model.Type.Name);
            }

            return string.Empty;
        }
Пример #18
0
        /// <summary>
        /// Gets the code for cleanup of local variables related to the specified parameter.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns>The code.</returns>
        public string GetLocalVariableCleanupCode(ParameterModel model)
        {
            if (model.Flags.HasFlag(ParameterModelFlags.IsOutput))
            {
                //TODO: This check against IUnknown is a hack for DXGISwapChain::GetBuffer. To remove the
                //      hack, it must be made possible to mark a function for removal -- GetBuffer must be
                //      hand-written in terms of the trampoline that would be generated for it, because it
                //      does not make sense for the model layer to understand the relationship between the
                //      output parameter in question, the IID parameter ("riid"), and the object factory.
                if (model.Type.Key == "IUnknown")
                {
                    return(string.Format("{0} = SlimDX.ObjectFactory.Create(_{0}, riid);", model.Name));
                }
                return(string.Format("{0} = _{0} != System.IntPtr.Zero ? new {1}(_{0}) : null;", model.Name, model.Type.Name));
            }

            return(string.Empty);
        }
Пример #19
0
        /// <summary>
        /// Gets the code for passing the specified model as parameter to a trampoline method.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns>The code.</returns>
        public string GetTrampolineParameterCode(ParameterModel model)
        {
            if (model.Flags.HasFlag(ParameterModelFlags.IsOutput))
            {
                return(string.Format("ref _{0}", model.Name));
            }

            TranslationModel translationModel = model.Type as TranslationModel;

            if (translationModel != null)
            {
                var type = Type.GetType(translationModel.TargetType);
                if (Marshal.SizeOf(type) > sizeof(long))
                {
                    return(string.Format("new System.IntPtr(&{0})", model.Name));
                }
            }

            return(model.Name);
        }
Пример #20
0
        public MarshalBehavior ResolveBehavior(ParameterModel model)
        {
            if (model.SizeParameter != null)
            {
                if (model.Flags.HasFlag(ParameterModelFlags.HasElementCount))
                {
                    return(MarshalBehavior.Array);
                }
                else
                {
                    return(MarshalBehavior.Indirect);
                }
            }

            var fallback = ResolveBehavior(model.Type);

            if (fallback == MarshalBehavior.Direct && model.IndirectionLevel > 0)
            {
                return(MarshalBehavior.Indirect);
            }
            return(fallback);
        }
Пример #21
0
 /// <summary>
 /// Gets the code for setup of local variables related to the specified parameter.
 /// </summary>
 /// <param name="marshaller">The marshalling service interface.</param>
 /// <param name="model">The model.</param>
 /// <returns>The code.</returns>
 public string GetLocalVariableSetupCode(MarshallingService marshaller, ParameterModel model)
 {
     if (model.Flags.HasFlag(ParameterModelFlags.IsOutput))
         return string.Format("int _{0} = default(int);", model.Name);
     return string.Empty;
 }
Пример #22
0
 public void AddParameter(ParameterModel parameter)
 {
     parameters.Add(parameter);
 }
Пример #23
0
 /// <summary>
 /// Gets the code for passing the specified model as parameter to a trampoline method.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <returns>The code.</returns>
 public string GetTrampolineParameterCode(ParameterModel model)
 {
     if (model.Flags.HasFlag(ParameterModelFlags.IsOutput))
         return string.Format("ref _{0}", model.Name);
     return string.Format("{0} != null ? {0}.NativePointer : System.IntPtr.Zero", model.Name);
 }
Пример #24
0
 public void AddParameter(ParameterModel parameter)
 {
     parameters.Add(parameter);
 }
Пример #25
0
 /// <summary>
 /// Gets the code for cleanup of local variables related to the specified parameter.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <returns>The code.</returns>
 public string GetLocalVariableCleanupCode(ParameterModel model)
 {
     return(string.Format("System.Runtime.InteropServices.Marshal.FreeHGlobal(_{0});", model.Name));
 }
Пример #26
0
 /// <summary>
 /// Gets the code for setup of local variables related to the specified parameter.
 /// </summary>
 /// <param name="marshaller">The marshalling service interface.</param>
 /// <param name="model">The model.</param>
 /// <returns>The code.</returns>
 public string GetLocalVariableSetupCode(MarshallingService marshaller, ParameterModel model)
 {
     return(string.Format("System.IntPtr _{0} = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi({0});", model.Name));
 }
Пример #27
0
 /// <summary>
 /// Gets the code for declaring the specified model as a parameter to a managed method.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <returns>The code.</returns>
 public string GetFormalParameterCode(ParameterModel model)
 {
     return string.Format("System.String {0}", model.Name);
 }
Пример #28
0
 /// <summary>
 /// Gets the code for setup of local variables related to the specified parameter.
 /// </summary>
 /// <param name="marshaller">The marshalling service interface.</param>
 /// <param name="model">The model.</param>
 /// <returns>The code.</returns>
 public string GetLocalVariableSetupCode(MarshallingService marshaller, ParameterModel model)
 {
     return string.Format("{0} = default(System.IntPtr);", model.Name);
 }
Пример #29
0
 /// <summary>
 /// Gets the code for setup of local variables related to the specified parameter.
 /// </summary>
 /// <param name="marshaller">The marshalling service interface.</param>
 /// <param name="model">The model.</param>
 /// <returns>The code.</returns>
 public string GetLocalVariableSetupCode(MarshallingService marshaller, ParameterModel model)
 {
     return(string.Format("{0} = default(System.IntPtr);", model.Name));
 }
Пример #30
0
 /// <summary>
 /// Gets the code for passing the specified model as parameter to a trampoline method.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <returns>The code.</returns>
 public string GetTrampolineParameterCode(ParameterModel model)
 {
     if (model.Flags.HasFlag(ParameterModelFlags.IsOutput))
         return "System.IntPtr.Zero";
     return string.Format("new System.IntPtr(_{0})", model.Name);
 }
Пример #31
0
 /// <summary>
 /// Gets the code for cleanup of local variables related to the specified parameter.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <returns>The code.</returns>
 public string GetLocalVariableCleanupCode(ParameterModel model)
 {
     if (model.Flags.HasFlag(ParameterModelFlags.IsOutput))
         return string.Format("{0} = null;", model.Name);
     return string.Empty;
 }
Пример #32
0
 /// <summary>
 /// Gets the code for passing the specified model as parameter to a trampoline method.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <returns>The code.</returns>
 public string GetTrampolineParameterCode(ParameterModel model)
 {
     return string.Format("_{0}", model.Name);
 }
Пример #33
0
 /// <summary>
 /// Gets the code for setup of local variables related to the specified parameter.
 /// </summary>
 /// <param name="marshaller">The marshalling service interface.</param>
 /// <param name="model">The model.</param>
 /// <returns>The code.</returns>
 public string GetLocalVariableSetupCode(MarshallingService marshaller, ParameterModel model)
 {
     return string.Format("System.IntPtr _{0} = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi({0});", model.Name);
 }
Пример #34
0
 /// <summary>
 /// Gets the code for passing the specified model as parameter to a trampoline method.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <returns>The code.</returns>
 public string GetTrampolineParameterCode(ParameterModel model)
 {
     return(string.Format("new System.IntPtr(&_{0})", model.Name));
 }
Пример #35
0
 /// <summary>
 /// Gets the code for passing the specified model as parameter to a trampoline method.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <returns>The code.</returns>
 public string GetTrampolineParameterCode(ParameterModel model)
 {
     return(string.Format("_{0}", model.Name));
 }
Пример #36
0
 /// <summary>
 /// Gets the code for cleanup of local variables related to the specified parameter.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <returns>The code.</returns>
 public string GetLocalVariableCleanupCode(ParameterModel model)
 {
     if (model.Flags.HasFlag(ParameterModelFlags.IsOutput))
         return string.Format("{0} = {1}.FromMarshaller(_{0});", model.Name, model.Type.Name);
     return string.Format("_{0}.Release();", model.Name);
 }
Пример #37
0
        public Type ResolveType(ParameterModel model)
        {
            if (model == null)
                return null;

            var behavior = new MarshallingService().ResolveBehavior(model);
            if (behavior == MarshalBehavior.Array || behavior == MarshalBehavior.Indirect)
                return typeof(IntPtr);
            return ResolveType(model.Type);
        }
Пример #38
0
 /// <summary>
 /// Gets the code for cleanup of local variables related to the specified parameter.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <returns>The code.</returns>
 public string GetLocalVariableCleanupCode(ParameterModel model)
 {
     return(string.Empty);
 }
Пример #39
0
 /// <summary>
 /// Gets the code for setup of local variables related to the specified parameter.
 /// </summary>
 /// <param name="marshaller">The marshalling service interface.</param>
 /// <param name="model">The model.</param>
 /// <returns>The code.</returns>
 public string GetLocalVariableSetupCode(MarshallingService marshaller, ParameterModel model)
 {
     if (model.Flags.HasFlag(ParameterModelFlags.IsOutput))
         return string.Format("{0}Marshaller _{1} = default({0}Marshaller);", model.Type.Name, model.Name);
     return string.Format("{0}Marshaller _{1} = {0}.ToMarshaller({1});", model.Type.Name, model.Name);
 }
Пример #40
0
 /// <summary>
 /// Gets the code for passing the specified model as parameter to a trampoline method.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <returns>The code.</returns>
 public string GetTrampolineParameterCode(ParameterModel model)
 {
     return string.Format("new System.IntPtr(&_{0})", model.Name);
 }
Пример #41
0
 /// <summary>
 /// Gets the code for declaring the specified model as a parameter to a managed method.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <returns>The code.</returns>
 public string GetFormalParameterCode(ParameterModel model)
 {
     return(string.Format("System.String {0}", model.Name));
 }
Пример #42
0
 /// <summary>
 /// Gets the code for cleanup of local variables related to the specified parameter.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <returns>The code.</returns>
 public string GetLocalVariableCleanupCode(ParameterModel model)
 {
     return string.Format("System.Runtime.InteropServices.Marshal.FreeHGlobal(_{0});", model.Name);
 }
Пример #43
0
 /// <summary>
 /// Gets the code for cleanup of local variables related to the specified parameter.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <returns>The code.</returns>
 public string GetLocalVariableCleanupCode(ParameterModel model)
 {
     return string.Empty;
 }