Commit 43dbac5d authored by aardappel's avatar aardappel

Lobster: added builder API for tables

parent 53ea1ab1
......@@ -815,10 +815,11 @@ our `orc` Monster, lets create some `Weapon`s: a `Sword` and an `Axe`.
let weapon_offsets = map(weapon_names) name, i:
let ns = builder.CreateString(name)
builder.MyGame_Sample_WeaponStart()
builder.MyGame_Sample_WeaponAddName(ns)
builder.MyGame_Sample_WeaponAddDamage(weapon_damages[i])
builder.MyGame_Sample_WeaponEnd()
MyGame_Sample_WeaponBuilder { b }
.start()
.add_name(ns)
.add_damage(weapon_damages[i])
.end()
~~~
</div>
<div class="language-rust">
......@@ -1503,17 +1504,18 @@ can serialize the monster itself:
</div>
<div class="language-lobster">
~~~{.lobster}
builder.MyGame_Sample_MonsterStart()
builder.MyGame_Sample_MonsterAddPos(builder.MyGame_Sample_CreateVec3(1.0, 2.0, 3.0))
builder.MyGame_Sample_MonsterAddHp(300)
builder.MyGame_Sample_MonsterAddName(name)
builder.MyGame_Sample_MonsterAddInventory(inv)
builder.MyGame_Sample_MonsterAddColor(MyGame_Sample_Color_Red)
builder.MyGame_Sample_MonsterAddWeapons(weapons)
builder.MyGame_Sample_MonsterAddEquippedType(MyGame_Sample_Equipment_Weapon)
builder.MyGame_Sample_MonsterAddEquipped(weapon_offsets[1])
builder.MyGame_Sample_MonsterAddPath(path)
let orc = builder.MyGame_Sample_MonsterEnd()
let orc = MyGame_Sample_MonsterBuilder { b }
.start()
.add_pos(b.MyGame_Sample_CreateVec3(1.0, 2.0, 3.0))
.add_hp(300)
.add_name(name)
.add_inventory(inv)
.add_color(MyGame_Sample_Color_Red)
.add_weapons(weapons)
.add_equipped_type(MyGame_Sample_Equipment_Weapon)
.add_equipped(weapon_offsets[1])
.add_path(path)
.end()
~~~
</div>
<div class="language-rust">
......@@ -1687,8 +1689,8 @@ Here is a repetition these lines, to help highlight them more clearly:
</div>
<div class="language-lobster">
~~~{.lobster}
builder.MyGame_Sample_MonsterAddEquippedType(MyGame_Sample_Equipment_Weapon)
builder.MyGame_Sample_MonsterAddEquipped(axe)
.add_equipped_type(MyGame_Sample_Equipment_Weapon)
.add_equipped(axe)
~~~
</div>
<div class="language-rust">
......
......@@ -282,4 +282,3 @@ class builder:
// elsewhere.
assert x.o == head
Slot(v)
......@@ -64,44 +64,60 @@ class Monster : flatbuffers_handle
def GetRootAsMonster(buf:string): return Monster { buf, buf.flatbuffers_indirect(0) }
def MonsterStart(b_:flatbuffers_builder):
b_.StartObject(11)
def MonsterAddPos(b_:flatbuffers_builder, pos:flatbuffers_offset):
b_.PrependStructSlot(0, pos)
def MonsterAddMana(b_:flatbuffers_builder, mana:int):
b_.PrependInt16Slot(1, mana, 150)
def MonsterAddHp(b_:flatbuffers_builder, hp:int):
b_.PrependInt16Slot(2, hp, 100)
def MonsterAddName(b_:flatbuffers_builder, name:flatbuffers_offset):
b_.PrependUOffsetTRelativeSlot(3, name)
def MonsterAddInventory(b_:flatbuffers_builder, inventory:flatbuffers_offset):
b_.PrependUOffsetTRelativeSlot(5, inventory)
struct MonsterBuilder:
b_:flatbuffers_builder
def start():
b_.StartObject(11)
return this
def add_pos(pos:flatbuffers_offset):
b_.PrependStructSlot(0, pos)
return this
def add_mana(mana:int):
b_.PrependInt16Slot(1, mana, 150)
return this
def add_hp(hp:int):
b_.PrependInt16Slot(2, hp, 100)
return this
def add_name(name:flatbuffers_offset):
b_.PrependUOffsetTRelativeSlot(3, name)
return this
def add_inventory(inventory:flatbuffers_offset):
b_.PrependUOffsetTRelativeSlot(5, inventory)
return this
def add_color(color:Color):
b_.PrependInt8Slot(6, color, 2)
return this
def add_weapons(weapons:flatbuffers_offset):
b_.PrependUOffsetTRelativeSlot(7, weapons)
return this
def add_equipped_type(equipped_type:Equipment):
b_.PrependUint8Slot(8, equipped_type, 0)
return this
def add_equipped(equipped:flatbuffers_offset):
b_.PrependUOffsetTRelativeSlot(9, equipped)
return this
def add_path(path:flatbuffers_offset):
b_.PrependUOffsetTRelativeSlot(10, path)
return this
def end():
return b_.EndObject()
def MonsterStartInventoryVector(b_:flatbuffers_builder, n_:int):
b_.StartVector(1, n_, 1)
def MonsterCreateInventoryVector(b_:flatbuffers_builder, v_:[int]):
b_.StartVector(1, v_.length, 1)
reverse(v_) e_: b_.PrependUint8(e_)
return b_.EndVector(v_.length)
def MonsterAddColor(b_:flatbuffers_builder, color:Color):
b_.PrependInt8Slot(6, color, 2)
def MonsterAddWeapons(b_:flatbuffers_builder, weapons:flatbuffers_offset):
b_.PrependUOffsetTRelativeSlot(7, weapons)
def MonsterStartWeaponsVector(b_:flatbuffers_builder, n_:int):
b_.StartVector(4, n_, 4)
def MonsterCreateWeaponsVector(b_:flatbuffers_builder, v_:[flatbuffers_offset]):
b_.StartVector(4, v_.length, 4)
reverse(v_) e_: b_.PrependUOffsetTRelative(e_)
return b_.EndVector(v_.length)
def MonsterAddEquippedType(b_:flatbuffers_builder, equipped_type:Equipment):
b_.PrependUint8Slot(8, equipped_type, 0)
def MonsterAddEquipped(b_:flatbuffers_builder, equipped:flatbuffers_offset):
b_.PrependUOffsetTRelativeSlot(9, equipped)
def MonsterAddPath(b_:flatbuffers_builder, path:flatbuffers_offset):
b_.PrependUOffsetTRelativeSlot(10, path)
def MonsterStartPathVector(b_:flatbuffers_builder, n_:int):
b_.StartVector(12, n_, 4)
def MonsterEnd(b_:flatbuffers_builder):
return b_.EndObject()
class Weapon : flatbuffers_handle
def name():
......@@ -111,12 +127,17 @@ class Weapon : flatbuffers_handle
def GetRootAsWeapon(buf:string): return Weapon { buf, buf.flatbuffers_indirect(0) }
def WeaponStart(b_:flatbuffers_builder):
b_.StartObject(2)
def WeaponAddName(b_:flatbuffers_builder, name:flatbuffers_offset):
b_.PrependUOffsetTRelativeSlot(0, name)
def WeaponAddDamage(b_:flatbuffers_builder, damage:int):
b_.PrependInt16Slot(1, damage, 0)
def WeaponEnd(b_:flatbuffers_builder):
return b_.EndObject()
struct WeaponBuilder:
b_:flatbuffers_builder
def start():
b_.StartObject(2)
return this
def add_name(name:flatbuffers_offset):
b_.PrependUOffsetTRelativeSlot(0, name)
return this
def add_damage(damage:int):
b_.PrependInt16Slot(1, damage, 0)
return this
def end():
return b_.EndObject()
......@@ -26,10 +26,11 @@ let weapon_damages = [ 3, 5 ]
let weapon_offsets = map(weapon_names) name, i:
let ns = b.CreateString(name)
b.MyGame_Sample_WeaponStart()
b.MyGame_Sample_WeaponAddName(ns)
b.MyGame_Sample_WeaponAddDamage(weapon_damages[i])
b.MyGame_Sample_WeaponEnd()
MyGame_Sample_WeaponBuilder { b }
.start()
.add_name(ns)
.add_damage(weapon_damages[i])
.end()
let weapons = b.MyGame_Sample_MonsterCreateWeaponsVector(weapon_offsets)
......@@ -40,16 +41,17 @@ let name = b.CreateString("Orc")
let inv = b.MyGame_Sample_MonsterCreateInventoryVector(map(10): _)
// Now pack it all together in our root monster object.
b.MyGame_Sample_MonsterStart()
b.MyGame_Sample_MonsterAddPos(b.MyGame_Sample_CreateVec3(1.0, 2.0, 3.0))
b.MyGame_Sample_MonsterAddHp(300)
b.MyGame_Sample_MonsterAddName(name)
b.MyGame_Sample_MonsterAddInventory(inv)
b.MyGame_Sample_MonsterAddColor(MyGame_Sample_Color_Red)
b.MyGame_Sample_MonsterAddWeapons(weapons)
b.MyGame_Sample_MonsterAddEquippedType(MyGame_Sample_Equipment_Weapon)
b.MyGame_Sample_MonsterAddEquipped(weapon_offsets[1])
let orc = b.MyGame_Sample_MonsterEnd()
let orc = MyGame_Sample_MonsterBuilder { b }
.start()
.add_pos(b.MyGame_Sample_CreateVec3(1.0, 2.0, 3.0))
.add_hp(300)
.add_name(name)
.add_inventory(inv)
.add_color(MyGame_Sample_Color_Red)
.add_weapons(weapons)
.add_equipped_type(MyGame_Sample_Equipment_Weapon)
.add_equipped(weapon_offsets[1])
.end()
// Finish the buffer!
b.Finish(orc)
......
......@@ -180,22 +180,28 @@ class LobsterGenerator : public BaseGenerator {
void GenTableBuilders(const StructDef &struct_def,
std::string *code_ptr) {
std::string &code = *code_ptr;
code += "def " + NormalizedName(struct_def) +
"Start(b_:flatbuffers_builder):\n b_.StartObject(" +
NumToString(struct_def.fields.vec.size()) + ")\n";
code += "struct " + NormalizedName(struct_def) +
"Builder:\n b_:flatbuffers_builder\n";
code += " def start():\n b_.StartObject(" +
NumToString(struct_def.fields.vec.size()) + ")\n return this\n";
for (auto it = struct_def.fields.vec.begin();
it != struct_def.fields.vec.end(); ++it) {
it != struct_def.fields.vec.end(); ++it) {
auto &field = **it;
if (field.deprecated) continue;
auto offset = it - struct_def.fields.vec.begin();
code += "def " + NormalizedName(struct_def) + "Add" +
MakeCamel(NormalizedName(field)) + "(b_:flatbuffers_builder, " +
code += " def add_" + NormalizedName(field) + "(" +
NormalizedName(field) + ":" + LobsterType(field.value.type) +
"):\n b_.Prepend" + GenMethod(field.value.type) + "Slot(" +
"):\n b_.Prepend" + GenMethod(field.value.type) + "Slot(" +
NumToString(offset) + ", " + NormalizedName(field);
if (IsScalar(field.value.type.base_type))
code += ", " + field.value.constant;
code += ")\n";
code += ")\n return this\n";
}
code += " def end():\n return b_.EndObject()\n\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_VECTOR) {
code += "def " + NormalizedName(struct_def) + "Start" +
MakeCamel(NormalizedName(field)) +
......@@ -217,10 +223,9 @@ class LobsterGenerator : public BaseGenerator {
GenMethod(vector_type) +
"(e_)\n return b_.EndVector(v_.length)\n";
}
code += "\n";
}
}
code += "def " + NormalizedName(struct_def) +
"End(b_:flatbuffers_builder):\n return b_.EndObject()\n\n";
}
void GenStructPreDecl(const StructDef &struct_def, std::string *code_ptr) {
......
......@@ -70,9 +70,10 @@ def make_monster_from_generated_code():
let inv = b.MyGame_Example_MonsterCreateInventoryVector([ 0, 1, 2, 3, 4 ])
b.MyGame_Example_MonsterStart()
b.MyGame_Example_MonsterAddName(fred)
let mon2 = b.MyGame_Example_MonsterEnd()
let mon2 = MyGame_Example_MonsterBuilder { b }
.start()
.add_name(fred)
.end()
b.MyGame_Example_MonsterStartTest4Vector(2)
b.MyGame_Example_CreateTest(10, 20)
......@@ -88,19 +89,20 @@ def make_monster_from_generated_code():
let vector_of_doubles = b.MyGame_Example_MonsterCreateVectorOfDoublesVector(
[ -1.7976931348623157e+308, 0, 1.7976931348623157e+308 ])
b.MyGame_Example_MonsterStart()
b.MyGame_Example_MonsterAddPos(b.MyGame_Example_CreateVec3(1.0, 2.0, 3.0, 3.0,
MyGame_Example_Color_Green, 5, 6))
b.MyGame_Example_MonsterAddHp(80)
b.MyGame_Example_MonsterAddName(name)
b.MyGame_Example_MonsterAddInventory(inv)
b.MyGame_Example_MonsterAddTestType(MyGame_Example_Any_Monster)
b.MyGame_Example_MonsterAddTest(mon2)
b.MyGame_Example_MonsterAddTest4(test4)
b.MyGame_Example_MonsterAddTestarrayofstring(test_array_of_string)
b.MyGame_Example_MonsterAddVectorOfLongs(vector_of_longs)
b.MyGame_Example_MonsterAddVectorOfDoubles(vector_of_doubles)
let mon = b.MyGame_Example_MonsterEnd()
let mon = MyGame_Example_MonsterBuilder { b }
.start()
.add_pos(b.MyGame_Example_CreateVec3(1.0, 2.0, 3.0, 3.0,
MyGame_Example_Color_Green, 5, 6))
.add_hp(80)
.add_name(name)
.add_inventory(inv)
.add_test_type(MyGame_Example_Any_Monster)
.add_test(mon2)
.add_test4(test4)
.add_testarrayofstring(test_array_of_string)
.add_vector_of_longs(vector_of_longs)
.add_vector_of_doubles(vector_of_doubles)
.end()
b.Finish(mon)
......
This diff is collapsed.
......@@ -18,12 +18,16 @@ class TableInNestedNS : flatbuffers_handle
def GetRootAsTableInNestedNS(buf:string): return TableInNestedNS { buf, buf.flatbuffers_indirect(0) }
def TableInNestedNSStart(b_:flatbuffers_builder):
b_.StartObject(1)
def TableInNestedNSAddFoo(b_:flatbuffers_builder, foo:int):
b_.PrependInt32Slot(0, foo, 0)
def TableInNestedNSEnd(b_:flatbuffers_builder):
return b_.EndObject()
struct TableInNestedNSBuilder:
b_:flatbuffers_builder
def start():
b_.StartObject(1)
return this
def add_foo(foo:int):
b_.PrependInt32Slot(0, foo, 0)
return this
def end():
return b_.EndObject()
class StructInNestedNS : flatbuffers_handle
def a():
......
......@@ -25,16 +25,22 @@ class TableInFirstNS : flatbuffers_handle
def GetRootAsTableInFirstNS(buf:string): return TableInFirstNS { buf, buf.flatbuffers_indirect(0) }
def TableInFirstNSStart(b_:flatbuffers_builder):
b_.StartObject(3)
def TableInFirstNSAddFooTable(b_:flatbuffers_builder, foo_table:flatbuffers_offset):
b_.PrependUOffsetTRelativeSlot(0, foo_table)
def TableInFirstNSAddFooEnum(b_:flatbuffers_builder, foo_enum:EnumInNestedNS):
b_.PrependInt8Slot(1, foo_enum, 0)
def TableInFirstNSAddFooStruct(b_:flatbuffers_builder, foo_struct:flatbuffers_offset):
b_.PrependStructSlot(2, foo_struct)
def TableInFirstNSEnd(b_:flatbuffers_builder):
return b_.EndObject()
struct TableInFirstNSBuilder:
b_:flatbuffers_builder
def start():
b_.StartObject(3)
return this
def add_foo_table(foo_table:flatbuffers_offset):
b_.PrependUOffsetTRelativeSlot(0, foo_table)
return this
def add_foo_enum(foo_enum:EnumInNestedNS):
b_.PrependInt8Slot(1, foo_enum, 0)
return this
def add_foo_struct(foo_struct:flatbuffers_offset):
b_.PrependStructSlot(2, foo_struct)
return this
def end():
return b_.EndObject()
namespace NamespaceC
......@@ -48,14 +54,19 @@ class TableInC : flatbuffers_handle
def GetRootAsTableInC(buf:string): return TableInC { buf, buf.flatbuffers_indirect(0) }
def TableInCStart(b_:flatbuffers_builder):
b_.StartObject(2)
def TableInCAddReferToA1(b_:flatbuffers_builder, refer_to_a1:flatbuffers_offset):
b_.PrependUOffsetTRelativeSlot(0, refer_to_a1)
def TableInCAddReferToA2(b_:flatbuffers_builder, refer_to_a2:flatbuffers_offset):
b_.PrependUOffsetTRelativeSlot(1, refer_to_a2)
def TableInCEnd(b_:flatbuffers_builder):
return b_.EndObject()
struct TableInCBuilder:
b_:flatbuffers_builder
def start():
b_.StartObject(2)
return this
def add_refer_to_a1(refer_to_a1:flatbuffers_offset):
b_.PrependUOffsetTRelativeSlot(0, refer_to_a1)
return this
def add_refer_to_a2(refer_to_a2:flatbuffers_offset):
b_.PrependUOffsetTRelativeSlot(1, refer_to_a2)
return this
def end():
return b_.EndObject()
namespace NamespaceA
......@@ -66,10 +77,14 @@ class SecondTableInA : flatbuffers_handle
def GetRootAsSecondTableInA(buf:string): return SecondTableInA { buf, buf.flatbuffers_indirect(0) }
def SecondTableInAStart(b_:flatbuffers_builder):
b_.StartObject(1)
def SecondTableInAAddReferToC(b_:flatbuffers_builder, refer_to_c:flatbuffers_offset):
b_.PrependUOffsetTRelativeSlot(0, refer_to_c)
def SecondTableInAEnd(b_:flatbuffers_builder):
return b_.EndObject()
struct SecondTableInABuilder:
b_:flatbuffers_builder
def start():
b_.StartObject(1)
return this
def add_refer_to_c(refer_to_c:flatbuffers_offset):
b_.PrependUOffsetTRelativeSlot(0, refer_to_c)
return this
def end():
return b_.EndObject()
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