コード例 #1
0
ファイル: Program.cs プロジェクト: SlimDX/SlimDX-v2
        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);
        }
コード例 #2
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);
        }