public NativeConstantsDeclaration(IEnumerable<TypeDesc> types, TextBlock container, AccessModifierType accessModifier)
            : base(new TypeDesc("void"), container, accessModifier)
        {
            var constants = types.SelectMany(x => x.Fields).OrderBy(x => x.Name).Select(x => new ConstDeclaration(x, this, AccessModifier)).ToList();

            WriteBaseLine(() => "{");
            constants.ForEach(AddTextBlock);
            WriteBaseLine(() => "}");
        }
        protected BaseTypeDeclaration(TypeDesc type, TextBlock container, AccessModifierType accessModifier)
            : base(container, 1)
        {
            Type = type;
            AccessModifier = accessModifier;

            foreach (string attribute in Attributes)
                WriteBaseLine(() => attribute);
            WriteBaseLine(() => AccessModifier + " " + DeclarationType + " " + TypeName + (BaseTypes.Any() ? " : " + string.Join(", ", BaseTypes) : string.Empty));
        }
        protected BaseTypeDeclaration(TypeDesc type, TextBlock container, AccessModifierType accessModifier)
            : base(container, 1)
        {
            Type           = type;
            AccessModifier = accessModifier;

            foreach (string attribute in Attributes)
            {
                WriteBaseLine(() => attribute);
            }
            WriteBaseLine(() => AccessModifier + " " + DeclarationType + " " + TypeName + (BaseTypes.Any() ? " : " + string.Join(", ", BaseTypes) : string.Empty));
        }
        public NativeMethodsDeclaration(IEnumerable<TypeDesc> types, TextBlock container, AccessModifierType accessModifier)
            : base(new TypeDesc("void"), container, accessModifier)
        {
            var methods = types.SelectMany(x => x.Methods).Where(x => x.Dll != null).OrderBy(x => x.Name).Select(x => new MethodDeclaration(x, this, AccessModifier)).ToArray();

            WriteBaseLine(() => "{");
            for (int i = 0; i < methods.Length; i++)
            {
                AddTextBlock(methods[i]);
                if (i != (methods.Length - 1))
                    WriteLine();
            }
            WriteBaseLine(() => "}");
        }
        public FieldDeclaration(ParameterDesc parameter, BaseTypeDeclaration container, AccessModifierType accessModifier)
            : base(container, 0)
        {
            if (parameter.Hidden)
                accessModifier = AccessModifierType.Private;

            string fieldType;
            if (parameter.ArraySizes.Any())
                fieldType = new ArrayWrapperDeclararation(parameter, container, accessModifier).TypeName;
            else
                fieldType = parameter.TypeDeclaration;

            WriteLine(() => accessModifier + " " + fieldType + " " + parameter.Name + ";");
        }
Beispiel #6
0
        public CodeWriter(LibDesc assembly, Settings config)
            : base(0)
        {
            AccessModifierType accessModifier = config.Internal ? AccessModifierType.Internal : AccessModifierType.Public;

            config.SuppressedTypes.Add(new Settings.Type {
                Name = "IntPtr"
            });
            config.SuppressedTypes.Add(new Settings.Type {
                Name = "Guid"
            });

            var allTypes = assembly.Types.Where(x => !x.AutoGeneratedName && !config.SuppressedTypes.Any(y => y.Name == x.Name)).ToArray();

            WriteBaseLine(() => "using System;");
            WriteBaseLine(() => "using System.Collections.Concurrent;");
            WriteBaseLine(() => "using System.Runtime.InteropServices;");
            for (int i = 1; i < config.Namespaces.Count; i++)
            {
                WriteBaseLine(() => "using " + config.Namespaces[i] + ";");
            }
            WriteLine();
            WriteBaseLine(() => "namespace " + (config.Namespaces.Any() ? config.Namespaces.First().Name : assembly.Name));
            WriteBaseLine(() => "{");

            List <BaseTypeDeclaration> typeDeclarations = new List <BaseTypeDeclaration>();

            typeDeclarations.AddRange(allTypes.Where(x => (x.Kind == TypeKind.Interface) && InterfaceDeclaration.IsDelegate(x)).OrderBy(x => x.Name).Select(x => new DelegateDeclaration(x, this, accessModifier)));
            typeDeclarations.AddRange(allTypes.Where(x => x.Kind == TypeKind.Enum).OrderBy(x => x.Name).Select(x => new EnumDeclaration(x, this, accessModifier)));
            typeDeclarations.AddRange(allTypes.Where(x => (x.Kind == TypeKind.Struct) || (x.Kind == TypeKind.Union)).OrderBy(x => x.Name).Select(x => new StructDeclaration(x, this, accessModifier)));
            typeDeclarations.AddRange(allTypes.Where(x => x.Kind == TypeKind.Interface).Where(x => !InterfaceDeclaration.IsSpecialInterface(x)).OrderBy(x => x.Name).Select(x => new InterfaceDeclaration(x, this, accessModifier)));
            typeDeclarations.AddRange(allTypes.Where(x => x.Kind == TypeKind.Interface).Where(x => !InterfaceDeclaration.IsSpecialInterface(x) && !InterfaceDeclaration.IsUserImplemented(x)).OrderBy(x => x.Name).Select(x => new InterfaceWrapperDeclaration(x, this, accessModifier)));
            typeDeclarations.AddRange(allTypes.Where(x => (x.Kind == TypeKind.Interface) && InterfaceDeclaration.IsDelegate(x)).OrderBy(x => x.Name).Select(x => new DelegateWrapperDeclaration(x, this, accessModifier)));
            typeDeclarations.Add(new NativeMethodsDeclaration(allTypes.Where(x => x.Kind == TypeKind.Class), this, accessModifier));
            typeDeclarations.Add(new NativeConstantsDeclaration(allTypes.Where(x => x.Kind == TypeKind.Class), this, accessModifier));

            for (int i = 0; i < typeDeclarations.Count; i++)
            {
                AddTextBlock(typeDeclarations[i]);
                if (i != (typeDeclarations.Count - 1))
                {
                    WriteLine();
                }
            }

            WriteBaseLine(() => "}");
        }
        public MethodDeclaration(MethodDesc method, BaseTypeDeclaration container, AccessModifierType accessModifier, bool isNew = false, TextBlock code = null, string nameOverride = null)
            : base(container, 0)
        {
            string additionalModifier = (accessModifier == AccessModifierType.Empty ? "" : " ") + ((method.Dll != null) ? "static extern " : "") + "unsafe " + (isNew ? "new " : "");

            if (code == null)
            {
                if (method.Dll != null)
                    WriteLine(() => "[DllImport(\"" + method.Dll + "\", EntryPoint = \"" + method.EntryPoint + "\", CallingConvention = CallingConvention." + method.Convention.ToString() + ", CharSet = CharSet.Unicode, SetLastError = true)]");
                else
                    WriteLine(() => "[PreserveSig]");
            }

            WriteLine(() => accessModifier + additionalModifier + GetReturnValue(method) + " " + ((nameOverride == null) ? method.Name : nameOverride) + "(" + GetParameters(method) + ")" + ((code == null) ? ";" : ""));
            if (code != null)
                AddTextBlock(code);
        }
        public InterfaceDeclaration(TypeDesc type, TextBlock container, AccessModifierType accessModifier)
            : base(type, container, accessModifier)
        {
            isIUnknown = type.BaseTypes.Any(x => x.Name == "IUnknown");
            isUserImplemented = IsUserImplemented(type);
            ignoreIdentifier = IsWithoutIdentifier(type);

            var methods = type.AllMethods.Select(x => new MethodDeclaration(x, this, AccessModifierType.Empty, IsInherited(x, type))).ToArray();

            WriteBaseLine(() => "{");
            for (int i = 0; i < methods.Length; i++)
            {
                AddTextBlock(methods[i]);
                if (i != (methods.Length - 1))
                    WriteLine();
            }
            WriteBaseLine(() => "}");
        }
        public EnumDeclaration(TypeDesc type, TextBlock container, AccessModifierType accessModifier)
            : base(type, container, accessModifier)
        {
            if (type.AutoGeneratedName)
                throw new Exception("Name of the enum should be specified");

            int counter = 0;
            var fields = Type.Fields.ToArray();

            WriteBaseLine(() => "{");
            foreach (var field in fields)
            {
                counter++;
                int currentIndex = counter;
                WriteLine(() => field.Name + (field.HasValue ? (" = " + field.Value.ToString()) : string.Empty) + ((currentIndex < fields.Length) ? "," : string.Empty));
            }
            WriteBaseLine(() => "}");
        }
        public EnumDeclaration(TypeDesc type, TextBlock container, AccessModifierType accessModifier)
            : base(type, container, accessModifier)
        {
            if (type.AutoGeneratedName)
            {
                throw new Exception("Name of the enum should be specified");
            }

            int counter = 0;
            var fields  = Type.Fields.ToArray();

            WriteBaseLine(() => "{");
            foreach (var field in fields)
            {
                counter++;
                int currentIndex = counter;
                WriteLine(() => field.Name + (field.HasValue ? (" = " + field.Value.ToString()) : string.Empty) + ((currentIndex < fields.Length) ? "," : string.Empty));
            }
            WriteBaseLine(() => "}");
        }
        public InterfaceDeclaration(TypeDesc type, TextBlock container, AccessModifierType accessModifier)
            : base(type, container, accessModifier)
        {
            isIUnknown        = type.BaseTypes.Any(x => x.Name == "IUnknown");
            isUserImplemented = IsUserImplemented(type);
            ignoreIdentifier  = IsWithoutIdentifier(type);

            var methods = type.AllMethods.Select(x => new MethodDeclaration(x, this, AccessModifierType.Empty, IsInherited(x, type))).ToArray();

            WriteBaseLine(() => "{");
            for (int i = 0; i < methods.Length; i++)
            {
                AddTextBlock(methods[i]);
                if (i != (methods.Length - 1))
                {
                    WriteLine();
                }
            }
            WriteBaseLine(() => "}");
        }
