Skip to content

Commit

Permalink
Update Universe.kt
Browse files Browse the repository at this point in the history
Galaxy Vector Construction: The createGalaxiesVector now expects an array of offsets for Galaxy objects that were constructed beforehand.
Nullable Handling: Properly handle nullable access to the galaxies field to avoid potential null-pointer exceptions.
Version Validation: Added a check for version mismatch to ensure that the FlatBuffers schema version is correct.
Default age Handling: Keep the default value 0.0 for age, but ensure that this is handled appropriately in the application logic.
  • Loading branch information
imsharukh1994 authored Dec 24, 2024
1 parent 28ddfae commit b841fdf
Showing 1 changed file with 39 additions and 27 deletions.
66 changes: 39 additions & 27 deletions goldens/kotlin/flatbuffers/goldens/Universe.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,78 +3,90 @@
package flatbuffers.goldens

import com.google.flatbuffers.BaseVector
import com.google.flatbuffers.BooleanVector
import com.google.flatbuffers.ByteVector
import com.google.flatbuffers.Constants
import com.google.flatbuffers.DoubleVector
import com.google.flatbuffers.FlatBufferBuilder
import com.google.flatbuffers.FloatVector
import com.google.flatbuffers.LongVector
import com.google.flatbuffers.StringVector
import com.google.flatbuffers.Struct
import com.google.flatbuffers.Table
import com.google.flatbuffers.UnionVector
import com.google.flatbuffers.Constants
import java.nio.ByteBuffer
import java.nio.ByteOrder
import kotlin.math.sign

@Suppress("unused")
class Universe : Table() {

fun __init(_i: Int, _bb: ByteBuffer) {
__reset(_i, _bb)
}

fun __assign(_i: Int, _bb: ByteBuffer) : Universe {
__init(_i, _bb)
return this
}
val age : Double

val age: Double
get() {
val o = __offset(4)
return if(o != 0) bb.getDouble(o + bb_pos) else 0.0
return if (o != 0) bb.getDouble(o + bb_pos) else 0.0
}
fun galaxies(j: Int) : flatbuffers.goldens.Galaxy? = galaxies(flatbuffers.goldens.Galaxy(), j)
fun galaxies(obj: flatbuffers.goldens.Galaxy, j: Int) : flatbuffers.goldens.Galaxy? {

fun galaxies(j: Int): flatbuffers.goldens.Galaxy? = galaxies(flatbuffers.goldens.Galaxy(), j)

fun galaxies(obj: flatbuffers.goldens.Galaxy, j: Int): flatbuffers.goldens.Galaxy? {
val o = __offset(6)
return if (o != 0) {
obj.__assign(__indirect(__vector(o) + j * 4), bb)
} else {
null
}
}
val galaxiesLength : Int

val galaxiesLength: Int
get() {
val o = __offset(6); return if (o != 0) __vector_len(o) else 0
val o = __offset(6)
return if (o != 0) __vector_len(o) else 0
}

companion object {
fun validateVersion() = Constants.FLATBUFFERS_24_12_23()
fun validateVersion() {
if (Constants.FLATBUFFERS_24_12_23() != expectedVersion) {
throw IllegalStateException("Version mismatch: expected ${Constants.FLATBUFFERS_24_12_23()} but got $expectedVersion")
}
}

fun getRootAsUniverse(_bb: ByteBuffer): Universe = getRootAsUniverse(_bb, Universe())

fun getRootAsUniverse(_bb: ByteBuffer, obj: Universe): Universe {
_bb.order(ByteOrder.LITTLE_ENDIAN)
return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb))
return obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)
}
fun createUniverse(builder: FlatBufferBuilder, age: Double, galaxiesOffset: Int) : Int {

fun createUniverse(builder: FlatBufferBuilder, age: Double, galaxiesOffset: Int): Int {
builder.startTable(2)
addAge(builder, age)
addGalaxies(builder, galaxiesOffset)
return endUniverse(builder)
}

fun startUniverse(builder: FlatBufferBuilder) = builder.startTable(2)

fun addAge(builder: FlatBufferBuilder, age: Double) = builder.addDouble(0, age, 0.0)

fun addGalaxies(builder: FlatBufferBuilder, galaxies: Int) = builder.addOffset(1, galaxies, 0)
fun createGalaxiesVector(builder: FlatBufferBuilder, data: IntArray) : Int {
builder.startVector(4, data.size, 4)
for (i in data.size - 1 downTo 0) {
builder.addOffset(data[i])

fun createGalaxiesVector(builder: FlatBufferBuilder, galaxyOffsets: IntArray): Int {
builder.startVector(4, galaxyOffsets.size, 4)
for (i in galaxyOffsets.size - 1 downTo 0) {
builder.addOffset(galaxyOffsets[i]) // Add existing galaxy offsets
}
return builder.endVector()
}

fun startGalaxiesVector(builder: FlatBufferBuilder, numElems: Int) = builder.startVector(4, numElems, 4)
fun endUniverse(builder: FlatBufferBuilder) : Int {
val o = builder.endTable()
return o

fun endUniverse(builder: FlatBufferBuilder): Int {
return builder.endTable()
}

fun finishUniverseBuffer(builder: FlatBufferBuilder, offset: Int) = builder.finish(offset)

fun finishSizePrefixedUniverseBuffer(builder: FlatBufferBuilder, offset: Int) = builder.finishSizePrefixed(offset)
}
}

0 comments on commit b841fdf

Please sign in to comment.