diff --git a/docs/source/JavaCsharpUsage.md b/docs/source/CsharpUsage.md similarity index 60% rename from docs/source/JavaCsharpUsage.md rename to docs/source/CsharpUsage.md index 102ce371d..90a757fb0 100644 --- a/docs/source/JavaCsharpUsage.md +++ b/docs/source/CsharpUsage.md @@ -1,54 +1,30 @@ -Use in Java/C# {#flatbuffers_guide_use_java_c-sharp} +Use in C# {#flatbuffers_guide_use_c-sharp} ============== ## Before you get started -Before diving into the FlatBuffers usage in Java or C#, it should be noted that +Before diving into the FlatBuffers usage in C#, it should be noted that the [Tutorial](@ref flatbuffers_guide_tutorial) page has a complete guide to -general FlatBuffers usage in all of the supported languages (including both Java -and C#). This page is designed to cover the nuances of FlatBuffers usage, -specific to Java and C#. +general FlatBuffers usage in all of the supported languages (including C#). +This page is designed to cover the nuances of FlatBuffers usage, +specific to C#. You should also have read the [Building](@ref flatbuffers_guide_building) documentation to build `flatc` and should be familiar with [Using the schema compiler](@ref flatbuffers_guide_using_schema_compiler) and [Writing a schema](@ref flatbuffers_guide_writing_schema). -## FlatBuffers Java and C-sharp code location - -#### Java - -The code for the FlatBuffers Java library can be found at -`flatbuffers/java/com/google/flatbuffers`. You can browse the library on the -[FlatBuffers GitHub page](https://github.com/google/flatbuffers/tree/master/ -java/com/google/flatbuffers). - -#### C-sharp +## FlatBuffers C-sharp code location The code for the FlatBuffers C# library can be found at `flatbuffers/net/FlatBuffers`. You can browse the library on the [FlatBuffers GitHub page](https://github.com/google/flatbuffers/tree/master/net/ FlatBuffers). -## Testing the FlatBuffers Java and C-sharp libraries +## Testing the FlatBuffers C-sharp libraries The code to test the libraries can be found at `flatbuffers/tests`. -#### Java - -The test code for Java is located in [JavaTest.java](https://github.com/google -/flatbuffers/blob/master/tests/JavaTest.java). - -To run the tests, use either [JavaTest.sh](https://github.com/google/ -flatbuffers/blob/master/tests/JavaTest.sh) or [JavaTest.bat](https://github.com/ -google/flatbuffers/blob/master/tests/JavaTest.bat), depending on your operating -system. - -*Note: These scripts require that [Java](https://www.oracle.com/java/index.html) -is installed.* - -#### C-sharp - The test code for C# is located in the [FlatBuffers.Test](https://github.com/ google/flatbuffers/tree/master/tests/FlatBuffers.Test) subfolder. To run the tests, open `FlatBuffers.Test.csproj` in [Visual Studio]( @@ -63,62 +39,44 @@ by running the following commands from inside the `FlatBuffers.Test` folder: mono Assert.exe ~~~ -## Using the FlatBuffers Java (and C#) library +## Using the FlatBuffers C# library *Note: See [Tutorial](@ref flatbuffers_guide_tutorial) for a more in-depth -example of how to use FlatBuffers in Java or C#.* +example of how to use FlatBuffers in C#.* -FlatBuffers supports reading and writing binary FlatBuffers in Java and C#. +FlatBuffers supports reading and writing binary FlatBuffers in C#. -To use FlatBuffers in your own code, first generate Java classes from your -schema with the `--java` option to `flatc`. (Or for C# with `--csharp`). +To use FlatBuffers in your own code, first generate C# classes from your +schema with the `--csharp` option to `flatc`. Then you can include both FlatBuffers and the generated code to read or write a FlatBuffer. -For example, here is how you would read a FlatBuffer binary file in Java: +For example, here is how you would read a FlatBuffer binary file in C#: First, import the library and generated code. Then, you read a FlatBuffer binary file into a `byte[]`. You then turn the `byte[]` into a `ByteBuffer`, which you -pass to the `getRootAsMyRootType` function: +pass to the `GetRootAsMyRootType` function: -*Note: The code here is written from the perspective of Java. Code for both -languages is both generated and used in nearly the exact same way, with only -minor differences. These differences are -[explained in a section below](#differences_in_c-sharp).* - -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.java} - import MyGame.Example.*; - import com.google.flatbuffers.FlatBufferBuilder; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cs} + using MyGame.Example; + using FlatBuffers; // This snippet ignores exceptions for brevity. - File file = new File("monsterdata_test.mon"); - RandomAccessFile f = new RandomAccessFile(file, "r"); - byte[] data = new byte[(int)f.length()]; - f.readFully(data); - f.close(); + byte[] data = File.ReadAllBytes("monsterdata_test.mon"); - ByteBuffer bb = ByteBuffer.wrap(data); - Monster monster = Monster.getRootAsMonster(bb); + ByteBuffer bb = new ByteBuffer(data); + Monster monster = Monster.GetRootAsMonster(bb); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Now you can access the data from the `Monster monster`: -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.java} - short hp = monster.hp(); - Vec3 pos = monster.pos(); +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cs} + short hp = monster.Hp; + Vec3 pos = monster.Pos; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -#### Differences in C-sharp - - -C# code works almost identically to Java, with only a few minor differences. -You can see an example of C# code in -`tests/FlatBuffers.Test/FlatBuffersExampleTests.cs` or -`samples/SampleBinary.cs`. - -First of all, naming follows standard C# style with `PascalCasing` identifiers, +C# code naming follows standard C# style with `PascalCasing` identifiers, e.g. `GetRootAsMyRootType`. Also, values (except vectors and unions) are -available as properties instead of parameterless accessor methods as in Java. +available as properties instead of parameterless accessor methods. The performance-enhancing methods to which you can pass an already created object are prefixed with `Get`, e.g.: @@ -147,13 +105,11 @@ To use it: array. - Instead of calling standard generated method, e.g.: `Monster.createTestarrayoftablesVector`, - call `CreateSortedVectorOfMonster` in C# or - `createSortedVectorOfTables` (from the `FlatBufferBuilder` object) in Java, + call `CreateSortedVectorOfMonster` in C# which will first sort all offsets such that the tables they refer to are sorted by the key field, then serialize it. - Now when you're accessing the FlatBuffer, you can use the `ByKey` accessor to access elements of the vector, e.g.: - `monster.testarrayoftablesByKey("Frodo")` in Java or `monster.TestarrayoftablesByKey("Frodo")` in C#, which returns an object of the corresponding table type, or `null` if not found. @@ -165,8 +121,34 @@ To use it: ## Text parsing There currently is no support for parsing text (Schema's and JSON) directly -from Java or C#, though you could use the C++ parser through native call +from C#, though you could use the C++ parser through native call interfaces available to each language. Please see the C++ documentation for more on text parsing. +## Object based API + +FlatBuffers is all about memory efficiency, which is why its base API is written +around using as little as possible of it. This does make the API clumsier +(requiring pre-order construction of all data, and making mutation harder). + +For times when efficiency is less important a more convenient object based API +can be used (through `--gen-object-api`) that is able to unpack & pack a +FlatBuffer into objects and standard System.Collections.Generic containers, allowing for convenient +construction, access and mutation. + +To use: + +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cs} + // Deserialize from buffer into object. + MonsterT monsterobj = GetMonster(flatbuffer).UnPack(); + + // Update object directly like a C# class instance. + Console.WriteLine(monsterobj.Name); + monsterobj.Name = "Bob"; // Change the name. + + // Serialize into new flatbuffer. + FlatBufferBuilder fbb = new FlatBufferBuilder(1); + fbb.Finish(Monster.Pack(fbb, monsterobj).Value); +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +
diff --git a/docs/source/FlatBuffers.md b/docs/source/FlatBuffers.md index 45ffbf22b..2a2133f7e 100644 --- a/docs/source/FlatBuffers.md +++ b/docs/source/FlatBuffers.md @@ -130,7 +130,9 @@ sections provide a more in-depth usage guide. - How to [write a schema](@ref flatbuffers_guide_writing_schema). - How to [use the generated C++ code](@ref flatbuffers_guide_use_cpp) in your own programs. -- How to [use the generated Java/C# code](@ref flatbuffers_guide_use_java_c-sharp) +- How to [use the generated Java code](@ref flatbuffers_guide_use_java) + in your own programs. +- How to [use the generated C# code](@ref flatbuffers_guide_use_c-sharp) in your own programs. - How to [use the generated Kotlin code](@ref flatbuffers_guide_use_kotlin) in your own programs. diff --git a/docs/source/JavaUsage.md b/docs/source/JavaUsage.md new file mode 100644 index 000000000..30ba06133 --- /dev/null +++ b/docs/source/JavaUsage.md @@ -0,0 +1,114 @@ +Use in Java {#flatbuffers_guide_use_java} +============== + +## Before you get started + +Before diving into the FlatBuffers usage in Java, it should be noted that +the [Tutorial](@ref flatbuffers_guide_tutorial) page has a complete guide to +general FlatBuffers usage in all of the supported languages (including Java). +This page is designed to cover the nuances of FlatBuffers usage, +specific to Java. + +You should also have read the [Building](@ref flatbuffers_guide_building) +documentation to build `flatc` and should be familiar with +[Using the schema compiler](@ref flatbuffers_guide_using_schema_compiler) and +[Writing a schema](@ref flatbuffers_guide_writing_schema). + +## FlatBuffers Java code location + +The code for the FlatBuffers Java library can be found at +`flatbuffers/java/com/google/flatbuffers`. You can browse the library on the +[FlatBuffers GitHub page](https://github.com/google/flatbuffers/tree/master/ +java/com/google/flatbuffers). + +## Testing the FlatBuffers Java libraries + +The code to test the libraries can be found at `flatbuffers/tests`. + +The test code for Java is located in [JavaTest.java](https://github.com/google +/flatbuffers/blob/master/tests/JavaTest.java). + +To run the tests, use either [JavaTest.sh](https://github.com/google/ +flatbuffers/blob/master/tests/JavaTest.sh) or [JavaTest.bat](https://github.com/ +google/flatbuffers/blob/master/tests/JavaTest.bat), depending on your operating +system. + +*Note: These scripts require that [Java](https://www.oracle.com/java/index.html) +is installed.* + +## Using the FlatBuffers Java library + +*Note: See [Tutorial](@ref flatbuffers_guide_tutorial) for a more in-depth +example of how to use FlatBuffers in Java.* + +FlatBuffers supports reading and writing binary FlatBuffers in Java. + +To use FlatBuffers in your own code, first generate Java classes from your +schema with the `--java` option to `flatc`. +Then you can include both FlatBuffers and the generated code to read +or write a FlatBuffer. + +For example, here is how you would read a FlatBuffer binary file in Java: +First, import the library and generated code. Then, you read a FlatBuffer binary +file into a `byte[]`. You then turn the `byte[]` into a `ByteBuffer`, which you +pass to the `getRootAsMyRootType` function: + +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.java} + import MyGame.Example.*; + import com.google.flatbuffers.FlatBufferBuilder; + + // This snippet ignores exceptions for brevity. + File file = new File("monsterdata_test.mon"); + RandomAccessFile f = new RandomAccessFile(file, "r"); + byte[] data = new byte[(int)f.length()]; + f.readFully(data); + f.close(); + + ByteBuffer bb = ByteBuffer.wrap(data); + Monster monster = Monster.getRootAsMonster(bb); +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Now you can access the data from the `Monster monster`: + +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.java} + short hp = monster.hp(); + Vec3 pos = monster.pos(); +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +## Storing dictionaries in a FlatBuffer + +FlatBuffers doesn't support dictionaries natively, but there is support to +emulate their behavior with vectors and binary search, which means you +can have fast lookups directly from a FlatBuffer without having to unpack +your data into a `Dictionary` or similar. + +To use it: +- Designate one of the fields in a table as the "key" field. You do this + by setting the `key` attribute on this field, e.g. + `name:string (key)`. + You may only have one key field, and it must be of string or scalar type. +- Write out tables of this type as usual, collect their offsets in an + array. +- Instead of calling standard generated method, + e.g.: `Monster.createTestarrayoftablesVector`, + call `createSortedVectorOfTables` (from the `FlatBufferBuilder` object). + which will first sort all offsets such that the tables they refer to + are sorted by the key field, then serialize it. +- Now when you're accessing the FlatBuffer, you can use + the `ByKey` accessor to access elements of the vector, e.g.: + `monster.testarrayoftablesByKey("Frodo")`. + which returns an object of the corresponding table type, + or `null` if not found. + `ByKey` performs a binary search, so should have a similar + speed to `Dictionary`, though may be faster because of better caching. + `ByKey` only works if the vector has been sorted, it will + likely not find elements if it hasn't been sorted. + +## Text parsing + +There currently is no support for parsing text (Schema's and JSON) directly +from Java, though you could use the C++ parser through native call +interfaces available to each language. Please see the +C++ documentation for more on text parsing. + +
diff --git a/docs/source/Tutorial.md b/docs/source/Tutorial.md index f871c4c0d..c0f3b9e79 100644 --- a/docs/source/Tutorial.md +++ b/docs/source/Tutorial.md @@ -3374,13 +3374,13 @@ For your chosen language, see: [Use in C++](@ref flatbuffers_guide_use_cpp)
-[Use in Java/C#](@ref flatbuffers_guide_use_java_c-sharp) +[Use in Java](@ref flatbuffers_guide_use_java)
[Use in Kotlin](@ref flatbuffers_guide_use_kotlin)
-[Use in Java/C#](@ref flatbuffers_guide_use_java_c-sharp) +[Use in C#](@ref flatbuffers_guide_use_c-sharp)
[Use in Go](@ref flatbuffers_guide_use_go) diff --git a/docs/source/doxygen_layout.xml b/docs/source/doxygen_layout.xml index 8253903dd..72888f951 100644 --- a/docs/source/doxygen_layout.xml +++ b/docs/source/doxygen_layout.xml @@ -29,8 +29,10 @@ title="Use in C"/> - + + field_has_create_set; flatbuffers::FieldDef *key_field = nullptr; if (struct_def.fixed) { + struct_has_create = true; // create a struct constructor function code += " public static " + GenOffsetType(struct_def) + " "; code += "Create"; @@ -907,6 +943,7 @@ class CSharpGenerator : public BaseGenerator { // JVM specifications restrict default constructor params to be < 255. // Longs and doubles take up 2 units, so we set the limit to be < 127. if (has_no_struct_fields && num_fields && num_fields < 127) { + struct_has_create = true; // Generate a table constructor of the form: // public static int createName(FlatBufferBuilder builder, args...) code += " public static " + GenOffsetType(struct_def) + " "; @@ -986,6 +1023,7 @@ class CSharpGenerator : public BaseGenerator { auto alignment = InlineAlignment(vector_type); auto elem_size = InlineSize(vector_type); if (!IsStruct(vector_type)) { + field_has_create_set.insert(&field); code += " public static VectorOffset "; code += "Create"; code += MakeCamel(field.name); @@ -1105,7 +1143,16 @@ class CSharpGenerator : public BaseGenerator { code += " return null;\n"; code += " }\n"; } + + if (opts.generate_object_based_api) { + GenPackUnPack_ObjectAPI(struct_def, code_ptr, opts, struct_has_create, + field_has_create_set); + } code += "};\n\n"; + + if (opts.generate_object_based_api) { + GenStruct_ObjectAPI(struct_def, code_ptr, opts); + } } void GenVectorAccessObject(StructDef &struct_def, @@ -1166,6 +1213,629 @@ class CSharpGenerator : public BaseGenerator { code += " }\n"; } + void GenEnum_ObjectAPI(EnumDef &enum_def, std::string *code_ptr, + const IDLOptions &opts) const { + auto &code = *code_ptr; + if (enum_def.generated) return; + if (!enum_def.is_union) return; + if (enum_def.attributes.Lookup("private")) { + code += "internal "; + } else { + code += "public "; + } + auto union_name = enum_def.name + "Union"; + code += "class " + union_name + " {\n"; + // Type + code += " public " + enum_def.name + " Type { get; set; }\n"; + // Value + code += " public object Value { get; set; }\n"; + code += "\n"; + // Constructor + code += " public " + union_name + "() {\n"; + code += " this.Type = " + enum_def.name + "." + + enum_def.Vals()[0]->name + ";\n"; + code += " this.Value = null;\n"; + code += " }\n\n"; + // As + code += " public T As() where T : class { return this.Value as T; }\n"; + // As + for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) { + auto &ev = **it; + if (ev.union_type.base_type == BASE_TYPE_NONE) continue; + auto type_name = GenTypeGet_ObjectAPI(ev.union_type, opts); + if (ev.union_type.base_type == BASE_TYPE_STRUCT && + ev.union_type.struct_def->attributes.Lookup("private")) { + code += " internal "; + } else { + code += " public "; + } + code += type_name + " As" + ev.name + "() { return this.As<" + type_name + + ">(); }\n"; + } + code += "\n"; + // Pack() + code += " public static int Pack(FlatBuffers.FlatBufferBuilder builder, " + + union_name + " _o) {\n"; + code += " switch (_o.Type) {\n"; + for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) { + auto &ev = **it; + if (ev.union_type.base_type == BASE_TYPE_NONE) { + code += " default: return 0;\n"; + } else { + code += " case " + enum_def.name + "." + ev.name + ": return "; + if (ev.union_type.base_type == BASE_TYPE_STRING) { + code += "builder.CreateString(_o.As" + ev.name + "()).Value;\n"; + } else { + code += GenTypeGet(ev.union_type) + ".Pack(builder, _o.As" + ev.name + + "()).Value;\n"; + } + } + } + code += " }\n"; + code += " }\n"; + code += "}\n\n"; + } + + std::string GenTypeName_ObjectAPI(const std::string &name, + const IDLOptions &opts) const { + return opts.object_prefix + name + opts.object_suffix; + } + + void GenUnionUnPack_ObjectAPI(const EnumDef &enum_def, std::string *code_ptr, + const std::string &camel_name, + bool is_vector) const { + auto &code = *code_ptr; + std::string varialbe_name = "_o." + camel_name; + std::string type_suffix = ""; + std::string func_suffix = "()"; + std::string indent = " "; + if (is_vector) { + varialbe_name = "_o_" + camel_name; + type_suffix = "(_j)"; + func_suffix = "(_j)"; + indent = " "; + } + if (is_vector) { + code += indent + "var " + varialbe_name + " = new "; + } else { + code += indent + varialbe_name + " = new "; + } + code += WrapInNameSpace(enum_def) + "Union();\n"; + code += indent + varialbe_name + ".Type = this." + camel_name + "Type" + + type_suffix + ";\n"; + code += + indent + "switch (this." + camel_name + "Type" + type_suffix + ") {\n"; + for (auto eit = enum_def.Vals().begin(); eit != enum_def.Vals().end(); + ++eit) { + auto &ev = **eit; + if (ev.union_type.base_type == BASE_TYPE_NONE) { + code += indent + " default: break;\n"; + } else { + code += indent + " case " + WrapInNameSpace(enum_def) + "." + ev.name + + ":\n"; + code += indent + " " + varialbe_name + ".Value = this." + camel_name; + if (ev.union_type.base_type == BASE_TYPE_STRING) { + code += "AsString" + func_suffix + ";\n"; + } else { + code += "<" + GenTypeGet(ev.union_type) + ">" + func_suffix; + code += ".HasValue ? this." + camel_name; + code += "<" + GenTypeGet(ev.union_type) + ">" + func_suffix + + ".Value.UnPack() : null;\n"; + } + code += indent + " break;\n"; + } + } + code += indent + "}\n"; + if (is_vector) { + code += indent + "_o." + camel_name + ".Add(" + varialbe_name + ");\n"; + } + } + + void GenPackUnPack_ObjectAPI( + StructDef &struct_def, std::string *code_ptr, const IDLOptions &opts, + bool struct_has_create, + const std::set &field_has_create) const { + auto &code = *code_ptr; + auto struct_name = GenTypeName_ObjectAPI(struct_def.name, opts); + // UnPack() + code += " public " + struct_name + " UnPack() {\n"; + code += " var _o = new " + struct_name + "();\n"; + code += " this.UnPackTo(_o);\n"; + code += " return _o;\n"; + code += " }\n"; + // UnPackTo() + code += " public void UnPackTo(" + struct_name + " _o) {\n"; + for (auto it = struct_def.fields.vec.begin(); + it != struct_def.fields.vec.end(); ++it) { + auto &field = **it; + if (field.deprecated) continue; + auto camel_name = MakeCamel(field.name); + auto start = " _o." + camel_name + " = "; + switch (field.value.type.base_type) { + case BASE_TYPE_STRUCT: { + auto fixed = struct_def.fixed && field.value.type.struct_def->fixed; + if (fixed) { + code += start + "this." + camel_name + ".UnPack();\n"; + } else { + code += start + "this." + camel_name + ".HasValue ? this." + + camel_name + ".Value.UnPack() : null;\n"; + } + break; + } + case BASE_TYPE_ARRAY: { + auto type_name = GenTypeGet_ObjectAPI(field.value.type, opts); + auto length_str = NumToString(field.value.type.fixed_length); + auto unpack_method = field.value.type.struct_def == nullptr + ? "" + : field.value.type.struct_def->fixed + ? ".UnPack()" + : "?.UnPack()"; + code += start + "new " + type_name.substr(0, type_name.length() - 1) + + length_str + "];\n"; + code += " for (var _j = 0; _j < " + length_str + "; ++_j) { _o." + + camel_name + "[_j] = this." + camel_name + "(_j)" + + unpack_method + "; }\n"; + break; + } + case BASE_TYPE_VECTOR: + if (field.value.type.element == BASE_TYPE_UNION) { + code += start + "new " + + GenTypeGet_ObjectAPI(field.value.type, opts) + "();\n"; + code += " for (var _j = 0; _j < this." + camel_name + + "Length; ++_j) {\n"; + GenUnionUnPack_ObjectAPI(*field.value.type.enum_def, code_ptr, + camel_name, true); + code += " }\n"; + } else if (field.value.type.element != BASE_TYPE_UTYPE) { + auto fixed = field.value.type.struct_def == nullptr; + code += start + "new " + + GenTypeGet_ObjectAPI(field.value.type, opts) + "();\n"; + code += " for (var _j = 0; _j < this." + camel_name + + "Length; ++_j) {"; + code += "_o." + camel_name + ".Add("; + if (fixed) { + code += "this." + camel_name + "(_j)"; + } else { + code += "this." + camel_name + "(_j).HasValue ? this." + + camel_name + "(_j).Value.UnPack() : null"; + } + code += ");}\n"; + } + break; + case BASE_TYPE_UTYPE: break; + case BASE_TYPE_UNION: { + GenUnionUnPack_ObjectAPI(*field.value.type.enum_def, code_ptr, + camel_name, false); + break; + } + default: { + code += start + "this." + camel_name + ";\n"; + break; + } + } + } + code += " }\n"; + // Pack() + code += " public static " + GenOffsetType(struct_def) + + " Pack(FlatBufferBuilder builder, " + struct_name + " _o) {\n"; + code += " if (_o == null) return default(" + GenOffsetType(struct_def) + + ");\n"; + for (auto it = struct_def.fields.vec.begin(); + it != struct_def.fields.vec.end(); ++it) { + auto &field = **it; + if (field.deprecated) continue; + auto camel_name = MakeCamel(field.name); + // pre + switch (field.value.type.base_type) { + case BASE_TYPE_STRUCT: { + if (!field.value.type.struct_def->fixed) { + code += " var _" + field.name + " = _o." + camel_name + + " == null ? default(" + + GenOffsetType(*field.value.type.struct_def) + + ") : " + GenTypeGet(field.value.type) + + ".Pack(builder, _o." + camel_name + ");\n"; + } + break; + } + case BASE_TYPE_STRING: { + std::string create_string = + field.shared ? "CreateSharedString" : "CreateString"; + code += " var _" + field.name + " = _o." + camel_name + + " == null ? default(StringOffset) : " + "builder." + + create_string + "(_o." + camel_name + ");\n"; + break; + } + case BASE_TYPE_VECTOR: { + if (field_has_create.find(&field) != field_has_create.end()) { + auto property_name = camel_name; + auto gen_for_loop = true; + std::string array_name = "__" + field.name; + std::string array_type = ""; + std::string to_array = ""; + switch (field.value.type.element) { + case BASE_TYPE_STRING: { + std::string create_string = + field.shared ? "CreateSharedString" : "CreateString"; + array_type = "StringOffset"; + to_array += "builder." + create_string + "(_o." + + property_name + "[_j])"; + break; + } + case BASE_TYPE_STRUCT: + array_type = "Offset<" + GenTypeGet(field.value.type) + ">"; + to_array = GenTypeGet(field.value.type) + ".Pack(builder, _o." + + property_name + "[_j])"; + break; + case BASE_TYPE_UTYPE: + property_name = camel_name.substr(0, camel_name.size() - 4); + array_type = WrapInNameSpace(*field.value.type.enum_def); + to_array = "_o." + property_name + "[_j].Type"; + break; + case BASE_TYPE_UNION: + array_type = "int"; + to_array = WrapInNameSpace(*field.value.type.enum_def) + + "Union.Pack(builder, _o." + property_name + "[_j])"; + break; + default: gen_for_loop = false; break; + } + code += " var _" + field.name + " = default(VectorOffset);\n"; + code += " if (_o." + property_name + " != null) {\n"; + if (gen_for_loop) { + code += " var " + array_name + " = new " + array_type + + "[_o." + property_name + ".Count];\n"; + code += " for (var _j = 0; _j < " + array_name + + ".Length; ++_j) { "; + code += array_name + "[_j] = " + to_array + "; }\n"; + } else { + code += " var " + array_name + " = _o." + property_name + + ".ToArray();\n"; + } + code += " _" + field.name + " = Create" + camel_name + + "Vector(builder, " + array_name + ");\n"; + code += " }\n"; + } else { + auto pack_method = + field.value.type.struct_def == nullptr + ? "builder.Add" + GenMethod(field.value.type.VectorType()) + + "(_o." + camel_name + "[_j]);" + : GenTypeGet(field.value.type) + ".Pack(builder, _o." + + camel_name + "[_j]);"; + code += " var _" + field.name + " = default(VectorOffset);\n"; + code += " if (_o." + camel_name + " != null) {\n"; + code += " Start" + camel_name + "Vector(builder, _o." + + camel_name + ".Count);\n"; + code += " for (var _j = _o." + camel_name + + ".Count - 1; _j >= 0; --_j) { " + pack_method + " }\n"; + code += " _" + field.name + " = builder.EndVector();\n"; + code += " }\n"; + } + break; + } + case BASE_TYPE_ARRAY: { + if (field.value.type.struct_def != nullptr) { + std::vector name_vec; + name_vec.push_back(field.name); + std::vector array_length_vec; + array_length_vec.push_back(field.value.type.fixed_length); + GenArrayPackDecl_ObjectAPI(*field.value.type.struct_def, code_ptr, + name_vec, array_length_vec); + } else { + code += " var _" + field.name + " = _o." + camel_name + ";\n"; + } + break; + } + case BASE_TYPE_UNION: { + code += " var _" + field.name + "_type = _o." + camel_name + + " == null ? " + WrapInNameSpace(*field.value.type.enum_def) + + ".NONE : " + "_o." + camel_name + ".Type;\n"; + code += + " var _" + field.name + " = _o." + camel_name + + " == null ? 0 : " + GenTypeGet_ObjectAPI(field.value.type, opts) + + ".Pack(builder, _o." + camel_name + ");\n"; + break; + } + default: break; + } + } + if (struct_has_create) { + // Create + code += " return Create" + struct_def.name + "(\n"; + code += " builder"; + for (auto it = struct_def.fields.vec.begin(); + it != struct_def.fields.vec.end(); ++it) { + auto &field = **it; + if (field.deprecated) continue; + auto camel_name = MakeCamel(field.name); + switch (field.value.type.base_type) { + case BASE_TYPE_STRUCT: { + if (struct_def.fixed) { + GenStructArgs_ObjectAPI(*field.value.type.struct_def, code_ptr, + " _o." + camel_name + "."); + } else { + code += ",\n"; + if (field.value.type.struct_def->fixed) { + code += " " + GenTypeGet(field.value.type) + + ".Pack(builder, _o." + camel_name + ")"; + } else { + code += " _" + field.name; + } + } + break; + } + case BASE_TYPE_ARRAY: { + if (field.value.type.struct_def != nullptr) { + GenArrayPackCall_ObjectAPI(*field.value.type.struct_def, code_ptr, + " _" + field.name + "_"); + } else { + code += ",\n"; + code += " _" + field.name; + } + break; + } + case BASE_TYPE_UNION: FLATBUFFERS_FALLTHROUGH(); // fall thru + case BASE_TYPE_UTYPE: FLATBUFFERS_FALLTHROUGH(); // fall thru + case BASE_TYPE_STRING: FLATBUFFERS_FALLTHROUGH(); // fall thru + case BASE_TYPE_VECTOR: { + code += ",\n"; + code += " _" + field.name; + break; + } + default: // scalar + code += ",\n"; + code += " _o." + camel_name; + break; + } + } + code += ");\n"; + } else { + // Start, End + code += " Start" + struct_def.name + "(builder);\n"; + for (auto it = struct_def.fields.vec.begin(); + it != struct_def.fields.vec.end(); ++it) { + auto &field = **it; + if (field.deprecated) continue; + auto camel_name = MakeCamel(field.name); + switch (field.value.type.base_type) { + case BASE_TYPE_STRUCT: { + if (field.value.type.struct_def->fixed) { + code += " Add" + camel_name + "(builder, " + + GenTypeGet(field.value.type) + ".Pack(builder, _o." + + camel_name + "));\n"; + } else { + code += + " Add" + camel_name + "(builder, _" + field.name + ");\n"; + } + break; + } + case BASE_TYPE_STRING: FLATBUFFERS_FALLTHROUGH(); // fall thru + case BASE_TYPE_ARRAY: FLATBUFFERS_FALLTHROUGH(); // fall thru + case BASE_TYPE_VECTOR: { + code += + " Add" + camel_name + "(builder, _" + field.name + ");\n"; + break; + } + case BASE_TYPE_UTYPE: break; + case BASE_TYPE_UNION: { + code += " Add" + camel_name + "Type(builder, _" + field.name + + "_type);\n"; + code += + " Add" + camel_name + "(builder, _" + field.name + ");\n"; + break; + } + // scalar + default: { + code += + " Add" + camel_name + "(builder, _o." + camel_name + ");\n"; + break; + } + } + } + code += " return End" + struct_def.name + "(builder);\n"; + } + code += " }\n"; + } + + void GenStructArgs_ObjectAPI(const StructDef &struct_def, + std::string *code_ptr, + std::string prefix) const { + auto &code = *code_ptr; + for (auto it = struct_def.fields.vec.begin(); + it != struct_def.fields.vec.end(); ++it) { + auto &field = **it; + const auto &field_type = field.value.type; + if (IsStruct(field_type)) { + GenStructArgs_ObjectAPI(*field_type.struct_def, code_ptr, + prefix + "." + MakeCamel(field.name) + "."); + } else { + code += ",\n"; + code += prefix + MakeCamel(field.name); + } + } + } + + void GenArrayPackDecl_ObjectAPI(const StructDef &struct_def, + std::string *code_ptr, + std::vector name_vec, + std::vector array_length_vec) const { + auto &code = *code_ptr; + for (auto it = struct_def.fields.vec.begin(); + it != struct_def.fields.vec.end(); ++it) { + auto &field = **it; + auto is_array = IsArray(field.value.type); + const auto &field_type = + is_array ? field.value.type.VectorType() : field.value.type; + if (!IsStruct(field_type)) { + auto tmp_name_vec = name_vec; + tmp_name_vec.push_back(field.name); + auto tmp_array_length_vec = array_length_vec; + if (is_array) { + tmp_array_length_vec.push_back(field_type.fixed_length); + } + std::string name; + for (size_t tmp_name_index = 0; tmp_name_index < tmp_name_vec.size(); + ++tmp_name_index) { + name += "_" + tmp_name_vec[tmp_name_index]; + } + code += " var " + name + " = new " + GenTypeBasic(field_type) + "["; + code += NumToString(tmp_array_length_vec[0]); + for (size_t i = 1; i < tmp_array_length_vec.size(); ++i) { + auto array_length = tmp_array_length_vec[i]; + code += "," + NumToString(array_length); + } + code += "];\n"; + code += " "; + // initialize array + for (size_t i = 0; i < tmp_array_length_vec.size(); ++i) { + auto array_length = tmp_array_length_vec[i]; + auto idx = "idx" + NumToString(i); + code += "for (var " + idx + " = 0; " + idx + " < " + + NumToString(array_length) + "; ++" + idx + ") {"; + } + code += name + "[idx0"; + for (size_t i = 1; i < tmp_array_length_vec.size(); ++i) { + auto idx = "idx" + NumToString(i); + code += "," + idx; + } + code += "] = _o"; + for (size_t i = 0; i < tmp_array_length_vec.size(); ++i) { + auto idx = "idx" + NumToString(i); + code += "." + MakeCamel(tmp_name_vec[i]) + "[" + idx + "]"; + } + if (!is_array) { code += "." + MakeCamel(field.name); } + code += ";"; + for (size_t i = 0; i < tmp_array_length_vec.size(); ++i) { + code += "}"; + } + code += "\n"; + } + } + } + + void GenArrayPackCall_ObjectAPI(const StructDef &struct_def, + std::string *code_ptr, + std::string prefix) const { + auto &code = *code_ptr; + for (auto it = struct_def.fields.vec.begin(); + it != struct_def.fields.vec.end(); ++it) { + auto &field = **it; + const auto &field_type = field.value.type; + if (IsStruct(field_type)) { + GenArrayPackCall_ObjectAPI(*field_type.struct_def, code_ptr, + prefix + field.name + "_"); + } else { + code += ",\n"; + code += prefix + field.name; + } + } + } + + std::string GenTypeGet_ObjectAPI(flatbuffers::Type type, + const IDLOptions &opts) const { + auto type_name = GenTypeGet(type); + // Replace to ObjectBaseAPI Type Name + switch (type.base_type) { + case BASE_TYPE_STRUCT: FLATBUFFERS_FALLTHROUGH(); // fall thru + case BASE_TYPE_ARRAY: FLATBUFFERS_FALLTHROUGH(); // fall thru + case BASE_TYPE_VECTOR: { + if (type.struct_def != nullptr) { + auto type_name_length = type.struct_def->name.length(); + auto new_type_name = + GenTypeName_ObjectAPI(type.struct_def->name, opts); + type_name.replace(type_name.length() - type_name_length, + type_name_length, new_type_name); + } else if (type.element == BASE_TYPE_UNION) { + type_name = WrapInNameSpace(*type.enum_def) + "Union"; + } + break; + } + + case BASE_TYPE_UNION: { + type_name = WrapInNameSpace(*type.enum_def) + "Union"; + break; + } + default: break; + } + + switch (type.base_type) { + case BASE_TYPE_ARRAY: { + type_name = type_name + "[]"; + break; + } + case BASE_TYPE_VECTOR: { + type_name = "List<" + type_name + ">"; + break; + } + default: break; + } + return type_name; + } + + void GenStruct_ObjectAPI(StructDef &struct_def, std::string *code_ptr, + const IDLOptions &opts) const { + auto &code = *code_ptr; + if (struct_def.attributes.Lookup("private")) { + code += "internal "; + } else { + code += "public "; + } + if (struct_def.attributes.Lookup("csharp_partial")) { + // generate a partial class for this C# struct/table + code += "partial "; + } + auto class_name = GenTypeName_ObjectAPI(struct_def.name, opts); + code += "class " + class_name; + code += "\n{\n"; + // Generate Properties + for (auto it = struct_def.fields.vec.begin(); + it != struct_def.fields.vec.end(); ++it) { + auto &field = **it; + if (field.deprecated) continue; + if (field.value.type.base_type == BASE_TYPE_UTYPE) continue; + if (field.value.type.element == BASE_TYPE_UTYPE) continue; + auto type_name = GenTypeGet_ObjectAPI(field.value.type, opts); + code += " public " + type_name + " " + MakeCamel(field.name, true) + + " { get; set; }\n"; + } + // Generate Constructor + code += "\n"; + code += " public " + class_name + "() {\n"; + for (auto it = struct_def.fields.vec.begin(); + it != struct_def.fields.vec.end(); ++it) { + auto &field = **it; + if (field.deprecated) continue; + if (field.value.type.base_type == BASE_TYPE_UTYPE) continue; + if (field.value.type.element == BASE_TYPE_UTYPE) continue; + code += " this." + MakeCamel(field.name) + " = "; + auto type_name = GenTypeGet_ObjectAPI(field.value.type, opts); + if (IsScalar(field.value.type.base_type)) { + code += GenDefaultValue(field) + ";\n"; + } else { + switch (field.value.type.base_type) { + case BASE_TYPE_STRUCT: { + if (IsStruct(field.value.type)) { + code += "new " + type_name + "();\n"; + } else { + code += "null;\n"; + } + break; + } + case BASE_TYPE_ARRAY: { + code += "new " + type_name.substr(0, type_name.length() - 1) + + NumToString(field.value.type.fixed_length) + "];\n"; + break; + } + default: { + code += "null;\n"; + break; + } + } + } + } + code += " }\n"; + code += "}\n\n"; + } + // This tracks the current namespace used to determine if a type need to be // prefixed by its namespace const Namespace *cur_name_space_; diff --git a/tests/FlatBuffers.Test/FlatBuffers.Test.csproj b/tests/FlatBuffers.Test/FlatBuffers.Test.csproj index 80ccc7a8f..3f064b466 100644 --- a/tests/FlatBuffers.Test/FlatBuffers.Test.csproj +++ b/tests/FlatBuffers.Test/FlatBuffers.Test.csproj @@ -1,4 +1,4 @@ - + Debug @@ -9,6 +9,7 @@ FlatBuffers.Test FlatBuffers.Test v3.5 + 4 512 @@ -66,6 +67,9 @@ FlatBuffers\Table.cs + + MyGame\Example2\Monster.cs + MyGame\Example\Any.cs @@ -96,6 +100,9 @@ MyGame\Example\TestSimpleTableWithEnum.cs + + MyGame\Example\TypeAliases.cs + MyGame\Example\Vec3.cs @@ -129,6 +136,21 @@ NamespaceA\TableInFirstNS.cs + + union_vector\Attacker.cs + + + union_vector\BookReader.cs + + + union_vector\Character.cs + + + union_vector\Movie.cs + + + union_vector\Rapunzel.cs + @@ -156,4 +178,4 @@ --> - + \ No newline at end of file diff --git a/tests/FlatBuffers.Test/FlatBuffersExampleTests.cs b/tests/FlatBuffers.Test/FlatBuffersExampleTests.cs index bbd69f79a..abe08572c 100644 --- a/tests/FlatBuffers.Test/FlatBuffersExampleTests.cs +++ b/tests/FlatBuffers.Test/FlatBuffersExampleTests.cs @@ -187,6 +187,7 @@ namespace FlatBuffers.Test Assert.AreEqual(pos.X, 1.0f); TestBuffer(dataBuffer); + TestObjectAPI(Monster.GetRootAsMonster(dataBuffer)); } private void TestBuffer(ByteBuffer bb) @@ -275,7 +276,7 @@ namespace FlatBuffers.Test Assert.IsTrue(monster.GetTestarrayofboolsBytes().HasValue); } #endif - } + } [FlatBuffersTestMethod] public void CanReadCppGeneratedWireFile() @@ -283,6 +284,7 @@ namespace FlatBuffers.Test var data = File.ReadAllBytes(@"Resources/monsterdata_test.mon"); var bb = new ByteBuffer(data); TestBuffer(bb); + TestObjectAPI(Monster.GetRootAsMonster(bb)); } [FlatBuffersTestMethod] @@ -311,6 +313,8 @@ namespace FlatBuffers.Test var mons = Monster.GetRootAsMonster(fbb.DataBuffer); var colors = mons.GetVectorOfEnumsArray(); Assert.ArrayEqual(colorVec, colors); + + TestObjectAPI(mons); } [FlatBuffersTestMethod] @@ -350,6 +354,9 @@ namespace FlatBuffers.Test Assert.AreEqual(nestedMonsterMana, nestedMonster.Mana); Assert.AreEqual(nestedMonsterHp, nestedMonster.Hp); Assert.AreEqual(nestedMonsterName, nestedMonster.Name); + + TestObjectAPI(mons); + TestObjectAPI(nestedMonster); } [FlatBuffersTestMethod] @@ -400,26 +407,392 @@ namespace FlatBuffers.Test ArrayTable table = ArrayTable.GetRootAsArrayTable(builder.DataBuffer); - Assert.AreEqual(table.A?.A, 0.5f); - for (int i = 0; i < 15; i++) Assert.AreEqual(table.A?.B(i), i); - Assert.AreEqual(table.A?.C, (sbyte)1); - Assert.AreEqual(table.A?.D(0).A(0), 1); - Assert.AreEqual(table.A?.D(0).A(1), 2); - Assert.AreEqual(table.A?.D(1).A(0), 3); - Assert.AreEqual(table.A?.D(1).A(1), 4); - Assert.AreEqual(table.A?.D(0).B, TestEnum.B); - Assert.AreEqual(table.A?.D(1).B, TestEnum.C); - Assert.AreEqual(table.A?.D(0).C(0), TestEnum.A); - Assert.AreEqual(table.A?.D(0).C(1), TestEnum.B); - Assert.AreEqual(table.A?.D(1).C(0), TestEnum.C); - Assert.AreEqual(table.A?.D(1).C(1), TestEnum.B); - Assert.AreEqual(table.A?.D(0).D(0), -1); - Assert.AreEqual(table.A?.D(0).D(1), 1); - Assert.AreEqual(table.A?.D(1).D(0), -2); - Assert.AreEqual(table.A?.D(1).D(1), 2); - Assert.AreEqual(table.A?.E, 2); - Assert.AreEqual(table.A?.F(0), -1); - Assert.AreEqual(table.A?.F(1), 1); + Assert.AreEqual(table.A.Value.A, 0.5f); + for (int i = 0; i < 15; i++) Assert.AreEqual(table.A.Value.B(i), i); + Assert.AreEqual(table.A.Value.C, (sbyte)1); + Assert.AreEqual(table.A.Value.D(0).A(0), 1); + Assert.AreEqual(table.A.Value.D(0).A(1), 2); + Assert.AreEqual(table.A.Value.D(1).A(0), 3); + Assert.AreEqual(table.A.Value.D(1).A(1), 4); + Assert.AreEqual(table.A.Value.D(0).B, TestEnum.B); + Assert.AreEqual(table.A.Value.D(1).B, TestEnum.C); + Assert.AreEqual(table.A.Value.D(0).C(0), TestEnum.A); + Assert.AreEqual(table.A.Value.D(0).C(1), TestEnum.B); + Assert.AreEqual(table.A.Value.D(1).C(0), TestEnum.C); + Assert.AreEqual(table.A.Value.D(1).C(1), TestEnum.B); + Assert.AreEqual(table.A.Value.D(0).D(0), -1); + Assert.AreEqual(table.A.Value.D(0).D(1), 1); + Assert.AreEqual(table.A.Value.D(1).D(0), -2); + Assert.AreEqual(table.A.Value.D(1).D(1), 2); + Assert.AreEqual(table.A.Value.E, 2); + Assert.AreEqual(table.A.Value.F(0), -1); + Assert.AreEqual(table.A.Value.F(1), 1); + + TestObjectAPI(table); + } + + [FlatBuffersTestMethod] + public void TestUnionVector() + { + var fbb = new FlatBufferBuilder(100); + var rapunzel = Rapunzel.CreateRapunzel(fbb, 40).Value; + + var characterTypes = new[] + { + Character.MuLan, + Character.Belle, + Character.Other, + }; + var characterTypesOffset = Movie.CreateCharactersTypeVector(fbb, characterTypes); + + var characters = new[] + { + Attacker.CreateAttacker(fbb, 10).Value, + BookReader.CreateBookReader(fbb, 20).Value, + fbb.CreateSharedString("Chip").Value, + }; + var charactersOffset = Movie.CreateCharactersVector(fbb, characters); + + var movieOffset = Movie.CreateMovie( + fbb, + Character.Rapunzel, + rapunzel, + characterTypesOffset, + charactersOffset); + Movie.FinishMovieBuffer(fbb, movieOffset); + + var movie = Movie.GetRootAsMovie(fbb.DataBuffer); + Assert.AreEqual(Character.Rapunzel, movie.MainCharacterType); + Assert.AreEqual(40, movie.MainCharacter().Value.HairLength); + + Assert.AreEqual(3, movie.CharactersLength); + Assert.AreEqual(Character.MuLan, movie.CharactersType(0)); + Assert.AreEqual(10, movie.Characters(0).Value.SwordAttackDamage); + Assert.AreEqual(Character.Belle, movie.CharactersType(1)); + Assert.AreEqual(20, movie.Characters(1).Value.BooksRead); + Assert.AreEqual(Character.Other, movie.CharactersType(2)); + Assert.AreEqual("Chip", movie.CharactersAsString(2)); + + TestObjectAPI(movie); + } + + private void AreEqual(Monster a, MonsterT b) + { + Assert.AreEqual(a.Hp, b.Hp); + Assert.AreEqual(a.Mana, b.Mana); + Assert.AreEqual(a.Name, b.Name); + + var posA = a.Pos; + var posB = b.Pos; + if (posA != null) + { + Assert.AreEqual(posA.Value.X, posB.X); + Assert.AreEqual(posA.Value.Y, posB.Y); + Assert.AreEqual(posA.Value.Z, posB.Z); + + Assert.AreEqual(posA.Value.Test1, posB.Test1); + Assert.AreEqual(posA.Value.Test2, posB.Test2); + var tA = posA.Value.Test3; + var tB = posB.Test3; + Assert.AreEqual(tA.A, tB.A); + Assert.AreEqual(tA.B, tB.B); + } + + Assert.AreEqual(a.TestType, b.Test.Type); + if (a.TestType == Any.Monster) + { + var monster2A = a.Test().Value; + var monster2B = b.Test.AsMonster(); + Assert.AreEqual(monster2A.Name, monster2B.Name); + } + + Assert.AreEqual(a.InventoryLength, b.Inventory.Count); + for (var i = 0; i < a.InventoryLength; ++i) + { + Assert.AreEqual(a.Inventory(i), b.Inventory[i]); + } + + var inventoryArray = a.GetInventoryArray(); + var inventoryArrayLength = inventoryArray == null ? 0 : inventoryArray.Length; + Assert.AreEqual(inventoryArrayLength, b.Inventory.Count); + for (var i = 0; i < inventoryArrayLength; ++i) + { + Assert.AreEqual(inventoryArray[i], b.Inventory[i]); + } + + Assert.AreEqual(a.Test4Length, b.Test4.Count); + for (var i = 0; i < a.Test4Length; ++i) + { + var t4A = a.Test4(i); + var t4B = b.Test4[i]; + Assert.AreEqual(t4A.Value.A, t4B.A); + Assert.AreEqual(t4A.Value.B, t4B.B); + } + + Assert.AreEqual(a.TestarrayofstringLength, b.Testarrayofstring.Count); + for (var i = 0; i < a.TestarrayofstringLength; ++i) + { + Assert.AreEqual(a.Testarrayofstring(i), b.Testarrayofstring[i]); + } + + Assert.AreEqual(a.Testbool, b.Testbool); + + Assert.AreEqual(a.TestarrayofboolsLength, b.Testarrayofbools.Count); + for (var i = 0; i < a.TestarrayofboolsLength; ++i) + { + Assert.AreEqual(a.Testarrayofbools(i), b.Testarrayofbools[i]); + } + + Assert.AreEqual(a.VectorOfLongsLength, b.VectorOfLongs.Count); + for (var i = 0; i < a.VectorOfLongsLength; ++i) + { + Assert.AreEqual(a.VectorOfLongs(i), b.VectorOfLongs[i]); + } + + Assert.AreEqual(a.VectorOfDoublesLength, b.VectorOfDoubles.Count); + for (var i = 0; i < a.VectorOfDoublesLength; ++i) + { + Assert.AreEqual(a.VectorOfDoubles(i), b.VectorOfDoubles[i]); + } + + Assert.AreEqual(a.VectorOfEnumsLength, b.VectorOfEnums.Count); + for (var i = 0; i < a.VectorOfEnumsLength; ++i) + { + Assert.AreEqual(a.VectorOfEnums(i), b.VectorOfEnums[i]); + } + } + + private void AreEqual(Monster a, Monster b) + { + Assert.AreEqual(a.Hp, b.Hp); + Assert.AreEqual(a.Mana, b.Mana); + Assert.AreEqual(a.Name, b.Name); + + var posA = a.Pos; + var posB = b.Pos; + if (posA != null) + { + Assert.AreEqual(posA.Value.X, posB.Value.X); + Assert.AreEqual(posA.Value.Y, posB.Value.Y); + Assert.AreEqual(posA.Value.Z, posB.Value.Z); + + Assert.AreEqual(posA.Value.Test1, posB.Value.Test1); + Assert.AreEqual(posA.Value.Test2, posB.Value.Test2); + var tA = posA.Value.Test3; + var tB = posB.Value.Test3; + Assert.AreEqual(tA.A, tB.A); + Assert.AreEqual(tA.B, tB.B); + } + + Assert.AreEqual(a.TestType, b.TestType); + if (a.TestType == Any.Monster) + { + var monster2A = a.Test().Value; + var monster2B = b.Test().Value; + Assert.AreEqual(monster2A.Name, monster2B.Name); + } + + Assert.AreEqual(a.InventoryLength, b.InventoryLength); + for (var i = 0; i < a.InventoryLength; ++i) + { + Assert.AreEqual(a.Inventory(i), b.Inventory(i)); + } + + var inventoryArrayA = a.GetInventoryArray(); + var inventoryArrayALength = inventoryArrayA == null ? 0 : inventoryArrayA.Length; + var inventoryArrayB = b.GetInventoryArray(); + var inventoryArrayBLength = inventoryArrayB == null ? 0 : inventoryArrayB.Length; + Assert.AreEqual(inventoryArrayALength, inventoryArrayBLength); + for (var i = 0; i < inventoryArrayALength; ++i) + { + Assert.AreEqual(inventoryArrayA[i], inventoryArrayB[i]); + } + + Assert.AreEqual(a.Test4Length, b.Test4Length); + for (var i = 0; i < a.Test4Length; ++i) + { + var t4A = a.Test4(i); + var t4B = b.Test4(i); + Assert.AreEqual(t4A.Value.A, t4B.Value.A); + Assert.AreEqual(t4A.Value.B, t4B.Value.B); + } + + Assert.AreEqual(a.TestarrayofstringLength, b.TestarrayofstringLength); + for (var i = 0; i < a.TestarrayofstringLength; ++i) + { + Assert.AreEqual(a.Testarrayofstring(i), b.Testarrayofstring(i)); + } + + Assert.AreEqual(a.Testbool, b.Testbool); + + Assert.AreEqual(a.TestarrayofboolsLength, b.TestarrayofboolsLength); + for (var i = 0; i < a.TestarrayofboolsLength; ++i) + { + Assert.AreEqual(a.Testarrayofbools(i), b.Testarrayofbools(i)); + } + + Assert.AreEqual(a.VectorOfLongsLength, b.VectorOfLongsLength); + for (var i = 0; i < a.VectorOfLongsLength; ++i) + { + Assert.AreEqual(a.VectorOfLongs(i), b.VectorOfLongs(i)); + } + + Assert.AreEqual(a.VectorOfDoublesLength, b.VectorOfDoublesLength); + for (var i = 0; i < a.VectorOfDoublesLength; ++i) + { + Assert.AreEqual(a.VectorOfDoubles(i), b.VectorOfDoubles(i)); + } + + Assert.AreEqual(a.VectorOfEnumsLength, b.VectorOfEnumsLength); + for (var i = 0; i < a.VectorOfEnumsLength; ++i) + { + Assert.AreEqual(a.VectorOfEnums(i), b.VectorOfEnums(i)); + } + } + + private void TestObjectAPI(Monster a) + { + var b = a.UnPack(); + AreEqual(a, b); + + var fbb = new FlatBufferBuilder(1); + fbb.Finish(Monster.Pack(fbb, b).Value); + var c = Monster.GetRootAsMonster(fbb.DataBuffer); + AreEqual(a, c); + } + + private void AreEqual(ArrayTable a, ArrayTableT b) + { + Assert.AreEqual(a.A.Value.A, b.A.A); + + for (int i = 0; i < 15; ++i) + { + Assert.AreEqual(a.A.Value.B(i), b.A.B[i]); + } + + Assert.AreEqual(a.A.Value.C, b.A.C); + + for (int i = 0; i < 2; ++i) + { + var ad = a.A.Value.D(i); + var bd = b.A.D[i]; + + for (int j = 0; j < 2; ++j) + { + Assert.AreEqual(ad.A(j), bd.A[j]); + } + + Assert.AreEqual(ad.B, bd.B); + + for (int j = 0; j < 2; ++j) + { + Assert.AreEqual(ad.C(j), bd.C[j]); + } + + for (int j = 0; j < 2; ++j) + { + Assert.AreEqual(ad.D(j), bd.D[j]); + } + } + + Assert.AreEqual(a.A.Value.E, b.A.E); + + for (int i = 0; i < 2; ++i) + { + Assert.AreEqual(a.A.Value.F(i), b.A.F[i]); + } + } + + private void AreEqual(ArrayTable a, ArrayTable b) + { + Assert.AreEqual(a.A.Value.A, b.A.Value.A); + + for (int i = 0; i < 15; ++i) + { + Assert.AreEqual(a.A.Value.B(i), b.A.Value.B(i)); + } + + Assert.AreEqual(a.A.Value.C, b.A.Value.C); + + for (int i = 0; i < 2; ++i) + { + var ad = a.A.Value.D(i); + var bd = b.A.Value.D(i); + + for (int j = 0; j < 2; ++j) + { + Assert.AreEqual(ad.A(j), bd.A(j)); + } + + Assert.AreEqual(ad.B, bd.B); + + for (int j = 0; j < 2; ++j) + { + Assert.AreEqual(ad.C(j), bd.C(j)); + } + + for (int j = 0; j < 2; ++j) + { + Assert.AreEqual(ad.D(j), bd.D(j)); + } + } + + Assert.AreEqual(a.A.Value.E, b.A.Value.E); + + for (int i = 0; i < 2; ++i) + { + Assert.AreEqual(a.A.Value.F(i), b.A.Value.F(i)); + } + } + + private void TestObjectAPI(ArrayTable a) + { + var b = a.UnPack(); + AreEqual(a, b); + + var fbb = new FlatBufferBuilder(1); + fbb.Finish(ArrayTable.Pack(fbb, b).Value); + var c = ArrayTable.GetRootAsArrayTable(fbb.DataBuffer); + AreEqual(a, c); + } + + private void AreEqual(Movie a, MovieT b) + { + Assert.AreEqual(a.MainCharacterType, b.MainCharacter.Type); + Assert.AreEqual(a.MainCharacter().Value.HairLength, b.MainCharacter.AsRapunzel().HairLength); + + Assert.AreEqual(a.CharactersLength, b.Characters.Count); + Assert.AreEqual(a.CharactersType(0), b.Characters[0].Type); + Assert.AreEqual(a.Characters(0).Value.SwordAttackDamage, b.Characters[0].AsMuLan().SwordAttackDamage); + Assert.AreEqual(a.CharactersType(1), b.Characters[1].Type); + Assert.AreEqual(a.Characters(1).Value.BooksRead, b.Characters[1].AsBelle().BooksRead); + Assert.AreEqual(a.CharactersType(2), b.Characters[2].Type); + Assert.AreEqual(a.CharactersAsString(2), b.Characters[2].AsOther()); + } + + private void AreEqual(Movie a, Movie b) + { + Assert.AreEqual(a.MainCharacterType, b.MainCharacterType); + Assert.AreEqual(a.MainCharacter().Value.HairLength, b.MainCharacter().Value.HairLength); + + Assert.AreEqual(a.CharactersLength, b.CharactersLength); + Assert.AreEqual(a.CharactersType(0), b.CharactersType(0)); + Assert.AreEqual(a.Characters(0).Value.SwordAttackDamage, b.Characters(0).Value.SwordAttackDamage); + Assert.AreEqual(a.CharactersType(1), b.CharactersType(1)); + Assert.AreEqual(a.Characters(1).Value.BooksRead, b.Characters(1).Value.BooksRead); + Assert.AreEqual(a.CharactersType(2), b.CharactersType(2)); + Assert.AreEqual(a.CharactersAsString(2), b.CharactersAsString(2)); + } + + private void TestObjectAPI(Movie a) + { + var b = a.UnPack(); + AreEqual(a, b); + + var fbb = new FlatBufferBuilder(1); + fbb.Finish(Movie.Pack(fbb, b).Value); + var c = Movie.GetRootAsMovie(fbb.DataBuffer); + AreEqual(a, c); } } } diff --git a/tests/FlatBuffers.Test/NetTest.sh b/tests/FlatBuffers.Test/NetTest.sh index 3822b4931..f03d97fbe 100644 --- a/tests/FlatBuffers.Test/NetTest.sh +++ b/tests/FlatBuffers.Test/NetTest.sh @@ -3,7 +3,7 @@ # Testing C# on Linux using Mono. mcs -debug -out:./fbnettest.exe \ - ../../net/FlatBuffers/*.cs ../MyGame/Example/*.cs ../MyGame/*.cs ../union_vector/*.cs \ + ../../net/FlatBuffers/*.cs ../MyGame/Example/*.cs ../MyGame/Example2/*.cs ../MyGame/*.cs ../union_vector/*.cs \ FlatBuffersTestClassAttribute.cs FlatBuffersTestMethodAttribute.cs Assert.cs FlatBuffersExampleTests.cs Program.cs ByteBufferTests.cs FlatBufferBuilderTests.cs FlatBuffersFuzzTests.cs FuzzTestData.cs Lcg.cs TestTable.cs mono --debug ./fbnettest.exe rm fbnettest.exe @@ -14,7 +14,7 @@ rm Resources/monsterdata_cstest_sp.mon mcs -debug -out:./fbnettest.exe \ -unsafe -d:UNSAFE_BYTEBUFFER \ - ../../net/FlatBuffers/*.cs ../MyGame/Example/*.cs ../MyGame/*.cs ../union_vector/*.cs\ + ../../net/FlatBuffers/*.cs ../MyGame/Example/*.cs ../MyGame/Example2/*.cs ../MyGame/*.cs ../union_vector/*.cs\ FlatBuffersTestClassAttribute.cs FlatBuffersTestMethodAttribute.cs Assert.cs FlatBuffersExampleTests.cs Program.cs ByteBufferTests.cs FlatBufferBuilderTests.cs FlatBuffersFuzzTests.cs FuzzTestData.cs Lcg.cs TestTable.cs mono --debug ./fbnettest.exe rm fbnettest.exe diff --git a/tests/MyGame/Example/Ability.cs b/tests/MyGame/Example/Ability.cs index 83159850c..1db6a0259 100644 --- a/tests/MyGame/Example/Ability.cs +++ b/tests/MyGame/Example/Ability.cs @@ -6,6 +6,7 @@ namespace MyGame.Example { using global::System; +using global::System.Collections.Generic; using global::FlatBuffers; public struct Ability : IFlatbufferObject @@ -26,7 +27,34 @@ public struct Ability : IFlatbufferObject builder.PutUint(Id); return new Offset(builder.Offset); } + public AbilityT UnPack() { + var _o = new AbilityT(); + this.UnPackTo(_o); + return _o; + } + public void UnPackTo(AbilityT _o) { + _o.Id = this.Id; + _o.Distance = this.Distance; + } + public static Offset Pack(FlatBufferBuilder builder, AbilityT _o) { + if (_o == null) return default(Offset); + return CreateAbility( + builder, + _o.Id, + _o.Distance); + } }; +public class AbilityT +{ + public uint Id { get; set; } + public uint Distance { get; set; } + + public AbilityT() { + this.Id = 0; + this.Distance = 0; + } +} + } diff --git a/tests/MyGame/Example/Any.cs b/tests/MyGame/Example/Any.cs index f95c6bc41..a33120943 100644 --- a/tests/MyGame/Example/Any.cs +++ b/tests/MyGame/Example/Any.cs @@ -13,5 +13,29 @@ public enum Any : byte MyGame_Example2_Monster = 3, }; +public class AnyUnion { + public Any Type { get; set; } + public object Value { get; set; } + + public AnyUnion() { + this.Type = Any.NONE; + this.Value = null; + } + + public T As() where T : class { return this.Value as T; } + public MyGame.Example.MonsterT AsMonster() { return this.As(); } + internal MyGame.Example.TestSimpleTableWithEnumT AsTestSimpleTableWithEnum() { return this.As(); } + public MyGame.Example2.MonsterT AsMyGame_Example2_Monster() { return this.As(); } + + public static int Pack(FlatBuffers.FlatBufferBuilder builder, AnyUnion _o) { + switch (_o.Type) { + default: return 0; + case Any.Monster: return MyGame.Example.Monster.Pack(builder, _o.AsMonster()).Value; + case Any.TestSimpleTableWithEnum: return MyGame.Example.TestSimpleTableWithEnum.Pack(builder, _o.AsTestSimpleTableWithEnum()).Value; + case Any.MyGame_Example2_Monster: return MyGame.Example2.Monster.Pack(builder, _o.AsMyGame_Example2_Monster()).Value; + } + } +} + } diff --git a/tests/MyGame/Example/AnyAmbiguousAliases.cs b/tests/MyGame/Example/AnyAmbiguousAliases.cs index c727b88b3..3c24a77d3 100644 --- a/tests/MyGame/Example/AnyAmbiguousAliases.cs +++ b/tests/MyGame/Example/AnyAmbiguousAliases.cs @@ -13,5 +13,29 @@ public enum AnyAmbiguousAliases : byte M3 = 3, }; +public class AnyAmbiguousAliasesUnion { + public AnyAmbiguousAliases Type { get; set; } + public object Value { get; set; } + + public AnyAmbiguousAliasesUnion() { + this.Type = AnyAmbiguousAliases.NONE; + this.Value = null; + } + + public T As() where T : class { return this.Value as T; } + public MyGame.Example.MonsterT AsM1() { return this.As(); } + public MyGame.Example.MonsterT AsM2() { return this.As(); } + public MyGame.Example.MonsterT AsM3() { return this.As(); } + + public static int Pack(FlatBuffers.FlatBufferBuilder builder, AnyAmbiguousAliasesUnion _o) { + switch (_o.Type) { + default: return 0; + case AnyAmbiguousAliases.M1: return MyGame.Example.Monster.Pack(builder, _o.AsM1()).Value; + case AnyAmbiguousAliases.M2: return MyGame.Example.Monster.Pack(builder, _o.AsM2()).Value; + case AnyAmbiguousAliases.M3: return MyGame.Example.Monster.Pack(builder, _o.AsM3()).Value; + } + } +} + } diff --git a/tests/MyGame/Example/AnyUniqueAliases.cs b/tests/MyGame/Example/AnyUniqueAliases.cs index 42a3e0b77..518e4662b 100644 --- a/tests/MyGame/Example/AnyUniqueAliases.cs +++ b/tests/MyGame/Example/AnyUniqueAliases.cs @@ -13,5 +13,29 @@ public enum AnyUniqueAliases : byte M2 = 3, }; +public class AnyUniqueAliasesUnion { + public AnyUniqueAliases Type { get; set; } + public object Value { get; set; } + + public AnyUniqueAliasesUnion() { + this.Type = AnyUniqueAliases.NONE; + this.Value = null; + } + + public T As() where T : class { return this.Value as T; } + public MyGame.Example.MonsterT AsM() { return this.As(); } + internal MyGame.Example.TestSimpleTableWithEnumT AsTS() { return this.As(); } + public MyGame.Example2.MonsterT AsM2() { return this.As(); } + + public static int Pack(FlatBuffers.FlatBufferBuilder builder, AnyUniqueAliasesUnion _o) { + switch (_o.Type) { + default: return 0; + case AnyUniqueAliases.M: return MyGame.Example.Monster.Pack(builder, _o.AsM()).Value; + case AnyUniqueAliases.TS: return MyGame.Example.TestSimpleTableWithEnum.Pack(builder, _o.AsTS()).Value; + case AnyUniqueAliases.M2: return MyGame.Example2.Monster.Pack(builder, _o.AsM2()).Value; + } + } +} + } diff --git a/tests/MyGame/Example/ArrayStruct.cs b/tests/MyGame/Example/ArrayStruct.cs index bdcbb3f31..1290d417c 100644 --- a/tests/MyGame/Example/ArrayStruct.cs +++ b/tests/MyGame/Example/ArrayStruct.cs @@ -6,6 +6,7 @@ namespace MyGame.Example { using global::System; +using global::System.Collections.Generic; using global::FlatBuffers; public struct ArrayStruct : IFlatbufferObject @@ -56,7 +57,66 @@ public struct ArrayStruct : IFlatbufferObject builder.PutFloat(A); return new Offset(builder.Offset); } + public ArrayStructT UnPack() { + var _o = new ArrayStructT(); + this.UnPackTo(_o); + return _o; + } + public void UnPackTo(ArrayStructT _o) { + _o.A = this.A; + _o.B = new int[15]; + for (var _j = 0; _j < 15; ++_j) { _o.B[_j] = this.B(_j); } + _o.C = this.C; + _o.D = new MyGame.Example.NestedStructT[2]; + for (var _j = 0; _j < 2; ++_j) { _o.D[_j] = this.D(_j).UnPack(); } + _o.E = this.E; + _o.F = new long[2]; + for (var _j = 0; _j < 2; ++_j) { _o.F[_j] = this.F(_j); } + } + public static Offset Pack(FlatBufferBuilder builder, ArrayStructT _o) { + if (_o == null) return default(Offset); + var _b = _o.B; + var _d_a = new int[2,2]; + for (var idx0 = 0; idx0 < 2; ++idx0) {for (var idx1 = 0; idx1 < 2; ++idx1) {_d_a[idx0,idx1] = _o.D[idx0].A[idx1];}} + var _d_b = new MyGame.Example.TestEnum[2]; + for (var idx0 = 0; idx0 < 2; ++idx0) {_d_b[idx0] = _o.D[idx0].B;} + var _d_c = new MyGame.Example.TestEnum[2,2]; + for (var idx0 = 0; idx0 < 2; ++idx0) {for (var idx1 = 0; idx1 < 2; ++idx1) {_d_c[idx0,idx1] = _o.D[idx0].C[idx1];}} + var _d_d = new long[2,2]; + for (var idx0 = 0; idx0 < 2; ++idx0) {for (var idx1 = 0; idx1 < 2; ++idx1) {_d_d[idx0,idx1] = _o.D[idx0].D[idx1];}} + var _f = _o.F; + return CreateArrayStruct( + builder, + _o.A, + _b, + _o.C, + _d_a, + _d_b, + _d_c, + _d_d, + _o.E, + _f); + } }; +public class ArrayStructT +{ + public float A { get; set; } + public int[] B { get; set; } + public sbyte C { get; set; } + public MyGame.Example.NestedStructT[] D { get; set; } + public int E { get; set; } + public long[] F { get; set; } + + public ArrayStructT() { + this.A = 0.0f; + this.B = new int[15]; + this.C = 0; + this.D = new MyGame.Example.NestedStructT[2]; + this.E = 0; + this.F = new long[2]; + } +} + } diff --git a/tests/MyGame/Example/ArrayTable.cs b/tests/MyGame/Example/ArrayTable.cs index e30321169..759e51151 100644 --- a/tests/MyGame/Example/ArrayTable.cs +++ b/tests/MyGame/Example/ArrayTable.cs @@ -6,6 +6,7 @@ namespace MyGame.Example { using global::System; +using global::System.Collections.Generic; using global::FlatBuffers; public struct ArrayTable : IFlatbufferObject @@ -29,7 +30,30 @@ public struct ArrayTable : IFlatbufferObject } public static void FinishArrayTableBuffer(FlatBufferBuilder builder, Offset offset) { builder.Finish(offset.Value, "ARRT"); } public static void FinishSizePrefixedArrayTableBuffer(FlatBufferBuilder builder, Offset offset) { builder.FinishSizePrefixed(offset.Value, "ARRT"); } + public ArrayTableT UnPack() { + var _o = new ArrayTableT(); + this.UnPackTo(_o); + return _o; + } + public void UnPackTo(ArrayTableT _o) { + _o.A = this.A.HasValue ? this.A.Value.UnPack() : null; + } + public static Offset Pack(FlatBufferBuilder builder, ArrayTableT _o) { + if (_o == null) return default(Offset); + StartArrayTable(builder); + AddA(builder, MyGame.Example.ArrayStruct.Pack(builder, _o.A)); + return EndArrayTable(builder); + } }; +public class ArrayTableT +{ + public MyGame.Example.ArrayStructT A { get; set; } + + public ArrayTableT() { + this.A = new MyGame.Example.ArrayStructT(); + } +} + } diff --git a/tests/MyGame/Example/Monster.cs b/tests/MyGame/Example/Monster.cs index 4aa0c2409..13bcb82fa 100644 --- a/tests/MyGame/Example/Monster.cs +++ b/tests/MyGame/Example/Monster.cs @@ -6,6 +6,7 @@ namespace MyGame.Example { using global::System; +using global::System.Collections.Generic; using global::FlatBuffers; /// an example documentation comment: monster object @@ -318,7 +319,374 @@ public struct Monster : IFlatbufferObject } return null; } + public MonsterT UnPack() { + var _o = new MonsterT(); + this.UnPackTo(_o); + return _o; + } + public void UnPackTo(MonsterT _o) { + _o.Pos = this.Pos.HasValue ? this.Pos.Value.UnPack() : null; + _o.Mana = this.Mana; + _o.Hp = this.Hp; + _o.Name = this.Name; + _o.Inventory = new List(); + for (var _j = 0; _j < this.InventoryLength; ++_j) {_o.Inventory.Add(this.Inventory(_j));} + _o.Color = this.Color; + _o.Test = new MyGame.Example.AnyUnion(); + _o.Test.Type = this.TestType; + switch (this.TestType) { + default: break; + case MyGame.Example.Any.Monster: + _o.Test.Value = this.Test().HasValue ? this.Test().Value.UnPack() : null; + break; + case MyGame.Example.Any.TestSimpleTableWithEnum: + _o.Test.Value = this.Test().HasValue ? this.Test().Value.UnPack() : null; + break; + case MyGame.Example.Any.MyGame_Example2_Monster: + _o.Test.Value = this.Test().HasValue ? this.Test().Value.UnPack() : null; + break; + } + _o.Test4 = new List(); + for (var _j = 0; _j < this.Test4Length; ++_j) {_o.Test4.Add(this.Test4(_j).HasValue ? this.Test4(_j).Value.UnPack() : null);} + _o.Testarrayofstring = new List(); + for (var _j = 0; _j < this.TestarrayofstringLength; ++_j) {_o.Testarrayofstring.Add(this.Testarrayofstring(_j));} + _o.Testarrayoftables = new List(); + for (var _j = 0; _j < this.TestarrayoftablesLength; ++_j) {_o.Testarrayoftables.Add(this.Testarrayoftables(_j).HasValue ? this.Testarrayoftables(_j).Value.UnPack() : null);} + _o.Enemy = this.Enemy.HasValue ? this.Enemy.Value.UnPack() : null; + _o.Testnestedflatbuffer = new List(); + for (var _j = 0; _j < this.TestnestedflatbufferLength; ++_j) {_o.Testnestedflatbuffer.Add(this.Testnestedflatbuffer(_j));} + _o.Testempty = this.Testempty.HasValue ? this.Testempty.Value.UnPack() : null; + _o.Testbool = this.Testbool; + _o.Testhashs32Fnv1 = this.Testhashs32Fnv1; + _o.Testhashu32Fnv1 = this.Testhashu32Fnv1; + _o.Testhashs64Fnv1 = this.Testhashs64Fnv1; + _o.Testhashu64Fnv1 = this.Testhashu64Fnv1; + _o.Testhashs32Fnv1a = this.Testhashs32Fnv1a; + _o.Testhashu32Fnv1a = this.Testhashu32Fnv1a; + _o.Testhashs64Fnv1a = this.Testhashs64Fnv1a; + _o.Testhashu64Fnv1a = this.Testhashu64Fnv1a; + _o.Testarrayofbools = new List(); + for (var _j = 0; _j < this.TestarrayofboolsLength; ++_j) {_o.Testarrayofbools.Add(this.Testarrayofbools(_j));} + _o.Testf = this.Testf; + _o.Testf2 = this.Testf2; + _o.Testf3 = this.Testf3; + _o.Testarrayofstring2 = new List(); + for (var _j = 0; _j < this.Testarrayofstring2Length; ++_j) {_o.Testarrayofstring2.Add(this.Testarrayofstring2(_j));} + _o.Testarrayofsortedstruct = new List(); + for (var _j = 0; _j < this.TestarrayofsortedstructLength; ++_j) {_o.Testarrayofsortedstruct.Add(this.Testarrayofsortedstruct(_j).HasValue ? this.Testarrayofsortedstruct(_j).Value.UnPack() : null);} + _o.Flex = new List(); + for (var _j = 0; _j < this.FlexLength; ++_j) {_o.Flex.Add(this.Flex(_j));} + _o.Test5 = new List(); + for (var _j = 0; _j < this.Test5Length; ++_j) {_o.Test5.Add(this.Test5(_j).HasValue ? this.Test5(_j).Value.UnPack() : null);} + _o.VectorOfLongs = new List(); + for (var _j = 0; _j < this.VectorOfLongsLength; ++_j) {_o.VectorOfLongs.Add(this.VectorOfLongs(_j));} + _o.VectorOfDoubles = new List(); + for (var _j = 0; _j < this.VectorOfDoublesLength; ++_j) {_o.VectorOfDoubles.Add(this.VectorOfDoubles(_j));} + _o.ParentNamespaceTest = this.ParentNamespaceTest.HasValue ? this.ParentNamespaceTest.Value.UnPack() : null; + _o.VectorOfReferrables = new List(); + for (var _j = 0; _j < this.VectorOfReferrablesLength; ++_j) {_o.VectorOfReferrables.Add(this.VectorOfReferrables(_j).HasValue ? this.VectorOfReferrables(_j).Value.UnPack() : null);} + _o.SingleWeakReference = this.SingleWeakReference; + _o.VectorOfWeakReferences = new List(); + for (var _j = 0; _j < this.VectorOfWeakReferencesLength; ++_j) {_o.VectorOfWeakReferences.Add(this.VectorOfWeakReferences(_j));} + _o.VectorOfStrongReferrables = new List(); + for (var _j = 0; _j < this.VectorOfStrongReferrablesLength; ++_j) {_o.VectorOfStrongReferrables.Add(this.VectorOfStrongReferrables(_j).HasValue ? this.VectorOfStrongReferrables(_j).Value.UnPack() : null);} + _o.CoOwningReference = this.CoOwningReference; + _o.VectorOfCoOwningReferences = new List(); + for (var _j = 0; _j < this.VectorOfCoOwningReferencesLength; ++_j) {_o.VectorOfCoOwningReferences.Add(this.VectorOfCoOwningReferences(_j));} + _o.NonOwningReference = this.NonOwningReference; + _o.VectorOfNonOwningReferences = new List(); + for (var _j = 0; _j < this.VectorOfNonOwningReferencesLength; ++_j) {_o.VectorOfNonOwningReferences.Add(this.VectorOfNonOwningReferences(_j));} + _o.AnyUnique = new MyGame.Example.AnyUniqueAliasesUnion(); + _o.AnyUnique.Type = this.AnyUniqueType; + switch (this.AnyUniqueType) { + default: break; + case MyGame.Example.AnyUniqueAliases.M: + _o.AnyUnique.Value = this.AnyUnique().HasValue ? this.AnyUnique().Value.UnPack() : null; + break; + case MyGame.Example.AnyUniqueAliases.TS: + _o.AnyUnique.Value = this.AnyUnique().HasValue ? this.AnyUnique().Value.UnPack() : null; + break; + case MyGame.Example.AnyUniqueAliases.M2: + _o.AnyUnique.Value = this.AnyUnique().HasValue ? this.AnyUnique().Value.UnPack() : null; + break; + } + _o.AnyAmbiguous = new MyGame.Example.AnyAmbiguousAliasesUnion(); + _o.AnyAmbiguous.Type = this.AnyAmbiguousType; + switch (this.AnyAmbiguousType) { + default: break; + case MyGame.Example.AnyAmbiguousAliases.M1: + _o.AnyAmbiguous.Value = this.AnyAmbiguous().HasValue ? this.AnyAmbiguous().Value.UnPack() : null; + break; + case MyGame.Example.AnyAmbiguousAliases.M2: + _o.AnyAmbiguous.Value = this.AnyAmbiguous().HasValue ? this.AnyAmbiguous().Value.UnPack() : null; + break; + case MyGame.Example.AnyAmbiguousAliases.M3: + _o.AnyAmbiguous.Value = this.AnyAmbiguous().HasValue ? this.AnyAmbiguous().Value.UnPack() : null; + break; + } + _o.VectorOfEnums = new List(); + for (var _j = 0; _j < this.VectorOfEnumsLength; ++_j) {_o.VectorOfEnums.Add(this.VectorOfEnums(_j));} + _o.SignedEnum = this.SignedEnum; + } + public static Offset Pack(FlatBufferBuilder builder, MonsterT _o) { + if (_o == null) return default(Offset); + var _name = _o.Name == null ? default(StringOffset) : builder.CreateString(_o.Name); + var _inventory = default(VectorOffset); + if (_o.Inventory != null) { + var __inventory = _o.Inventory.ToArray(); + _inventory = CreateInventoryVector(builder, __inventory); + } + var _test_type = _o.Test == null ? MyGame.Example.Any.NONE : _o.Test.Type; + var _test = _o.Test == null ? 0 : MyGame.Example.AnyUnion.Pack(builder, _o.Test); + var _test4 = default(VectorOffset); + if (_o.Test4 != null) { + StartTest4Vector(builder, _o.Test4.Count); + for (var _j = _o.Test4.Count - 1; _j >= 0; --_j) { MyGame.Example.Test.Pack(builder, _o.Test4[_j]); } + _test4 = builder.EndVector(); + } + var _testarrayofstring = default(VectorOffset); + if (_o.Testarrayofstring != null) { + var __testarrayofstring = new StringOffset[_o.Testarrayofstring.Count]; + for (var _j = 0; _j < __testarrayofstring.Length; ++_j) { __testarrayofstring[_j] = builder.CreateString(_o.Testarrayofstring[_j]); } + _testarrayofstring = CreateTestarrayofstringVector(builder, __testarrayofstring); + } + var _testarrayoftables = default(VectorOffset); + if (_o.Testarrayoftables != null) { + var __testarrayoftables = new Offset[_o.Testarrayoftables.Count]; + for (var _j = 0; _j < __testarrayoftables.Length; ++_j) { __testarrayoftables[_j] = MyGame.Example.Monster.Pack(builder, _o.Testarrayoftables[_j]); } + _testarrayoftables = CreateTestarrayoftablesVector(builder, __testarrayoftables); + } + var _enemy = _o.Enemy == null ? default(Offset) : MyGame.Example.Monster.Pack(builder, _o.Enemy); + var _testnestedflatbuffer = default(VectorOffset); + if (_o.Testnestedflatbuffer != null) { + var __testnestedflatbuffer = _o.Testnestedflatbuffer.ToArray(); + _testnestedflatbuffer = CreateTestnestedflatbufferVector(builder, __testnestedflatbuffer); + } + var _testempty = _o.Testempty == null ? default(Offset) : MyGame.Example.Stat.Pack(builder, _o.Testempty); + var _testarrayofbools = default(VectorOffset); + if (_o.Testarrayofbools != null) { + var __testarrayofbools = _o.Testarrayofbools.ToArray(); + _testarrayofbools = CreateTestarrayofboolsVector(builder, __testarrayofbools); + } + var _testarrayofstring2 = default(VectorOffset); + if (_o.Testarrayofstring2 != null) { + var __testarrayofstring2 = new StringOffset[_o.Testarrayofstring2.Count]; + for (var _j = 0; _j < __testarrayofstring2.Length; ++_j) { __testarrayofstring2[_j] = builder.CreateString(_o.Testarrayofstring2[_j]); } + _testarrayofstring2 = CreateTestarrayofstring2Vector(builder, __testarrayofstring2); + } + var _testarrayofsortedstruct = default(VectorOffset); + if (_o.Testarrayofsortedstruct != null) { + StartTestarrayofsortedstructVector(builder, _o.Testarrayofsortedstruct.Count); + for (var _j = _o.Testarrayofsortedstruct.Count - 1; _j >= 0; --_j) { MyGame.Example.Ability.Pack(builder, _o.Testarrayofsortedstruct[_j]); } + _testarrayofsortedstruct = builder.EndVector(); + } + var _flex = default(VectorOffset); + if (_o.Flex != null) { + var __flex = _o.Flex.ToArray(); + _flex = CreateFlexVector(builder, __flex); + } + var _test5 = default(VectorOffset); + if (_o.Test5 != null) { + StartTest5Vector(builder, _o.Test5.Count); + for (var _j = _o.Test5.Count - 1; _j >= 0; --_j) { MyGame.Example.Test.Pack(builder, _o.Test5[_j]); } + _test5 = builder.EndVector(); + } + var _vector_of_longs = default(VectorOffset); + if (_o.VectorOfLongs != null) { + var __vector_of_longs = _o.VectorOfLongs.ToArray(); + _vector_of_longs = CreateVectorOfLongsVector(builder, __vector_of_longs); + } + var _vector_of_doubles = default(VectorOffset); + if (_o.VectorOfDoubles != null) { + var __vector_of_doubles = _o.VectorOfDoubles.ToArray(); + _vector_of_doubles = CreateVectorOfDoublesVector(builder, __vector_of_doubles); + } + var _parent_namespace_test = _o.ParentNamespaceTest == null ? default(Offset) : MyGame.InParentNamespace.Pack(builder, _o.ParentNamespaceTest); + var _vector_of_referrables = default(VectorOffset); + if (_o.VectorOfReferrables != null) { + var __vector_of_referrables = new Offset[_o.VectorOfReferrables.Count]; + for (var _j = 0; _j < __vector_of_referrables.Length; ++_j) { __vector_of_referrables[_j] = MyGame.Example.Referrable.Pack(builder, _o.VectorOfReferrables[_j]); } + _vector_of_referrables = CreateVectorOfReferrablesVector(builder, __vector_of_referrables); + } + var _vector_of_weak_references = default(VectorOffset); + if (_o.VectorOfWeakReferences != null) { + var __vector_of_weak_references = _o.VectorOfWeakReferences.ToArray(); + _vector_of_weak_references = CreateVectorOfWeakReferencesVector(builder, __vector_of_weak_references); + } + var _vector_of_strong_referrables = default(VectorOffset); + if (_o.VectorOfStrongReferrables != null) { + var __vector_of_strong_referrables = new Offset[_o.VectorOfStrongReferrables.Count]; + for (var _j = 0; _j < __vector_of_strong_referrables.Length; ++_j) { __vector_of_strong_referrables[_j] = MyGame.Example.Referrable.Pack(builder, _o.VectorOfStrongReferrables[_j]); } + _vector_of_strong_referrables = CreateVectorOfStrongReferrablesVector(builder, __vector_of_strong_referrables); + } + var _vector_of_co_owning_references = default(VectorOffset); + if (_o.VectorOfCoOwningReferences != null) { + var __vector_of_co_owning_references = _o.VectorOfCoOwningReferences.ToArray(); + _vector_of_co_owning_references = CreateVectorOfCoOwningReferencesVector(builder, __vector_of_co_owning_references); + } + var _vector_of_non_owning_references = default(VectorOffset); + if (_o.VectorOfNonOwningReferences != null) { + var __vector_of_non_owning_references = _o.VectorOfNonOwningReferences.ToArray(); + _vector_of_non_owning_references = CreateVectorOfNonOwningReferencesVector(builder, __vector_of_non_owning_references); + } + var _any_unique_type = _o.AnyUnique == null ? MyGame.Example.AnyUniqueAliases.NONE : _o.AnyUnique.Type; + var _any_unique = _o.AnyUnique == null ? 0 : MyGame.Example.AnyUniqueAliasesUnion.Pack(builder, _o.AnyUnique); + var _any_ambiguous_type = _o.AnyAmbiguous == null ? MyGame.Example.AnyAmbiguousAliases.NONE : _o.AnyAmbiguous.Type; + var _any_ambiguous = _o.AnyAmbiguous == null ? 0 : MyGame.Example.AnyAmbiguousAliasesUnion.Pack(builder, _o.AnyAmbiguous); + var _vector_of_enums = default(VectorOffset); + if (_o.VectorOfEnums != null) { + var __vector_of_enums = _o.VectorOfEnums.ToArray(); + _vector_of_enums = CreateVectorOfEnumsVector(builder, __vector_of_enums); + } + StartMonster(builder); + AddPos(builder, MyGame.Example.Vec3.Pack(builder, _o.Pos)); + AddMana(builder, _o.Mana); + AddHp(builder, _o.Hp); + AddName(builder, _name); + AddInventory(builder, _inventory); + AddColor(builder, _o.Color); + AddTestType(builder, _test_type); + AddTest(builder, _test); + AddTest4(builder, _test4); + AddTestarrayofstring(builder, _testarrayofstring); + AddTestarrayoftables(builder, _testarrayoftables); + AddEnemy(builder, _enemy); + AddTestnestedflatbuffer(builder, _testnestedflatbuffer); + AddTestempty(builder, _testempty); + AddTestbool(builder, _o.Testbool); + AddTesthashs32Fnv1(builder, _o.Testhashs32Fnv1); + AddTesthashu32Fnv1(builder, _o.Testhashu32Fnv1); + AddTesthashs64Fnv1(builder, _o.Testhashs64Fnv1); + AddTesthashu64Fnv1(builder, _o.Testhashu64Fnv1); + AddTesthashs32Fnv1a(builder, _o.Testhashs32Fnv1a); + AddTesthashu32Fnv1a(builder, _o.Testhashu32Fnv1a); + AddTesthashs64Fnv1a(builder, _o.Testhashs64Fnv1a); + AddTesthashu64Fnv1a(builder, _o.Testhashu64Fnv1a); + AddTestarrayofbools(builder, _testarrayofbools); + AddTestf(builder, _o.Testf); + AddTestf2(builder, _o.Testf2); + AddTestf3(builder, _o.Testf3); + AddTestarrayofstring2(builder, _testarrayofstring2); + AddTestarrayofsortedstruct(builder, _testarrayofsortedstruct); + AddFlex(builder, _flex); + AddTest5(builder, _test5); + AddVectorOfLongs(builder, _vector_of_longs); + AddVectorOfDoubles(builder, _vector_of_doubles); + AddParentNamespaceTest(builder, _parent_namespace_test); + AddVectorOfReferrables(builder, _vector_of_referrables); + AddSingleWeakReference(builder, _o.SingleWeakReference); + AddVectorOfWeakReferences(builder, _vector_of_weak_references); + AddVectorOfStrongReferrables(builder, _vector_of_strong_referrables); + AddCoOwningReference(builder, _o.CoOwningReference); + AddVectorOfCoOwningReferences(builder, _vector_of_co_owning_references); + AddNonOwningReference(builder, _o.NonOwningReference); + AddVectorOfNonOwningReferences(builder, _vector_of_non_owning_references); + AddAnyUniqueType(builder, _any_unique_type); + AddAnyUnique(builder, _any_unique); + AddAnyAmbiguousType(builder, _any_ambiguous_type); + AddAnyAmbiguous(builder, _any_ambiguous); + AddVectorOfEnums(builder, _vector_of_enums); + AddSignedEnum(builder, _o.SignedEnum); + return EndMonster(builder); + } }; +public class MonsterT +{ + public MyGame.Example.Vec3T Pos { get; set; } + public short Mana { get; set; } + public short Hp { get; set; } + public string Name { get; set; } + public List Inventory { get; set; } + public MyGame.Example.Color Color { get; set; } + public MyGame.Example.AnyUnion Test { get; set; } + public List Test4 { get; set; } + public List Testarrayofstring { get; set; } + public List Testarrayoftables { get; set; } + public MyGame.Example.MonsterT Enemy { get; set; } + public List Testnestedflatbuffer { get; set; } + public MyGame.Example.StatT Testempty { get; set; } + public bool Testbool { get; set; } + public int Testhashs32Fnv1 { get; set; } + public uint Testhashu32Fnv1 { get; set; } + public long Testhashs64Fnv1 { get; set; } + public ulong Testhashu64Fnv1 { get; set; } + public int Testhashs32Fnv1a { get; set; } + public uint Testhashu32Fnv1a { get; set; } + public long Testhashs64Fnv1a { get; set; } + public ulong Testhashu64Fnv1a { get; set; } + public List Testarrayofbools { get; set; } + public float Testf { get; set; } + public float Testf2 { get; set; } + public float Testf3 { get; set; } + public List Testarrayofstring2 { get; set; } + public List Testarrayofsortedstruct { get; set; } + public List Flex { get; set; } + public List Test5 { get; set; } + public List VectorOfLongs { get; set; } + public List VectorOfDoubles { get; set; } + public MyGame.InParentNamespaceT ParentNamespaceTest { get; set; } + public List VectorOfReferrables { get; set; } + public ulong SingleWeakReference { get; set; } + public List VectorOfWeakReferences { get; set; } + public List VectorOfStrongReferrables { get; set; } + public ulong CoOwningReference { get; set; } + public List VectorOfCoOwningReferences { get; set; } + public ulong NonOwningReference { get; set; } + public List VectorOfNonOwningReferences { get; set; } + public MyGame.Example.AnyUniqueAliasesUnion AnyUnique { get; set; } + public MyGame.Example.AnyAmbiguousAliasesUnion AnyAmbiguous { get; set; } + public List VectorOfEnums { get; set; } + public MyGame.Example.Race SignedEnum { get; set; } + + public MonsterT() { + this.Pos = new MyGame.Example.Vec3T(); + this.Mana = 150; + this.Hp = 100; + this.Name = null; + this.Inventory = null; + this.Color = MyGame.Example.Color.Blue; + this.Test = null; + this.Test4 = null; + this.Testarrayofstring = null; + this.Testarrayoftables = null; + this.Enemy = null; + this.Testnestedflatbuffer = null; + this.Testempty = null; + this.Testbool = false; + this.Testhashs32Fnv1 = 0; + this.Testhashu32Fnv1 = 0; + this.Testhashs64Fnv1 = 0; + this.Testhashu64Fnv1 = 0; + this.Testhashs32Fnv1a = 0; + this.Testhashu32Fnv1a = 0; + this.Testhashs64Fnv1a = 0; + this.Testhashu64Fnv1a = 0; + this.Testarrayofbools = null; + this.Testf = 3.14159f; + this.Testf2 = 3.0f; + this.Testf3 = 0.0f; + this.Testarrayofstring2 = null; + this.Testarrayofsortedstruct = null; + this.Flex = null; + this.Test5 = null; + this.VectorOfLongs = null; + this.VectorOfDoubles = null; + this.ParentNamespaceTest = null; + this.VectorOfReferrables = null; + this.SingleWeakReference = 0; + this.VectorOfWeakReferences = null; + this.VectorOfStrongReferrables = null; + this.CoOwningReference = 0; + this.VectorOfCoOwningReferences = null; + this.NonOwningReference = 0; + this.VectorOfNonOwningReferences = null; + this.AnyUnique = null; + this.AnyAmbiguous = null; + this.VectorOfEnums = null; + this.SignedEnum = MyGame.Example.Race.None; + } +} + } diff --git a/tests/MyGame/Example/NestedStruct.cs b/tests/MyGame/Example/NestedStruct.cs index 1e74524b7..190ffa02f 100644 --- a/tests/MyGame/Example/NestedStruct.cs +++ b/tests/MyGame/Example/NestedStruct.cs @@ -6,6 +6,7 @@ namespace MyGame.Example { using global::System; +using global::System.Collections.Generic; using global::FlatBuffers; public struct NestedStruct : IFlatbufferObject @@ -39,7 +40,48 @@ public struct NestedStruct : IFlatbufferObject } return new Offset(builder.Offset); } + public NestedStructT UnPack() { + var _o = new NestedStructT(); + this.UnPackTo(_o); + return _o; + } + public void UnPackTo(NestedStructT _o) { + _o.A = new int[2]; + for (var _j = 0; _j < 2; ++_j) { _o.A[_j] = this.A(_j); } + _o.B = this.B; + _o.C = new MyGame.Example.TestEnum[2]; + for (var _j = 0; _j < 2; ++_j) { _o.C[_j] = this.C(_j); } + _o.D = new long[2]; + for (var _j = 0; _j < 2; ++_j) { _o.D[_j] = this.D(_j); } + } + public static Offset Pack(FlatBufferBuilder builder, NestedStructT _o) { + if (_o == null) return default(Offset); + var _a = _o.A; + var _c = _o.C; + var _d = _o.D; + return CreateNestedStruct( + builder, + _a, + _o.B, + _c, + _d); + } }; +public class NestedStructT +{ + public int[] A { get; set; } + public MyGame.Example.TestEnum B { get; set; } + public MyGame.Example.TestEnum[] C { get; set; } + public long[] D { get; set; } + + public NestedStructT() { + this.A = new int[2]; + this.B = MyGame.Example.TestEnum.A; + this.C = new MyGame.Example.TestEnum[2]; + this.D = new long[2]; + } +} + } diff --git a/tests/MyGame/Example/Referrable.cs b/tests/MyGame/Example/Referrable.cs index 5a79f9115..43382a0ed 100644 --- a/tests/MyGame/Example/Referrable.cs +++ b/tests/MyGame/Example/Referrable.cs @@ -6,6 +6,7 @@ namespace MyGame.Example { using global::System; +using global::System.Collections.Generic; using global::FlatBuffers; public struct Referrable : IFlatbufferObject @@ -59,7 +60,30 @@ public struct Referrable : IFlatbufferObject } return null; } + public ReferrableT UnPack() { + var _o = new ReferrableT(); + this.UnPackTo(_o); + return _o; + } + public void UnPackTo(ReferrableT _o) { + _o.Id = this.Id; + } + public static Offset Pack(FlatBufferBuilder builder, ReferrableT _o) { + if (_o == null) return default(Offset); + return CreateReferrable( + builder, + _o.Id); + } }; +public class ReferrableT +{ + public ulong Id { get; set; } + + public ReferrableT() { + this.Id = 0; + } +} + } diff --git a/tests/MyGame/Example/Stat.cs b/tests/MyGame/Example/Stat.cs index 7b6c4f0c6..0c7160d2f 100644 --- a/tests/MyGame/Example/Stat.cs +++ b/tests/MyGame/Example/Stat.cs @@ -6,6 +6,7 @@ namespace MyGame.Example { using global::System; +using global::System.Collections.Generic; using global::FlatBuffers; public struct Stat : IFlatbufferObject @@ -49,7 +50,39 @@ public struct Stat : IFlatbufferObject int o = builder.EndTable(); return new Offset(o); } + public StatT UnPack() { + var _o = new StatT(); + this.UnPackTo(_o); + return _o; + } + public void UnPackTo(StatT _o) { + _o.Id = this.Id; + _o.Val = this.Val; + _o.Count = this.Count; + } + public static Offset Pack(FlatBufferBuilder builder, StatT _o) { + if (_o == null) return default(Offset); + var _id = _o.Id == null ? default(StringOffset) : builder.CreateString(_o.Id); + return CreateStat( + builder, + _id, + _o.Val, + _o.Count); + } }; +public class StatT +{ + public string Id { get; set; } + public long Val { get; set; } + public ushort Count { get; set; } + + public StatT() { + this.Id = null; + this.Val = 0; + this.Count = 0; + } +} + } diff --git a/tests/MyGame/Example/Test.cs b/tests/MyGame/Example/Test.cs index cd9145454..caac6fec1 100644 --- a/tests/MyGame/Example/Test.cs +++ b/tests/MyGame/Example/Test.cs @@ -6,6 +6,7 @@ namespace MyGame.Example { using global::System; +using global::System.Collections.Generic; using global::FlatBuffers; public struct Test : IFlatbufferObject @@ -27,7 +28,34 @@ public struct Test : IFlatbufferObject builder.PutShort(A); return new Offset(builder.Offset); } + public TestT UnPack() { + var _o = new TestT(); + this.UnPackTo(_o); + return _o; + } + public void UnPackTo(TestT _o) { + _o.A = this.A; + _o.B = this.B; + } + public static Offset Pack(FlatBufferBuilder builder, TestT _o) { + if (_o == null) return default(Offset); + return CreateTest( + builder, + _o.A, + _o.B); + } }; +public class TestT +{ + public short A { get; set; } + public sbyte B { get; set; } + + public TestT() { + this.A = 0; + this.B = 0; + } +} + } diff --git a/tests/MyGame/Example/TestSimpleTableWithEnum.cs b/tests/MyGame/Example/TestSimpleTableWithEnum.cs index f9ac42ebe..98d56d1fa 100644 --- a/tests/MyGame/Example/TestSimpleTableWithEnum.cs +++ b/tests/MyGame/Example/TestSimpleTableWithEnum.cs @@ -6,6 +6,7 @@ namespace MyGame.Example { using global::System; +using global::System.Collections.Generic; using global::FlatBuffers; internal partial struct TestSimpleTableWithEnum : IFlatbufferObject @@ -34,7 +35,30 @@ internal partial struct TestSimpleTableWithEnum : IFlatbufferObject int o = builder.EndTable(); return new Offset(o); } + public TestSimpleTableWithEnumT UnPack() { + var _o = new TestSimpleTableWithEnumT(); + this.UnPackTo(_o); + return _o; + } + public void UnPackTo(TestSimpleTableWithEnumT _o) { + _o.Color = this.Color; + } + public static Offset Pack(FlatBufferBuilder builder, TestSimpleTableWithEnumT _o) { + if (_o == null) return default(Offset); + return CreateTestSimpleTableWithEnum( + builder, + _o.Color); + } }; +internal partial class TestSimpleTableWithEnumT +{ + public MyGame.Example.Color Color { get; set; } + + public TestSimpleTableWithEnumT() { + this.Color = MyGame.Example.Color.Green; + } +} + } diff --git a/tests/MyGame/Example/TypeAliases.cs b/tests/MyGame/Example/TypeAliases.cs index 8b1281906..b74b8c914 100644 --- a/tests/MyGame/Example/TypeAliases.cs +++ b/tests/MyGame/Example/TypeAliases.cs @@ -6,6 +6,7 @@ namespace MyGame.Example { using global::System; +using global::System.Collections.Generic; using global::FlatBuffers; public struct TypeAliases : IFlatbufferObject @@ -109,7 +110,86 @@ public struct TypeAliases : IFlatbufferObject int o = builder.EndTable(); return new Offset(o); } + public TypeAliasesT UnPack() { + var _o = new TypeAliasesT(); + this.UnPackTo(_o); + return _o; + } + public void UnPackTo(TypeAliasesT _o) { + _o.I8 = this.I8; + _o.U8 = this.U8; + _o.I16 = this.I16; + _o.U16 = this.U16; + _o.I32 = this.I32; + _o.U32 = this.U32; + _o.I64 = this.I64; + _o.U64 = this.U64; + _o.F32 = this.F32; + _o.F64 = this.F64; + _o.V8 = new List(); + for (var _j = 0; _j < this.V8Length; ++_j) {_o.V8.Add(this.V8(_j));} + _o.Vf64 = new List(); + for (var _j = 0; _j < this.Vf64Length; ++_j) {_o.Vf64.Add(this.Vf64(_j));} + } + public static Offset Pack(FlatBufferBuilder builder, TypeAliasesT _o) { + if (_o == null) return default(Offset); + var _v8 = default(VectorOffset); + if (_o.V8 != null) { + var __v8 = _o.V8.ToArray(); + _v8 = CreateV8Vector(builder, __v8); + } + var _vf64 = default(VectorOffset); + if (_o.Vf64 != null) { + var __vf64 = _o.Vf64.ToArray(); + _vf64 = CreateVf64Vector(builder, __vf64); + } + return CreateTypeAliases( + builder, + _o.I8, + _o.U8, + _o.I16, + _o.U16, + _o.I32, + _o.U32, + _o.I64, + _o.U64, + _o.F32, + _o.F64, + _v8, + _vf64); + } }; +public class TypeAliasesT +{ + public sbyte I8 { get; set; } + public byte U8 { get; set; } + public short I16 { get; set; } + public ushort U16 { get; set; } + public int I32 { get; set; } + public uint U32 { get; set; } + public long I64 { get; set; } + public ulong U64 { get; set; } + public float F32 { get; set; } + public double F64 { get; set; } + public List V8 { get; set; } + public List Vf64 { get; set; } + + public TypeAliasesT() { + this.I8 = 0; + this.U8 = 0; + this.I16 = 0; + this.U16 = 0; + this.I32 = 0; + this.U32 = 0; + this.I64 = 0; + this.U64 = 0; + this.F32 = 0.0f; + this.F64 = 0.0; + this.V8 = null; + this.Vf64 = null; + } +} + } diff --git a/tests/MyGame/Example/Vec3.cs b/tests/MyGame/Example/Vec3.cs index 1dbb31584..9c106f52a 100644 --- a/tests/MyGame/Example/Vec3.cs +++ b/tests/MyGame/Example/Vec3.cs @@ -6,6 +6,7 @@ namespace MyGame.Example { using global::System; +using global::System.Collections.Generic; using global::FlatBuffers; public struct Vec3 : IFlatbufferObject @@ -43,7 +44,51 @@ public struct Vec3 : IFlatbufferObject builder.PutFloat(X); return new Offset(builder.Offset); } + public Vec3T UnPack() { + var _o = new Vec3T(); + this.UnPackTo(_o); + return _o; + } + public void UnPackTo(Vec3T _o) { + _o.X = this.X; + _o.Y = this.Y; + _o.Z = this.Z; + _o.Test1 = this.Test1; + _o.Test2 = this.Test2; + _o.Test3 = this.Test3.UnPack(); + } + public static Offset Pack(FlatBufferBuilder builder, Vec3T _o) { + if (_o == null) return default(Offset); + return CreateVec3( + builder, + _o.X, + _o.Y, + _o.Z, + _o.Test1, + _o.Test2, + _o.Test3.A, + _o.Test3.B); + } }; +public class Vec3T +{ + public float X { get; set; } + public float Y { get; set; } + public float Z { get; set; } + public double Test1 { get; set; } + public MyGame.Example.Color Test2 { get; set; } + public MyGame.Example.TestT Test3 { get; set; } + + public Vec3T() { + this.X = 0.0f; + this.Y = 0.0f; + this.Z = 0.0f; + this.Test1 = 0.0; + this.Test2 = 0; + this.Test3 = new MyGame.Example.TestT(); + } +} + } diff --git a/tests/MyGame/Example2/Monster.cs b/tests/MyGame/Example2/Monster.cs index a6e9dcee7..cf4a692c1 100644 --- a/tests/MyGame/Example2/Monster.cs +++ b/tests/MyGame/Example2/Monster.cs @@ -6,6 +6,7 @@ namespace MyGame.Example2 { using global::System; +using global::System.Collections.Generic; using global::FlatBuffers; public struct Monster : IFlatbufferObject @@ -24,7 +25,26 @@ public struct Monster : IFlatbufferObject int o = builder.EndTable(); return new Offset(o); } + public MonsterT UnPack() { + var _o = new MonsterT(); + this.UnPackTo(_o); + return _o; + } + public void UnPackTo(MonsterT _o) { + } + public static Offset Pack(FlatBufferBuilder builder, MonsterT _o) { + if (_o == null) return default(Offset); + StartMonster(builder); + return EndMonster(builder); + } }; +public class MonsterT +{ + + public MonsterT() { + } +} + } diff --git a/tests/MyGame/InParentNamespace.cs b/tests/MyGame/InParentNamespace.cs index 869c400f3..c252a0d0b 100644 --- a/tests/MyGame/InParentNamespace.cs +++ b/tests/MyGame/InParentNamespace.cs @@ -6,6 +6,7 @@ namespace MyGame { using global::System; +using global::System.Collections.Generic; using global::FlatBuffers; public struct InParentNamespace : IFlatbufferObject @@ -24,7 +25,26 @@ public struct InParentNamespace : IFlatbufferObject int o = builder.EndTable(); return new Offset(o); } + public InParentNamespaceT UnPack() { + var _o = new InParentNamespaceT(); + this.UnPackTo(_o); + return _o; + } + public void UnPackTo(InParentNamespaceT _o) { + } + public static Offset Pack(FlatBufferBuilder builder, InParentNamespaceT _o) { + if (_o == null) return default(Offset); + StartInParentNamespace(builder); + return EndInParentNamespace(builder); + } }; +public class InParentNamespaceT +{ + + public InParentNamespaceT() { + } +} + } diff --git a/tests/MyGame/MonsterExtra.cs b/tests/MyGame/MonsterExtra.cs index fa145b501..b85426d9b 100644 --- a/tests/MyGame/MonsterExtra.cs +++ b/tests/MyGame/MonsterExtra.cs @@ -6,6 +6,7 @@ namespace MyGame { using global::System; +using global::System.Collections.Generic; using global::FlatBuffers; public struct MonsterExtra : IFlatbufferObject @@ -102,7 +103,78 @@ public struct MonsterExtra : IFlatbufferObject } public static void FinishMonsterExtraBuffer(FlatBufferBuilder builder, Offset offset) { builder.Finish(offset.Value, "MONE"); } public static void FinishSizePrefixedMonsterExtraBuffer(FlatBufferBuilder builder, Offset offset) { builder.FinishSizePrefixed(offset.Value, "MONE"); } + public MonsterExtraT UnPack() { + var _o = new MonsterExtraT(); + this.UnPackTo(_o); + return _o; + } + public void UnPackTo(MonsterExtraT _o) { + _o.D0 = this.D0; + _o.D1 = this.D1; + _o.D2 = this.D2; + _o.D3 = this.D3; + _o.F0 = this.F0; + _o.F1 = this.F1; + _o.F2 = this.F2; + _o.F3 = this.F3; + _o.Dvec = new List(); + for (var _j = 0; _j < this.DvecLength; ++_j) {_o.Dvec.Add(this.Dvec(_j));} + _o.Fvec = new List(); + for (var _j = 0; _j < this.FvecLength; ++_j) {_o.Fvec.Add(this.Fvec(_j));} + } + public static Offset Pack(FlatBufferBuilder builder, MonsterExtraT _o) { + if (_o == null) return default(Offset); + var _dvec = default(VectorOffset); + if (_o.Dvec != null) { + var __dvec = _o.Dvec.ToArray(); + _dvec = CreateDvecVector(builder, __dvec); + } + var _fvec = default(VectorOffset); + if (_o.Fvec != null) { + var __fvec = _o.Fvec.ToArray(); + _fvec = CreateFvecVector(builder, __fvec); + } + return CreateMonsterExtra( + builder, + _o.D0, + _o.D1, + _o.D2, + _o.D3, + _o.F0, + _o.F1, + _o.F2, + _o.F3, + _dvec, + _fvec); + } }; +public class MonsterExtraT +{ + public double D0 { get; set; } + public double D1 { get; set; } + public double D2 { get; set; } + public double D3 { get; set; } + public float F0 { get; set; } + public float F1 { get; set; } + public float F2 { get; set; } + public float F3 { get; set; } + public List Dvec { get; set; } + public List Fvec { get; set; } + + public MonsterExtraT() { + this.D0 = Double.NaN; + this.D1 = Double.NaN; + this.D2 = Double.PositiveInfinity; + this.D3 = Double.NegativeInfinity; + this.F0 = Single.NaN; + this.F1 = Single.NaN; + this.F2 = Single.PositiveInfinity; + this.F3 = Single.NegativeInfinity; + this.Dvec = null; + this.Fvec = null; + } +} + } diff --git a/tests/namespace_test/NamespaceA/NamespaceB/StructInNestedNS.cs b/tests/namespace_test/NamespaceA/NamespaceB/StructInNestedNS.cs index 9aba8db7e..9f6d8dc76 100644 --- a/tests/namespace_test/NamespaceA/NamespaceB/StructInNestedNS.cs +++ b/tests/namespace_test/NamespaceA/NamespaceB/StructInNestedNS.cs @@ -6,6 +6,7 @@ namespace NamespaceA.NamespaceB { using global::System; +using global::System.Collections.Generic; using global::FlatBuffers; public struct StructInNestedNS : IFlatbufferObject @@ -26,7 +27,34 @@ public struct StructInNestedNS : IFlatbufferObject builder.PutInt(A); return new Offset(builder.Offset); } + public StructInNestedNST UnPack() { + var _o = new StructInNestedNST(); + this.UnPackTo(_o); + return _o; + } + public void UnPackTo(StructInNestedNST _o) { + _o.A = this.A; + _o.B = this.B; + } + public static Offset Pack(FlatBufferBuilder builder, StructInNestedNST _o) { + if (_o == null) return default(Offset); + return CreateStructInNestedNS( + builder, + _o.A, + _o.B); + } }; +public class StructInNestedNST +{ + public int A { get; set; } + public int B { get; set; } + + public StructInNestedNST() { + this.A = 0; + this.B = 0; + } +} + } diff --git a/tests/namespace_test/NamespaceA/NamespaceB/TableInNestedNS.cs b/tests/namespace_test/NamespaceA/NamespaceB/TableInNestedNS.cs index f8ea32b57..88d034255 100644 --- a/tests/namespace_test/NamespaceA/NamespaceB/TableInNestedNS.cs +++ b/tests/namespace_test/NamespaceA/NamespaceB/TableInNestedNS.cs @@ -6,6 +6,7 @@ namespace NamespaceA.NamespaceB { using global::System; +using global::System.Collections.Generic; using global::FlatBuffers; public struct TableInNestedNS : IFlatbufferObject @@ -34,7 +35,30 @@ public struct TableInNestedNS : IFlatbufferObject int o = builder.EndTable(); return new Offset(o); } + public TableInNestedNST UnPack() { + var _o = new TableInNestedNST(); + this.UnPackTo(_o); + return _o; + } + public void UnPackTo(TableInNestedNST _o) { + _o.Foo = this.Foo; + } + public static Offset Pack(FlatBufferBuilder builder, TableInNestedNST _o) { + if (_o == null) return default(Offset); + return CreateTableInNestedNS( + builder, + _o.Foo); + } }; +public class TableInNestedNST +{ + public int Foo { get; set; } + + public TableInNestedNST() { + this.Foo = 0; + } +} + } diff --git a/tests/namespace_test/NamespaceA/SecondTableInA.cs b/tests/namespace_test/NamespaceA/SecondTableInA.cs index 7e08de853..57d7741de 100644 --- a/tests/namespace_test/NamespaceA/SecondTableInA.cs +++ b/tests/namespace_test/NamespaceA/SecondTableInA.cs @@ -6,6 +6,7 @@ namespace NamespaceA { using global::System; +using global::System.Collections.Generic; using global::FlatBuffers; public struct SecondTableInA : IFlatbufferObject @@ -33,7 +34,31 @@ public struct SecondTableInA : IFlatbufferObject int o = builder.EndTable(); return new Offset(o); } + public SecondTableInAT UnPack() { + var _o = new SecondTableInAT(); + this.UnPackTo(_o); + return _o; + } + public void UnPackTo(SecondTableInAT _o) { + _o.ReferToC = this.ReferToC.HasValue ? this.ReferToC.Value.UnPack() : null; + } + public static Offset Pack(FlatBufferBuilder builder, SecondTableInAT _o) { + if (_o == null) return default(Offset); + var _refer_to_c = _o.ReferToC == null ? default(Offset) : NamespaceC.TableInC.Pack(builder, _o.ReferToC); + return CreateSecondTableInA( + builder, + _refer_to_c); + } }; +public class SecondTableInAT +{ + public NamespaceC.TableInCT ReferToC { get; set; } + + public SecondTableInAT() { + this.ReferToC = null; + } +} + } diff --git a/tests/namespace_test/NamespaceA/TableInFirstNS.cs b/tests/namespace_test/NamespaceA/TableInFirstNS.cs index d0d8ed0af..759e53e6a 100644 --- a/tests/namespace_test/NamespaceA/TableInFirstNS.cs +++ b/tests/namespace_test/NamespaceA/TableInFirstNS.cs @@ -6,6 +6,7 @@ namespace NamespaceA { using global::System; +using global::System.Collections.Generic; using global::FlatBuffers; public struct TableInFirstNS : IFlatbufferObject @@ -31,7 +32,39 @@ public struct TableInFirstNS : IFlatbufferObject int o = builder.EndTable(); return new Offset(o); } + public TableInFirstNST UnPack() { + var _o = new TableInFirstNST(); + this.UnPackTo(_o); + return _o; + } + public void UnPackTo(TableInFirstNST _o) { + _o.FooTable = this.FooTable.HasValue ? this.FooTable.Value.UnPack() : null; + _o.FooEnum = this.FooEnum; + _o.FooStruct = this.FooStruct.HasValue ? this.FooStruct.Value.UnPack() : null; + } + public static Offset Pack(FlatBufferBuilder builder, TableInFirstNST _o) { + if (_o == null) return default(Offset); + var _foo_table = _o.FooTable == null ? default(Offset) : NamespaceA.NamespaceB.TableInNestedNS.Pack(builder, _o.FooTable); + StartTableInFirstNS(builder); + AddFooTable(builder, _foo_table); + AddFooEnum(builder, _o.FooEnum); + AddFooStruct(builder, NamespaceA.NamespaceB.StructInNestedNS.Pack(builder, _o.FooStruct)); + return EndTableInFirstNS(builder); + } }; +public class TableInFirstNST +{ + public NamespaceA.NamespaceB.TableInNestedNST FooTable { get; set; } + public NamespaceA.NamespaceB.EnumInNestedNS FooEnum { get; set; } + public NamespaceA.NamespaceB.StructInNestedNST FooStruct { get; set; } + + public TableInFirstNST() { + this.FooTable = null; + this.FooEnum = NamespaceA.NamespaceB.EnumInNestedNS.A; + this.FooStruct = new NamespaceA.NamespaceB.StructInNestedNST(); + } +} + } diff --git a/tests/namespace_test/NamespaceC/TableInC.cs b/tests/namespace_test/NamespaceC/TableInC.cs index 74b85a4c9..358ca59c4 100644 --- a/tests/namespace_test/NamespaceC/TableInC.cs +++ b/tests/namespace_test/NamespaceC/TableInC.cs @@ -6,6 +6,7 @@ namespace NamespaceC { using global::System; +using global::System.Collections.Generic; using global::FlatBuffers; public struct TableInC : IFlatbufferObject @@ -37,7 +38,36 @@ public struct TableInC : IFlatbufferObject int o = builder.EndTable(); return new Offset(o); } + public TableInCT UnPack() { + var _o = new TableInCT(); + this.UnPackTo(_o); + return _o; + } + public void UnPackTo(TableInCT _o) { + _o.ReferToA1 = this.ReferToA1.HasValue ? this.ReferToA1.Value.UnPack() : null; + _o.ReferToA2 = this.ReferToA2.HasValue ? this.ReferToA2.Value.UnPack() : null; + } + public static Offset Pack(FlatBufferBuilder builder, TableInCT _o) { + if (_o == null) return default(Offset); + var _refer_to_a1 = _o.ReferToA1 == null ? default(Offset) : NamespaceA.TableInFirstNS.Pack(builder, _o.ReferToA1); + var _refer_to_a2 = _o.ReferToA2 == null ? default(Offset) : NamespaceA.SecondTableInA.Pack(builder, _o.ReferToA2); + return CreateTableInC( + builder, + _refer_to_a1, + _refer_to_a2); + } }; +public class TableInCT +{ + public NamespaceA.TableInFirstNST ReferToA1 { get; set; } + public NamespaceA.SecondTableInAT ReferToA2 { get; set; } + + public TableInCT() { + this.ReferToA1 = null; + this.ReferToA2 = null; + } +} + } diff --git a/tests/union_vector/Attacker.cs b/tests/union_vector/Attacker.cs index 0e3300a5c..d36555d38 100644 --- a/tests/union_vector/Attacker.cs +++ b/tests/union_vector/Attacker.cs @@ -3,6 +3,7 @@ // using global::System; +using global::System.Collections.Generic; using global::FlatBuffers; public struct Attacker : IFlatbufferObject @@ -31,5 +32,28 @@ public struct Attacker : IFlatbufferObject int o = builder.EndTable(); return new Offset(o); } + public AttackerT UnPack() { + var _o = new AttackerT(); + this.UnPackTo(_o); + return _o; + } + public void UnPackTo(AttackerT _o) { + _o.SwordAttackDamage = this.SwordAttackDamage; + } + public static Offset Pack(FlatBufferBuilder builder, AttackerT _o) { + if (_o == null) return default(Offset); + return CreateAttacker( + builder, + _o.SwordAttackDamage); + } }; +public class AttackerT +{ + public int SwordAttackDamage { get; set; } + + public AttackerT() { + this.SwordAttackDamage = 0; + } +} + diff --git a/tests/union_vector/BookReader.cs b/tests/union_vector/BookReader.cs index 53fe7363c..c40c0df40 100644 --- a/tests/union_vector/BookReader.cs +++ b/tests/union_vector/BookReader.cs @@ -3,6 +3,7 @@ // using global::System; +using global::System.Collections.Generic; using global::FlatBuffers; public struct BookReader : IFlatbufferObject @@ -20,5 +21,28 @@ public struct BookReader : IFlatbufferObject builder.PutInt(BooksRead); return new Offset(builder.Offset); } + public BookReaderT UnPack() { + var _o = new BookReaderT(); + this.UnPackTo(_o); + return _o; + } + public void UnPackTo(BookReaderT _o) { + _o.BooksRead = this.BooksRead; + } + public static Offset Pack(FlatBufferBuilder builder, BookReaderT _o) { + if (_o == null) return default(Offset); + return CreateBookReader( + builder, + _o.BooksRead); + } }; +public class BookReaderT +{ + public int BooksRead { get; set; } + + public BookReaderT() { + this.BooksRead = 0; + } +} + diff --git a/tests/union_vector/Character.cs b/tests/union_vector/Character.cs index 73a5cba49..cb16e6f85 100644 --- a/tests/union_vector/Character.cs +++ b/tests/union_vector/Character.cs @@ -13,3 +13,33 @@ public enum Character : byte Unused = 6, }; +public class CharacterUnion { + public Character Type { get; set; } + public object Value { get; set; } + + public CharacterUnion() { + this.Type = Character.NONE; + this.Value = null; + } + + public T As() where T : class { return this.Value as T; } + public AttackerT AsMuLan() { return this.As(); } + public RapunzelT AsRapunzel() { return this.As(); } + public BookReaderT AsBelle() { return this.As(); } + public BookReaderT AsBookFan() { return this.As(); } + public string AsOther() { return this.As(); } + public string AsUnused() { return this.As(); } + + public static int Pack(FlatBuffers.FlatBufferBuilder builder, CharacterUnion _o) { + switch (_o.Type) { + default: return 0; + case Character.MuLan: return Attacker.Pack(builder, _o.AsMuLan()).Value; + case Character.Rapunzel: return Rapunzel.Pack(builder, _o.AsRapunzel()).Value; + case Character.Belle: return BookReader.Pack(builder, _o.AsBelle()).Value; + case Character.BookFan: return BookReader.Pack(builder, _o.AsBookFan()).Value; + case Character.Other: return builder.CreateString(_o.AsOther()).Value; + case Character.Unused: return builder.CreateString(_o.AsUnused()).Value; + } + } +} + diff --git a/tests/union_vector/Movie.cs b/tests/union_vector/Movie.cs index 29f680e7d..7fc103882 100644 --- a/tests/union_vector/Movie.cs +++ b/tests/union_vector/Movie.cs @@ -3,6 +3,7 @@ // using global::System; +using global::System.Collections.Generic; using global::FlatBuffers; public struct Movie : IFlatbufferObject @@ -18,6 +19,7 @@ public struct Movie : IFlatbufferObject public Character MainCharacterType { get { int o = __p.__offset(4); return o != 0 ? (Character)__p.bb.Get(o + __p.bb_pos) : Character.NONE; } } public TTable? MainCharacter() where TTable : struct, IFlatbufferObject { int o = __p.__offset(6); return o != 0 ? (TTable?)__p.__union(o + __p.bb_pos) : null; } + public string MainCharacterAsString() { int o = __p.__offset(6); return o != 0 ? __p.__string(o + __p.bb_pos) : null; } public Character CharactersType(int j) { int o = __p.__offset(8); return o != 0 ? (Character)__p.bb.Get(__p.__vector(o) + j * 1) : (Character)0; } public int CharactersTypeLength { get { int o = __p.__offset(8); return o != 0 ? __p.__vector_len(o) : 0; } } #if ENABLE_SPAN_T @@ -27,6 +29,7 @@ public struct Movie : IFlatbufferObject #endif public Character[] GetCharactersTypeArray() { int o = __p.__offset(8); if (o == 0) return null; int p = __p.__vector(o); int l = __p.__vector_len(o); Character[] a = new Character[l]; for (int i = 0; i < l; i++) { a[i] = (Character)__p.bb.Get(p + i * 1); } return a; } public TTable? Characters(int j) where TTable : struct, IFlatbufferObject { int o = __p.__offset(10); return o != 0 ? (TTable?)__p.__union(__p.__vector(o) + j * 4) : null; } + public string CharactersAsString(int j) { int o = __p.__offset(10); return o != 0 ? __p.__string(__p.__vector(o) + j * 4) : null; } public int CharactersLength { get { int o = __p.__offset(10); return o != 0 ? __p.__vector_len(o) : 0; } } public static Offset CreateMovie(FlatBufferBuilder builder, @@ -59,5 +62,96 @@ public struct Movie : IFlatbufferObject } public static void FinishMovieBuffer(FlatBufferBuilder builder, Offset offset) { builder.Finish(offset.Value, "MOVI"); } public static void FinishSizePrefixedMovieBuffer(FlatBufferBuilder builder, Offset offset) { builder.FinishSizePrefixed(offset.Value, "MOVI"); } + public MovieT UnPack() { + var _o = new MovieT(); + this.UnPackTo(_o); + return _o; + } + public void UnPackTo(MovieT _o) { + _o.MainCharacter = new CharacterUnion(); + _o.MainCharacter.Type = this.MainCharacterType; + switch (this.MainCharacterType) { + default: break; + case Character.MuLan: + _o.MainCharacter.Value = this.MainCharacter().HasValue ? this.MainCharacter().Value.UnPack() : null; + break; + case Character.Rapunzel: + _o.MainCharacter.Value = this.MainCharacter().HasValue ? this.MainCharacter().Value.UnPack() : null; + break; + case Character.Belle: + _o.MainCharacter.Value = this.MainCharacter().HasValue ? this.MainCharacter().Value.UnPack() : null; + break; + case Character.BookFan: + _o.MainCharacter.Value = this.MainCharacter().HasValue ? this.MainCharacter().Value.UnPack() : null; + break; + case Character.Other: + _o.MainCharacter.Value = this.MainCharacterAsString(); + break; + case Character.Unused: + _o.MainCharacter.Value = this.MainCharacterAsString(); + break; + } + _o.Characters = new List(); + for (var _j = 0; _j < this.CharactersLength; ++_j) { + var _o_Characters = new CharacterUnion(); + _o_Characters.Type = this.CharactersType(_j); + switch (this.CharactersType(_j)) { + default: break; + case Character.MuLan: + _o_Characters.Value = this.Characters(_j).HasValue ? this.Characters(_j).Value.UnPack() : null; + break; + case Character.Rapunzel: + _o_Characters.Value = this.Characters(_j).HasValue ? this.Characters(_j).Value.UnPack() : null; + break; + case Character.Belle: + _o_Characters.Value = this.Characters(_j).HasValue ? this.Characters(_j).Value.UnPack() : null; + break; + case Character.BookFan: + _o_Characters.Value = this.Characters(_j).HasValue ? this.Characters(_j).Value.UnPack() : null; + break; + case Character.Other: + _o_Characters.Value = this.CharactersAsString(_j); + break; + case Character.Unused: + _o_Characters.Value = this.CharactersAsString(_j); + break; + } + _o.Characters.Add(_o_Characters); + } + } + public static Offset Pack(FlatBufferBuilder builder, MovieT _o) { + if (_o == null) return default(Offset); + var _main_character_type = _o.MainCharacter == null ? Character.NONE : _o.MainCharacter.Type; + var _main_character = _o.MainCharacter == null ? 0 : CharacterUnion.Pack(builder, _o.MainCharacter); + var _characters_type = default(VectorOffset); + if (_o.Characters != null) { + var __characters_type = new Character[_o.Characters.Count]; + for (var _j = 0; _j < __characters_type.Length; ++_j) { __characters_type[_j] = _o.Characters[_j].Type; } + _characters_type = CreateCharactersTypeVector(builder, __characters_type); + } + var _characters = default(VectorOffset); + if (_o.Characters != null) { + var __characters = new int[_o.Characters.Count]; + for (var _j = 0; _j < __characters.Length; ++_j) { __characters[_j] = CharacterUnion.Pack(builder, _o.Characters[_j]); } + _characters = CreateCharactersVector(builder, __characters); + } + return CreateMovie( + builder, + _main_character_type, + _main_character, + _characters_type, + _characters); + } }; +public class MovieT +{ + public CharacterUnion MainCharacter { get; set; } + public List Characters { get; set; } + + public MovieT() { + this.MainCharacter = null; + this.Characters = null; + } +} + diff --git a/tests/union_vector/Rapunzel.cs b/tests/union_vector/Rapunzel.cs index cb05d4a68..3cb6f1963 100644 --- a/tests/union_vector/Rapunzel.cs +++ b/tests/union_vector/Rapunzel.cs @@ -3,6 +3,7 @@ // using global::System; +using global::System.Collections.Generic; using global::FlatBuffers; public struct Rapunzel : IFlatbufferObject @@ -20,5 +21,28 @@ public struct Rapunzel : IFlatbufferObject builder.PutInt(HairLength); return new Offset(builder.Offset); } + public RapunzelT UnPack() { + var _o = new RapunzelT(); + this.UnPackTo(_o); + return _o; + } + public void UnPackTo(RapunzelT _o) { + _o.HairLength = this.HairLength; + } + public static Offset Pack(FlatBufferBuilder builder, RapunzelT _o) { + if (_o == null) return default(Offset); + return CreateRapunzel( + builder, + _o.HairLength); + } }; +public class RapunzelT +{ + public int HairLength { get; set; } + + public RapunzelT() { + this.HairLength = 0; + } +} +