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 InterfaceVtblDeclaration(TypeDesc type, InterfaceWrapperDeclaration container) : base(type, container, AccessModifierType.Private) { isIUnknown = type.BaseTypes.Any(x => x.Name == "IUnknown"); isUserImplemented = InterfaceDeclaration.IsUserImplemented(type); ignoreIdentifier = InterfaceDeclaration.IsWithoutIdentifier(type); var methods = type.AllMethods.OrderBy(x => x.Name).Select(x => new MethodDeclaration(x, this, AccessModifierType.Internal)).ToArray(); WriteBaseLine(() => "{"); for(int i = 0; i < methods.Length; i++) AddTextBlock(new FieldDeclaration(new TypeDesc("IntPtr"), container.GetMethodName(i), this, AccessModifierType.Internal)); 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(() => "}"); }
private static TypeDesc CheckBitField(TypeDesc type) { if (!type.Fields.Any(x => x.SpecialFlag)) return null; if (!type.Fields.All(x => x.SpecialFlag)) throw new Exception("All bit fields should be marked with special flag"); if (type.Fields.Select(x => x.Type.Name).Distinct().Count() > 1) throw new Exception("All bit fields should have the same type"); if (type.Fields.Any(x => x.IndirectionLevel > 0)) throw new Exception("Bit fields cannot be pointer type"); if (!type.Fields.All(x => x.ArraySizes.Count == 1)) throw new Exception("To specify size bit fields should be declared as arrays"); string fieldType = type.Fields.Select(x => x.Type.Name).Distinct().Single(); int sizeInBits; switch (fieldType) { case "byte": case "sbyte": sizeInBits = 8; break; case "short": case "ushort": sizeInBits = 16; break; case "int": case "uint": sizeInBits = 32; break; case "long": case "ulong": sizeInBits = 64; break; default: throw new Exception("Bit fields cannot be of type " + fieldType); } if (type.Fields.Select(x => x.ArraySizes.Single()).Sum() > sizeInBits) throw new Exception("Total size of bit field cannot be greater than underlying type"); return new TypeDesc(fieldType); }
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 StructDeclaration(TypeDesc type, TextBlock container, AccessModifierType accessModifier) : this(type, container, accessModifier, true) { }
public unsafe ParameterDesc(ELEMDESC elemdesc, string name, Func<uint, TypeDesc> typeFactory, VARFLAGS flags, bool hasValue, object value) { Name = EscapeParameterName(name); SpecialFlag = flags.HasFlag(VARFLAGS.VARFLAG_FREPLACEABLE); Hidden = flags.HasFlag(VARFLAGS.VARFLAG_FHIDDEN); In = elemdesc.__union1.paramdesc.wParamFlags.HasFlag(PARAMFLAGS.PARAMFLAG_FIN); Out = elemdesc.__union1.paramdesc.wParamFlags.HasFlag(PARAMFLAGS.PARAMFLAG_FOUT); Optional = elemdesc.__union1.paramdesc.wParamFlags.HasFlag(PARAMFLAGS.PARAMFLAG_FOPT); RetVal = elemdesc.__union1.paramdesc.wParamFlags.HasFlag(PARAMFLAGS.PARAMFLAG_FRETVAL); HasValue = hasValue; Value = value; this.typeFactory = typeFactory; TYPEDESC typedesc = elemdesc.tdesc; while ((typedesc.vt & VARENUM.VT_TYPEMASK) == VARENUM.VT_PTR) { if ((typedesc.vt & ~VARENUM.VT_TYPEMASK) != VARENUM.VT_EMPTY) throw new Exception("Variant type " + typedesc.vt + " is not supported"); IndirectionLevel++; typedesc = *typedesc.__union1.lptdesc; } List<int> arraySizes = new List<int>(); if ((typedesc.vt & VARENUM.VT_TYPEMASK) == VARENUM.VT_CARRAY) { SAFEARRAYBOUND* boundsPtr = &typedesc.__union1.lpadesc->rgbounds; for (int i = 0; i < typedesc.__union1.lpadesc->cDims; i++) arraySizes.Add((int)boundsPtr[i].cElements - boundsPtr[i].lBound); typedesc = typedesc.__union1.lpadesc->tdescElem; } ArraySizes = arraySizes.AsReadOnly(); if ((typedesc.vt & ~VARENUM.VT_TYPEMASK) != VARENUM.VT_EMPTY) throw new Exception("Variant type " + typedesc.vt + " is not supported"); if (elemdesc.__union1.paramdesc.wParamFlags.HasFlag(PARAMFLAGS.PARAMFLAG_FHASDEFAULT)) { if (hasValue) throw new Exception("Explicit value cannot be set if default value is present"); if (elemdesc.__union1.paramdesc.pparamdescex == null) throw new Exception("Value address is null"); Value = Marshal.GetObjectForNativeVariant(new IntPtr(&elemdesc.__union1.paramdesc.pparamdescex->varDefaultValue)); HasValue = true; } switch (typedesc.vt & VARENUM.VT_TYPEMASK) { case VARENUM.VT_VARIANT: type = new TypeDesc("VARIANT", TypeKind.Struct); break; case VARENUM.VT_SAFEARRAY: type = new TypeDesc("SAFEARRAY", TypeKind.Struct); break; case VARENUM.VT_USERDEFINED: typeReference = typedesc.__union1.hreftype; break; case VARENUM.VT_UNKNOWN: type = new TypeDesc("IUnknown", TypeKind.Interface); IndirectionLevel++; break; case VARENUM.VT_DISPATCH: type = new TypeDesc("IDispatch", TypeKind.Interface); IndirectionLevel++; break; case VARENUM.VT_ERROR: case VARENUM.VT_HRESULT: type = new TypeDesc("HRESULT"); break; case VARENUM.VT_LPSTR: type = new TypeDesc("sbyte"); IndirectionLevel++; break; case VARENUM.VT_BSTR: case VARENUM.VT_LPWSTR: type = new TypeDesc("char"); IndirectionLevel++; break; case VARENUM.VT_NULL: type = new TypeDesc("void"); break; case VARENUM.VT_BOOL: type = new TypeDesc("bool"); break; case VARENUM.VT_I1: type = new TypeDesc("sbyte"); break; case VARENUM.VT_I2: type = new TypeDesc("short"); break; case VARENUM.VT_I4: case VARENUM.VT_INT: type = new TypeDesc("int"); break; case VARENUM.VT_I8: case VARENUM.VT_FILETIME: case VARENUM.VT_CY: type = new TypeDesc("long"); break; case VARENUM.VT_UI1: type = new TypeDesc("byte"); break; case VARENUM.VT_UI2: type = new TypeDesc("ushort"); break; case VARENUM.VT_UI4: case VARENUM.VT_UINT: type = new TypeDesc("uint"); break; case VARENUM.VT_UI8: type = new TypeDesc("ulong"); break; case VARENUM.VT_R4: type = new TypeDesc("float"); break; case VARENUM.VT_R8: case VARENUM.VT_DATE: type = new TypeDesc("double"); break; case VARENUM.VT_DECIMAL: type = new TypeDesc("decimal"); break; case VARENUM.VT_VOID: type = new TypeDesc("void"); break; case VARENUM.VT_INT_PTR: case VARENUM.VT_UINT_PTR: type = new TypeDesc("void"); IndirectionLevel++; break; default: throw new Exception("Variant type " + typedesc.vt + " is not supported"); } }
public ConstDeclaration(TypeDesc type, string name, BaseTypeDeclaration container, AccessModifierType accessModifier, object value) : base(container, 0) { WriteLine(() => accessModifier + " const " + type.Name + " " + name + " = " + GetValueInitializer(value) + ";"); }
public static bool IsUserImplemented(TypeDesc type) { return type.BaseTypes.Any(x => x.Name == "__UserImplemented"); }
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 static bool IsDelegate(TypeDesc type) { return type.BaseTypes.Any(x => x.Name == "__Delegate"); }
public static string GetInterfaceName(TypeDesc type) { if (IsUserImplemented(type)) return type.Name; else return "_" + type.Name.TrimStart('@'); }
private static bool IsInherited(MethodDesc method, TypeDesc type) { string[] array1 = new string[] { method.Name, method.ReturnValue.TypeDeclaration }.Union(method.Parameters.Select(x => x.TypeDeclaration)).ToArray(); foreach (MethodDesc currentMethod in type.Methods) { string[] array2 = new string[] { currentMethod.Name, currentMethod.ReturnValue.TypeDeclaration }.Union(currentMethod.Parameters.Select(x => x.TypeDeclaration)).ToArray(); if (array1.SequenceEqual(array2)) return false; } return true; }
public static bool IsWithoutIdentifier(TypeDesc type) { return type.BaseTypes.Any(x => x.Name == "__IgnoreIdentifier"); }
public FieldDeclaration(TypeDesc type, string name, BaseTypeDeclaration container, AccessModifierType accessModifier) : base(container, 0) { WriteLine(() => accessModifier + " " + type.Name + " " + name + ";"); }
public FieldDeclaration(TypeDesc type, string name, BaseTypeDeclaration container, AccessModifierType accessModifier, int arraySize) : base(container, 0) { WriteLine(() => accessModifier + " fixed " + type.Name + " " + name + "[" + arraySize + "];"); }
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(() => "}"); }
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 static bool IsSpecialInterface(TypeDesc type) { return (type.Name == "__UserImplemented") || (type.Name == "__IgnoreIdentifier") || (type.Name == "__Delegate") || IsDelegate(type); }