private void WriteApplicationEventHandler(WindowsAccessBridgeModel model, SourceCodeWriter sourceWriter, EventDefinition definition) { sourceWriter.WriteIndent(); sourceWriter.Write("protected virtual "); WriteFunctionSignature(model, sourceWriter, definition.DelegateFunction, "On" + definition.Name); sourceWriter.Write(" {{"); sourceWriter.WriteLine(); sourceWriter.IncIndent(); sourceWriter.WriteLine("var handler = _{0};", ToPascalCase(definition)); sourceWriter.WriteLine("if (handler != null)"); sourceWriter.IncIndent(); sourceWriter.WriteIndent(); sourceWriter.Write("handler("); var first = true; foreach (var p in definition.DelegateFunction.Parameters) { if (first) { first = false; } else { sourceWriter.Write(", "); } sourceWriter.Write("{0}", p.Name); } sourceWriter.Write(");"); sourceWriter.WriteLine(); sourceWriter.DecIndent(); sourceWriter.DecIndent(); sourceWriter.WriteLine("}}"); }
private static void WriteSourceFileHeader(SourceCodeWriter sourceWriter) { sourceWriter.WriteLine("// Copyright 2016 Google Inc. All Rights Reserved."); sourceWriter.WriteLine("// "); sourceWriter.WriteLine("// Licensed under the Apache License, Version 2.0 (the \"License\");"); sourceWriter.WriteLine("// you may not use this file except in compliance with the License."); sourceWriter.WriteLine("// You may obtain a copy of the License at"); sourceWriter.WriteLine("// "); sourceWriter.WriteLine("// http://www.apache.org/licenses/LICENSE-2.0"); sourceWriter.WriteLine("// "); sourceWriter.WriteLine("// Unless required by applicable law or agreed to in writing, software"); sourceWriter.WriteLine("// distributed under the License is distributed on an \"AS IS\" BASIS,"); sourceWriter.WriteLine("// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied."); sourceWriter.WriteLine("// See the License for the specific language governing permissions and"); sourceWriter.WriteLine("// limitations under the License."); sourceWriter.WriteLine(); sourceWriter.WriteLine("// ReSharper disable InconsistentNaming"); sourceWriter.WriteLine("// ReSharper disable DelegateSubtraction"); sourceWriter.WriteLine("// ReSharper disable UseObjectOrCollectionInitializer"); sourceWriter.WriteLine("// ReSharper disable UnusedParameter.Local"); sourceWriter.WriteLine("// ReSharper disable UnusedMember.Local"); sourceWriter.WriteLine("// ReSharper disable ConvertIfStatementToConditionalTernaryExpression"); sourceWriter.AddUsing("System"); sourceWriter.AddUsing("System.Runtime.InteropServices"); sourceWriter.AddUsing("System.Text"); sourceWriter.AddUsing("WindowHandle = System.IntPtr"); sourceWriter.AddUsing("BOOL = System.Int32"); sourceWriter.WriteLine(); }
private void WriteApplicationEventProperty(SourceCodeWriter sourceWriter, EventDefinition definition) { sourceWriter.WriteLine("public override event {0}EventHandler {0} {{", definition.Name); sourceWriter.IncIndent(); sourceWriter.WriteLine("add {{"); sourceWriter.IncIndent(); sourceWriter.WriteLine("if (_{0} == null) {{", ToPascalCase(definition)); sourceWriter.IncIndent(); sourceWriter.WriteLine("_forward{0}KeepAlive = Forward{0};", definition.Name); sourceWriter.WriteLine("EntryPoints.Set{0}(_forward{0}KeepAlive);", definition.Name); sourceWriter.DecIndent(); sourceWriter.WriteLine("}}"); //sourceWriter.DecIndent(); sourceWriter.WriteLine("_{0} += value;", ToPascalCase(definition)); sourceWriter.DecIndent(); sourceWriter.WriteLine("}}"); sourceWriter.WriteLine("remove{{"); sourceWriter.IncIndent(); sourceWriter.WriteLine("_{0} -= value;", ToPascalCase(definition)); sourceWriter.WriteLine("if (_{0} == null) {{", ToPascalCase(definition)); sourceWriter.IncIndent(); sourceWriter.WriteLine("EntryPoints.Set{0}(null);", definition.Name); sourceWriter.WriteLine("_forward{0}KeepAlive = null;", definition.Name); sourceWriter.DecIndent(); sourceWriter.WriteLine("}}"); sourceWriter.DecIndent(); sourceWriter.WriteLine("}}"); sourceWriter.DecIndent(); sourceWriter.WriteLine("}}"); }
private void WriteLibraryClass(WindowsAccessBridgeModel model, SourceCodeWriter sourceWriter, ClassDefinition definition) { WriteStructLayoutAttributeLine(sourceWriter); sourceWriter.WriteLine("internal class {0} {{", sourceWriter.GetTypeName(definition.Name)); WriteFields(model, sourceWriter, definition); sourceWriter.WriteLine("}}"); }
private void WriteLibraryEventDelegateType(SourceCodeWriter sourceWriter, EventDefinition definition) { sourceWriter.WriteIndent(); sourceWriter.Write("public delegate BOOL Set{0}FP({0}EventHandler handler)", definition.Name); sourceWriter.Write(";"); sourceWriter.WriteLine(); }
private void WriteParameter(WindowsAccessBridgeModel model, SourceCodeWriter sourceWriter, ParameterDefinition parameterDefinition) { if (parameterDefinition.IsOutAttribute) { if (sourceWriter.IsNativeTypes || !model.IsClass(parameterDefinition.Type)) { sourceWriter.Write("[Out]"); } } if (parameterDefinition.IsOut) { sourceWriter.Write("out "); } else if (parameterDefinition.IsOutAttribute && model.IsClass(parameterDefinition.Type) && !sourceWriter.IsNativeTypes) { sourceWriter.Write("out "); } else if (parameterDefinition.IsRef) { sourceWriter.Write("ref "); } if (sourceWriter.IsNativeTypes) { sourceWriter.WriteMashalAs(parameterDefinition.MarshalAs); } sourceWriter.WriteType(parameterDefinition.Type); sourceWriter.Write(" "); sourceWriter.Write(parameterDefinition.Name); }
private void WriteApplicationStructs(WindowsAccessBridgeModel model, SourceCodeWriter sourceWriter, StreamWriter writer) { sourceWriter.IsNativeTypes = false; model.Structs.ForEach(x => { WriteApplicationStruct(model, sourceWriter, x); writer.WriteLine(); }); }
private void WriteLibrayrFunctionsDelegate(WindowsAccessBridgeModel model, SourceCodeWriter sourceWriter, FunctionDefinition function) { sourceWriter.WriteLine("[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]"); sourceWriter.WriteIndent(); sourceWriter.Write("public delegate "); WriteFunctionSignature(model, sourceWriter, function, string.Format("{0}FP", function.Name)); sourceWriter.Write(";"); sourceWriter.WriteLine(); }
private void WriteApplicationFunction(WindowsAccessBridgeModel model, SourceCodeWriter sourceWriter, FunctionDefinition definition) { WriteXmlDocDefinition(sourceWriter, definition.XmlDocDefinition); sourceWriter.WriteIndent(); sourceWriter.Write("public abstract "); WriteFunctionSignature(model, sourceWriter, definition); sourceWriter.Write(";"); sourceWriter.WriteLine(); }
private void WriteApplicationClass(WindowsAccessBridgeModel model, SourceCodeWriter sourceWriter, ClassDefinition definition) { if (!model.ClassNeedsWrapper(definition)) { WriteStructLayoutAttributeLine(sourceWriter); } sourceWriter.WriteLine("public class {0} {{", sourceWriter.GetTypeName(definition.Name)); WriteFields(model, sourceWriter, definition); sourceWriter.WriteLine("}}"); }
private void WriteEntryPointsClass(WindowsAccessBridgeModel model, SourceCodeWriter sourceWriter) { sourceWriter.IsNativeTypes = true; sourceWriter.WriteLine("/// <summary>"); sourceWriter.WriteLine("/// Container of {0} WindowAccessBridge DLL entry points", GetLegacySuffix(sourceWriter)); sourceWriter.WriteLine("/// </summary>"); sourceWriter.WriteLine("internal class AccessBridgeEntryPoints{0} {{", GetLegacySuffix(sourceWriter)); sourceWriter.IncIndent(); sourceWriter.WriteLine("#region Functions"); foreach (var function in model.Functions) { WriteLibraryFunctionProperty(sourceWriter, function); } sourceWriter.WriteLine("#endregion"); sourceWriter.WriteLine(); sourceWriter.WriteLine("#region Event functions"); foreach (var eventDefinition in model.Events) { WriteLibraryEventProperty(sourceWriter, eventDefinition); } sourceWriter.WriteLine("#endregion"); sourceWriter.WriteLine(); sourceWriter.WriteLine("#region Function delegate types"); foreach (var function in model.Functions) { WriteLibrayrFunctionsDelegate(model, sourceWriter, function); } sourceWriter.WriteLine("#endregion"); sourceWriter.WriteLine(); sourceWriter.WriteLine("#region Event delegate types"); foreach (var definition in model.Events) { sourceWriter.WriteLine("[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]"); WriteDelegateType(model, sourceWriter, definition.DelegateFunction); } sourceWriter.WriteLine("#endregion"); sourceWriter.WriteLine(); sourceWriter.WriteLine("#region Event function delegate types"); foreach (var eventDefinition in model.Events) { sourceWriter.WriteLine("[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]"); WriteLibraryEventDelegateType(sourceWriter, eventDefinition); } sourceWriter.WriteLine("#endregion"); sourceWriter.DecIndent(); sourceWriter.WriteLine("}}"); sourceWriter.WriteLine(); }
private void WriteLibraryStructs(WindowsAccessBridgeModel model, SourceCodeWriter sourceWriter, StreamWriter writer) { sourceWriter.IsNativeTypes = true; model.Structs.ForEach(x => { if (model.StructNeedsWrapper(x)) { WriteLibraryStruct(model, sourceWriter, x); writer.WriteLine(); } }); }
// ReSharper disable once UnusedParameter.Local private void WriteEnumMembers(WindowsAccessBridgeModel model, SourceCodeWriter sourceWriter, EnumDefinition definition) { sourceWriter.IncIndent(); definition.Members.ForEach(x => { sourceWriter.WriteIndent(); sourceWriter.Write("{0}", x.Name); if (!string.IsNullOrEmpty(x.Value)) { sourceWriter.Write(" = {0}", x.Value); } sourceWriter.Write(","); sourceWriter.WriteLine(); }); sourceWriter.DecIndent(); }
private void WriteApplicationFunctionsClass(WindowsAccessBridgeModel model, SourceCodeWriter sourceWriter) { sourceWriter.IsNativeTypes = false; sourceWriter.WriteLine("/// <summary>"); sourceWriter.WriteLine("/// Implementation of <see cref=\"AccessBridgeFunctions\"/> using <code>WindowsAccessBridge DLL</code>", GetLegacySuffix(sourceWriter)); sourceWriter.WriteLine("/// entry points implemented in <see cref=\"AccessBridgeEntryPoints{0}\"/>", GetLegacySuffix(sourceWriter)); sourceWriter.WriteLine("/// </summary>"); sourceWriter.WriteLine("internal partial class AccessBridgeNativeFunctions{0} : AccessBridgeFunctions {{", GetLegacySuffix(sourceWriter)); sourceWriter.IncIndent(); sourceWriter.WriteLine(); sourceWriter.WriteLine("#region Function implementations"); sourceWriter.WriteLine(); foreach (var function in model.Functions) { WriteApplicationFunctionImplementation(model, sourceWriter, function); } sourceWriter.WriteLine("#endregion"); sourceWriter.WriteLine(); sourceWriter.WriteLine("#region Wrap/Unwrap structs"); sourceWriter.WriteLine(); foreach (var definition in model.Structs) { if (model.StructNeedsWrapper(definition)) { WriteApplicationWrapStructFunctions(model, sourceWriter, definition); } } sourceWriter.WriteLine("#endregion"); sourceWriter.WriteLine(); sourceWriter.WriteLine("#region CopyWrap/CopyUnwrap classes"); sourceWriter.WriteLine(); foreach (var definition in model.Classes) { if (model.ClassNeedsWrapper(definition)) { WriteApplicationCopyClassFunctions(model, sourceWriter, definition); } } sourceWriter.WriteLine("#endregion"); sourceWriter.WriteLine(); sourceWriter.DecIndent(); sourceWriter.WriteLine("}}"); sourceWriter.WriteLine(); }
private void WriteCopyField(WindowsAccessBridgeModel model, SourceCodeWriter sourceWriter, string infosrc, string infodest, string fieldName, TypeReference type) { if (model.IsStruct(type)) { sourceWriter.WriteLine("{0}{1} = {4}(vmid, {2}{3});", infodest, fieldName, infosrc, fieldName, sourceWriter.IsNativeTypes ? "Wrap" : "Unwrap"); } else if (model.IsClass(type)) { sourceWriter.WriteLine("{4}(vmid, {0}{1}, {2}{3});", infosrc, fieldName, infodest, fieldName, sourceWriter.IsNativeTypes ? "CopyWrap" : "CopyUnwrap"); } else if (IsJavaObjectHandle(type)) { sourceWriter.WriteLine("{0}{1} = {4}(vmid, {2}{3});", infodest, fieldName, infosrc, fieldName, sourceWriter.IsNativeTypes ? "Wrap" : "Unwrap"); } else if (type is ArrayTypeReference) { var elementType = ((ArrayTypeReference)type).ElementType; var elementCountFiedName = ((ArrayTypeReference)type).ElementCountAttribute == null ? null : ((ArrayTypeReference)type).ElementCountAttribute.FieldName; sourceWriter.WriteLine("if ({0}{1} != null) {{", infosrc, fieldName); sourceWriter.IncIndent(); if (elementCountFiedName == null) { sourceWriter.WriteLine("var count = {0}{1}.Length;", infosrc, fieldName); } else { sourceWriter.WriteLine("var count = {0}.{1};", infosrc, elementCountFiedName); } sourceWriter.IsNativeTypes = !sourceWriter.IsNativeTypes; sourceWriter.WriteLine("{0}{1} = new {2}[count];", infodest, fieldName, sourceWriter.GetTypeName(elementType)); sourceWriter.IsNativeTypes = !sourceWriter.IsNativeTypes; sourceWriter.WriteLine("for(var i = 0; i < count; i++) {{"); sourceWriter.IncIndent(); WriteCopyField(model, sourceWriter, infosrc + fieldName, infodest + fieldName, "[i]", elementType); sourceWriter.DecIndent(); sourceWriter.WriteLine("}}"); sourceWriter.DecIndent(); sourceWriter.WriteLine("}}"); } else { sourceWriter.WriteLine("{0}{1} = {2}{3};", infodest, fieldName, infosrc, fieldName); } }
private void WriteDelegateType(WindowsAccessBridgeModel model, SourceCodeWriter sourceWriter, FunctionDefinition definition) { if (sourceWriter.IsNativeTypes) { if (definition.MarshalAs != null) { sourceWriter.WriteIndent(); sourceWriter.WriteMashalAs(definition.MarshalAs); sourceWriter.WriteLine(); } } sourceWriter.WriteIndent(); sourceWriter.Write("public delegate "); WriteFunctionSignature(model, sourceWriter, definition); sourceWriter.Write(";"); sourceWriter.WriteLine(); }
private static void WriteXmlDocDefinition(SourceCodeWriter sourceWriter, XmlDocDefinition def) { if (def != null && def.Summary != null) { var lines = def.Summary.Split('\n'); foreach (var line in lines) { sourceWriter.WriteIndent(); sourceWriter.Write("/// "); // Hack: Replace XML DOC references from CodeGen assembly to relative to source references. var lineText = line.Replace("<see cref=\"T:CodeGen.Interop.NativeStructures.", "<see cref=\""); lineText = lineText.Trim(); sourceWriter.Write(lineText); sourceWriter.WriteLine(); } } }
private void WriteEnum(WindowsAccessBridgeModel model, SourceCodeWriter sourceWriter, EnumDefinition definition) { if (definition.IsFlags) { sourceWriter.WriteLine("[Flags]"); } sourceWriter.WriteIndent(); sourceWriter.Write("public enum {0}", definition.Name); if (definition.Type.Name != "int") { sourceWriter.Write(" : "); sourceWriter.WriteType(definition.Type); } sourceWriter.Write(" {{"); sourceWriter.WriteLine(); WriteEnumMembers(model, sourceWriter, definition); sourceWriter.WriteLine("}}"); }
private void WriteField(WindowsAccessBridgeModel model, SourceCodeWriter sourceWriter, TypeDefinition container, FieldDefinition definition) { if (sourceWriter.IsNativeTypes || !model.TypeDefinitionNeedsWrapper(container)) { if (definition.MarshalAs != null) { sourceWriter.WriteIndent(); sourceWriter.WriteMashalAs(definition.MarshalAs); sourceWriter.WriteLine(); } } sourceWriter.WriteIndent(); sourceWriter.Write("public "); sourceWriter.WriteType(definition.Type); sourceWriter.Write(" "); sourceWriter.Write("{0};", definition.Name); sourceWriter.WriteLine(); }
private void WriteApplicationEventsInterface(WindowsAccessBridgeModel model, SourceCodeWriter sourceWriter) { sourceWriter.IsNativeTypes = false; sourceWriter.WriteLine("/// <summary>"); sourceWriter.WriteLine("/// Common (i.e. legacy and non-legacy) abstraction over <code>WindowsAccessBridge DLL</code> events"); sourceWriter.WriteLine("/// </summary>"); sourceWriter.WriteLine("public abstract class AccessBridgeEvents : IDisposable {{"); sourceWriter.IncIndent(); foreach (var eventDefinition in model.Events) { WriteApplicationEvent(sourceWriter, eventDefinition); } sourceWriter.WriteLine(); sourceWriter.WriteLine("public abstract void Dispose();"); sourceWriter.DecIndent(); sourceWriter.WriteLine("}}"); sourceWriter.WriteLine(); }
private void WriteFunctionSignature(WindowsAccessBridgeModel model, SourceCodeWriter sourceWriter, FunctionDefinition definition, string name) { sourceWriter.WriteType(definition.ReturnType); sourceWriter.Write(" "); sourceWriter.Write(name); sourceWriter.Write("("); bool first = true; foreach (var p in definition.Parameters) { if (first) { first = false; } else { sourceWriter.Write(", "); } WriteParameter(model, sourceWriter, p); } sourceWriter.Write(")"); }
private void WriteApplicationEventForwarder(WindowsAccessBridgeModel model, SourceCodeWriter sourceWriter, EventDefinition definition) { sourceWriter.IsNativeTypes = true; sourceWriter.WriteIndent(); sourceWriter.Write("private "); WriteFunctionSignature(model, sourceWriter, definition.DelegateFunction, "Forward" + definition.Name); sourceWriter.Write(" {{"); sourceWriter.WriteLine(); sourceWriter.IsNativeTypes = false; sourceWriter.IncIndent(); sourceWriter.WriteIndent(); sourceWriter.Write("On{0}(", definition.Name); var first = true; foreach (var p in definition.DelegateFunction.Parameters) { if (first) { first = false; } else { sourceWriter.Write(", "); } if (IsJavaObjectHandle(p.Type)) { sourceWriter.Write("Wrap(vmid, {0})", p.Name); } else { sourceWriter.Write("{0}", p.Name); } } sourceWriter.Write(");"); sourceWriter.WriteLine(); sourceWriter.DecIndent(); sourceWriter.WriteLine("}}"); }
private void WriteFile(WindowsAccessBridgeModel model) { using (var writer = File.CreateText(_options.PublicClassesOutputFileName)) { using (var sourceWriter = new SourceCodeWriter(writer, model)) { WriteSourceFileHeader(sourceWriter); sourceWriter.StartNamespace("WindowsAccessBridgeInterop"); WriteApplicationFunctionsInterface(model, sourceWriter); WriteApplicationEventsInterface(model, sourceWriter); WriteApplicationEventHandlerTypes(model, sourceWriter); WriteApplicationEnums(model, writer, sourceWriter); WriteApplicationStructs(model, sourceWriter, writer); WriteApplicationClasses(model, writer, sourceWriter); sourceWriter.EndNamespace(); } } foreach (var legacy in new[] { false, true }) { using (var writer = File.CreateText(legacy ? _options.InternalLegacyClassesOutputFileName : _options.InternalClassesOutputFileName)) { using (var sourceWriter = new SourceCodeWriter(writer, model)) { sourceWriter.IsLegacy = legacy; WriteSourceFileHeader(sourceWriter); sourceWriter.StartNamespace("WindowsAccessBridgeInterop"); WriteApplicationFunctionsClass(model, sourceWriter); WriteApplicationEventsClass(model, sourceWriter); sourceWriter.IsNativeTypes = true; WriteEntryPointsClass(model, sourceWriter); WriteLibraryStructs(model, sourceWriter, writer); WriteLibraryClasses(model, writer, sourceWriter); sourceWriter.EndNamespace(); } } } }
private void WriteCopyFields(WindowsAccessBridgeModel model, SourceCodeWriter sourceWriter, string infosrc, string infodest, TypeDefinition definition) { definition.Fields.ForEach(field => { WriteCopyField(model, sourceWriter, infosrc, infodest, "." + field.Name, field.Type); }); }
private void WriteApplicationCopyClassFunctions(WindowsAccessBridgeModel model, SourceCodeWriter sourceWriter, TypeDefinition definition) { sourceWriter.WriteIndent(); sourceWriter.Write("private void CopyWrap(int vmid, "); sourceWriter.IsNativeTypes = true; sourceWriter.WriteType(definition.Name); sourceWriter.Write(" infoSrc, "); sourceWriter.IsNativeTypes = false; sourceWriter.WriteType(definition.Name); sourceWriter.Write(" infoDest)"); sourceWriter.Write(" {{"); sourceWriter.WriteLine(); sourceWriter.IncIndent(); sourceWriter.IsNativeTypes = true; WriteCopyFields(model, sourceWriter, "infoSrc", "infoDest", definition); sourceWriter.DecIndent(); sourceWriter.WriteLine("}}"); sourceWriter.WriteLine(); sourceWriter.WriteIndent(); sourceWriter.Write("private void CopyUnwrap(int vmid, "); sourceWriter.IsNativeTypes = false; sourceWriter.WriteType(definition.Name); sourceWriter.Write(" infoSrc, "); sourceWriter.IsNativeTypes = true; sourceWriter.WriteType(definition.Name); sourceWriter.Write(" infoDest)"); sourceWriter.Write(" {{"); sourceWriter.WriteLine(); sourceWriter.IncIndent(); sourceWriter.IsNativeTypes = false; WriteCopyFields(model, sourceWriter, "infoSrc", "infoDest", definition); sourceWriter.DecIndent(); sourceWriter.WriteLine("}}"); sourceWriter.WriteLine(); }
private void WriteApplicationEventField(SourceCodeWriter sourceWriter, EventDefinition definition) { sourceWriter.WriteLine("private {0}EventHandler _{1};", definition.Name, ToPascalCase(definition)); }
private void WriteApplicationEventKeepAliveField(SourceCodeWriter sourceWriter, EventDefinition definition) { sourceWriter.WriteLine("private AccessBridgeEntryPoints{0}.{1}EventHandler _forward{1}KeepAlive;", GetLegacySuffix(sourceWriter), definition.Name); }
private void WriteLibraryEventProperty(SourceCodeWriter sourceWriter, EventDefinition definition) { sourceWriter.WriteLine("public Set{0}FP Set{0} {{ get; set; }}", definition.Name); }
private void WriteFunctionSignature(WindowsAccessBridgeModel model, SourceCodeWriter sourceWriter, FunctionDefinition definition) { WriteFunctionSignature(model, sourceWriter, definition, definition.Name); }
private void WriteLibraryFunctionProperty(SourceCodeWriter sourceWriter, FunctionDefinition function) { sourceWriter.WriteLine("public {0}FP {0} {{ get; set; }}", function.Name); }