void GenerateToString(Class @class, Block block, Method method) { needsStreamInclude = true; block.WriteLine("std::ostringstream os;"); block.WriteLine("os << *NativePtr;"); block.Write("return clix::marshalString<clix::E_UTF8>(os.str());"); }
void GenerateEquals(Class @class, Block block, Method method) { var cliTypePrinter = new CLITypePrinter(Driver); var classCliType = @class.Visit(cliTypePrinter); block.WriteLine("if (!object) return false;"); block.WriteLine("auto obj = dynamic_cast<{0}>({1});", classCliType, method.Parameters[0].Name); block.NewLine(); block.WriteLine("if (!obj) return false;"); block.Write("return __Instance == obj->__Instance;"); }
public void AddBlock(Block block) { if (Text.StringBuilder.Length != 0 || hasIndentChanged) { hasIndentChanged = false; var newBlock = new Block { Text = Text.Clone(), isSubBlock = true }; Text.StringBuilder.Clear(); AddBlock(newBlock); } block.Parent = this; Blocks.Add(block); }
private void VisitMethod(Method method, Block block) { if (!method.IsSynthetized) return; var @class = (Class)method.Namespace; if (method.Name == "GetHashCode" && method.Parameters.Count == 0) GenerateGetHashCode(block); if (method.Name == "Equals" && method.Parameters.Count == 1) GenerateEquals(@class, block, method); if (method.Name == "ToString" && method.Parameters.Count == 0) GenerateToString(@class, block, method); }
private void OnUnitGenerated(GeneratorOutput generatorOutput) { var blocks = (from template in generatorOutput.Templates from block in template.FindBlocks(CSharpBlockKind.Method) where this.events.Contains(block.Declaration) select block).ToList(); foreach (var block in blocks) { var method = (Function) block.Declaration; var @event = char.ToUpperInvariant(method.OriginalName[0]) + method.OriginalName.Substring(1); var blockIndex = block.Parent.Blocks.IndexOf(block); var eventBlock = new Block(CSharpBlockKind.Event); eventBlock.WriteLine("public event global::System.Action<object, {0}> {1};", method.Parameters[0].Type, @event); eventBlock.NewLine(); const string eventHandler = @"__eventHandler"; var raiseEvent = string.Format( @" var {0} = {1}; if ({0} != null) {0}(this, {2}); ", eventHandler, @event, method.Parameters[0].Name); if (block.Blocks.Count > 0 && block.Blocks[0].Kind == BlockKind.BlockComment) { eventBlock.Blocks.Add(block.Blocks[0]); } block.Parent.Blocks.Insert(blockIndex, eventBlock); var stringBuilder = block.Text.StringBuilder; if (method.OriginalReturnType.Type.IsPrimitiveType(PrimitiveType.Void)) { stringBuilder.Insert(stringBuilder.Length - 1 - Environment.NewLine.Length, raiseEvent); } else { const string @return = " return "; stringBuilder.Replace(@return, raiseEvent + @return); } } }
void GenerateGetHashCode(Block block) { block.Write("return (int)NativePtr;"); }
public void PushBlock(Block block) { block.Parent = ActiveBlock; ActiveBlock.AddBlock(block); ActiveBlock = block; }
public void PushBlock(int kind, Declaration decl = null) { var block = new Block { Kind = kind, Declaration = decl }; PushBlock(block); }
public void AddBlock(Block block) { ActiveBlock.AddBlock(block); }
protected Template(BindingContext context, IEnumerable<TranslationUnit> units) { Context = context; TranslationUnits = new List<TranslationUnit>(units); RootBlock = new Block(); ActiveBlock = RootBlock; }
protected Template(Driver driver, TranslationUnit unit) { Driver = driver; Options = driver.Options; TranslationUnit = unit; RootBlock = new Block(); ActiveBlock = RootBlock; }
private void OnUnitGenerated(GeneratorOutput generatorOutput) { var blocks = (from template in generatorOutput.Templates from block in template.FindBlocks(CSharpBlockKind.Method) where this.events.Contains(block.Declaration) select block).ToList(); foreach (var block in blocks) { var method = (Function) block.Declaration; string @event; if (((Class) method.Namespace).Methods.Any(m => m != method && m.OriginalName == method.OriginalName)) { @event = method.OriginalName; } else { @event = char.ToUpperInvariant(method.OriginalName[0]) + method.OriginalName.Substring(1); } var blockIndex = block.Parent.Blocks.IndexOf(block); var eventBlock = new Block(CSharpBlockKind.Event); eventBlock.WriteLine("public event global::System.Action<object, {0}> {1};", method.Parameters[0].Type, @event); eventBlock.NewLine(); const string eventHandler = @"__eventHandler"; var raiseEvent = string.Format( @"var {0} = {1}; if ({0} != null) {0}(this, {2}); if ({2}.Handled) return{3}; ", eventHandler, @event, method.Parameters[0].Name, method.OriginalReturnType.Type.IsPrimitiveType(PrimitiveType.Void) ? string.Empty : " true"); if (block.Blocks.Count > 0 && block.Blocks[0].Kind == BlockKind.BlockComment) { eventBlock.Blocks.Add(block.Blocks[0]); } block.Parent.Blocks.Insert(blockIndex, eventBlock); block.Text.StringBuilder.Replace("var __slot", raiseEvent + " var __slot"); } }
protected Template(Driver driver, IEnumerable<TranslationUnit> units) { Driver = driver; Options = driver.Options; TranslationUnits = new List<TranslationUnit>(units); RootBlock = new Block(); ActiveBlock = RootBlock; }
void VisitMethodBody(Block block) { var method = block.Declaration as Method; var @class = (Class) method.Namespace; if (method.Name == "GetHashCode" && method.Parameters.Count == 0) GenerateGetHashCode(@class, block); if (method.Name == "Equals" && method.Parameters.Count == 1) GenerateEquals(@class, block, method); }
void GenerateGetHashCode(Class @class, Block block) { var hashcodeOverride = @class.Methods.FirstOrDefault(m => { var expansions = m.PreprocessedEntities.OfType<MacroExpansion>(); return expansions.Any(e => e.Text == "FLD_HASHCODE"); }); if (hashcodeOverride == null) return; var cppTypePrinter = new CppTypePrinter(Driver.TypeDatabase); var classCppType = @class.Visit(cppTypePrinter); block.Text.StringBuilder.Clear(); block.WriteLine("return (({0}*)NativePtr)->{1}();", classCppType, hashcodeOverride.OriginalName); }
void GenerateEquals(Class @class, Block block, Method method) { var equalsOverride = @class.Methods.FirstOrDefault(m => { var expansions = m.PreprocessedEntities.OfType<MacroExpansion>(); return expansions.Any(e => e.Text == "FLD_EQUALS"); }); if (equalsOverride == null) return; var cliTypePrinter = new CLITypePrinter(Driver); var cppTypePrinter = new CppTypePrinter(Driver.TypeDatabase); var paramCppType = equalsOverride.Parameters[0].Visit(cppTypePrinter); var classCppType = @class.Visit(cppTypePrinter); var classCliType = @class.Visit(cliTypePrinter); block.Text.StringBuilder.Clear(); block.WriteLine("if (!object) return false;"); block.WriteLine("auto obj = dynamic_cast<{0}>({1});", classCliType, method.Parameters[0].Name); block.NewLine(); block.WriteLine("if (!obj) return false;"); block.WriteLine("auto param = ({0})obj->NativePtr;", paramCppType); block.WriteLine("return (({0}*)NativePtr)->{1}(param);", classCppType, equalsOverride.OriginalName); }