Example #1
0
        private void WriteToStringEntry(ToStringEntry toStringEntry)
        {
            if (toStringEntry.Text != null && toStringEntry.Variable == null)
            {
                this.WriteLiteralTextAppend(toStringEntry.Text);
                return;
            }

            this.WriteToStringEntryRecursive(toStringEntry, _model.GetTypeForMember(toStringEntry.Variable).Kind, "this." + toStringEntry.Variable.CSharpName, toStringEntry.Variable.Rank);
        }
Example #2
0
        public override DataModelType ToImmutable()
        {
            bool isEnum = this.SerializedValues != null &&
                          this.SerializedValues.Count > 0;

            return(new DataModelType(
                       this.RootObject,
                       this.SummaryText,
                       this.RemarksText,
                       this.G4DeclaredName,
                       this.CSharpName,
                       this.InterfaceName,
                       this.Members.ToImmutable(),
                       this.SerializedValues.ToImmutableArray <string>(),
                       this.G4DeclaredValues.ToImmutableArray <string>(),
                       ToStringEntry.Coalesce(this.ToStringEntries),
                       this.Base,
                       isEnum ? DataModelTypeKind.Enum : DataModelTypeKind.Leaf
                       ));
        }
Example #3
0
        private void WriteToStringEntryRecursive(ToStringEntry toStringEntry, DataModelTypeKind memberTypeKind, string sourceVariable, int ranksRemaining)
        {
            if (ranksRemaining == 0)
            {
                switch (memberTypeKind)
                {
                case DataModelTypeKind.Leaf:
                case DataModelTypeKind.Base:
                case DataModelTypeKind.BuiltInUri:
                case DataModelTypeKind.BuiltInVersion:
                    _codeWriter.WriteLine("sb.Append({0} == null ? \"{1}(null)\" : {0}.ToString());", sourceVariable, toStringEntry.Variable.CSharpName);
                    break;

                case DataModelTypeKind.BuiltInString:
                    _codeWriter.WriteLine("sb.Append({0} == null ? \"{1}(null)\" : {0});", sourceVariable, toStringEntry.Variable.CSharpName);
                    break;

                case DataModelTypeKind.BuiltInNumber:
                case DataModelTypeKind.BuiltInBoolean:
                    _codeWriter.WriteLine("sb.Append({0}.ToString(CultureInfo.InvariantCulture));", sourceVariable);
                    break;

                case DataModelTypeKind.BuiltInDictionary:
                    string value = _valueNamer.MakeName();
                    _codeWriter.OpenBrace("if (this.{0} == null)", toStringEntry.Variable.CSharpName);
                    _codeWriter.WriteLine("sb.Append(\"{0}(null)\");", toStringEntry.Variable.CSharpName);
                    _codeWriter.CloseBrace();
                    _codeWriter.OpenBrace("else");
                    _codeWriter.WriteLine("sb.Append('{');");
                    _codeWriter.OpenBrace("using (var {0} = this.{1}.GetEnumerator())", value, toStringEntry.Variable.CSharpName);
                    _codeWriter.OpenBrace("if ({0}.MoveNext())", value);
                    _codeWriter.WriteLine("sb.Append(\"{{\" + {0}.Current.Key + \", \" + {0}.Current.Value + \"}}\");", value);
                    _codeWriter.OpenBrace("while ({0}.MoveNext())", value);
                    _codeWriter.WriteLine("sb.Append(\", {{\" + {0}.Current.Key + \", \" + {0}.Current.Value + \"}}\");", value);
                    _codeWriter.CloseBrace();     // while
                    _codeWriter.CloseBrace();     // if
                    _codeWriter.CloseBrace();     // using
                    _codeWriter.WriteLine("sb.Append('}');");
                    _codeWriter.CloseBrace();     // else
                    break;

                default:
                    Debug.Fail("Bad DataModelTypeKind " + _writtenType.Kind);
                    break;
                }
            }
            else
            {
                string indexVariable      = _indexNamer.MakeName();
                string valueVariable      = _valueNamer.MakeName();
                string skipDelimiterLabel = _labelNamer.MakeName();
                string loopHeadLabel      = _labelNamer.MakeName();

                // This code makes the:
                // Write(0)
                // for (i = 1 -> N) {
                //   Delimeter();
                //   Write(i);
                // }
                // pattern; the "goto" mess is to avoid n^2 code size explosion due to repeating the "Write" call.
                // (If this were not generated code it would make sense to stamp out separate functions)
                //
                // if (source.Count != 0) {
                //   int index = 0;
                //   goto skipDelimeter;
                // loopHead:
                //   WriteDelimeter();
                // skipDelimeter:
                //   Write();
                //   ++index;
                //   if (index < source.Count) {
                //     goto loopHead;
                //   }
                // }
                //


                _codeWriter.OpenBrace("if ({0}.Count != 0)", sourceVariable);
                _codeWriter.WriteLine("int {0} = 0;", indexVariable);
                _codeWriter.WriteLine("goto {0};", skipDelimiterLabel);
                _codeWriter.WriteLine(loopHeadLabel + ":");
                this.WriteLiteralTextAppend(toStringEntry.Text ?? ", ");
                _codeWriter.WriteLine(skipDelimiterLabel + ":");
                _codeWriter.WriteLine("var {0} = {1}[{2}];", valueVariable, sourceVariable, indexVariable);
                this.WriteToStringEntryRecursive(toStringEntry, memberTypeKind, valueVariable, ranksRemaining - 1);
                _codeWriter.WriteLine("++{0};", indexVariable);
                _codeWriter.OpenBrace("if ({0} < {1}.Count)", indexVariable, sourceVariable);
                _codeWriter.WriteLine("goto {0};", loopHeadLabel);
                _codeWriter.CloseBrace(); // if (index < source.Count)
                _codeWriter.CloseBrace(); // if (count != 0)
                _codeWriter.WriteLine();
            }
        }