Beispiel #12
0
        public MethodDeclaration(MethodDesc method, BaseTypeDeclaration container, AccessModifierType accessModifier, bool isNew = false, TextBlock code = null, string nameOverride = null)
            : base(container, 0)
        {
            string additionalModifier = (accessModifier == AccessModifierType.Empty ? "" : " ") + ((method.Dll != null) ? "static extern " : "") + "unsafe " + (isNew ? "new " : "");

            if (code == null)
            {
                if (method.Dll != null)
                {
                    WriteLine(() => "[DllImport(\"" + method.Dll + "\", EntryPoint = \"" + method.EntryPoint + "\", CallingConvention = CallingConvention." + method.Convention.ToString() + ", CharSet = CharSet.Unicode, SetLastError = true)]");
                }
                else
                {
                    WriteLine(() => "[PreserveSig]");
                }
            }

            WriteLine(() => accessModifier + additionalModifier + GetReturnValue(method) + " " + ((nameOverride == null) ? method.Name : nameOverride) + "(" + GetParameters(method) + ")" + ((code == null) ? ";" : ""));
            if (code != null)
            {
                AddTextBlock(code);
            }
        }
Beispiel #13
0
 public StructDeclaration(TypeDesc type, TextBlock container, AccessModifierType accessModifier)
     : this(type, container, accessModifier, true)
 {
 }
 public FieldDeclaration(TypeDesc type, string name, BaseTypeDeclaration container, AccessModifierType accessModifier, int arraySize)
     : base(container, 0)
 {
     WriteLine(() => accessModifier + " fixed " + type.Name + " " + name + "[" + arraySize + "];");
 }
        public NativeMethodsDeclaration(IEnumerable <TypeDesc> types, TextBlock container, AccessModifierType accessModifier)
            : base(new TypeDesc("void"), container, accessModifier)
        {
            var methods = types.SelectMany(x => x.Methods).Where(x => x.Dll != null).OrderBy(x => x.Name).Select(x => new MethodDeclaration(x, this, AccessModifier)).ToArray();

            WriteBaseLine(() => "{");
            for (int i = 0; i < methods.Length; i++)
            {
                AddTextBlock(methods[i]);
                if (i != (methods.Length - 1))
                {
                    WriteLine();
                }
            }
            WriteBaseLine(() => "}");
        }
 public StructDeclaration(TypeDesc type, TextBlock container, AccessModifierType accessModifier)
     : this(type, container, accessModifier, true)
 {
 }
 public DelegateDeclaration(TypeDesc type, TextBlock container, AccessModifierType accessModifier)
     : base(new TypeDesc("void"), container, accessModifier)
 {
     this.method = type.Methods.Single(x => x.Name == type.Name);
     prefix      = "_";
 }
        public InterfaceWrapperDeclaration(TypeDesc type, TextBlock container, AccessModifierType accessModifier)
            : base(type, container, accessModifier)
        {
            isIUnknown        = type.BaseTypes.Any(x => x.Name == "IUnknown");
            isUserImplemented = InterfaceDeclaration.IsUserImplemented(type);
            ignoreIdentifier  = InterfaceDeclaration.IsWithoutIdentifier(type);

            var methods = type.AllMethods.ToArray();

            vtbl = new InterfaceVtblDeclaration(type, this);

            WriteBaseLine(() => "{");
            AddTextBlock(vtbl);
            WriteLine();

            for (int i = 0; i < methods.Length; i++)
            {
                int        index      = i;
                MethodDesc method     = type.AllMethods.ToArray()[i];
                string     parameters = vtbl.TypeName + "** @this";
                if (MethodDeclaration.GetParameterNames(method).Any())
                {
                    parameters += ", " + MethodDeclaration.GetParameters(method);
                }
                WriteLine(() => "private unsafe delegate " + MethodDeclaration.GetReturnValue(method) + " " + GetDelegateName(index) + "(" + parameters + ");");
            }
            WriteLine();

            WriteLine(() => "private readonly void* reference;");
            if (!ignoreIdentifier)
            {
                WriteLine(() => "private static readonly Guid iid = new Guid(\"" + type.CLSID.ToString() + "\");");
                WriteLine();
                WriteLine(() => AccessModifier + " static unsafe Guid IID");
                WriteLine(() => "{");
                WriteLine(() => "    get");
                WriteLine(() => "    {");
                WriteLine(() => "        return iid;");
                WriteLine(() => "    }");
                WriteLine(() => "}");
            }

            for (int i = 0; i < methods.Length; i++)
            {
                WriteLine();
                bool isNew = ((methods[i].Name == "GetType" || methods[i].Name == "ToString") && !methods[i].Parameters.Any());
                AddTextBlock(new MethodDeclaration(methods[i], this, AccessModifierType.Public, isNew, new DelegateCaller(i, methods[i], this)));
            }

            if (isIUnknown)
            {
                WriteLine();
                WriteLine(() => "void IDisposable.Dispose()");
                WriteLine(() => "{");
                WriteLine(() => "    while ((reference != null) && (Release() > 0))");
                WriteLine(() => "    {");
                WriteLine(() => "    }");
                WriteLine(() => "}");
            }

            WriteLine();
            WriteLine(() => AccessModifier + " unsafe " + TypeName + "(IntPtr value)");
            WriteLine(() => "{");
            WriteLine(() => "    reference = (void*)value;");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => AccessModifier + " unsafe " + TypeName + "(void* value)");
            WriteLine(() => "{");
            WriteLine(() => "    reference = value;");
            WriteLine(() => "}");
            foreach (string baseTypeRefName in type.BaseTypes.Where(x => !InterfaceDeclaration.IsSpecialInterface(x)).Select(x => x.Name))
            {
                WriteLine();
                WriteLine(() => "public static unsafe explicit operator " + TypeName + "(" + baseTypeRefName + " value)");
                WriteLine(() => "{");
                WriteLine(() => "    return new " + TypeName + "(*((void**)(&value)));");
                WriteLine(() => "}");
                WriteLine();
                WriteLine(() => "public static unsafe implicit operator " + baseTypeRefName + "(" + TypeName + " value)");
                WriteLine(() => "{");
                WriteLine(() => "    return new " + baseTypeRefName + "(value.reference);");
                WriteLine(() => "}");
            }

            WriteLine();
            WriteLine(() => "public static unsafe implicit operator IntPtr(" + TypeName + " value)");
            WriteLine(() => "{");
            WriteLine(() => "    return new IntPtr(value.reference);");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public static unsafe implicit operator void*(" + TypeName + " value)");
            WriteLine(() => "{");
            WriteLine(() => "    return value.reference;");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public static unsafe implicit operator " + TypeName + "(IntPtr value)");
            WriteLine(() => "{");
            WriteLine(() => "    return new " + TypeName + "(value);");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public static unsafe implicit operator " + TypeName + "(void* value)");
            WriteLine(() => "{");
            WriteLine(() => "    return new " + TypeName + "(value);");
            WriteLine(() => "}");

            WriteLine();
            WriteLine(() => "public unsafe override bool Equals(object obj)");
            WriteLine(() => "{");
            WriteLine(() => "    " + TypeName + " other = (" + TypeName + ")obj;");
            WriteLine(() => "    return reference == other.reference;");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public unsafe override int GetHashCode()");
            WriteLine(() => "{");
            WriteLine(() => "    return new IntPtr(reference).GetHashCode();");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public unsafe static bool operator ==(" + TypeName + " left, " + TypeName + " right)");
            WriteLine(() => "{");
            WriteLine(() => "    return left.reference == right.reference;");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public unsafe static bool operator !=(" + TypeName + " left, " + TypeName + " right)");
            WriteLine(() => "{");
            WriteLine(() => "    return left.reference != right.reference;");
            WriteLine(() => "}");

            WriteBaseLine(() => "}");
        }
 public DelegateDeclaration(TypeDesc type, TextBlock container, AccessModifierType accessModifier)
     : base(new TypeDesc("void"), container, accessModifier)
 {
     this.method = type.Methods.Single(x => x.Name == type.Name);
     prefix = "_";
 }
 public DelegateDeclaration(MethodDesc method, TextBlock container, AccessModifierType accessModifier)
     : base(new TypeDesc("void"), container, accessModifier)
 {
     this.method = method;
 }
 public ConstDeclaration(ParameterDesc parameter, BaseTypeDeclaration container, AccessModifierType accessModifier)
     : base(container, 0)
 {
     WriteLine(() => accessModifier + " const " + ((parameter.Value is string) ? "string" : parameter.TypeDeclaration) + " " + parameter.Name + " = " + GetValueInitializer(parameter.Value) + ";");
 }
 public ConstDeclaration(TypeDesc type, string name, BaseTypeDeclaration container, AccessModifierType accessModifier, object value)
     : base(container, 0)
 {
     WriteLine(() => accessModifier + " const " + type.Name + " " + name + " = " + GetValueInitializer(value) + ";");
 }
