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;"); }
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); } } }
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"); } }
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); }