예제 #1
0
 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());");
 }
예제 #2
0
        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;");
        }
예제 #3
0
        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);
        }
예제 #4
0
        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);
        }
예제 #5
0
 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;");
 }
예제 #7
0
 public void PushBlock(Block block)
 {
     block.Parent = ActiveBlock;
     ActiveBlock.AddBlock(block);
     ActiveBlock = block;
 }
예제 #8
0
 public void PushBlock(int kind, Declaration decl = null)
 {
     var block = new Block { Kind = kind, Declaration = decl };
     PushBlock(block);
 }
예제 #9
0
 public void AddBlock(Block block)
 {
     ActiveBlock.AddBlock(block);
 }
예제 #10
0
 protected Template(BindingContext context, IEnumerable<TranslationUnit> units)
 {
     Context = context;
     TranslationUnits = new List<TranslationUnit>(units);
     RootBlock = new Block();
     ActiveBlock = RootBlock;
 }
예제 #11
0
 protected Template(Driver driver, TranslationUnit unit)
 {
     Driver = driver;
     Options = driver.Options;
     TranslationUnit = unit;
     RootBlock = new Block();
     ActiveBlock = RootBlock;
 }
예제 #12
0
 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");
     }
 }
예제 #13
0
 protected Template(Driver driver, IEnumerable<TranslationUnit> units)
 {
     Driver = driver;
     Options = driver.Options;
     TranslationUnits = new List<TranslationUnit>(units);
     RootBlock = new Block();
     ActiveBlock = RootBlock;
 }
예제 #14
0
파일: Flood.cs 프로젝트: chartly/flood
        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);
        }
예제 #15
0
파일: Flood.cs 프로젝트: chartly/flood
        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);
        }
예제 #16
0
파일: Flood.cs 프로젝트: chartly/flood
        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);
        }