Beispiel #23
0
        private StructDeclaration(TypeDesc type, TextBlock container, AccessModifierType accessModifier, bool root)
            : base(type, container, accessModifier)
        {
            if (type.BaseTypes.Any())
            {
                throw new Exception("Base types are not expected for struct");
            }
            if (type.Methods.Any())
            {
                throw new Exception("Methods are not expected for struct");
            }

            TypeDesc bitField = CheckBitField(Type);

            var fields = type.Fields.ToArray();

            WriteBaseLine(() => "{");

            if (bitField == null)
            {
                if (root)
                {
                    Action <TypeDesc, List <TypeDesc>, List <ParameterDesc> > getNestedTypes = null;
                    getNestedTypes = (currentType, types, parameters) =>
                    {
                        if (!types.Any(x => x.Name == currentType.Name))
                        {
                            types.Add(currentType);
                            parameters.AddRange(currentType.Fields);
                            if (CheckBitField(currentType) == null)
                            {
                                currentType.Fields.ToList().ForEach(x => getNestedTypes(x.Type, types, parameters));
                            }
                        }
                    };

                    List <TypeDesc>      nestedTypeDescList = new List <TypeDesc>();
                    List <ParameterDesc> parameterDescList  = new List <ParameterDesc>();
                    getNestedTypes(type, nestedTypeDescList, parameterDescList);

                    List <BaseTypeDeclaration> nestedTypes = new List <BaseTypeDeclaration>();

                    nestedTypes.AddRange(nestedTypeDescList.Where(x => x.AutoGeneratedName).Select(x => (BaseTypeDeclaration) new StructDeclaration(x, this, accessModifier, false)));
                    nestedTypes.AddRange(parameterDescList.Where(x => x.ArraySizes.Any()).Select(x => (BaseTypeDeclaration) new ArrayWrapperDeclararation(x, this, accessModifier)).Where(x => !nestedTypes.Any(y => y.TypeName == x.TypeName)));

                    nestedTypes.ForEach(x =>
                    {
                        AddTextBlock(x);
                        WriteLine();
                    });
                }

                Action <List <ParameterDesc> > processNamelessField = null;
                processNamelessField = list =>
                {
                    ParameterDesc currentParameter = list[list.Count - 1];
                    if (currentParameter.AutoGeneratedName)
                    {
                        foreach (var item in currentParameter.Type.Fields.ToArray())
                        {
                            processNamelessField(list.Union(new ParameterDesc[] { item }).ToList());
                        }
                    }
                    else
                    {
                        if (!currentParameter.Hidden)
                        {
                            string propertyType = currentParameter.TypeDeclaration;
                            if (currentParameter.ArraySizes.Any() && !currentParameter.SpecialFlag)
                            {
                                propertyType = new ArrayWrapperDeclararation(currentParameter, this, accessModifier).TypeName;
                            }

                            WriteLine();
                            WriteLine(() => AccessModifier + " " + propertyType + " " + currentParameter.Name);
                            WriteLine(() => "{");
                            WriteLine(() => "    get");
                            WriteLine(() => "    {");
                            WriteLine(() => "        return " + string.Join(".", list.Select(x => x.Name)) + ";");
                            WriteLine(() => "    }");
                            WriteLine(() => "    set");
                            WriteLine(() => "    {");
                            WriteLine(() => "        " + string.Join(".", list.Select(x => x.Name)) + " = value;");
                            WriteLine(() => "    }");
                            WriteLine(() => "}");
                        }
                    }
                };

                for (int i = 0; i < fields.Length; i++)
                {
                    ParameterDesc field = fields[i];

                    if (type.Kind == TypeKind.Union)
                    {
                        WriteLine(() => "[FieldOffset(0)]");
                    }

                    AddTextBlock(new FieldDeclaration(field, this, AccessModifier));
                }

                foreach (var field in type.Fields.Where(x => x.AutoGeneratedName))
                {
                    processNamelessField(new ParameterDesc[] { field }.ToList());
                }
            }
            else
            {
                AddTextBlock(new FieldDeclaration(bitField, "__bit_field_value", this, AccessModifierType.Private));

                int bitFieldOffset = 0;
                for (int i = 0; i < fields.Length; i++)
                {
                    ParameterDesc field        = fields[i];
                    int           bitFieldSize = field.ArraySizes.Single();
                    int           bitFieldMask = 0;
                    for (int j = 0; j < bitFieldSize; j++)
                    {
                        bitFieldMask |= (1 << j);
                    }

                    WriteLine();
                    WriteLine(() => AccessModifier + " " + bitField.Name + " " + field.Name);
                    WriteLine(() => "{");
                    WriteLine(() => "    get");
                    WriteLine(() => "    {");
                    WriteLine(() => "        return (" + bitField.Name + ")((__bit_field_value >> " + bitFieldOffset.ToString() + ") & " + bitFieldMask.ToString() + ");");
                    WriteLine(() => "    }");
                    WriteLine(() => "    set");
                    WriteLine(() => "    {");
                    WriteLine(() => "        __bit_field_value = (" + bitField.Name + ")((value & " + bitFieldMask.ToString() + ") << " + bitFieldOffset.ToString() + ");");
                    WriteLine(() => "    }");
                    WriteLine(() => "}");

                    bitFieldOffset += bitFieldSize;
                }
            }

            WriteLine();
            WriteLine(() => AccessModifier + " unsafe static int GetSize()");
            WriteLine(() => "{");
            WriteLine(() => "    return Marshal.SizeOf(typeof(" + TypeName + "));");
            WriteLine(() => "}");

            if ((TypeName == "BOOL") || (TypeName == "BOOLEAN"))
            {
                WriteLine();
                WriteLine(() => "public static unsafe implicit operator bool(" + TypeName + " value)");
                WriteLine(() => "{");
                WriteLine(() => "    return (value.Value != 0);");
                WriteLine(() => "}");
                WriteLine();
                WriteLine(() => "public static unsafe implicit operator " + TypeName + "(bool value)");
                WriteLine(() => "{");
                WriteLine(() => "    return new " + TypeName + " { Value = (byte)(value ? 1 : 0) };");
                WriteLine(() => "}");
            }

            WriteLine();
            WriteLine(() => "public unsafe override bool Equals(object obj)");
            WriteLine(() => "{");
            WriteLine(() => "    if (!(obj is " + TypeName + "))");
            WriteLine(() => "        return false;");
            WriteLine(() => "    fixed (" + TypeName + "* address = &this)");
            WriteLine(() => "    {");
            WriteLine(() => "        " + TypeName + " other = (" + TypeName + ")obj;");
            WriteLine(() => "        IntPtr size = new IntPtr(GetSize());");
            WriteLine(() => "        IntPtr equalBytes = NativeMethods.RtlCompareMemory(address, &other, size);");
            WriteLine(() => "        return size == equalBytes;");
            WriteLine(() => "    }");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public unsafe override int GetHashCode()");
            WriteLine(() => "{");
            WriteLine(() => "    int result = 0;");
            WriteLine(() => "    int size = GetSize();");
            WriteLine(() => "    fixed (" + TypeName + "* address = &this)");
            WriteLine(() => "    {");
            WriteLine(() => "        byte* byteAddress = (byte*)address;");
            WriteLine(() => "        for (int i = 0; i < size; i++)");
            WriteLine(() => "            result ^= byteAddress[i] << i;");
            WriteLine(() => "    }");
            WriteLine(() => "    return result;");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public unsafe static bool operator ==(" + TypeName + " left, " + TypeName + " right)");
            WriteLine(() => "{");
            WriteLine(() => "    IntPtr size = new IntPtr(GetSize());");
            WriteLine(() => "    IntPtr equalBytes = NativeMethods.RtlCompareMemory(&left, &right, size);");
            WriteLine(() => "    return size == equalBytes;");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public unsafe static bool operator !=(" + TypeName + " left, " + TypeName + " right)");
            WriteLine(() => "{");
            WriteLine(() => "    IntPtr size = new IntPtr(GetSize());");
            WriteLine(() => "    IntPtr equalBytes = NativeMethods.RtlCompareMemory(&left, &right, size);");
            WriteLine(() => "    return size != equalBytes;");
            WriteLine(() => "}");

            WriteBaseLine(() => "}");
        }
