예제 #1
0
        public override void VisitClassDeclaration(ClassDeclarationSyntax node)
        {
            // generate empty ctor if necessary
              if (node.HasInitializableMembers(model) && !node.HasDefaultConstructor())
              {
            cb.AppendWithIndent(node.Identifier.ToString())
              .Append("::")
              .Append(node.Identifier.ToString())
              .AppendLine("() :");

            //cb.Indent(() =>
            //{
            //  AppendFieldInitializers(node);
            //});

            cb.AppendLine("{}").AppendLine();
              }

              // generate static initialization
              foreach (var fields in node.ChildNodes().OfType<FieldDeclarationSyntax>())
              {
            foreach (var v in fields.Declaration.Variables)
            {
              var z = (IFieldSymbol)model.GetDeclaredSymbol(v);
              if (z.IsStatic && v.Initializer != null)
              {
            cb.AppendWithIndent("extern ")
              .Append(node.Identifier.ToString())
              .Append("::")
              .Append(v.Identifier.ToString())
              .Append(" = ")
              .Append(v.Initializer.Value.ToString())
              .AppendLine(";").AppendLine();
              }
            }
              }

              base.VisitClassDeclaration(node);
        }
예제 #2
0
        public override void VisitClassDeclaration(ClassDeclarationSyntax node)
        {
            // be sure to add the includes
              cb.AppendLine($"#include <{node.Identifier.Text}.h>")
            .AppendLine();

              // generate empty ctor if necessary
              if (node.HasInitializableMembers(model) && !node.HasDefaultConstructor())
              {
            cb.AppendWithIndent(node.Identifier.ToString())
              .Append("::")
              .Append(node.Identifier.ToString())
              .AppendLine("() :");

            //cb.Indent(() =>
            //{
            //  AppendFieldInitializers(node);
            //});

            cb.AppendLine("{}").AppendLine();
              }

              // generate static initialization
              foreach (var fields in node.ChildNodes().OfType<FieldDeclarationSyntax>())
              {
            foreach (var v in fields.Declaration.Variables)
            {
              var z = (IFieldSymbol)model.GetDeclaredSymbol(v);
              if (z.IsStatic && v.Initializer != null)
              {
            var cppType = z.Type.ToCppType();

            string initExpression;
            if (cppType.Contains("shared_ptr"))
            {
              var argumentList = new StringBuilder();

              var evcs = v.Initializer as EqualsValueClauseSyntax;
              if (evcs != null)
              {
                var oces = evcs.Value as ObjectCreationExpressionSyntax;
                if (oces != null)
                {
                  var count = oces.ArgumentList.Arguments.Count;
                  for (int i = 0; i < count; i++)
                  {
                    var a = oces.ArgumentList.Arguments[i];
                    argumentList.Append(a.ToFullString());
                    if (i + 1 != count)
                      argumentList.Append(",");
                  }
                }
              }

              initExpression = $"std::make_shared<{z.Type.Name}>({argumentList})";
            }
            else
            {
              initExpression = v.Initializer.Value.ToString();
            }

            cb.AppendWithIndent("extern ")
              .Append(node.Identifier.ToString())
              .Append("::")
              .Append(v.Identifier.ToString())
              .Append(" = ")
              .Append(initExpression)
              .AppendLine(";").AppendLine();
              }
            }
              }

              base.VisitClassDeclaration(node);
        }