Exemplo n.º 1
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());
        }
Exemplo n.º 2
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();
        }
Exemplo n.º 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.Format("System.IntPtr _{0} = default(System.IntPtr);", model.Name));
     }
     return(string.Empty);
 }
 /// <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));
 }
Exemplo n.º 5
0
        static void BuildTrampolineAssembly(ApiModel api, string outputDirectory, string outputFile)
        {
            MarshallingService marshaller = new MarshallingService();
            TrampolineAssemblyBuilder trampolineBuilder = new TrampolineAssemblyBuilder();
            foreach (var interfaceModel in api.Interfaces)
            {
                foreach (var methodModel in interfaceModel.Methods)
                {
                    var methodType = marshaller.ResolveType(methodModel.Type);
                    if (methodType == null)
                        throw new InvalidOperationException(string.Format("Could not resolve return type for method '{0}.'", methodModel.Name));

                    var parameters = new List<TrampolineParameter>();
                    foreach (var parameterModel in methodModel.Parameters)
                    {
                        var parameterType = marshaller.ResolveType(parameterModel);
                        if (parameterType == null)
                            throw new InvalidOperationException(string.Format("Could not resolve type for parameter '{0}.'", parameterModel.Name));

                        TrampolineParameterFlags flags = TrampolineParameterFlags.Default;
                        if (parameterModel.Flags.HasFlag(ParameterModelFlags.IsOutput) && marshaller.ResolveBehavior(parameterModel) != MarshalBehavior.Structure && marshaller.ResolveBehavior(parameterModel) != MarshalBehavior.Enumeration && marshaller.ResolveBehavior(parameterModel) != MarshalBehavior.Array)
                            flags |= TrampolineParameterFlags.Reference;

                        parameters.Add(parameterModel.Type == ApiModel.VoidModel ? new TrampolineParameter(typeof(IntPtr), flags) : new TrampolineParameter(parameterType, flags));
                    }

                    trampolineBuilder.Add(new Trampoline(true, methodType, parameters.ToArray()));
                }
            }

            foreach (var functionModel in api.Functions)
            {
                var methodType = marshaller.ResolveType(functionModel.Type);
                if (methodType == null)
                    throw new InvalidOperationException(string.Format("Could not resolve return type for method '{0}.'", functionModel.Name));

                var parameters = new List<TrampolineParameter>();
                foreach (var parameterModel in functionModel.Parameters)
                {
                    var parameterType = marshaller.ResolveType(parameterModel);
                    if (parameterType == null)
                        throw new InvalidOperationException(string.Format("Could not resolve type for parameter '{0}.'", parameterModel.Name));

                    TrampolineParameterFlags flags = TrampolineParameterFlags.Default;
                    if (parameterModel.Flags.HasFlag(ParameterModelFlags.IsOutput))
                        flags |= TrampolineParameterFlags.Reference;

                    parameters.Add(parameterModel.Type == ApiModel.VoidModel ? new TrampolineParameter(typeof(IntPtr), flags) : new TrampolineParameter(parameterType, flags));
                }

                trampolineBuilder.Add(new Trampoline(false, methodType, parameters.ToArray()));
            }

            trampolineBuilder.CreateAssembly(outputDirectory, outputFile);
        }
Exemplo n.º 6
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));
        }
Exemplo n.º 7
0
        static void BuildTrampolineAssembly(ApiModel api, string outputDirectory, string outputFile)
        {
            MarshallingService        marshaller        = new MarshallingService();
            TrampolineAssemblyBuilder trampolineBuilder = new TrampolineAssemblyBuilder();

            foreach (var interfaceModel in api.Interfaces)
            {
                foreach (var methodModel in interfaceModel.Methods)
                {
                    var methodType = marshaller.ResolveType(methodModel.Type);
                    if (methodType == null)
                    {
                        throw new InvalidOperationException(string.Format("Could not resolve return type for method '{0}.'", methodModel.Name));
                    }

                    var parameters = new List <TrampolineParameter>();
                    foreach (var parameterModel in methodModel.Parameters)
                    {
                        var parameterType = marshaller.ResolveType(parameterModel);
                        if (parameterType == null)
                        {
                            throw new InvalidOperationException(string.Format("Could not resolve type for parameter '{0}.'", parameterModel.Name));
                        }

                        TrampolineParameterFlags flags = TrampolineParameterFlags.Default;
                        if (parameterModel.Flags.HasFlag(ParameterModelFlags.IsOutput) && marshaller.ResolveBehavior(parameterModel) != MarshalBehavior.Structure && marshaller.ResolveBehavior(parameterModel) != MarshalBehavior.Enumeration && marshaller.ResolveBehavior(parameterModel) != MarshalBehavior.Array)
                        {
                            flags |= TrampolineParameterFlags.Reference;
                        }

                        parameters.Add(parameterModel.Type == ApiModel.VoidModel ? new TrampolineParameter(typeof(IntPtr), flags) : new TrampolineParameter(parameterType, flags));
                    }

                    trampolineBuilder.Add(new Trampoline(true, methodType, parameters.ToArray()));
                }
            }

            foreach (var functionModel in api.Functions)
            {
                var methodType = marshaller.ResolveType(functionModel.Type);
                if (methodType == null)
                {
                    throw new InvalidOperationException(string.Format("Could not resolve return type for method '{0}.'", functionModel.Name));
                }

                var parameters = new List <TrampolineParameter>();
                foreach (var parameterModel in functionModel.Parameters)
                {
                    var parameterType = marshaller.ResolveType(parameterModel);
                    if (parameterType == null)
                    {
                        throw new InvalidOperationException(string.Format("Could not resolve type for parameter '{0}.'", parameterModel.Name));
                    }

                    TrampolineParameterFlags flags = TrampolineParameterFlags.Default;
                    if (parameterModel.Flags.HasFlag(ParameterModelFlags.IsOutput))
                    {
                        flags |= TrampolineParameterFlags.Reference;
                    }

                    parameters.Add(parameterModel.Type == ApiModel.VoidModel ? new TrampolineParameter(typeof(IntPtr), flags) : new TrampolineParameter(parameterType, flags));
                }

                trampolineBuilder.Add(new Trampoline(false, methodType, parameters.ToArray()));
            }

            trampolineBuilder.CreateAssembly(outputDirectory, outputFile);
        }
Exemplo n.º 8
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;
 }
Exemplo n.º 9
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));
 }
Exemplo n.º 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("{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);
 }
Exemplo n.º 11
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);
 }
Exemplo n.º 12
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);
        }
Exemplo n.º 13
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);
 }
Exemplo n.º 14
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));
 }