Beispiel #24
0
 public FieldDeclaration(TypeDesc type, string name, BaseTypeDeclaration container, AccessModifierType accessModifier)
     : base(container, 0)
 {
     WriteLine(() => accessModifier + " " + type.Name + " " + name + ";");
 }
Beispiel #25
0
 public FieldDeclaration(TypeDesc type, string name, BaseTypeDeclaration container, AccessModifierType accessModifier, int arraySize)
     : base(container, 0)
 {
     WriteLine(() => accessModifier + " fixed " + type.Name + " " + name + "[" + arraySize + "];");
 }
 public DelegateDeclaration(MethodDesc method, TextBlock container, AccessModifierType accessModifier)
     : base(new TypeDesc("void"), container, accessModifier)
 {
     this.method = method;
 }
        public ArrayWrapperDeclararation(ParameterDesc parameter, BaseTypeDeclaration container, AccessModifierType accessModifier)
            : base(parameter.Type, container, accessModifier)
        {
            this.arraySizes = parameter.ArraySizes;
            int totalLength = 1;

            int[] sizes = arraySizes.ToArray();

            List<string> lengthConstants = new List<string>();
            List<string> indexerParameters = new List<string>();

            for(int i = 0; i < sizes.Length; i++)
            {
                totalLength *= sizes[i];

                lengthConstants.Add("length" + i.ToString());
                indexerParameters.Add("index" + i.ToString());
            }

            List<string> parts = new List<string>();
            for (int i = 0; i < sizes.Length; i++)
            {
                List<string> multipliers = new List<string>();
                multipliers.Add(indexerParameters[i]);
                for (int j = i; j < (sizes.Length - 1); j++)
                    multipliers.Add(lengthConstants[j]);
                parts.Add(string.Join(" * ", multipliers));
            }

            string index = string.Join(" + ", parts);

            bool canBeFixedArray = IsPrimitiveType(parameter.Type.Name) && (parameter.IndirectionLevel == 0);

            WriteBaseLine(() => "{");

            for (int i = 0; i < sizes.Length; i++)
                AddTextBlock(new ConstDeclaration(new TypeDesc("int"), lengthConstants[i], this, AccessModifierType.Private, sizes[i]));
            WriteLine();

            if (!canBeFixedArray)
            {
                for (int i = 0; i < totalLength; i++)
                    AddTextBlock(new FieldDeclaration(parameter.Type, "data" + i.ToString(), this, AccessModifierType.Private));
            }
            else
                AddTextBlock(new FieldDeclaration(parameter.Type, "data", this, AccessModifierType.Private, totalLength));

            WriteLine();

            WriteLine(() => AccessModifier + " unsafe " + parameter.Type.Name + " this[" + string.Join(", ", indexerParameters.Select(x => "int " + x)) + "]");
            WriteLine(() => "{");
            WriteLine(() => "    get");
            WriteLine(() => "    {");
            WriteLine(() => "        fixed (" + parameter.Type.Name + "* pointer = " + (canBeFixedArray ? "data" : "&data0") + ")");
            WriteLine(() => "        {");
            WriteLine(() => "            return pointer[" + index + "];");
            WriteLine(() => "        }");
            WriteLine(() => "    }");
            WriteLine(() => "    set");
            WriteLine(() => "    {");
            WriteLine(() => "        fixed (" + parameter.Type.Name + "* pointer = " + (canBeFixedArray ? "data" : "&data0") + ")");
            WriteLine(() => "        {");
            WriteLine(() => "            pointer[" + index + "] = value;");
            WriteLine(() => "        }");
            WriteLine(() => "    }");
            WriteLine(() => "}");

            WriteLine();

            WriteLine(() => "public static unsafe implicit operator " + parameter.Type.Name + "[](" + TypeName + " value)");
            WriteLine(() => "{");
            WriteLine(() => "    " + parameter.Type.Name + "[] result = new " + parameter.Type.Name + "[" + totalLength + "];");
            WriteLine(() => "    " + parameter.Type.Name + "* pointer = " + (canBeFixedArray ? "value.data" : "&value.data0") + ";");
            WriteLine(() => "    {");
            WriteLine(() => "        for (int i = 0; i < " + totalLength + "; i++)");
            WriteLine(() => "            result[i] = pointer[i];");
            WriteLine(() => "    }");
            WriteLine(() => "    return result;");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public static unsafe implicit operator " + TypeName + "(" + parameter.Type.Name + "[] value)");
            WriteLine(() => "{");
            WriteLine(() => "    if (value == null)");
            WriteLine(() => "       throw new ArgumentNullException(\"value\");");
            WriteLine(() => "    " + TypeName + " result = new " + TypeName + "();");
            WriteLine(() => "    " + parameter.Type.Name + "* pointer = " + (canBeFixedArray ? "result.data" : "&result.data0") + ";");
            WriteLine(() => "    for (int i = 0; i < ((value.Length < " + totalLength + ") ? value.Length : " + totalLength + "); i++)");
            WriteLine(() => "        pointer[i] = value[i];");
            WriteLine(() => "    return result;");
            WriteLine(() => "}");

            WriteBaseLine(() => "}");
        }
