Commit de9aa0cd authored by Paulo Pinheiro's avatar Paulo Pinheiro Committed by Wouter van Oortmerssen

Add basic Kotlin support (#5409)

* [Kotlin] Add kotlin generate code for tests and add
kotlin test to TestAll.sh

* [Kotlin] Add Kotlin generator

This change adds support for generating Kotlin classes.

The approach of this generator is to keep it as close
as possible to the java generator for now, in order
to keep the change simple.

It uses the already implemented java runtime,
so we don't support cross-platform nor js Kotlin yet.

Kotlin tests are just a copy of the java tests.

* Add optional ident support for CodeWriter

Identation is important for some languages and
different projects have different ways of ident
code, e.g. tabs vs spaces, so we are adding optional
support on CodeWriter for identation.

* [Kotlin] Add Documentation for Kotlin

* [Kotlin] Modify generated code to use experimental Unsigned types.
parent a752d1b8
......@@ -86,6 +86,7 @@ cc_binary(
"src/idl_gen_cpp.cpp",
"src/idl_gen_dart.cpp",
"src/idl_gen_general.cpp",
"src/idl_gen_kotlin.cpp",
"src/idl_gen_go.cpp",
"src/idl_gen_grpc.cpp",
"src/idl_gen_js_ts.cpp",
......
......@@ -83,6 +83,7 @@ set(FlatBuffers_Compiler_SRCS
src/idl_gen_cpp.cpp
src/idl_gen_dart.cpp
src/idl_gen_general.cpp
src/idl_gen_kotlin.cpp
src/idl_gen_go.cpp
src/idl_gen_js_ts.cpp
src/idl_gen_php.cpp
......
......@@ -23,6 +23,8 @@ For any schema input files, one or more generators can be specified:
- `--java`, `-j` : Generate Java code.
- `--kotlin`, `-k` : Generate Kotlin code.
- `--csharp`, `-n` : Generate C# code.
- `--go`, `-g` : Generate Go code.
......
......@@ -4,7 +4,7 @@ FlatBuffers {#flatbuffers_index}
# Overview {#flatbuffers_overview}
[FlatBuffers](@ref flatbuffers_overview) is an efficient cross platform
serialization library for C++, C#, C, Go, Java, JavaScript, Lobster, Lua, TypeScript, PHP, Python, and Rust.
serialization library for C++, C#, C, Go, Java, Kotlin, JavaScript, Lobster, Lua, TypeScript, PHP, Python, and Rust.
It was originally created at Google for game development and other
performance-critical applications.
......@@ -51,7 +51,7 @@ under the Apache license, v2 (see LICENSE.txt).
needed (faster and more memory efficient than other JSON
parsers).
Java and Go code supports object-reuse. C# has efficient struct based
Java, Kotlin and Go code supports object-reuse. C# has efficient struct based
accessors.
- **Cross platform code with no dependencies** - C++ code will work
......@@ -108,7 +108,7 @@ sections provide a more in-depth usage guide.
present for every object instance.
- Use `flatc` (the FlatBuffer compiler) to generate a C++ header (or
Java/C#/Go/Python.. classes) with helper classes to access and construct
Java/Kotlin/C#/Go/Python.. classes) with helper classes to access and construct
serialized data. This header (say `mydata_generated.h`) only depends on
`flatbuffers.h`, which defines the core functionality.
......@@ -132,6 +132,8 @@ sections provide a more in-depth usage guide.
own programs.
- How to [use the generated Java/C# code](@ref flatbuffers_guide_use_java_c-sharp)
in your own programs.
- How to [use the generated Kotlin code](@ref flatbuffers_guide_use_kotlin)
in your own programs.
- How to [use the generated Go code](@ref flatbuffers_guide_use_go) in your
own programs.
- How to [use the generated Lua code](@ref flatbuffers_guide_use_lua) in your
......
Use in Kotlin {#flatbuffers_guide_use_kotlin}
==============
## Before you get started
Before diving into the FlatBuffers usage in Kotlin, 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 K).
This page is designed to cover the nuances of FlatBuffers usage, specific to Kotlin.
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).
## Kotlin and FlatBuffers Java code location
Code generated for Kotlin currently uses the flatbuffers java runtime library. That means that Kotlin generated code can only have Java virtual machine as target architecture (which includes Android). Kotlin Native and Kotlin.js are currently not supported.
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 FlatBuffers Kotlin
The test code for Java is located in [KotlinTest.java](https://github.com/google
/flatbuffers/blob/master/tests/KotlinTest.kt).
To run the tests, use [KotlinTest.sh](https://github.com/google/
flatbuffers/blob/master/tests/KotlinTest.sh) shell script.
*Note: These scripts require that [Kotlin](https://kotlinlang.org/) is installed.*
## Using the FlatBuffers Kotlin library
*Note: See [Tutorial](@ref flatbuffers_guide_tutorial) for a more in-depth
example of how to use FlatBuffers in Kotlin.*
FlatBuffers supports reading and writing binary FlatBuffers in Kotlin.
To use FlatBuffers in your own code, first generate Java classes from your
schema with the `--kotlin` 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 Kotlin:
First, import the library and generated code. Then, you read a FlatBuffer binary
file into a `ByteArray`. You then turn the `ByteArray` into a `ByteBuffer`, which you
pass to the `getRootAsMyRootType` function:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.kt}
import MyGame.Example.*
import com.google.flatbuffers.FlatBufferBuilder
// This snippet ignores exceptions for brevity.
val data = RandomAccessFile(File("monsterdata_test.mon"), "r").use {
val temp = ByteArray(it.length().toInt())
it.readFully(temp)
temp
}
val bb = ByteBuffer.wrap(data)
val monster = Monster.getRootAsMonster(bb)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Now you can access the data from the `Monster monster`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.kt}
val hp = monster.hp
val pos = monster.pos!!;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## Differences between Kotlin and Java code
Kotlin generated code was designed to be as close as possible to the java counterpart, as for now, we only support kotlin on java virtual machine. So the differences in implementation and usage are basically the ones introduced by the Kotlin language itself. You can find more in-depth information [here](https://kotlinlang.org/docs/reference/comparison-to-java.html).
The most obvious ones are:
* Fields as accessed as Kotlin [properties](https://kotlinlang.org/docs/reference/properties.html)
* Static methods are accessed in [companion object](https://kotlinlang.org/docs/reference/classes.html#companion-objects)
\ No newline at end of file
This diff is collapsed.
......@@ -26,7 +26,7 @@ namespace flatbuffers {
// Utility class to assist in generating code through use of text templates.
//
// Example code:
// CodeWriter code;
// CodeWriter code("\t");
// code.SetValue("NAME", "Foo");
// code += "void {{NAME}}() { printf("%s", "{{NAME}}"); }";
// code.SetValue("NAME", "Bar");
......@@ -38,7 +38,8 @@ namespace flatbuffers {
// void Bar() { printf("%s", "Bar"); }
class CodeWriter {
public:
CodeWriter() {}
CodeWriter(std::string pad = std::string())
: pad_(pad), cur_ident_lvl_(0), ignore_ident_(false) {}
// Clears the current "written" code.
void Clear() {
......@@ -67,9 +68,22 @@ class CodeWriter {
// Returns the current contents of the CodeWriter as a std::string.
std::string ToString() const { return stream_.str(); }
// Increase ident level for writing code
void IncrementIdentLevel() { cur_ident_lvl_++; }
// Decrease ident level for writing code
void DecrementIdentLevel() {
if (cur_ident_lvl_) cur_ident_lvl_--;
}
private:
std::map<std::string, std::string> value_map_;
std::stringstream stream_;
std::string pad_;
int cur_ident_lvl_;
bool ignore_ident_;
// Add ident padding (tab or space) based on ident level
void AppendIdent(std::stringstream &stream);
};
class BaseGenerator {
......
......@@ -47,26 +47,26 @@ namespace flatbuffers {
// of type tokens.
// clang-format off
#define FLATBUFFERS_GEN_TYPES_SCALAR(TD) \
TD(NONE, "", uint8_t, byte, byte, byte, uint8, u8) \
TD(UTYPE, "", uint8_t, byte, byte, byte, uint8, u8) /* begin scalar/int */ \
TD(BOOL, "bool", uint8_t, boolean,bool, bool, bool, bool) \
TD(CHAR, "byte", int8_t, byte, int8, sbyte, int8, i8) \
TD(UCHAR, "ubyte", uint8_t, byte, byte, byte, uint8, u8) \
TD(SHORT, "short", int16_t, short, int16, short, int16, i16) \
TD(USHORT, "ushort", uint16_t, short, uint16, ushort, uint16, u16) \
TD(INT, "int", int32_t, int, int32, int, int32, i32) \
TD(UINT, "uint", uint32_t, int, uint32, uint, uint32, u32) \
TD(LONG, "long", int64_t, long, int64, long, int64, i64) \
TD(ULONG, "ulong", uint64_t, long, uint64, ulong, uint64, u64) /* end int */ \
TD(FLOAT, "float", float, float, float32, float, float32, f32) /* begin float */ \
TD(DOUBLE, "double", double, double, float64, double, float64, f64) /* end float/scalar */
TD(NONE, "", uint8_t, byte, byte, byte, uint8, u8, UByte) \
TD(UTYPE, "", uint8_t, byte, byte, byte, uint8, u8, UByte) /* begin scalar/int */ \
TD(BOOL, "bool", uint8_t, boolean,bool, bool, bool, bool, Boolean) \
TD(CHAR, "byte", int8_t, byte, int8, sbyte, int8, i8, Byte) \
TD(UCHAR, "ubyte", uint8_t, byte, byte, byte, uint8, u8, UByte) \
TD(SHORT, "short", int16_t, short, int16, short, int16, i16, Short) \
TD(USHORT, "ushort", uint16_t, short, uint16, ushort, uint16, u16, UShort) \
TD(INT, "int", int32_t, int, int32, int, int32, i32, Int) \
TD(UINT, "uint", uint32_t, int, uint32, uint, uint32, u32, UInt) \
TD(LONG, "long", int64_t, long, int64, long, int64, i64, Long) \
TD(ULONG, "ulong", uint64_t, long, uint64, ulong, uint64, u64, ULong) /* end int */ \
TD(FLOAT, "float", float, float, float32, float, float32, f32, Float) /* begin float */ \
TD(DOUBLE, "double", double, double, float64, double, float64, f64, Double) /* end float/scalar */
#define FLATBUFFERS_GEN_TYPES_POINTER(TD) \
TD(STRING, "string", Offset<void>, int, int, StringOffset, int, unused) \
TD(VECTOR, "", Offset<void>, int, int, VectorOffset, int, unused) \
TD(STRUCT, "", Offset<void>, int, int, int, int, unused) \
TD(UNION, "", Offset<void>, int, int, int, int, unused)
TD(STRING, "string", Offset<void>, int, int, StringOffset, int, unused, Int) \
TD(VECTOR, "", Offset<void>, int, int, VectorOffset, int, unused, Int) \
TD(STRUCT, "", Offset<void>, int, int, int, int, unused, Int) \
TD(UNION, "", Offset<void>, int, int, int, int, unused, Int)
#define FLATBUFFERS_GEN_TYPE_ARRAY(TD) \
TD(ARRAY, "", int, int, int, int, int, unused)
TD(ARRAY, "", int, int, int, int, int, unused, Int)
// The fields are:
// - enum
// - FlatBuffers schema type.
......@@ -76,13 +76,14 @@ namespace flatbuffers {
// - C# / .Net type.
// - Python type.
// - Rust type.
// - Kotlin type.
// using these macros, we can now write code dealing with types just once, e.g.
/*
switch (type) {
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, \
RTYPE) \
RTYPE, KTYPE) \
case BASE_TYPE_ ## ENUM: \
// do something specific to CTYPE here
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
......@@ -101,14 +102,14 @@ __extension__ // Stop GCC complaining about trailing comma with -Wpendantic.
#endif
enum BaseType {
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, \
RTYPE) \
RTYPE, KTYPE) \
BASE_TYPE_ ## ENUM,
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
#undef FLATBUFFERS_TD
};
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, \
RTYPE) \
RTYPE, KTYPE) \
static_assert(sizeof(CTYPE) <= sizeof(largest_scalar_t), \
"define largest_scalar_t as " #CTYPE);
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
......@@ -546,6 +547,7 @@ struct IDLOptions {
kLua = 1 << 12,
kLobster = 1 << 13,
kRust = 1 << 14,
kKotlin = 1 << 15,
kMAX
};
......@@ -1009,6 +1011,9 @@ extern bool GenerateJsonSchema(const Parser &parser,
const std::string &path,
const std::string &file_name);
extern bool GenerateKotlin(const Parser &parser, const std::string &path,
const std::string &file_name);
// Generate Java/C#/.. files from the definitions in the Parser object.
// See idl_gen_general.cpp.
extern bool GenerateGeneral(const Parser &parser,
......
/*
* Copyright 2015 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Run this file with the `java_sample.sh` script.
import MyGame.Sample.Color
import MyGame.Sample.Equipment
import MyGame.Sample.Monster
import MyGame.Sample.Vec3
import MyGame.Sample.Weapon
import com.google.flatbuffers.FlatBufferBuilder
class SampleBinary {
companion object {
// Example how to use FlatBuffers to create and read binary buffers.
@JvmStatic
fun main(args: Array<String>) {
val builder = FlatBufferBuilder(0)
// Create some weapons for our Monster ('Sword' and 'Axe').
val weaponOneName = builder.createString("Sword")
val weaponOneDamage: Short = 3
val weaponTwoName = builder.createString("Axe")
val weaponTwoDamage: Short = 5
// Use the `createWeapon()` helper function to create the weapons, since we set every field.
val weaps = IntArray(2)
weaps[0] = Weapon.createWeapon(builder, weaponOneName, weaponOneDamage)
weaps[1] = Weapon.createWeapon(builder, weaponTwoName, weaponTwoDamage)
// Serialize the FlatBuffer data.
val name = builder.createString("Orc")
val treasure = byteArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
val inv = Monster.createInventoryVector(builder, treasure)
val weapons = Monster.createWeaponsVector(builder, weaps)
val pos = Vec3.createVec3(builder, 1.0f, 2.0f, 3.0f)
Monster.startMonster(builder)
Monster.addPos(builder, pos)
Monster.addName(builder, name)
Monster.addColor(builder, Color.Red)
Monster.addHp(builder, 300.toShort())
Monster.addInventory(builder, inv)
Monster.addWeapons(builder, weapons)
Monster.addEquippedType(builder, Equipment.Weapon)
Monster.addEquipped(builder, weaps[1])
val orc = Monster.endMonster(builder)
builder.finish(orc) // You could also call `Monster.finishMonsterBuffer(builder, orc);`.
// We now have a FlatBuffer that can be stored on disk or sent over a network.
// ...Code to store to disk or send over a network goes here...
// Instead, we are going to access it right away, as if we just received it.
val buf = builder.dataBuffer()
// Get access to the root:
val monster = Monster.getRootAsMonster(buf)
// Note: We did not set the `mana` field explicitly, so we get back the default value.
assert(monster.mana == 150.toShort())
assert(monster.hp == 300.toShort())
assert(monster.name.equals("Orc"))
assert(monster.color == Color.Red)
assert(monster.pos!!.x == 1.0f)
assert(monster.pos!!.y == 2.0f)
assert(monster.pos!!.z == 3.0f)
// Get and test the `inventory` FlatBuffer `vector`.
for (i in 0 until monster.inventoryLength) {
assert(monster.inventory(i) == i.toByte().toInt())
}
// Get and test the `weapons` FlatBuffer `vector` of `table`s.
val expectedWeaponNames = arrayOf("Sword", "Axe")
val expectedWeaponDamages = intArrayOf(3, 5)
for (i in 0 until monster.weaponsLength) {
assert(monster.weapons(i)!!.name.equals(expectedWeaponNames[i]))
assert(monster.weapons(i)!!.damage.toInt() == expectedWeaponDamages[i])
}
// Get and test the `equipped` FlatBuffer `union`.
assert(monster.equippedType == Equipment.Weapon)
val equipped = monster.equipped(Weapon()) as Weapon?
assert(equipped!!.name.equals("Axe"))
assert(equipped.damage == 5.toShort())
println("The FlatBuffer was successfully created and verified!")
}
}
}
#!/bin/bash
#
# Copyright 2015 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Note: This script runs on Mac and Linux. It requires `kotlin` to be installed
# and `flatc` to be built (using `cmake` in the root directory).
sampledir=$(cd $(dirname $BASH_SOURCE) && pwd)
rootdir=$(cd $sampledir/.. && pwd)
currentdir=$(pwd)
if [[ "$sampledir" != "$currentdir" ]]; then
echo Error: This script must be run from inside the $sampledir directory.
echo You executed it from the $currentdir directory.
exit 1
fi
# Run `flatc`. Note: This requires you to compile using `cmake` from the
# root `/flatbuffers` directory.
if [ -e ../flatc ]; then
echo "compiling now"
../flatc --kotlin --gen-mutable monster.fbs
elif [ -e ../Debug/flatc ]; then
../Debug/flatc --kotlin --gen-mutable monster.fbs
else
echo 'flatc' could not be found. Make sure to build FlatBuffers from the \
$rootdir directory.
exit 1
fi
echo Compiling and running the Kotlin sample
all_kt_files=`find $sampledir -name "*.kt" -print`
# Run test
mkdir -v "${sampledir}/kotlin"
echo Compiling Java Runtime
javac ${rootdir}/java/com/google/flatbuffers/*.java -d ${sampledir}/kotlin
echo Compiling Kotlin source
kotlinc -classpath ${sampledir}/../java:${sampledir}/kotlin $all_kt_files SampleBinary.kt -include-runtime -d ${sampledir}/kotlin
# Make jar
echo Making jar
jar cvf ${sampledir}/kotlin/kotlin_sample.jar -C ${sampledir}/kotlin . > /dev/null
echo Running test
kotlin -cp ${sampledir}/kotlin/kotlin_sample.jar SampleBinary
# Cleanup temporary files.
# rm -rf MyGame/
# rm -rf ${sampledir}/kotlin
......@@ -29,6 +29,8 @@
namespace flatbuffers {
void CodeWriter::operator+=(std::string text) {
if (!ignore_ident_ && !text.empty()) AppendIdent(stream_);
while (true) {
auto begin = text.find("{{");
if (begin == std::string::npos) { break; }
......@@ -58,12 +60,21 @@ void CodeWriter::operator+=(std::string text) {
}
if (!text.empty() && string_back(text) == '\\') {
text.pop_back();
ignore_ident_ = true;
stream_ << text;
} else {
ignore_ident_ = false;
stream_ << text << std::endl;
}
}
void CodeWriter::AppendIdent(std::stringstream &stream) {
int lvl = cur_ident_lvl_;
while (lvl--) {
stream.write(pad_.c_str(), static_cast<std::streamsize>(pad_.size()));
}
}
const char *BaseGenerator::FlatBuffersGeneratedWarning() {
return "automatically generated by the FlatBuffers compiler,"
" do not modify";
......
......@@ -90,6 +90,9 @@ int main(int argc, const char *argv[]) {
{ flatbuffers::GeneratePhp, nullptr, "--php", "PHP", true, nullptr,
flatbuffers::IDLOptions::kPhp, "Generate PHP files for tables/structs",
flatbuffers::GeneralMakeRule },
{ flatbuffers::GenerateKotlin, nullptr, "--kotlin", "Kotlin", true, nullptr,
flatbuffers::IDLOptions::kKotlin, "Generate Kotlin classes for tables/structs",
flatbuffers::GeneralMakeRule },
{ flatbuffers::GenerateJsonSchema, nullptr, "--jsonschema", "JsonSchema",
true, nullptr, flatbuffers::IDLOptions::kJsonSchema,
"Generate Json schema", flatbuffers::GeneralMakeRule },
......
......@@ -538,7 +538,7 @@ class CppGenerator : public BaseGenerator {
// clang-format off
static const char *const ctypename[] = {
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, \
RTYPE) \
RTYPE, KTYPE) \
#CTYPE,
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
#undef FLATBUFFERS_TD
......
......@@ -258,7 +258,7 @@ class GeneralGenerator : public BaseGenerator {
// clang-format off
static const char * const java_typename[] = {
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE) \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
#JTYPE,
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
#undef FLATBUFFERS_TD
......@@ -266,7 +266,7 @@ class GeneralGenerator : public BaseGenerator {
static const char * const csharp_typename[] = {
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE) \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
#NTYPE,
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
#undef FLATBUFFERS_TD
......
......@@ -827,7 +827,7 @@ class GoGenerator : public BaseGenerator {
static const char *ctypename[] = {
// clang-format off
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE) \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
#GTYPE,
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
#undef FLATBUFFERS_TD
......
This diff is collapsed.
......@@ -83,7 +83,7 @@ class LobsterGenerator : public BaseGenerator {
static const char *ctypename[] = {
// clang-format off
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE) \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
#PTYPE,
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
#undef FLATBUFFERS_TD
......
......@@ -606,7 +606,7 @@ namespace lua {
static const char *ctypename[] = {
// clang-format off
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE) \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
#PTYPE,
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
#undef FLATBUFFERS_TD
......
......@@ -862,7 +862,7 @@ class PhpGenerator : public BaseGenerator {
static const char *ctypename[] = {
// clang-format off
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE) \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
#NTYPE,
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
#undef FLATBUFFERS_TD
......
......@@ -680,7 +680,7 @@ class PythonGenerator : public BaseGenerator {
static const char *ctypename[] = {
// clang-format off
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE) \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
#PTYPE,
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
#undef FLATBUFFERS_TD
......
......@@ -516,7 +516,7 @@ class RustGenerator : public BaseGenerator {
// clang-format off
static const char * const ctypename[] = {
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, \
RTYPE) \
RTYPE, KTYPE) \
#RTYPE,
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
#undef FLATBUFFERS_TD
......@@ -538,7 +538,7 @@ class RustGenerator : public BaseGenerator {
static const char *ctypename[] = {
// clang-format off
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, \
RTYPE) \
RTYPE, KTYPE) \
#RTYPE,
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
#undef FLATBUFFERS_TD
......
......@@ -146,7 +146,7 @@ bool Print<const void *>(const void *val, Type type, int indent,
// clang-format off
switch (vec_type.base_type) {
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE) \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
case BASE_TYPE_ ## ENUM: \
if (!PrintVector<CTYPE>( \
*reinterpret_cast<const Vector<CTYPE> *>(val), \
......@@ -166,7 +166,7 @@ bool Print<const void *>(const void *val, Type type, int indent,
// clang-format off
switch (vec_type.base_type) {
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE) \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
case BASE_TYPE_ ## ENUM: \
if (!PrintArray<CTYPE>( \
*reinterpret_cast<const Array<CTYPE, 0xFFFF> *>(val), \
......@@ -266,7 +266,7 @@ static bool GenStruct(const StructDef &struct_def, const Table *table,
switch (fd.value.type.base_type) {
// clang-format off
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE) \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
case BASE_TYPE_ ## ENUM: \
if (!GenField<CTYPE>(fd, table, struct_def.fixed, \
opts, indent + Indent(opts), _text)) { \
......@@ -277,7 +277,7 @@ static bool GenStruct(const StructDef &struct_def, const Table *table,
#undef FLATBUFFERS_TD
// Generate drop-thru case statements for all pointer types:
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE) \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
case BASE_TYPE_ ## ENUM:
FLATBUFFERS_GEN_TYPES_POINTER(FLATBUFFERS_TD)
FLATBUFFERS_GEN_TYPE_ARRAY(FLATBUFFERS_TD)
......
......@@ -29,7 +29,7 @@ namespace flatbuffers {
// Reflects the version at the compiling time of binary(lib/dll/so).
const char *FLATBUFFERS_VERSION() {
// clang-format off
return
return
FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MAJOR) "."
FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MINOR) "."
FLATBUFFERS_STRING(FLATBUFFERS_VERSION_REVISION);
......@@ -41,7 +41,7 @@ const double kPi = 3.14159265358979323846;
const char *const kTypeNames[] = {
// clang-format off
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE) \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
IDLTYPE,
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
#undef FLATBUFFERS_TD
......@@ -52,7 +52,7 @@ const char *const kTypeNames[] = {
const char kTypeSizes[] = {
// clang-format off
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE) \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
sizeof(CTYPE),
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
#undef FLATBUFFERS_TD
......@@ -212,7 +212,7 @@ static std::string TokenToString(int t) {
FLATBUFFERS_GEN_TOKENS(FLATBUFFERS_TOKEN)
#undef FLATBUFFERS_TOKEN
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE) \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
IDLTYPE,
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
#undef FLATBUFFERS_TD
......@@ -1169,7 +1169,7 @@ CheckedError Parser::ParseTable(const StructDef &struct_def, std::string *value,
switch (field_value.type.base_type) {
// clang-format off
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE) \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
case BASE_TYPE_ ## ENUM: \
builder_.Pad(field->padding); \
if (struct_def.fixed) { \
......@@ -1186,7 +1186,7 @@ CheckedError Parser::ParseTable(const StructDef &struct_def, std::string *value,
FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD);
#undef FLATBUFFERS_TD
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE) \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
case BASE_TYPE_ ## ENUM: \
builder_.Pad(field->padding); \
if (IsStruct(field->value.type)) { \
......@@ -1267,7 +1267,7 @@ CheckedError Parser::ParseVector(const Type &type, uoffset_t *ovalue,
switch (val.type.base_type) {
// clang-format off
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE) \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
case BASE_TYPE_ ## ENUM: \
if (IsStruct(val.type)) SerializeStruct(*val.type.struct_def, val); \
else { \
......@@ -1313,7 +1313,7 @@ CheckedError Parser::ParseArray(Value &array) {
// clang-format off
switch (val.type.base_type) {
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE) \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
case BASE_TYPE_ ## ENUM: \
if (IsStruct(val.type)) { \
SerializeStruct(builder, *val.type.struct_def, val); \
......@@ -1661,7 +1661,7 @@ CheckedError Parser::ParseSingleValue(const std::string *name, Value &e,
// clang-format off
switch (match_type) {
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE) \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
case BASE_TYPE_ ## ENUM: {\
CTYPE val; \
ECHECK(atot(e.constant.c_str(), *this, &val)); \
......@@ -1897,7 +1897,7 @@ struct EnumValBuilder {
// clang-format off
switch (enum_def.underlying_type.base_type) {
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, \
PTYPE, RTYPE) \
PTYPE, RTYPE, KTYPE) \
case BASE_TYPE_##ENUM: { \
if (!IsInteger(BASE_TYPE_##ENUM)) break; \
return ValidateImpl<BASE_TYPE_##ENUM, CTYPE>(ev, next ? 1 : 0); \
......@@ -2091,6 +2091,7 @@ bool Parser::SupportsAdvancedUnionFeatures() const {
(opts.lang_to_generate & ~(IDLOptions::kCpp | IDLOptions::kJs |
IDLOptions::kTs | IDLOptions::kPhp |
IDLOptions::kJava | IDLOptions::kCSharp |
IDLOptions::kKotlin |
IDLOptions::kBinary)) == 0;
}
......
This diff is collapsed.
#!/bin/sh
# Copyright 2014 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
echo Compile then run the Kotlin test.
testdir=$(dirname $0)
targetdir="${testdir}/kotlin"
if [[ -e "${targetdir}" ]]; then
echo "cleaning target"
rm -rf "${targetdir}"
fi
mkdir -v "${targetdir}"
if ! find "${testdir}/../java" -type f -name "*.class" -delete; then
echo "failed to clean .class files from java directory" >&2
exit 1
fi
all_kt_files=`find . -name "*.kt" -print`
# Compile java FlatBuffer library
javac ${testdir}/../java/com/google/flatbuffers/*.java -d $targetdir
# Compile Kotlin files
kotlinc $all_kt_files -classpath $targetdir -include-runtime -d $targetdir
# Make jar
jar cvf ${testdir}/kotlin_test.jar -C $targetdir . > /dev/null
# Run test
kotlin -cp ${testdir}/kotlin_test.jar KotlinTest
# clean up
rm -rf $targetdir
rm ${testdir}/kotlin_test.jar
// automatically generated by the FlatBuffers compiler, do not modify
package MyGame.Example
import java.nio.*
import kotlin.math.sign
import com.google.flatbuffers.*
@Suppress("unused")
@ExperimentalUnsignedTypes
class Ability : Struct() {
fun __init(_i: Int, _bb: ByteBuffer) {
__reset(_i, _bb)
}
fun __assign(_i: Int, _bb: ByteBuffer) : Ability {
__init(_i, _bb)
return this
}
val id : UInt get() = bb.getInt(bb_pos + 0).toUInt()
fun mutateId(id: UInt) : ByteBuffer = bb.putInt(bb_pos + 0, id.toInt())
val distance : UInt get() = bb.getInt(bb_pos + 4).toUInt()
fun mutateDistance(distance: UInt) : ByteBuffer = bb.putInt(bb_pos + 4, distance.toInt())
companion object {
fun createAbility(builder: FlatBufferBuilder, id: UInt, distance: UInt) : Int {
builder.prep(4, 8)
builder.putInt(distance.toInt())
builder.putInt(id.toInt())
return builder.offset()
}
}
}
// automatically generated by the FlatBuffers compiler, do not modify
package MyGame.Example
@Suppress("unused")
@ExperimentalUnsignedTypes
class Any_ private constructor() {
companion object {
const val NONE: UByte = 0u
const val Monster: UByte = 1u
const val TestSimpleTableWithEnum: UByte = 2u
const val MyGameExample2Monster: UByte = 3u
val names : Array<String> = arrayOf("NONE", "Monster", "TestSimpleTableWithEnum", "MyGame_Example2_Monster")
fun name(e: Int) : String = names[e]
}
}
// automatically generated by the FlatBuffers compiler, do not modify
package MyGame.Example
@Suppress("unused")
@ExperimentalUnsignedTypes
class AnyAmbiguousAliases private constructor() {
companion object {
const val NONE: UByte = 0u
const val M1: UByte = 1u
const val M2: UByte = 2u
const val M3: UByte = 3u
val names : Array<String> = arrayOf("NONE", "M1", "M2", "M3")
fun name(e: Int) : String = names[e]
}
}
// automatically generated by the FlatBuffers compiler, do not modify
package MyGame.Example
@Suppress("unused")
@ExperimentalUnsignedTypes
class AnyUniqueAliases private constructor() {
companion object {
const val NONE: UByte = 0u
const val M: UByte = 1u
const val TS: UByte = 2u
const val M2: UByte = 3u
val names : Array<String> = arrayOf("NONE", "M", "TS", "M2")
fun name(e: Int) : String = names[e]
}
}
// automatically generated by the FlatBuffers compiler, do not modify
package MyGame.Example
/**
* Composite components of Monster color.
*/
@Suppress("unused")
@ExperimentalUnsignedTypes
class Color private constructor() {
companion object {
const val Red: UByte = 1u
/**
* \brief color Green
* Green is bit_flag with value (1u << 1)
*/
const val Green: UByte = 2u
/**
* \brief color Blue (1u << 3)
*/
const val Blue: UByte = 8u
val names : Array<String> = arrayOf("Red", "Green", "", "", "", "", "", "Blue")
fun name(e: Int) : String = names[e - Red.toInt()]
}
}
This diff is collapsed.
// automatically generated by the FlatBuffers compiler, do not modify
package MyGame.Example
import java.nio.*
import kotlin.math.sign
import com.google.flatbuffers.*
@Suppress("unused")
@ExperimentalUnsignedTypes
class Referrable : Table() {
fun __init(_i: Int, _bb: ByteBuffer) {
__reset(_i, _bb)
}
fun __assign(_i: Int, _bb: ByteBuffer) : Referrable {
__init(_i, _bb)
return this
}
val id : ULong
get() {
val o = __offset(4)
return if(o != 0) bb.getLong(o + bb_pos).toULong() else 0UL
}
fun mutateId(id: ULong) : Boolean {
val o = __offset(4)
return if (o != 0) {
bb.putLong(o + bb_pos, id.toLong())
true
} else {
false
}
}
override fun keysCompare(o1: Int, o2: Int, _bb: ByteBuffer) : Int {
val val_1 = _bb.getLong(__offset(4, o1, _bb))
val val_2 = _bb.getLong(__offset(4, o2, _bb))
return (val_1 - val_2).sign
}
companion object {
fun validateVersion() = Constants.FLATBUFFERS_1_11_1()
fun getRootAsReferrable(_bb: ByteBuffer): Referrable = getRootAsReferrable(_bb, Referrable())
fun getRootAsReferrable(_bb: ByteBuffer, obj: Referrable): Referrable {
_bb.order(ByteOrder.LITTLE_ENDIAN)
return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb))
}
fun createReferrable(builder: FlatBufferBuilder, id: ULong) : Int {
builder.startTable(1)
addId(builder, id)
return endReferrable(builder)
}
fun startReferrable(builder: FlatBufferBuilder) = builder.startTable(1)
fun addId(builder: FlatBufferBuilder, id: ULong) = builder.addLong(0, id.toLong(), 0)
fun endReferrable(builder: FlatBufferBuilder) : Int {
val o = builder.endTable()
return o
}
fun __lookup_by_key(obj: Referrable?, vectorLocation: Int, key: ULong, bb: ByteBuffer) : Referrable? {
var span = bb.getInt(vectorLocation - 4)
var start = 0
while (span != 0) {
var middle = span / 2
val tableOffset = __indirect(vectorLocation + 4 * (start + middle), bb)
val value = bb.getLong(__offset(4, bb.capacity() - tableOffset, bb)).toULong()
val comp = value.compareTo(key)
when {
comp > 0 -> span = middle
comp < 0 -> {
middle++
start += middle
span -= middle
}
else -> {
return (obj ?: Referrable()).__assign(tableOffset, bb)
}
}
}
return null
}
}
}
// automatically generated by the FlatBuffers compiler, do not modify
package MyGame.Example
import java.nio.*
import kotlin.math.sign
import com.google.flatbuffers.*
@Suppress("unused")
@ExperimentalUnsignedTypes
class Stat : Table() {
fun __init(_i: Int, _bb: ByteBuffer) {
__reset(_i, _bb)
}
fun __assign(_i: Int, _bb: ByteBuffer) : Stat {
__init(_i, _bb)
return this
}
val id : String?
get() {
val o = __offset(4)
return if (o != 0) __string(o + bb_pos) else null
}
val idAsByteBuffer : ByteBuffer get() = __vector_as_bytebuffer(4, 1)
fun idInByteBuffer(_bb: ByteBuffer) : ByteBuffer = __vector_in_bytebuffer(_bb, 4, 1)
val val_ : Long
get() {
val o = __offset(6)
return if(o != 0) bb.getLong(o + bb_pos) else 0L
}
fun mutateVal_(val_: Long) : Boolean {
val o = __offset(6)
return if (o != 0) {
bb.putLong(o + bb_pos, val_)
true
} else {
false
}
}
val count : UShort
get() {
val o = __offset(8)
return if(o != 0) bb.getShort(o + bb_pos).toUShort() else 0u
}
fun mutateCount(count: UShort) : Boolean {
val o = __offset(8)
return if (o != 0) {
bb.putShort(o + bb_pos, count.toShort())
true
} else {
false
}
}
companion object {
fun validateVersion() = Constants.FLATBUFFERS_1_11_1()
fun getRootAsStat(_bb: ByteBuffer): Stat = getRootAsStat(_bb, Stat())
fun getRootAsStat(_bb: ByteBuffer, obj: Stat): Stat {
_bb.order(ByteOrder.LITTLE_ENDIAN)
return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb))
}
fun createStat(builder: FlatBufferBuilder, idOffset: Int, val_: Long, count: UShort) : Int {
builder.startTable(3)
addVal_(builder, val_)
addId(builder, idOffset)
addCount(builder, count)
return endStat(builder)
}
fun startStat(builder: FlatBufferBuilder) = builder.startTable(3)
fun addId(builder: FlatBufferBuilder, id: Int) = builder.addOffset(0, id, 0)
fun addVal_(builder: FlatBufferBuilder, val_: Long) = builder.addLong(1, val_, 0L)
fun addCount(builder: FlatBufferBuilder, count: UShort) = builder.addShort(2, count.toShort(), 0)
fun endStat(builder: FlatBufferBuilder) : Int {
val o = builder.endTable()
return o
}
}
}
// automatically generated by the FlatBuffers compiler, do not modify
package MyGame.Example
import java.nio.*
import kotlin.math.sign
import com.google.flatbuffers.*
@Suppress("unused")
@ExperimentalUnsignedTypes
class Test : Struct() {
fun __init(_i: Int, _bb: ByteBuffer) {
__reset(_i, _bb)
}
fun __assign(_i: Int, _bb: ByteBuffer) : Test {
__init(_i, _bb)
return this
}
val a : Short get() = bb.getShort(bb_pos + 0)
fun mutateA(a: Short) : ByteBuffer = bb.putShort(bb_pos + 0, a)
val b : Byte get() = bb.get(bb_pos + 2)
fun mutateB(b: Byte) : ByteBuffer = bb.put(bb_pos + 2, b)
companion object {
fun createTest(builder: FlatBufferBuilder, a: Short, b: Byte) : Int {
builder.prep(2, 4)
builder.pad(1)
builder.putByte(b)
builder.putShort(a)
return builder.offset()
}
}
}
// automatically generated by the FlatBuffers compiler, do not modify
package MyGame.Example
@Suppress("unused")
class TestEnum private constructor() {
companion object {
const val A: Byte = 0
const val B: Byte = 1
const val C: Byte = 2
val names : Array<String> = arrayOf("A", "B", "C")
fun name(e: Int) : String = names[e]
}
}
// automatically generated by the FlatBuffers compiler, do not modify
package MyGame.Example
import java.nio.*
import kotlin.math.sign
import com.google.flatbuffers.*
@Suppress("unused")
@ExperimentalUnsignedTypes
class TestSimpleTableWithEnum : Table() {
fun __init(_i: Int, _bb: ByteBuffer) {
__reset(_i, _bb)
}
fun __assign(_i: Int, _bb: ByteBuffer) : TestSimpleTableWithEnum {
__init(_i, _bb)
return this
}
val color : UByte
get() {
val o = __offset(4)
return if(o != 0) bb.get(o + bb_pos).toUByte() else 2u
}
fun mutateColor(color: UByte) : Boolean {
val o = __offset(4)
return if (o != 0) {
bb.put(o + bb_pos, color.toByte())
true
} else {
false
}
}
companion object {
fun validateVersion() = Constants.FLATBUFFERS_1_11_1()
fun getRootAsTestSimpleTableWithEnum(_bb: ByteBuffer): TestSimpleTableWithEnum = getRootAsTestSimpleTableWithEnum(_bb, TestSimpleTableWithEnum())
fun getRootAsTestSimpleTableWithEnum(_bb: ByteBuffer, obj: TestSimpleTableWithEnum): TestSimpleTableWithEnum {
_bb.order(ByteOrder.LITTLE_ENDIAN)
return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb))
}
fun createTestSimpleTableWithEnum(builder: FlatBufferBuilder, color: UByte) : Int {
builder.startTable(1)
addColor(builder, color)
return endTestSimpleTableWithEnum(builder)
}
fun startTestSimpleTableWithEnum(builder: FlatBufferBuilder) = builder.startTable(1)
fun addColor(builder: FlatBufferBuilder, color: UByte) = builder.addByte(0, color.toByte(), 2)
fun endTestSimpleTableWithEnum(builder: FlatBufferBuilder) : Int {
val o = builder.endTable()
return o
}
}
}
// automatically generated by the FlatBuffers compiler, do not modify
package MyGame.Example
import java.nio.*
import kotlin.math.sign
import com.google.flatbuffers.*
@Suppress("unused")
@ExperimentalUnsignedTypes
class TypeAliases : Table() {
fun __init(_i: Int, _bb: ByteBuffer) {
__reset(_i, _bb)
}
fun __assign(_i: Int, _bb: ByteBuffer) : TypeAliases {
__init(_i, _bb)
return this
}
val i8 : Byte
get() {
val o = __offset(4)
return if(o != 0) bb.get(o + bb_pos) else 0
}
fun mutateI8(i8: Byte) : Boolean {
val o = __offset(4)
return if (o != 0) {
bb.put(o + bb_pos, i8)
true
} else {
false
}
}
val u8 : UByte
get() {
val o = __offset(6)
return if(o != 0) bb.get(o + bb_pos).toUByte() else 0u
}
fun mutateU8(u8: UByte) : Boolean {
val o = __offset(6)
return if (o != 0) {
bb.put(o + bb_pos, u8.toByte())
true
} else {
false
}
}
val i16 : Short
get() {
val o = __offset(8)
return if(o != 0) bb.getShort(o + bb_pos) else 0
}
fun mutateI16(i16: Short) : Boolean {
val o = __offset(8)
return if (o != 0) {
bb.putShort(o + bb_pos, i16)
true
} else {
false
}
}
val u16 : UShort
get() {
val o = __offset(10)
return if(o != 0) bb.getShort(o + bb_pos).toUShort() else 0u
}
fun mutateU16(u16: UShort) : Boolean {
val o = __offset(10)
return if (o != 0) {
bb.putShort(o + bb_pos, u16.toShort())
true
} else {
false
}
}
val i32 : Int
get() {
val o = __offset(12)
return if(o != 0) bb.getInt(o + bb_pos) else 0
}
fun mutateI32(i32: Int) : Boolean {
val o = __offset(12)
return if (o != 0) {
bb.putInt(o + bb_pos, i32)
true
} else {
false
}
}
val u32 : UInt
get() {
val o = __offset(14)
return if(o != 0) bb.getInt(o + bb_pos).toUInt() else 0u
}
fun mutateU32(u32: UInt) : Boolean {
val o = __offset(14)
return if (o != 0) {
bb.putInt(o + bb_pos, u32.toInt())
true
} else {
false
}
}
val i64 : Long
get() {
val o = __offset(16)
return if(o != 0) bb.getLong(o + bb_pos) else 0L
}
fun mutateI64(i64: Long) : Boolean {
val o = __offset(16)
return if (o != 0) {
bb.putLong(o + bb_pos, i64)
true
} else {
false
}
}
val u64 : ULong
get() {
val o = __offset(18)
return if(o != 0) bb.getLong(o + bb_pos).toULong() else 0UL
}
fun mutateU64(u64: ULong) : Boolean {
val o = __offset(18)
return if (o != 0) {
bb.putLong(o + bb_pos, u64.toLong())
true
} else {
false
}
}
val f32 : Float
get() {
val o = __offset(20)
return if(o != 0) bb.getFloat(o + bb_pos) else 0.0f
}
fun mutateF32(f32: Float) : Boolean {
val o = __offset(20)
return if (o != 0) {
bb.putFloat(o + bb_pos, f32)
true
} else {
false
}
}
val f64 : Double
get() {
val o = __offset(22)
return if(o != 0) bb.getDouble(o + bb_pos) else 0.0
}
fun mutateF64(f64: Double) : Boolean {
val o = __offset(22)
return if (o != 0) {
bb.putDouble(o + bb_pos, f64)
true
} else {
false
}
}
fun v8(j: Int) : Byte {
val o = __offset(24)
return if (o != 0) {
bb.get(__vector(o) + j * 1)
} else {
0
}
}
val v8Length : Int
get() {
val o = __offset(24); return if (o != 0) __vector_len(o) else 0
}
val v8AsByteBuffer : ByteBuffer get() = __vector_as_bytebuffer(24, 1)
fun v8InByteBuffer(_bb: ByteBuffer) : ByteBuffer = __vector_in_bytebuffer(_bb, 24, 1)
fun mutateV8(j: Int, v8: Byte) : Boolean {
val o = __offset(24)
return if (o != 0) {
bb.put(__vector(o) + j * 1, v8)
true
} else {
false
}
}
fun vf64(j: Int) : Double {
val o = __offset(26)
return if (o != 0) {
bb.getDouble(__vector(o) + j * 8)
} else {
0.0
}
}
val vf64Length : Int
get() {
val o = __offset(26); return if (o != 0) __vector_len(o) else 0
}
val vf64AsByteBuffer : ByteBuffer get() = __vector_as_bytebuffer(26, 8)
fun vf64InByteBuffer(_bb: ByteBuffer) : ByteBuffer = __vector_in_bytebuffer(_bb, 26, 8)
fun mutateVf64(j: Int, vf64: Double) : Boolean {
val o = __offset(26)
return if (o != 0) {
bb.putDouble(__vector(o) + j * 8, vf64)
true
} else {
false
}
}
companion object {
fun validateVersion() = Constants.FLATBUFFERS_1_11_1()
fun getRootAsTypeAliases(_bb: ByteBuffer): TypeAliases = getRootAsTypeAliases(_bb, TypeAliases())
fun getRootAsTypeAliases(_bb: ByteBuffer, obj: TypeAliases): TypeAliases {
_bb.order(ByteOrder.LITTLE_ENDIAN)
return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb))
}
fun createTypeAliases(builder: FlatBufferBuilder, i8: Byte, u8: UByte, i16: Short, u16: UShort, i32: Int, u32: UInt, i64: Long, u64: ULong, f32: Float, f64: Double, v8Offset: Int, vf64Offset: Int) : Int {
builder.startTable(12)
addF64(builder, f64)
addU64(builder, u64)
addI64(builder, i64)
addVf64(builder, vf64Offset)
addV8(builder, v8Offset)
addF32(builder, f32)
addU32(builder, u32)
addI32(builder, i32)
addU16(builder, u16)
addI16(builder, i16)
addU8(builder, u8)
addI8(builder, i8)
return endTypeAliases(builder)
}
fun startTypeAliases(builder: FlatBufferBuilder) = builder.startTable(12)
fun addI8(builder: FlatBufferBuilder, i8: Byte) = builder.addByte(0, i8, 0)
fun addU8(builder: FlatBufferBuilder, u8: UByte) = builder.addByte(1, u8.toByte(), 0)
fun addI16(builder: FlatBufferBuilder, i16: Short) = builder.addShort(2, i16, 0)
fun addU16(builder: FlatBufferBuilder, u16: UShort) = builder.addShort(3, u16.toShort(), 0)
fun addI32(builder: FlatBufferBuilder, i32: Int) = builder.addInt(4, i32, 0)
fun addU32(builder: FlatBufferBuilder, u32: UInt) = builder.addInt(5, u32.toInt(), 0)
fun addI64(builder: FlatBufferBuilder, i64: Long) = builder.addLong(6, i64, 0L)
fun addU64(builder: FlatBufferBuilder, u64: ULong) = builder.addLong(7, u64.toLong(), 0)
fun addF32(builder: FlatBufferBuilder, f32: Float) = builder.addFloat(8, f32, 0.0)
fun addF64(builder: FlatBufferBuilder, f64: Double) = builder.addDouble(9, f64, 0.0)
fun addV8(builder: FlatBufferBuilder, v8: Int) = builder.addOffset(10, v8, 0)
fun createV8Vector(builder: FlatBufferBuilder, data: ByteArray) : Int {
builder.startVector(1, data.size, 1)
for (i in data.size - 1 downTo 0) {
builder.addByte(data[i])
}
return builder.endVector()
}
fun startV8Vector(builder: FlatBufferBuilder, numElems: Int) = builder.startVector(1, numElems, 1)
fun addVf64(builder: FlatBufferBuilder, vf64: Int) = builder.addOffset(11, vf64, 0)
fun createVf64Vector(builder: FlatBufferBuilder, data: DoubleArray) : Int {
builder.startVector(8, data.size, 8)
for (i in data.size - 1 downTo 0) {
builder.addDouble(data[i])
}
return builder.endVector()
}
fun startVf64Vector(builder: FlatBufferBuilder, numElems: Int) = builder.startVector(8, numElems, 8)
fun endTypeAliases(builder: FlatBufferBuilder) : Int {
val o = builder.endTable()
return o
}
}
}
// automatically generated by the FlatBuffers compiler, do not modify
package MyGame.Example
import java.nio.*
import kotlin.math.sign
import com.google.flatbuffers.*
@Suppress("unused")
@ExperimentalUnsignedTypes
class Vec3 : Struct() {
fun __init(_i: Int, _bb: ByteBuffer) {
__reset(_i, _bb)
}
fun __assign(_i: Int, _bb: ByteBuffer) : Vec3 {
__init(_i, _bb)
return this
}
val x : Float get() = bb.getFloat(bb_pos + 0)
fun mutateX(x: Float) : ByteBuffer = bb.putFloat(bb_pos + 0, x)
val y : Float get() = bb.getFloat(bb_pos + 4)
fun mutateY(y: Float) : ByteBuffer = bb.putFloat(bb_pos + 4, y)
val z : Float get() = bb.getFloat(bb_pos + 8)
fun mutateZ(z: Float) : ByteBuffer = bb.putFloat(bb_pos + 8, z)
val test1 : Double get() = bb.getDouble(bb_pos + 16)
fun mutateTest1(test1: Double) : ByteBuffer = bb.putDouble(bb_pos + 16, test1)
val test2 : UByte get() = bb.get(bb_pos + 24).toUByte()
fun mutateTest2(test2: UByte) : ByteBuffer = bb.put(bb_pos + 24, test2.toByte())
val test3 : MyGame.Example.Test? get() = test3(MyGame.Example.Test())
fun test3(obj: MyGame.Example.Test) : MyGame.Example.Test? = obj.__assign(bb_pos + 26, bb)
companion object {
fun createVec3(builder: FlatBufferBuilder, x: Float, y: Float, z: Float, test1: Double, test2: UByte, test3_a: Short, test3_b: Byte) : Int {
builder.prep(8, 32)
builder.pad(2)
builder.prep(2, 4)
builder.pad(1)
builder.putByte(test3_b)
builder.putShort(test3_a)
builder.pad(1)
builder.putByte(test2.toByte())
builder.putDouble(test1)
builder.pad(4)
builder.putFloat(z)
builder.putFloat(y)
builder.putFloat(x)
return builder.offset()
}
}
}
// automatically generated by the FlatBuffers compiler, do not modify
package MyGame.Example2
import java.nio.*
import kotlin.math.sign
import com.google.flatbuffers.*
@Suppress("unused")
@ExperimentalUnsignedTypes
class Monster : Table() {
fun __init(_i: Int, _bb: ByteBuffer) {
__reset(_i, _bb)
}
fun __assign(_i: Int, _bb: ByteBuffer) : Monster {
__init(_i, _bb)
return this
}
companion object {
fun validateVersion() = Constants.FLATBUFFERS_1_11_1()
fun getRootAsMonster(_bb: ByteBuffer): Monster = getRootAsMonster(_bb, Monster())
fun getRootAsMonster(_bb: ByteBuffer, obj: Monster): Monster {
_bb.order(ByteOrder.LITTLE_ENDIAN)
return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb))
}
fun startMonster(builder: FlatBufferBuilder) = builder.startTable(0)
fun endMonster(builder: FlatBufferBuilder) : Int {
val o = builder.endTable()
return o
}
}
}
// automatically generated by the FlatBuffers compiler, do not modify
package MyGame
import java.nio.*
import kotlin.math.sign
import com.google.flatbuffers.*
@Suppress("unused")
@ExperimentalUnsignedTypes
class InParentNamespace : Table() {
fun __init(_i: Int, _bb: ByteBuffer) {
__reset(_i, _bb)
}
fun __assign(_i: Int, _bb: ByteBuffer) : InParentNamespace {
__init(_i, _bb)
return this
}
companion object {
fun validateVersion() = Constants.FLATBUFFERS_1_11_1()
fun getRootAsInParentNamespace(_bb: ByteBuffer): InParentNamespace = getRootAsInParentNamespace(_bb, InParentNamespace())
fun getRootAsInParentNamespace(_bb: ByteBuffer, obj: InParentNamespace): InParentNamespace {
_bb.order(ByteOrder.LITTLE_ENDIAN)
return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb))
}
fun startInParentNamespace(builder: FlatBufferBuilder) = builder.startTable(0)
fun endInParentNamespace(builder: FlatBufferBuilder) : Int {
val o = builder.endTable()
return o
}
}
}
// automatically generated by the FlatBuffers compiler, do not modify
package MyGame
import java.nio.*
import kotlin.math.sign
import com.google.flatbuffers.*
@Suppress("unused")
@ExperimentalUnsignedTypes
class MonsterExtra : Table() {
fun __init(_i: Int, _bb: ByteBuffer) {
__reset(_i, _bb)
}
fun __assign(_i: Int, _bb: ByteBuffer) : MonsterExtra {
__init(_i, _bb)
return this
}
val testfNan : Float
get() {
val o = __offset(4)
return if(o != 0) bb.getFloat(o + bb_pos) else Float.NaN
}
fun mutateTestfNan(testfNan: Float) : Boolean {
val o = __offset(4)
return if (o != 0) {
bb.putFloat(o + bb_pos, testfNan)
true
} else {
false
}
}
val testfPinf : Float
get() {
val o = __offset(6)
return if(o != 0) bb.getFloat(o + bb_pos) else Float.POSITIVE_INFINITY
}
fun mutateTestfPinf(testfPinf: Float) : Boolean {
val o = __offset(6)
return if (o != 0) {
bb.putFloat(o + bb_pos, testfPinf)
true
} else {
false
}
}
val testfNinf : Float
get() {
val o = __offset(8)
return if(o != 0) bb.getFloat(o + bb_pos) else Float.NEGATIVE_INFINITY
}
fun mutateTestfNinf(testfNinf: Float) : Boolean {
val o = __offset(8)
return if (o != 0) {
bb.putFloat(o + bb_pos, testfNinf)
true
} else {
false
}
}
val testdNan : Double
get() {
val o = __offset(10)
return if(o != 0) bb.getDouble(o + bb_pos) else Double.NaN
}
fun mutateTestdNan(testdNan: Double) : Boolean {
val o = __offset(10)
return if (o != 0) {
bb.putDouble(o + bb_pos, testdNan)
true
} else {
false
}
}
val testdPinf : Double
get() {
val o = __offset(12)
return if(o != 0) bb.getDouble(o + bb_pos) else Double.POSITIVE_INFINITY
}
fun mutateTestdPinf(testdPinf: Double) : Boolean {
val o = __offset(12)
return if (o != 0) {
bb.putDouble(o + bb_pos, testdPinf)
true
} else {
false
}
}
val testdNinf : Double
get() {
val o = __offset(14)
return if(o != 0) bb.getDouble(o + bb_pos) else Double.NEGATIVE_INFINITY
}
fun mutateTestdNinf(testdNinf: Double) : Boolean {
val o = __offset(14)
return if (o != 0) {
bb.putDouble(o + bb_pos, testdNinf)
true
} else {
false
}
}
fun testfVec(j: Int) : Float {
val o = __offset(16)
return if (o != 0) {
bb.getFloat(__vector(o) + j * 4)
} else {
0.0f
}
}
val testfVecLength : Int
get() {
val o = __offset(16); return if (o != 0) __vector_len(o) else 0
}
val testfVecAsByteBuffer : ByteBuffer get() = __vector_as_bytebuffer(16, 4)
fun testfVecInByteBuffer(_bb: ByteBuffer) : ByteBuffer = __vector_in_bytebuffer(_bb, 16, 4)
fun mutateTestfVec(j: Int, testfVec: Float) : Boolean {
val o = __offset(16)
return if (o != 0) {
bb.putFloat(__vector(o) + j * 4, testfVec)
true
} else {
false
}
}
fun testdVec(j: Int) : Double {
val o = __offset(18)
return if (o != 0) {
bb.getDouble(__vector(o) + j * 8)
} else {
0.0
}
}
val testdVecLength : Int
get() {
val o = __offset(18); return if (o != 0) __vector_len(o) else 0
}
val testdVecAsByteBuffer : ByteBuffer get() = __vector_as_bytebuffer(18, 8)
fun testdVecInByteBuffer(_bb: ByteBuffer) : ByteBuffer = __vector_in_bytebuffer(_bb, 18, 8)
fun mutateTestdVec(j: Int, testdVec: Double) : Boolean {
val o = __offset(18)
return if (o != 0) {
bb.putDouble(__vector(o) + j * 8, testdVec)
true
} else {
false
}
}
companion object {
fun validateVersion() = Constants.FLATBUFFERS_1_11_1()
fun getRootAsMonsterExtra(_bb: ByteBuffer): MonsterExtra = getRootAsMonsterExtra(_bb, MonsterExtra())
fun getRootAsMonsterExtra(_bb: ByteBuffer, obj: MonsterExtra): MonsterExtra {
_bb.order(ByteOrder.LITTLE_ENDIAN)
return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb))
}
fun MonsterExtraBufferHasIdentifier(_bb: ByteBuffer) : Boolean = __has_identifier(_bb, "MONE")
fun createMonsterExtra(builder: FlatBufferBuilder, testfNan: Float, testfPinf: Float, testfNinf: Float, testdNan: Double, testdPinf: Double, testdNinf: Double, testfVecOffset: Int, testdVecOffset: Int) : Int {
builder.startTable(8)
addTestdNinf(builder, testdNinf)
addTestdPinf(builder, testdPinf)
addTestdNan(builder, testdNan)
addTestdVec(builder, testdVecOffset)
addTestfVec(builder, testfVecOffset)
addTestfNinf(builder, testfNinf)
addTestfPinf(builder, testfPinf)
addTestfNan(builder, testfNan)
return endMonsterExtra(builder)
}
fun startMonsterExtra(builder: FlatBufferBuilder) = builder.startTable(8)
fun addTestfNan(builder: FlatBufferBuilder, testfNan: Float) = builder.addFloat(0, testfNan, Double.NaN)
fun addTestfPinf(builder: FlatBufferBuilder, testfPinf: Float) = builder.addFloat(1, testfPinf, Double.POSITIVE_INFINITY)
fun addTestfNinf(builder: FlatBufferBuilder, testfNinf: Float) = builder.addFloat(2, testfNinf, Double.NEGATIVE_INFINITY)
fun addTestdNan(builder: FlatBufferBuilder, testdNan: Double) = builder.addDouble(3, testdNan, Double.NaN)
fun addTestdPinf(builder: FlatBufferBuilder, testdPinf: Double) = builder.addDouble(4, testdPinf, Double.POSITIVE_INFINITY)
fun addTestdNinf(builder: FlatBufferBuilder, testdNinf: Double) = builder.addDouble(5, testdNinf, Double.NEGATIVE_INFINITY)
fun addTestfVec(builder: FlatBufferBuilder, testfVec: Int) = builder.addOffset(6, testfVec, 0)
fun createTestfVecVector(builder: FlatBufferBuilder, data: FloatArray) : Int {
builder.startVector(4, data.size, 4)
for (i in data.size - 1 downTo 0) {
builder.addFloat(data[i])
}
return builder.endVector()
}
fun startTestfVecVector(builder: FlatBufferBuilder, numElems: Int) = builder.startVector(4, numElems, 4)
fun addTestdVec(builder: FlatBufferBuilder, testdVec: Int) = builder.addOffset(7, testdVec, 0)
fun createTestdVecVector(builder: FlatBufferBuilder, data: DoubleArray) : Int {
builder.startVector(8, data.size, 8)
for (i in data.size - 1 downTo 0) {
builder.addDouble(data[i])
}
return builder.endVector()
}
fun startTestdVecVector(builder: FlatBufferBuilder, numElems: Int) = builder.startVector(8, numElems, 8)
fun endMonsterExtra(builder: FlatBufferBuilder) : Int {
val o = builder.endTable()
return o
}
fun finishMonsterExtraBuffer(builder: FlatBufferBuilder, offset: Int) = builder.finish(offset, "MONE")
fun finishSizePrefixedMonsterExtraBuffer(builder: FlatBufferBuilder, offset: Int) = builder.finishSizePrefixed(offset, "MONE")
}
}
......@@ -2,6 +2,10 @@ echo "************************ Java:"
sh JavaTest.sh
echo "************************ Kotlin:"
sh KotlinTest.sh
echo "************************ Go:"
sh GoTest.sh
......
......@@ -15,16 +15,16 @@
# limitations under the License.
set -e
../flatc --cpp --java --csharp --dart --go --binary --lobster --lua --python --js --ts --php --rust --grpc --gen-mutable --reflect-names --gen-object-api --gen-compare --no-includes --cpp-ptr-type flatbuffers::unique_ptr --no-fb-import -I include_test monster_test.fbs monsterdata_test.json
../flatc --cpp --java --csharp --dart --go --binary --lobster --lua --python --js --ts --php --rust --gen-mutable --reflect-names --no-fb-import --cpp-ptr-type flatbuffers::unique_ptr -o namespace_test namespace_test/namespace_test1.fbs namespace_test/namespace_test2.fbs
../flatc --cpp --java --csharp --js --ts --php --gen-mutable --reflect-names --gen-object-api --gen-compare --cpp-ptr-type flatbuffers::unique_ptr -o union_vector ./union_vector/union_vector.fbs
../flatc --cpp --java --kotlin --csharp --dart --go --binary --lobster --lua --python --js --ts --php --rust --grpc --gen-mutable --reflect-names --gen-object-api --gen-compare --no-includes --cpp-ptr-type flatbuffers::unique_ptr --no-fb-import -I include_test monster_test.fbs monsterdata_test.json
../flatc --cpp --java --kotlin --csharp --dart --go --binary --lobster --lua --python --js --ts --php --rust --gen-mutable --reflect-names --no-fb-import --cpp-ptr-type flatbuffers::unique_ptr -o namespace_test namespace_test/namespace_test1.fbs namespace_test/namespace_test2.fbs
../flatc --cpp --java --kotlin --csharp --js --ts --php --gen-mutable --reflect-names --gen-object-api --gen-compare --cpp-ptr-type flatbuffers::unique_ptr -o union_vector ./union_vector/union_vector.fbs
../flatc -b --schema --bfbs-comments --bfbs-builtins -I include_test monster_test.fbs
../flatc -b --schema --bfbs-comments --bfbs-builtins -I include_test arrays_test.fbs
../flatc --jsonschema --schema -I include_test monster_test.fbs
../flatc --cpp --java --csharp --python --gen-mutable --reflect-names --gen-object-api --gen-compare --no-includes monster_extra.fbs monsterdata_extra.json
../flatc --cpp --java --kotlin --csharp --python --gen-mutable --reflect-names --gen-object-api --gen-compare --no-includes monster_extra.fbs monsterdata_extra.json
../flatc --cpp --java --csharp --python --gen-mutable --reflect-names --gen-object-api --gen-compare --no-includes --scoped-enums --jsonschema --cpp-ptr-type flatbuffers::unique_ptr arrays_test.fbs
cd ../samples
../flatc --cpp --lobster --gen-mutable --reflect-names --gen-object-api --gen-compare --cpp-ptr-type flatbuffers::unique_ptr monster.fbs
../flatc --cpp --kotlin --lobster --gen-mutable --reflect-names --gen-object-api --gen-compare --cpp-ptr-type flatbuffers::unique_ptr monster.fbs
../flatc -b --schema --bfbs-comments --bfbs-builtins monster.fbs
cd ../reflection
./generate_code.sh
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment