Class: Divine::CsharpHelperMethods
- Inherits:
-
BabelHelperMethods
- Object
- BabelHelperMethods
- Divine::CsharpHelperMethods
- Defined in:
- lib/divine/code_generators/csharp.rb
Overview
-
C# Helper :
Support base function needed to build Divine enviroment and classes corresponding to DSL structs
Direct Known Subclasses
Instance Method Summary collapse
-
#csharp_base_class_template_str ⇒ Object
Generate the base Divine Csharp Class that contains the main methods: * serialize * serialize Internal * deserialize * Read Methods * Write Methods.
-
#csharp_class_template(sh) ⇒ Object
Generate C# Class that corresponding to the struct definition * Args : -
sh
-> Struct Name. -
#csharp_deserialize_internal(var, types) ⇒ Object
Generate the way of deserializing different DSL types * Args : -
var
-> variable name -types
-> variable type. -
#csharp_get_empty_declaration(types, is_reference_type = false) ⇒ Object
Generate default C# data types declaration values corresponding to each DSL types: * DSL Type –> Corresponding Default C# Value * dint63 –> 0 range -> [0 - 9,223,372,036,854,775,807] * 1 byte: range -> [0 - 127] * 2 bytes: range -> [0 - 16,383] * 3 bytes: range -> [0 - 2,097,151] * 4 bytes: range -> [0 - 268,435,455] * 5 bytes: range -> [0 - 34,359,738,367] * 6 bytes: range -> [0 - 4,398,046,511,103] * 7 bytes: range -> [0 - 562,949,953,421,311] * 8 bytes: range -> [0 - 72,057,594,037,927,935] * 9 bytes: range -> [0 - 9,223,372,036,854,775,807] * int8 –> 0 Range -> [0 - 255] * int16 –> 0 Range -> [0 - 65535] * int32 –> 0 Range -> [0 - 4.294.967.295] * sint32 –> 0 Range -> [-2.147.483.648 - 2.147.483.647] * sint64 –> 0 Range -> [-9.223.372.036.854.775.808, 9.223.372.036.854.775.807] * string –> “” * ip_number–> “” * binary –> new byte * short_binary –> new byte * list –> new List<type>() * map –> new Dictionary<keyType, valueType>().
-
#csharp_get_type_declaration(types, is_reference_type = false) ⇒ Object
Generate C# data types declaration corresponding to each DSL type * DSL Type –> Corresponding C# Type * int8 –> byte * int16 –> ushort * int32 –> uint * sint32 –> int * sint64 –> long * string –> string * ip_number–> string * binary –> byte[] * short_binary –> byte[] * list –> List<type> * map –> Dictionary<keyType, valueType>.
-
#csharp_serialize_internal(var, types) ⇒ Object
Generate the way of serializing different DSL types * Args : -
var
-> variable name -types
-> variable type. -
#get_header_comment ⇒ Object
Return the header comment.
Methods inherited from BabelHelperMethods
#camelize, #format_src, #get_fresh_variable_name, #get_header_comment_text, #sanity_check
Instance Method Details
#csharp_base_class_template_str ⇒ Object
Generate the base Divine Csharp Class that contains the main methods:
-
serialize
-
serialize Internal
-
deserialize
-
Read Methods
-
Write Methods
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 |
# File 'lib/divine/code_generators/csharp.rb', line 27 def csharp_base_class_template_str <<EOS namespace divine { public abstract class Divine { public byte[] serialize() { try { MemoryStream baos = new MemoryStream(); serializeInternal(baos); baos.Close(); return baos.ToArray(); } catch (System.IO.IOException ex) { throw ex; } } public abstract void serializeInternal(MemoryStream baos); public abstract void deserialize(MemoryStream baos); protected byte readInt8(MemoryStream data) { return (byte)(data.ReadByte() & 0xff); } protected ushort readInt16(MemoryStream data) { return (ushort)((data.ReadByte() << 8) | readInt8(data)); } protected int readInt24(MemoryStream data) { return (data.ReadByte() << 16) | readInt16(data); } protected uint readInt32(MemoryStream data) { uint x = (uint)data.ReadByte() << 24; uint y = (uint)readInt24(data); return x | y; } protected int readSint32(MemoryStream data) { return (data.ReadByte() << 24) | readInt24(data); } protected long readSint64(MemoryStream data) { return ((long)readInt32(data) << 32) | (readInt32(data) & 0xFFFFFFFF); } protected long readDint63(MemoryStream data) { int b = this.readInt8(data); long val = b & 0x7F; while ((b >> 7) == 1) { b = this.readInt8(data); val = val << 7; val = val | (uint)(b & 0x7F); } return val; } protected bool readBool(MemoryStream data) { return readInt8(data) == 1; } protected String readString(MemoryStream data) { // Force utf8 try { return System.Text.Encoding.UTF8.GetString(readBytes(readInt16(data), data)); } catch (System.IO.IOException ex) { throw ex; } } private byte[] readBytes(int size, MemoryStream data) { try { byte[] bs = new byte[size]; data.Read(bs, 0, size); return bs; } catch (System.IO.IOException ex) { throw ex; } } protected byte[] readBinary(MemoryStream data) { try { long c = readInt32(data); if (c > int.MaxValue) { throw new System.IndexOutOfRangeException("Binary data to big for csharp"); } return readBytes((int)c, data); } catch (System.IO.IOException ex) { throw ex; } } protected byte[] readShortBinary(MemoryStream data) { try { return readBytes(readInt8(data), data); } catch (System.IO.IOException ex) { throw ex; } } protected String readIpNumber(MemoryStream data) { try { byte[] ips = readShortBinary(data); if (ips.Length == 4) { return readIpv4Number(ips); } else { return readIpv6Number(ips); } } catch (System.IO.IOException ex) { throw ex; } } protected String readIpv4Number(byte[] ips) { String ip = ""; foreach (byte b in ips) { if (ip.Length > 0) { ip += "."; } ip += (b & 0xFF); } return ip; } protected String readIpv6Number(byte[] ips) { try { String ip = ""; int part1, part2; for (int i = 0; i < ips.Length; i += 2) { part1 = ips[i] & 0xFF; part2 = ips[i + 1] & 0xFF; ip += part1 == 0 ? "" : part1.ToString("X"); ip += (part1 == 0 && part2 == 0) ? "" : (part2 < 10 && part1 != 0 ? "0" + part2.ToString("X") : part2.ToString("X")); if (i < ips.Length - 2) { ip += ":"; } } ip = System.Text.RegularExpressions.Regex.Replace(ip, ":{3,}", "::"); return ip; } catch (System.IO.IOException ex) { throw ex; } } protected void writeInt8(byte v, MemoryStream output) { if (v > 0xFF) { // Max 255 raiseError("Too large int8 number: " + v); } else if (v < 0) { raiseError("a negative number passed to int8 number: " + v); } output.WriteByte((byte) v); } protected void writeInt16(ushort v, MemoryStream output) { if (v > 0xFFFF) { // Max 65.535 raiseError("Too large int16 number: " + v); } else if (v < 0) { raiseError("a negative number passed to int16 number: " + v); } writeInt8((byte)(v >> 8 & 0xFF), output); writeInt8((byte)(v & 0xFF), output); } protected void writeInt24(int v, MemoryStream output) { if (v > 0xFFFFFF) { // Max 16.777.215 raiseError("Too large int24 number: " + v); } else if (v < 0) { // In Case added to csharp declaration raiseError("a negative number passed to int24 number: " + v); } writeInt8((byte)(v >> 16 & 0xFF), output); writeInt16((ushort)(v & 0xFFFF), output); } protected void writeInt32(uint v, MemoryStream output) { if (v > 0xFFFFFFFF) { // Max 4.294.967.295 raiseError("Too large int32 number: " + v); } else if (v < 0) { raiseError("a negative number passed to int32 number: " + v); } writeInt8((byte)((v >> 24) & 0xFF), output); writeInt24((int)(v & 0xFFFFFF), output); } protected void writeSint32(int v, MemoryStream output) { if (v > int.MaxValue) { // Max 2.147.483.647 raiseError("Too large sInt32 number: " + v + ", Max = " + int.MaxValue); } else if (v < int.MinValue) { // Min -2.147.483.648 raiseError("Too small sInt32 number: " + v + ", Min = " + int.MinValue); } writeInt8((byte)((v >> 24) & 0xFF), output); writeInt24((int)(v & 0xFFFFFF), output); } protected void writeSint64(long v, MemoryStream output) { if (v > long.MaxValue ) { // Max 9,223,372,036,854,775,807 raiseError("Too large sInt64 number: " + v + ", Max = " + long.MaxValue); }else if(v < long.MinValue){ // Min -9,223,372,036,854,775,808 raiseError("Too small sInt64 number: " + v + ", Min = " + long.MinValue); } writeInt32((uint)((v >> 32) & 0xFFFFFFFF), output); writeInt32((uint)(v & 0xFFFFFFFF), output); } protected void writeDint63(long v, MemoryStream output) { if (v > long.MaxValue) { // Max 9,223,372,036,854,775,807 raiseError("Too large Dynamic Int63 number: " + v + ", Max = " + long.MaxValue); } else if (v < long.MinValue) { // Min 0 raiseError("Too small Dynamic Int63 number: " + v + ", Min = " + 0); } char[] charArray = Convert.ToString(v, 2).ToCharArray(); Array.Reverse(charArray); MatchCollection matches = Regex.Matches(new String(charArray), ".{1,7}"); for (int i = matches.Count - 1; i >= 0 ; i--) { String val = matches[i].Value; val += new String(new char[7 - val.Length]).Replace("\\\\0", "0") + Math.Min(i, 1); charArray = val.ToCharArray(); Array.Reverse(charArray); String str = new String(charArray); int t = Convert.ToInt32(str, 2); this.writeInt8((byte)t, output); } } protected void writeBool(bool v, MemoryStream output) { writeInt8((byte)(v ? 1 : 0), output); } protected void writeString(String v, MemoryStream output) { try { byte[] bs = System.Text.Encoding.UTF8.GetBytes (v); if (bs.Length > 0xFFFF) { raiseError("Too large string: " + bs.Length + " bytes"); } writeInt16((ushort)bs.Length, output); output.Write(bs, 0, bs.Length); } catch (System.IO.IOException ex) { throw ex; } } protected void writeBinary(byte[] v, MemoryStream output) { try { if ((uint)v.Length > 0xFFFFFFFF) { raiseError("Too large binary: " + v.Length + " bytes"); } writeInt32((uint)v.Length, output); output.Write(v, 0, v.Length); } catch (System.IO.IOException ex) { throw ex; } } protected void write16Binary(int[] v, MemoryStream output) { try { if (v.Length * 2 > 0xFF) { raiseError("Too large 16_binary: " + (v.Length * 2) + " bytes"); } writeInt8((byte)(v.Length * 2), output); for (int i = 0; i < v.Length; i++) { this.writeInt16((ushort)v[i], output); } } catch (System.IO.IOException ex) { throw ex; } } protected void writeShortBinary(byte[] v, MemoryStream output) { try { if (v.Length > 0xFF) { raiseError("Too large short_binary: " + v.Length + " bytes"); } writeInt8((byte)v.Length, output); output.Write(v, 0, v.Length); } catch (System.IO.IOException ex) { throw ex; } } protected void writeIpNumber(String v, MemoryStream output) { try { if (v.Contains(":")) { writeIpv6Number(v, output); } else { writeIpv4Number(v, output); } } catch (System.IO.IOException ex) { throw ex; } } protected void writeIpv4Number(String v, MemoryStream output) { try { byte[] ss = new byte[0]; if (v.Length != 0) { String[] bs = v.Split('.'); ss = new byte[bs.Length]; for (int i = 0; i < bs.Length; i++) { // TODO: check that each part is in range from 0 to 255 ss[i] = (byte)(int.Parse(bs[i]) & 0xFF); } } if (ss.Length == 0 || ss.Length == 4) { writeShortBinary(ss, output); } else { raiseError("Unknown IP v4 number " + v); // Only IPv4 for now } } catch (System.IO.IOException ex) { throw ex; } } protected void writeIpv6Number(String v, MemoryStream output) { try { v = v.Replace(" ", ""); int[] ss = new int[0]; bool contains_ipv6_letters = Regex.IsMatch(v.Trim().ToLower(), "[0-9a-f]+"); bool contains_other_letters = Regex.IsMatch(v.Trim().ToLower(), "[^:0-9a-f]+"); bool contains_more_seprators = Regex.IsMatch(v.Trim().ToLower(), ":{3,}"); bool contains_one_shorthand = Regex.Matches(v.Trim().ToLower(), ":{2}").Count <= 1; // make sure of v must have only one "::" and no more than two of ":". // e.g. 1::1::1 & 1:::1:205 if (v.Trim().Length != 0 && !contains_more_seprators && contains_one_shorthand && !contains_other_letters && contains_ipv6_letters) { String[] bs = v.Split(':'); ss = new int[bs.Length]; for (int i = 0; i < bs.Length; i++) { String s = bs[i].Trim(); if (s.Length <= 4) { // to avoid such number 0125f ss[i] = int.Parse(((s.Length == 0 ? "0" : bs[i].Trim())), System.Globalization.NumberStyles.HexNumber); } else { raiseError("Unknown IPv6 Group " + i + " which is " + s); } } } // Check for make sure of the size of the IP groups in case "::" is used // [> 2 & < 8]or not [must == 8] if (!contains_other_letters && (!v.Contains("::") && ss.Length == 0 || ss.Length == 8) || (v.Contains("::") && ss.Length > 2 && ss.Length < 8)) { write16Binary(ss, output); } else { raiseError("Unknown IP v6 number " + v); } } catch (System.IO.IOException ex) { throw ex; } } protected void raiseError(String msg) { throw new System.InvalidOperationException("[" + GetType() + "] " + msg); } } EOS end |
#csharp_class_template(sh) ⇒ Object
Generate C# Class that corresponding to the struct definition
* *Args* :
- +sh+ -> Struct Name
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 |
# File 'lib/divine/code_generators/csharp.rb', line 515 def csharp_class_template(sh) code = [ "public class #{sh.name} : Divine {", :indent, "", # PROPERTIES "public int struct_version = #{sh.latest_version};", sh.field_names.map do |fn| f = sh.field(fn).last "public #{csharp_get_type_declaration(f)} #{fn} = #{csharp_get_empty_declaration(f)};" end, "", # SERiALIZE INTERNAL "override", "public void serializeInternal(MemoryStream baos) {", :indent, "try{", :indent, "writeInt8((byte)this.struct_version, baos);", sh.structs.map do |s| [ "if(this.struct_version == #{s.version}) {", :indent, s.simple_fields.map do |f| "#{camelize("write", f.type)}(this.#{f.name}, baos);" end, s.complex_fields.map do |f| [ "", "// Serialize #{f.type} '#{f.name}'", csharp_serialize_internal("this.#{f.name}", f.referenced_types) ] end, "return;", :deindent, "}", "" ] end, "", "throw new System.InvalidOperationException(\"Unsupported version \" + this.struct_version + \" for type '#{sh.name}'\");", :deindent, "}catch (System.IO.IOException ex){", :indent, "throw ex;", :deindent, "}", :deindent, "}", "", # DESERIALIZE "override", "public void deserialize(MemoryStream bais) {", :indent, "try{", :indent, "this.struct_version = readInt8(bais);", sh.structs.map do |s| [ "if(this.struct_version == #{s.version}) {", :indent, s.simple_fields.map do |f| "this.#{f.name} = #{camelize("read", f.type)}(bais);" end, s.complex_fields.map do |f| [ "", "// Read #{f.type} '#{f.name}'", csharp_deserialize_internal("this.#{f.name}", f.referenced_types) ] end, "return;", :deindent, "}" ] end, "", "throw new System.InvalidOperationException(\"Unsupported version \" + this.struct_version + \" for type '#{sh.name}'\");", :deindent, "}catch (System.IO.IOException ex){", :indent, "throw ex;", :deindent, "}", :deindent, "}", "", # END OF CLASS :deindent, "}" ] format_src(3, 3, code) end |
#csharp_deserialize_internal(var, types) ⇒ Object
Generate the way of deserializing different DSL types
* *Args* :
- +var+ -> variable name
- +types+ -> variable type
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 |
# File 'lib/divine/code_generators/csharp.rb', line 785 def csharp_deserialize_internal(var, types) if types.respond_to? :first case types.first when :list count = get_fresh_variable_name nv = get_fresh_variable_name iter = get_fresh_variable_name return [ "#{"#{csharp_get_type_declaration(types)} " unless var.include? "this."}#{var} = #{csharp_get_empty_declaration(types)};", "uint #{count} = this.readInt32(bais);", "for(int #{iter}=0; #{iter}<#{count}; #{iter}++) {", :indent, csharp_deserialize_internal(nv, types[1]), "#{var}.Add(#{nv});", :deindent, "}" ] when :map count = get_fresh_variable_name nv1 = get_fresh_variable_name nv2 = get_fresh_variable_name iter = get_fresh_variable_name return ["#{"#{csharp_get_type_declaration(types)} " unless var.include? "this."}#{var} = #{csharp_get_empty_declaration(types)};", "uint #{count} = readInt32(bais);", "for(int #{iter}=0; #{iter}<#{count}; #{iter}++) {", :indent, csharp_deserialize_internal(nv1, types[1]), csharp_deserialize_internal(nv2, types[2]), "#{var}.Add(#{nv1}, #{nv2});", :deindent, "}" ] else raise "Missing serialization for #{var}" end else # case types # when :map # "#{var} = #{csharp_get_empty_declaration(types)}" # when :list # "#{var} = #{csharp_get_empty_declaration(types)}" # else if $all_structs.key? types [ "#{types} #{var} = new #{types}();", "#{var}.deserialize(bais);" ] else "#{"#{csharp_get_type_declaration(types)} " unless var.include? "this."}#{var} = #{self.camelize("read", types)}(bais);" end # end end end |
#csharp_get_empty_declaration(types, is_reference_type = false) ⇒ Object
Generate default C# data types declaration values corresponding to each DSL types:
* DSL Type --> Corresponding Default C# Value
* dint63 --> 0 range -> [0 - 9,223,372,036,854,775,807]
* 1 byte: range -> [0 - 127]
* 2 bytes: range -> [0 - 16,383]
* 3 bytes: range -> [0 - 2,097,151]
* 4 bytes: range -> [0 - 268,435,455]
* 5 bytes: range -> [0 - 34,359,738,367]
* 6 bytes: range -> [0 - 4,398,046,511,103]
* 7 bytes: range -> [0 - 562,949,953,421,311]
* 8 bytes: range -> [0 - 72,057,594,037,927,935]
* 9 bytes: range -> [0 - 9,223,372,036,854,775,807]
* int8 --> 0 Range -> [0 - 255]
* int16 --> 0 Range -> [0 - 65535]
* int32 --> 0 Range -> [0 - 4.294.967.295]
* sint32 --> 0 Range -> [-2.147.483.648 - 2.147.483.647]
* sint64 --> 0 Range -> [-9.223.372.036.854.775.808, 9.223.372.036.854.775.807]
* string --> ""
* ip_number--> ""
* binary --> new byte[0]
* short_binary --> new byte[0]
* list --> new List<type>()
* map --> new Dictionary<keyType, valueType>()
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 |
# File 'lib/divine/code_generators/csharp.rb', line 634 def csharp_get_empty_declaration(types, is_reference_type = false) if types.respond_to? :referenced_types csharp_get_empty_declaration(types.referenced_types) elsif types.respond_to?(:first) && types.size > 1 case types.first when :list "new #{csharp_get_type_declaration(types, true)}()" when :map "new #{csharp_get_type_declaration(types, true)}()" else raise "Missing empty declaration for #{types}" end elsif types.respond_to?(:first) && types.size == 1 csharp_get_empty_declaration(types.first, is_reference_type) else case types when :binary, :short_binary "new byte[0]" when :int8, :int16, :int32, :sint32, :sint64, :dint63 "0" when :string, :ip_number "\"\"" else if $all_structs[types] types else raise "Unkown field type #{types}" end end end end |
#csharp_get_type_declaration(types, is_reference_type = false) ⇒ Object
Generate C# data types declaration corresponding to each DSL type
* DSL Type --> Corresponding C# Type
* int8 --> byte
* int16 --> ushort
* int32 --> uint
* sint32 --> int
* sint64 --> long
* string --> string
* ip_number--> string
* binary --> byte[]
* short_binary --> byte[]
* list --> List<type>
* map --> Dictionary<keyType, valueType>
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 |
# File 'lib/divine/code_generators/csharp.rb', line 684 def csharp_get_type_declaration(types, is_reference_type = false) if types.respond_to? :referenced_types csharp_get_type_declaration(types.referenced_types, is_reference_type) elsif types.respond_to?(:first) && types.size > 1 case types.first when :list subtypes = csharp_get_type_declaration(types[1], true) return "List<#{subtypes}>" when :map key_type = csharp_get_type_declaration(types[1], true) value_type = csharp_get_type_declaration(types[2], true) return "Dictionary<#{key_type}, #{value_type}>" else raise "Missing serialization for #{types}" end elsif types.respond_to?(:first) && types.size == 1 csharp_get_type_declaration(types.first, is_reference_type) else case types when :binary, :short_binary "byte[]" when :int8 "byte" when :int16 "ushort" when :int32 "uint" when :sint32 "int" when :sint64, :dint63 "long" when :string, :ip_number "string" else if $all_structs[types] types else raise "Unkown field type #{types}" end end end end |
#csharp_serialize_internal(var, types) ⇒ Object
Generate the way of serializing different DSL types
* *Args* :
- +var+ -> variable name
- +types+ -> variable type
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 |
# File 'lib/divine/code_generators/csharp.rb', line 735 def csharp_serialize_internal(var, types) if types.respond_to? :first case types.first when :list nv = get_fresh_variable_name idx = get_fresh_variable_name return [ "writeInt32((uint)#{var}.Count, baos);", "for(int #{idx}=0; #{idx}<#{var}.Count; #{idx}++) {", :indent, "#{csharp_get_type_declaration types[1]} #{nv} = #{var}[#{idx}];", csharp_serialize_internal(nv, types[1]), :deindent, "}" ] when :map nv1 = get_fresh_variable_name nv2 = get_fresh_variable_name return [ "writeInt32((uint)#{var}.Count, baos);", "foreach (#{csharp_get_type_declaration types[1]} #{nv1} in #{var}.Keys) {", :indent, "#{csharp_get_type_declaration types[2]} #{nv2} = #{var}[#{nv1}];", csharp_serialize_internal(nv1, types[1]), csharp_serialize_internal(nv2, types[2]), :deindent, "}" ] else raise "Missing serialization for #{var}" end else if $all_structs[types] "#{var}.serializeInternal(baos);" elsif $available_types[types] && $available_types[types].ancestors.include?(SimpleDefinition) "#{self.camelize "write", types}(#{var}, baos);" else raise "Missing code generation case #{types}" end end end |
#get_header_comment ⇒ Object
Return the header comment
12 13 14 15 16 |
# File 'lib/divine/code_generators/csharp.rb', line 12 def get_header_comment get_header_comment_text.map do |s| "// #{s}" end.join("\n") end |