Beispiel #28
0
        public ArrayWrapperDeclararation(ParameterDesc parameter, BaseTypeDeclaration container, AccessModifierType accessModifier)
            : base(parameter.Type, container, accessModifier)
        {
            this.arraySizes = parameter.ArraySizes;
            int totalLength = 1;

            int[] sizes = arraySizes.ToArray();

            List <string> lengthConstants   = new List <string>();
            List <string> indexerParameters = new List <string>();

            for (int i = 0; i < sizes.Length; i++)
            {
                totalLength *= sizes[i];

                lengthConstants.Add("length" + i.ToString());
                indexerParameters.Add("index" + i.ToString());
            }

            List <string> parts = new List <string>();

            for (int i = 0; i < sizes.Length; i++)
            {
                List <string> multipliers = new List <string>();
                multipliers.Add(indexerParameters[i]);
                for (int j = i; j < (sizes.Length - 1); j++)
                {
                    multipliers.Add(lengthConstants[j]);
                }
                parts.Add(string.Join(" * ", multipliers));
            }

            string index = string.Join(" + ", parts);

            bool canBeFixedArray = IsPrimitiveType(parameter.Type.Name) && (parameter.IndirectionLevel == 0);

            WriteBaseLine(() => "{");

            for (int i = 0; i < sizes.Length; i++)
            {
                AddTextBlock(new ConstDeclaration(new TypeDesc("int"), lengthConstants[i], this, AccessModifierType.Private, sizes[i]));
            }
            WriteLine();

            if (!canBeFixedArray)
            {
                for (int i = 0; i < totalLength; i++)
                {
                    AddTextBlock(new FieldDeclaration(parameter.Type, "data" + i.ToString(), this, AccessModifierType.Private));
                }
            }
            else
            {
                AddTextBlock(new FieldDeclaration(parameter.Type, "data", this, AccessModifierType.Private, totalLength));
            }

            WriteLine();

            WriteLine(() => AccessModifier + " unsafe " + parameter.Type.Name + " this[" + string.Join(", ", indexerParameters.Select(x => "int " + x)) + "]");
            WriteLine(() => "{");
            WriteLine(() => "    get");
            WriteLine(() => "    {");
            WriteLine(() => "        fixed (" + parameter.Type.Name + "* pointer = " + (canBeFixedArray ? "data" : "&data0") + ")");
            WriteLine(() => "        {");
            WriteLine(() => "            return pointer[" + index + "];");
            WriteLine(() => "        }");
            WriteLine(() => "    }");
            WriteLine(() => "    set");
            WriteLine(() => "    {");
            WriteLine(() => "        fixed (" + parameter.Type.Name + "* pointer = " + (canBeFixedArray ? "data" : "&data0") + ")");
            WriteLine(() => "        {");
            WriteLine(() => "            pointer[" + index + "] = value;");
            WriteLine(() => "        }");
            WriteLine(() => "    }");
            WriteLine(() => "}");

            WriteLine();

            WriteLine(() => "public static unsafe implicit operator " + parameter.Type.Name + "[](" + TypeName + " value)");
            WriteLine(() => "{");
            WriteLine(() => "    " + parameter.Type.Name + "[] result = new " + parameter.Type.Name + "[" + totalLength + "];");
            WriteLine(() => "    " + parameter.Type.Name + "* pointer = " + (canBeFixedArray ? "value.data" : "&value.data0") + ";");
            WriteLine(() => "    {");
            WriteLine(() => "        for (int i = 0; i < " + totalLength + "; i++)");
            WriteLine(() => "            result[i] = pointer[i];");
            WriteLine(() => "    }");
            WriteLine(() => "    return result;");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public static unsafe implicit operator " + TypeName + "(" + parameter.Type.Name + "[] value)");
            WriteLine(() => "{");
            WriteLine(() => "    if (value == null)");
            WriteLine(() => "       throw new ArgumentNullException(\"value\");");
            WriteLine(() => "    " + TypeName + " result = new " + TypeName + "();");
            WriteLine(() => "    " + parameter.Type.Name + "* pointer = " + (canBeFixedArray ? "result.data" : "&result.data0") + ";");
            WriteLine(() => "    for (int i = 0; i < ((value.Length < " + totalLength + ") ? value.Length : " + totalLength + "); i++)");
            WriteLine(() => "        pointer[i] = value[i];");
            WriteLine(() => "    return result;");
            WriteLine(() => "}");

            WriteBaseLine(() => "}");
        }
 public ConstDeclaration(TypeDesc type, string name, BaseTypeDeclaration container, AccessModifierType accessModifier, object value)
     : base(container, 0)
 {
     WriteLine(() => accessModifier + " const " + type.Name + " " + name + " = " + GetValueInitializer(value) + ";");
 }
        private StructDeclaration(TypeDesc type, TextBlock container, AccessModifierType accessModifier, bool root)
            : base(type, container, accessModifier)
        {
            if (type.BaseTypes.Any())
                throw new Exception("Base types are not expected for struct");
            if (type.Methods.Any())
                throw new Exception("Methods are not expected for struct");

            TypeDesc bitField = CheckBitField(Type);

            var fields = type.Fields.ToArray();

            WriteBaseLine(() => "{");

            if (bitField == null)
            {
                if (root)
                {
                    Action<TypeDesc, List<TypeDesc>, List<ParameterDesc>> getNestedTypes = null;
                    getNestedTypes = (currentType, types, parameters) =>
                    {
                        if (!types.Any(x => x.Name == currentType.Name))
                        {
                            types.Add(currentType);
                            parameters.AddRange(currentType.Fields);
                            if (CheckBitField(currentType) == null)
                                currentType.Fields.ToList().ForEach(x => getNestedTypes(x.Type, types, parameters));
                        }
                    };

                    List<TypeDesc> nestedTypeDescList = new List<TypeDesc>();
                    List<ParameterDesc> parameterDescList = new List<ParameterDesc>();
                    getNestedTypes(type, nestedTypeDescList, parameterDescList);

                    List<BaseTypeDeclaration> nestedTypes = new List<BaseTypeDeclaration>();

                    nestedTypes.AddRange(nestedTypeDescList.Where(x => x.AutoGeneratedName).Select(x => (BaseTypeDeclaration)new StructDeclaration(x, this, accessModifier, false)));
                    nestedTypes.AddRange(parameterDescList.Where(x => x.ArraySizes.Any()).Select(x => (BaseTypeDeclaration)new ArrayWrapperDeclararation(x, this, accessModifier)).Where(x => !nestedTypes.Any(y => y.TypeName == x.TypeName)));

                    nestedTypes.ForEach(x =>
                    {
                        AddTextBlock(x);
                        WriteLine();
                    });
                }

                Action<List<ParameterDesc>> processNamelessField = null;
                processNamelessField = list =>
                {
                    ParameterDesc currentParameter = list[list.Count - 1];
                    if (currentParameter.AutoGeneratedName)
                    {
                        foreach (var item in currentParameter.Type.Fields.ToArray())
                            processNamelessField(list.Union(new ParameterDesc[] { item }).ToList());
                    }
                    else
                    {
                        if (!currentParameter.Hidden)
                        {
                            string propertyType = currentParameter.TypeDeclaration;
                            if (currentParameter.ArraySizes.Any() && !currentParameter.SpecialFlag)
                                propertyType = new ArrayWrapperDeclararation(currentParameter, this, accessModifier).TypeName;

                            WriteLine();
                            WriteLine(() => AccessModifier + " " + propertyType + " " + currentParameter.Name);
                            WriteLine(() => "{");
                            WriteLine(() => "    get");
                            WriteLine(() => "    {");
                            WriteLine(() => "        return " + string.Join(".", list.Select(x => x.Name)) + ";");
                            WriteLine(() => "    }");
                            WriteLine(() => "    set");
                            WriteLine(() => "    {");
                            WriteLine(() => "        " + string.Join(".", list.Select(x => x.Name)) + " = value;");
                            WriteLine(() => "    }");
                            WriteLine(() => "}");
                        }
                    }
                };

                for (int i = 0; i < fields.Length; i++)
                {
                    ParameterDesc field = fields[i];

                    if (type.Kind == TypeKind.Union)
                        WriteLine(() => "[FieldOffset(0)]");

                    AddTextBlock(new FieldDeclaration(field, this, AccessModifier));
                }

                foreach (var field in type.Fields.Where(x => x.AutoGeneratedName))
                    processNamelessField(new ParameterDesc[] { field }.ToList());
            }
            else
            {
                AddTextBlock(new FieldDeclaration(bitField, "__bit_field_value", this, AccessModifierType.Private));

                int bitFieldOffset = 0;
                for (int i = 0; i < fields.Length; i++)
                {
                    ParameterDesc field = fields[i];
                    int bitFieldSize = field.ArraySizes.Single();
                    int bitFieldMask = 0;
                    for (int j = 0; j < bitFieldSize; j++)
                        bitFieldMask |= (1 << j);

                    WriteLine();
                    WriteLine(() => AccessModifier + " " + bitField.Name + " " + field.Name);
                    WriteLine(() => "{");
                    WriteLine(() => "    get");
                    WriteLine(() => "    {");
                    WriteLine(() => "        return (" + bitField.Name + ")((__bit_field_value >> " + bitFieldOffset.ToString() + ") & " + bitFieldMask.ToString() + ");");
                    WriteLine(() => "    }");
                    WriteLine(() => "    set");
                    WriteLine(() => "    {");
                    WriteLine(() => "        __bit_field_value = (" + bitField.Name + ")((value & " + bitFieldMask.ToString() + ") << " + bitFieldOffset.ToString() + ");");
                    WriteLine(() => "    }");
                    WriteLine(() => "}");

                    bitFieldOffset += bitFieldSize;
                }
            }

            WriteLine();
            WriteLine(() => AccessModifier + " unsafe static int GetSize()");
            WriteLine(() => "{");
            WriteLine(() => "    return Marshal.SizeOf(typeof(" + TypeName + "));");
            WriteLine(() => "}");

            if ((TypeName == "BOOL") || (TypeName == "BOOLEAN"))
            {
                WriteLine();
                WriteLine(() => "public static unsafe implicit operator bool(" + TypeName + " value)");
                WriteLine(() => "{");
                WriteLine(() => "    return (value.Value != 0);");
                WriteLine(() => "}");
                WriteLine();
                WriteLine(() => "public static unsafe implicit operator " + TypeName + "(bool value)");
                WriteLine(() => "{");
                WriteLine(() => "    return new " + TypeName + " { Value = (byte)(value ? 1 : 0) };");
                WriteLine(() => "}");
            }

            WriteLine();
            WriteLine(() => "public unsafe override bool Equals(object obj)");
            WriteLine(() => "{");
            WriteLine(() => "    if (!(obj is " + TypeName + "))");
            WriteLine(() => "        return false;");
            WriteLine(() => "    fixed (" + TypeName + "* address = &this)");
            WriteLine(() => "    {");
            WriteLine(() => "        " + TypeName + " other = (" + TypeName + ")obj;");
            WriteLine(() => "        IntPtr size = new IntPtr(GetSize());");
            WriteLine(() => "        IntPtr equalBytes = NativeMethods.RtlCompareMemory(address, &other, size);");
            WriteLine(() => "        return size == equalBytes;");
            WriteLine(() => "    }");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public unsafe override int GetHashCode()");
            WriteLine(() => "{");
            WriteLine(() => "    int result = 0;");
            WriteLine(() => "    int size = GetSize();");
            WriteLine(() => "    fixed (" + TypeName + "* address = &this)");
            WriteLine(() => "    {");
            WriteLine(() => "        byte* byteAddress = (byte*)address;");
            WriteLine(() => "        for (int i = 0; i < size; i++)");
            WriteLine(() => "            result ^= byteAddress[i] << i;");
            WriteLine(() => "    }");
            WriteLine(() => "    return result;");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public unsafe static bool operator ==(" + TypeName + " left, " + TypeName + " right)");
            WriteLine(() => "{");
            WriteLine(() => "    IntPtr size = new IntPtr(GetSize());");
            WriteLine(() => "    IntPtr equalBytes = NativeMethods.RtlCompareMemory(&left, &right, size);");
            WriteLine(() => "    return size == equalBytes;");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public unsafe static bool operator !=(" + TypeName + " left, " + TypeName + " right)");
            WriteLine(() => "{");
            WriteLine(() => "    IntPtr size = new IntPtr(GetSize());");
            WriteLine(() => "    IntPtr equalBytes = NativeMethods.RtlCompareMemory(&left, &right, size);");
            WriteLine(() => "    return size != equalBytes;");
            WriteLine(() => "}");

            WriteBaseLine(() => "}");
        }
 public ConstDeclaration(ParameterDesc parameter, BaseTypeDeclaration container, AccessModifierType accessModifier)
     : base(container, 0)
 {
     WriteLine(() => accessModifier + " const " + ((parameter.Value is string) ? "string" : parameter.TypeDeclaration) + " " + parameter.Name + " = " + GetValueInitializer(parameter.Value) + ";");
 }
 public FieldDeclaration(TypeDesc type, string name, BaseTypeDeclaration container, AccessModifierType accessModifier)
     : base(container, 0)
 {
     WriteLine(() => accessModifier + " " + type.Name + " " + name + ";");
 }
        public NativeConstantsDeclaration(IEnumerable <TypeDesc> types, TextBlock container, AccessModifierType accessModifier)
            : base(new TypeDesc("void"), container, accessModifier)
        {
            var constants = types.SelectMany(x => x.Fields).OrderBy(x => x.Name).Select(x => new ConstDeclaration(x, this, AccessModifier)).ToList();

            WriteBaseLine(() => "{");
            constants.ForEach(AddTextBlock);
            WriteBaseLine(() => "}");
        }
        public InterfaceWrapperDeclaration(TypeDesc type, TextBlock container, AccessModifierType accessModifier)
            : base(type, container, accessModifier)
        {
            isIUnknown = type.BaseTypes.Any(x => x.Name == "IUnknown");
            isUserImplemented = InterfaceDeclaration.IsUserImplemented(type);
            ignoreIdentifier = InterfaceDeclaration.IsWithoutIdentifier(type);

            var methods = type.AllMethods.ToArray();

            vtbl = new InterfaceVtblDeclaration(type, this);

            WriteBaseLine(() => "{");
            AddTextBlock(vtbl);
            WriteLine();

            for(int i = 0; i < methods.Length; i++)
            {
                int index = i;
                MethodDesc method = type.AllMethods.ToArray()[i];
                string parameters = vtbl.TypeName + "** @this";
                if (MethodDeclaration.GetParameterNames(method).Any())
                    parameters += ", " + MethodDeclaration.GetParameters(method);
                WriteLine(() => "private unsafe delegate " + MethodDeclaration.GetReturnValue(method) + " " + GetDelegateName(index) + "(" + parameters + ");");
            }
            WriteLine();

            WriteLine(() => "private readonly void* reference;");
            if (!ignoreIdentifier)
            {
                WriteLine(() => "private static readonly Guid iid = new Guid(\"" + type.CLSID.ToString() + "\");");
                WriteLine();
                WriteLine(() => AccessModifier + " static unsafe Guid IID");
                WriteLine(() => "{");
                WriteLine(() => "    get");
                WriteLine(() => "    {");
                WriteLine(() => "        return iid;");
                WriteLine(() => "    }");
                WriteLine(() => "}");
            }

            for (int i = 0; i < methods.Length; i++)
            {
                WriteLine();
                bool isNew = ((methods[i].Name == "GetType" || methods[i].Name == "ToString") && !methods[i].Parameters.Any());
                AddTextBlock(new MethodDeclaration(methods[i], this, AccessModifierType.Public, isNew, new DelegateCaller(i, methods[i], this)));
            }

            if (isIUnknown)
            {
                WriteLine();
                WriteLine(() => "void IDisposable.Dispose()");
                WriteLine(() => "{");
                WriteLine(() => "    while ((reference != null) && (Release() > 0))");
                WriteLine(() => "    {");
                WriteLine(() => "    }");
                WriteLine(() => "}");
            }

            WriteLine();
            WriteLine(() => AccessModifier + " unsafe " + TypeName + "(IntPtr value)");
            WriteLine(() => "{");
            WriteLine(() => "    reference = (void*)value;");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => AccessModifier + " unsafe " + TypeName + "(void* value)");
            WriteLine(() => "{");
            WriteLine(() => "    reference = value;");
            WriteLine(() => "}");
            foreach (string baseTypeRefName in type.BaseTypes.Where(x => !InterfaceDeclaration.IsSpecialInterface(x)).Select(x => x.Name))
            {
                WriteLine();
                WriteLine(() => "public static unsafe explicit operator " + TypeName + "(" + baseTypeRefName + " value)");
                WriteLine(() => "{");
                WriteLine(() => "    return new " + TypeName + "(*((void**)(&value)));");
                WriteLine(() => "}");
                WriteLine();
                WriteLine(() => "public static unsafe implicit operator " + baseTypeRefName + "(" + TypeName + " value)");
                WriteLine(() => "{");
                WriteLine(() => "    return new " + baseTypeRefName + "(value.reference);");
                WriteLine(() => "}");
            }

            WriteLine();
            WriteLine(() => "public static unsafe implicit operator IntPtr(" + TypeName + " value)");
            WriteLine(() => "{");
            WriteLine(() => "    return new IntPtr(value.reference);");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public static unsafe implicit operator void*(" + TypeName + " value)");
            WriteLine(() => "{");
            WriteLine(() => "    return value.reference;");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public static unsafe implicit operator " + TypeName + "(IntPtr value)");
            WriteLine(() => "{");
            WriteLine(() => "    return new " + TypeName + "(value);");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public static unsafe implicit operator " + TypeName + "(void* value)");
            WriteLine(() => "{");
            WriteLine(() => "    return new " + TypeName + "(value);");
            WriteLine(() => "}");

            WriteLine();
            WriteLine(() => "public unsafe override bool Equals(object obj)");
            WriteLine(() => "{");
            WriteLine(() => "    " + TypeName + " other = (" + TypeName + ")obj;");
            WriteLine(() => "    return reference == other.reference;");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public unsafe override int GetHashCode()");
            WriteLine(() => "{");
            WriteLine(() => "    return new IntPtr(reference).GetHashCode();");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public unsafe static bool operator ==(" + TypeName + " left, " + TypeName + " right)");
            WriteLine(() => "{");
            WriteLine(() => "    return left.reference == right.reference;");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public unsafe static bool operator !=(" + TypeName + " left, " + TypeName + " right)");
            WriteLine(() => "{");
            WriteLine(() => "    return left.reference != right.reference;");
            WriteLine(() => "}");

            WriteBaseLine(() => "}");
        }
