Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TrampolineParameter"/> class.
        /// </summary>
        /// <param name="type">The type of the parameter.</param>
        /// <param name="flags">Flags specifying the behavior of the parameter.</param>
        public TrampolineParameter(Type type, TrampolineParameterFlags flags)
        {
            if (type == null)
                throw new ArgumentNullException("type");

            if (flags.HasFlag(TrampolineParameterFlags.Reference))
                Type = type.MakeByRefType();
            else
                Type = type;
        }
Beispiel #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TrampolineParameter"/> class.
        /// </summary>
        /// <param name="type">The type of the parameter.</param>
        /// <param name="flags">Flags specifying the behavior of the parameter.</param>
        public TrampolineParameter(Type type, TrampolineParameterFlags flags)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            if (flags.HasFlag(TrampolineParameterFlags.Reference))
            {
                Type = type.MakeByRefType();
            }
            else
            {
                Type = type;
            }
        }
Beispiel #3
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);
        }