Beispiel #35
0
        public DelegateWrapperDeclaration(TypeDesc type, TextBlock container, AccessModifierType accessModifier)
            : base(type, container, accessModifier)
        {
            DelegateDeclaration delegateDeclaration = new DelegateDeclaration(type, container, accessModifier);

            WriteBaseLine(() => "{");
            WriteLine(() => "private static readonly ConcurrentDictionary<IntPtr, " + delegateDeclaration.DelegateTypeName + "> references = new ConcurrentDictionary<IntPtr, " + delegateDeclaration.DelegateTypeName + ">();");
            WriteLine();
            WriteLine(() => "private readonly IntPtr reference;");
            WriteLine();
            WriteLine(() => AccessModifier + " unsafe " + TypeName + "(" + delegateDeclaration.DelegateTypeName + " value)");
            WriteLine(() => "    : this()");
            WriteLine(() => "{");
            WriteLine(() => "    IntPtr reference = IntPtr.Zero;");
            WriteLine(() => "    if (value != null)");
            WriteLine(() => "    {");
            WriteLine(() => "        value = new " + delegateDeclaration.DelegateTypeName + "(value);");
            WriteLine(() => "        reference = Marshal.GetFunctionPointerForDelegate(value);");
            WriteLine(() => "        references.TryAdd(reference, value);");
            WriteLine(() => "    }");
            WriteLine(() => "    this.reference = reference;");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => AccessModifier + " unsafe " + TypeName + "(IntPtr value)");
            WriteLine(() => "{");
            WriteLine(() => "    reference = value;");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => AccessModifier + " unsafe " + TypeName + "(void* value)");
            WriteLine(() => "{");
            WriteLine(() => "    reference = new IntPtr(value);");
            WriteLine(() => "}");

            WriteLine();
            WriteLine(() => "public static unsafe implicit operator IntPtr(" + TypeName + " value)");
            WriteLine(() => "{");
            WriteLine(() => "    return value.reference;");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public static unsafe implicit operator void*(" + TypeName + " value)");
            WriteLine(() => "{");
            WriteLine(() => "    return (void*)value.reference;");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public static unsafe implicit operator " + TypeName + "(IntPtr value)");
            WriteLine(() => "{");
            WriteLine(() => "    return new " + TypeName + "(value);");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public static unsafe implicit operator " + TypeName + "(void* value)");
            WriteLine(() => "{");
            WriteLine(() => "    return new " + TypeName + "(value);");
            WriteLine(() => "}");

            WriteLine();
            WriteLine(() => "public static unsafe implicit operator " + TypeName + "(" + delegateDeclaration.DelegateTypeName + " value)");
            WriteLine(() => "{");
            WriteLine(() => "    return new " + TypeName + "(value);");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public static unsafe implicit operator " + delegateDeclaration.DelegateTypeName + "(" + TypeName + " value)");
            WriteLine(() => "{");
            WriteLine(() => "    IntPtr reference = value.reference;");
            WriteLine(() => "    return (reference == IntPtr.Zero) ? null : (" + delegateDeclaration.DelegateTypeName + ")Marshal.GetDelegateForFunctionPointer(reference, typeof(" + delegateDeclaration.DelegateTypeName + "));");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public void Dispose()");
            WriteLine(() => "{");
            WriteLine(() => "    " + delegateDeclaration.DelegateTypeName + " removedReference;");
            WriteLine(() => "    references.TryRemove(reference, out removedReference);");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => AccessModifier + " void RemoveAllReferences()");
            WriteLine(() => "{");
            WriteLine(() => "    references.Clear();");
            WriteLine(() => "}");

            WriteLine();
            WriteLine(() => "public unsafe override bool Equals(object obj)");
            WriteLine(() => "{");
            WriteLine(() => "    " + TypeName + " other = (" + TypeName + ")obj;");
            WriteLine(() => "    return reference == other.reference;");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public unsafe override int GetHashCode()");
            WriteLine(() => "{");
            WriteLine(() => "    return reference.GetHashCode();");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public unsafe static bool operator ==(" + TypeName + " left, " + TypeName + " right)");
            WriteLine(() => "{");
            WriteLine(() => "    return left.reference == right.reference;");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public unsafe static bool operator !=(" + TypeName + " left, " + TypeName + " right)");
            WriteLine(() => "{");
            WriteLine(() => "    return left.reference != right.reference;");
            WriteLine(() => "}");

            WriteLine();

            MethodDesc method = type.AllMethods.Single();

            AddTextBlock(new MethodDeclaration(method, this, AccessModifier, false, new DelegateCaller(method, this), "Invoke"));

            WriteBaseLine(() => "}");
        }
Beispiel #36
0
        public FieldDeclaration(ParameterDesc parameter, BaseTypeDeclaration container, AccessModifierType accessModifier)
            : base(container, 0)
        {
            if (parameter.Hidden)
            {
                accessModifier = AccessModifierType.Private;
            }

            string fieldType;

            if (parameter.ArraySizes.Any())
            {
                fieldType = new ArrayWrapperDeclararation(parameter, container, accessModifier).TypeName;
            }
            else
            {
                fieldType = parameter.TypeDeclaration;
            }

            WriteLine(() => accessModifier + " " + fieldType + " " + parameter.Name + ";");
        }
        public DelegateWrapperDeclaration(TypeDesc type, TextBlock container, AccessModifierType accessModifier)
            : base(type, container, accessModifier)
        {
            DelegateDeclaration delegateDeclaration = new DelegateDeclaration(type, container, accessModifier);

            WriteBaseLine(() => "{");
            WriteLine(() => "private static readonly ConcurrentDictionary<IntPtr, " + delegateDeclaration.DelegateTypeName + "> references = new ConcurrentDictionary<IntPtr, " + delegateDeclaration.DelegateTypeName + ">();");
            WriteLine();
            WriteLine(() => "private readonly IntPtr reference;");
            WriteLine();
            WriteLine(() => AccessModifier + " unsafe " + TypeName + "(" + delegateDeclaration.DelegateTypeName + " value)");
            WriteLine(() => "    : this()");
            WriteLine(() => "{");
            WriteLine(() => "    IntPtr reference = IntPtr.Zero;");
            WriteLine(() => "    if (value != null)");
            WriteLine(() => "    {");
            WriteLine(() => "        value = new " + delegateDeclaration.DelegateTypeName + "(value);");
            WriteLine(() => "        reference = Marshal.GetFunctionPointerForDelegate(value);");
            WriteLine(() => "        references.TryAdd(reference, value);");
            WriteLine(() => "    }");
            WriteLine(() => "    this.reference = reference;");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => AccessModifier + " unsafe " + TypeName + "(IntPtr value)");
            WriteLine(() => "{");
            WriteLine(() => "    reference = value;");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => AccessModifier + " unsafe " + TypeName + "(void* value)");
            WriteLine(() => "{");
            WriteLine(() => "    reference = new IntPtr(value);");
            WriteLine(() => "}");

            WriteLine();
            WriteLine(() => "public static unsafe implicit operator IntPtr(" + TypeName + " value)");
            WriteLine(() => "{");
            WriteLine(() => "    return value.reference;");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public static unsafe implicit operator void*(" + TypeName + " value)");
            WriteLine(() => "{");
            WriteLine(() => "    return (void*)value.reference;");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public static unsafe implicit operator " + TypeName + "(IntPtr value)");
            WriteLine(() => "{");
            WriteLine(() => "    return new " + TypeName + "(value);");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public static unsafe implicit operator " + TypeName + "(void* value)");
            WriteLine(() => "{");
            WriteLine(() => "    return new " + TypeName + "(value);");
            WriteLine(() => "}");

            WriteLine();
            WriteLine(() => "public static unsafe implicit operator " + TypeName + "(" + delegateDeclaration.DelegateTypeName + " value)");
            WriteLine(() => "{");
            WriteLine(() => "    return new " + TypeName + "(value);");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public static unsafe implicit operator " + delegateDeclaration.DelegateTypeName + "(" + TypeName + " value)");
            WriteLine(() => "{");
            WriteLine(() => "    IntPtr reference = value.reference;");
            WriteLine(() => "    return (reference == IntPtr.Zero) ? null : (" + delegateDeclaration.DelegateTypeName + ")Marshal.GetDelegateForFunctionPointer(reference, typeof(" + delegateDeclaration.DelegateTypeName + "));");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public void Dispose()");
            WriteLine(() => "{");
            WriteLine(() => "    " + delegateDeclaration.DelegateTypeName + " removedReference;");
            WriteLine(() => "    references.TryRemove(reference, out removedReference);");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => AccessModifier + " void RemoveAllReferences()");
            WriteLine(() => "{");
            WriteLine(() => "    references.Clear();");
            WriteLine(() => "}");

            WriteLine();
            WriteLine(() => "public unsafe override bool Equals(object obj)");
            WriteLine(() => "{");
            WriteLine(() => "    " + TypeName + " other = (" + TypeName + ")obj;");
            WriteLine(() => "    return reference == other.reference;");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public unsafe override int GetHashCode()");
            WriteLine(() => "{");
            WriteLine(() => "    return reference.GetHashCode();");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public unsafe static bool operator ==(" + TypeName + " left, " + TypeName + " right)");
            WriteLine(() => "{");
            WriteLine(() => "    return left.reference == right.reference;");
            WriteLine(() => "}");
            WriteLine();
            WriteLine(() => "public unsafe static bool operator !=(" + TypeName + " left, " + TypeName + " right)");
            WriteLine(() => "{");
            WriteLine(() => "    return left.reference != right.reference;");
            WriteLine(() => "}");

            WriteLine();

            MethodDesc method = type.AllMethods.Single();

            AddTextBlock(new MethodDeclaration(method, this, AccessModifier, false, new DelegateCaller(method, this), "Invoke"));

            WriteBaseLine(() => "}");
        }