Skip to content

Commit

Permalink
[C#, Java, C++] Fixes issue #5399 by always including namespaces (#5404)
Browse files Browse the repository at this point in the history
* [C#] Fixes issue #5399 by always including namespaces

* Updated tests for code generator changes

* Fixed 'As' method names
  • Loading branch information
lux authored and aardappel committed Jun 14, 2019
1 parent a6be1d0 commit a80db85
Show file tree
Hide file tree
Showing 26 changed files with 560 additions and 561 deletions.
5 changes: 3 additions & 2 deletions include/flatbuffers/code_generators.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ class BaseGenerator {
// which works for js and php
virtual const Namespace *CurrentNameSpace() const { return nullptr; }

// Ensure that a type is prefixed with its namespace whenever it is used
// outside of its namespace.
// Ensure that a type is prefixed with its namespace even within
// its own namespace to avoid conflict between generated method
// names and similarly named classes or structs
std::string WrapInNameSpace(const Namespace *ns,
const std::string &name) const;

Expand Down
218 changes: 109 additions & 109 deletions include/flatbuffers/reflection_generated.h

Large diffs are not rendered by default.

138 changes: 69 additions & 69 deletions samples/monster_generated.h

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions src/code_generators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,9 @@ std::string BaseGenerator::LastNamespacePart(const Namespace &ns) {
return std::string("");
}

// Ensure that a type is prefixed with its namespace whenever it is used
// outside of its namespace.
// Ensure that a type is prefixed with its namespace.
std::string BaseGenerator::WrapInNameSpace(const Namespace *ns,
const std::string &name) const {
if (CurrentNameSpace() == ns) return name;
std::string qualified_name = qualifying_start_;
for (auto it = ns->components.begin(); it != ns->components.end(); ++it)
qualified_name += *it + qualifying_separator_;
Expand Down
2 changes: 1 addition & 1 deletion src/idl_gen_general.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,7 @@ class GeneralGenerator : public BaseGenerator {
auto nested_type_name = WrapInNameSpace(*field.nested_flatbuffer);
auto nested_method_name =
MakeCamel(field.name, lang_.first_camel_upper) + "As" +
nested_type_name;
field.nested_flatbuffer->name;
auto get_nested_method_name = nested_method_name;
if (lang_.language == IDLOptions::kCSharp) {
get_nested_method_name = "Get" + nested_method_name;
Expand Down
4 changes: 2 additions & 2 deletions tests/MyGame/Example/Ability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public struct Ability : IFlatbufferObject
public uint Distance { get { return __p.bb.GetUint(__p.bb_pos + 4); } }
public void MutateDistance(uint distance) { __p.bb.PutUint(__p.bb_pos + 4, distance); }

public static Offset<Ability> CreateAbility(FlatBufferBuilder builder, uint Id, uint Distance) {
public static Offset<MyGame.Example.Ability> CreateAbility(FlatBufferBuilder builder, uint Id, uint Distance) {
builder.Prep(4, 8);
builder.PutUint(Distance);
builder.PutUint(Id);
return new Offset<Ability>(builder.Offset);
return new Offset<MyGame.Example.Ability>(builder.Offset);
}
};

Expand Down
86 changes: 43 additions & 43 deletions tests/MyGame/Example/Monster.cs

Large diffs are not rendered by default.

52 changes: 26 additions & 26 deletions tests/MyGame/Example/Monster.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public final class Monster extends Table {
public void __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; vtable_start = bb_pos - bb.getInt(bb_pos); vtable_size = bb.getShort(vtable_start); }
public Monster __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }

public Vec3 pos() { return pos(new Vec3()); }
public Vec3 pos(Vec3 obj) { int o = __offset(4); return o != 0 ? obj.__assign(o + bb_pos, bb) : null; }
public MyGame.Example.Vec3 pos() { return pos(new MyGame.Example.Vec3()); }
public MyGame.Example.Vec3 pos(MyGame.Example.Vec3 obj) { int o = __offset(4); return o != 0 ? obj.__assign(o + bb_pos, bb) : null; }
public short mana() { int o = __offset(6); return o != 0 ? bb.getShort(o + bb_pos) : 150; }
public boolean mutateMana(short mana) { int o = __offset(6); if (o != 0) { bb.putShort(o + bb_pos, mana); return true; } else { return false; } }
public short hp() { int o = __offset(8); return o != 0 ? bb.getShort(o + bb_pos) : 100; }
Expand All @@ -37,31 +37,31 @@ public final class Monster extends Table {
public byte testType() { int o = __offset(18); return o != 0 ? bb.get(o + bb_pos) : 0; }
public boolean mutateTestType(byte test_type) { int o = __offset(18); if (o != 0) { bb.put(o + bb_pos, test_type); return true; } else { return false; } }
public Table test(Table obj) { int o = __offset(20); return o != 0 ? __union(obj, o) : null; }
public Test test4(int j) { return test4(new Test(), j); }
public Test test4(Test obj, int j) { int o = __offset(22); return o != 0 ? obj.__assign(__vector(o) + j * 4, bb) : null; }
public MyGame.Example.Test test4(int j) { return test4(new MyGame.Example.Test(), j); }
public MyGame.Example.Test test4(MyGame.Example.Test obj, int j) { int o = __offset(22); return o != 0 ? obj.__assign(__vector(o) + j * 4, bb) : null; }
public int test4Length() { int o = __offset(22); return o != 0 ? __vector_len(o) : 0; }
public String testarrayofstring(int j) { int o = __offset(24); return o != 0 ? __string(__vector(o) + j * 4) : null; }
public int testarrayofstringLength() { int o = __offset(24); return o != 0 ? __vector_len(o) : 0; }
/**
* an example documentation comment: this will end up in the generated code
* multiline too
*/
public Monster testarrayoftables(int j) { return testarrayoftables(new Monster(), j); }
public Monster testarrayoftables(Monster obj, int j) { int o = __offset(26); return o != 0 ? obj.__assign(__indirect(__vector(o) + j * 4), bb) : null; }
public MyGame.Example.Monster testarrayoftables(int j) { return testarrayoftables(new MyGame.Example.Monster(), j); }
public MyGame.Example.Monster testarrayoftables(MyGame.Example.Monster obj, int j) { int o = __offset(26); return o != 0 ? obj.__assign(__indirect(__vector(o) + j * 4), bb) : null; }
public int testarrayoftablesLength() { int o = __offset(26); return o != 0 ? __vector_len(o) : 0; }
public Monster testarrayoftablesByKey(String key) { int o = __offset(26); return o != 0 ? Monster.__lookup_by_key(null, __vector(o), key, bb) : null; }
public Monster testarrayoftablesByKey(Monster obj, String key) { int o = __offset(26); return o != 0 ? Monster.__lookup_by_key(obj, __vector(o), key, bb) : null; }
public Monster enemy() { return enemy(new Monster()); }
public Monster enemy(Monster obj) { int o = __offset(28); return o != 0 ? obj.__assign(__indirect(o + bb_pos), bb) : null; }
public MyGame.Example.Monster testarrayoftablesByKey(String key) { int o = __offset(26); return o != 0 ? MyGame.Example.Monster.__lookup_by_key(null, __vector(o), key, bb) : null; }
public MyGame.Example.Monster testarrayoftablesByKey(MyGame.Example.Monster obj, String key) { int o = __offset(26); return o != 0 ? MyGame.Example.Monster.__lookup_by_key(obj, __vector(o), key, bb) : null; }
public MyGame.Example.Monster enemy() { return enemy(new MyGame.Example.Monster()); }
public MyGame.Example.Monster enemy(MyGame.Example.Monster obj) { int o = __offset(28); return o != 0 ? obj.__assign(__indirect(o + bb_pos), bb) : null; }
public int testnestedflatbuffer(int j) { int o = __offset(30); return o != 0 ? bb.get(__vector(o) + j * 1) & 0xFF : 0; }
public int testnestedflatbufferLength() { int o = __offset(30); return o != 0 ? __vector_len(o) : 0; }
public ByteBuffer testnestedflatbufferAsByteBuffer() { return __vector_as_bytebuffer(30, 1); }
public ByteBuffer testnestedflatbufferInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 30, 1); }
public Monster testnestedflatbufferAsMonster() { return testnestedflatbufferAsMonster(new Monster()); }
public Monster testnestedflatbufferAsMonster(Monster obj) { int o = __offset(30); return o != 0 ? obj.__assign(__indirect(__vector(o)), bb) : null; }
public MyGame.Example.Monster testnestedflatbufferAsMonster() { return testnestedflatbufferAsMonster(new MyGame.Example.Monster()); }
public MyGame.Example.Monster testnestedflatbufferAsMonster(MyGame.Example.Monster obj) { int o = __offset(30); return o != 0 ? obj.__assign(__indirect(__vector(o)), bb) : null; }
public boolean mutateTestnestedflatbuffer(int j, int testnestedflatbuffer) { int o = __offset(30); if (o != 0) { bb.put(__vector(o) + j * 1, (byte)testnestedflatbuffer); return true; } else { return false; } }
public Stat testempty() { return testempty(new Stat()); }
public Stat testempty(Stat obj) { int o = __offset(32); return o != 0 ? obj.__assign(__indirect(o + bb_pos), bb) : null; }
public MyGame.Example.Stat testempty() { return testempty(new MyGame.Example.Stat()); }
public MyGame.Example.Stat testempty(MyGame.Example.Stat obj) { int o = __offset(32); return o != 0 ? obj.__assign(__indirect(o + bb_pos), bb) : null; }
public boolean testbool() { int o = __offset(34); return o != 0 ? 0!=bb.get(o + bb_pos) : false; }
public boolean mutateTestbool(boolean testbool) { int o = __offset(34); if (o != 0) { bb.put(o + bb_pos, (byte)(testbool ? 1 : 0)); return true; } else { return false; } }
public int testhashs32Fnv1() { int o = __offset(36); return o != 0 ? bb.getInt(o + bb_pos) : 0; }
Expand Down Expand Up @@ -93,16 +93,16 @@ public final class Monster extends Table {
public boolean mutateTestf3(float testf3) { int o = __offset(58); if (o != 0) { bb.putFloat(o + bb_pos, testf3); return true; } else { return false; } }
public String testarrayofstring2(int j) { int o = __offset(60); return o != 0 ? __string(__vector(o) + j * 4) : null; }
public int testarrayofstring2Length() { int o = __offset(60); return o != 0 ? __vector_len(o) : 0; }
public Ability testarrayofsortedstruct(int j) { return testarrayofsortedstruct(new Ability(), j); }
public Ability testarrayofsortedstruct(Ability obj, int j) { int o = __offset(62); return o != 0 ? obj.__assign(__vector(o) + j * 8, bb) : null; }
public MyGame.Example.Ability testarrayofsortedstruct(int j) { return testarrayofsortedstruct(new MyGame.Example.Ability(), j); }
public MyGame.Example.Ability testarrayofsortedstruct(MyGame.Example.Ability obj, int j) { int o = __offset(62); return o != 0 ? obj.__assign(__vector(o) + j * 8, bb) : null; }
public int testarrayofsortedstructLength() { int o = __offset(62); return o != 0 ? __vector_len(o) : 0; }
public int flex(int j) { int o = __offset(64); return o != 0 ? bb.get(__vector(o) + j * 1) & 0xFF : 0; }
public int flexLength() { int o = __offset(64); return o != 0 ? __vector_len(o) : 0; }
public ByteBuffer flexAsByteBuffer() { return __vector_as_bytebuffer(64, 1); }
public ByteBuffer flexInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 64, 1); }
public boolean mutateFlex(int j, int flex) { int o = __offset(64); if (o != 0) { bb.put(__vector(o) + j * 1, (byte)flex); return true; } else { return false; } }
public Test test5(int j) { return test5(new Test(), j); }
public Test test5(Test obj, int j) { int o = __offset(66); return o != 0 ? obj.__assign(__vector(o) + j * 4, bb) : null; }
public MyGame.Example.Test test5(int j) { return test5(new MyGame.Example.Test(), j); }
public MyGame.Example.Test test5(MyGame.Example.Test obj, int j) { int o = __offset(66); return o != 0 ? obj.__assign(__vector(o) + j * 4, bb) : null; }
public int test5Length() { int o = __offset(66); return o != 0 ? __vector_len(o) : 0; }
public long vectorOfLongs(int j) { int o = __offset(68); return o != 0 ? bb.getLong(__vector(o) + j * 8) : 0; }
public int vectorOfLongsLength() { int o = __offset(68); return o != 0 ? __vector_len(o) : 0; }
Expand All @@ -116,23 +116,23 @@ public final class Monster extends Table {
public boolean mutateVectorOfDoubles(int j, double vector_of_doubles) { int o = __offset(70); if (o != 0) { bb.putDouble(__vector(o) + j * 8, vector_of_doubles); return true; } else { return false; } }
public MyGame.InParentNamespace parentNamespaceTest() { return parentNamespaceTest(new MyGame.InParentNamespace()); }
public MyGame.InParentNamespace parentNamespaceTest(MyGame.InParentNamespace obj) { int o = __offset(72); return o != 0 ? obj.__assign(__indirect(o + bb_pos), bb) : null; }
public Referrable vectorOfReferrables(int j) { return vectorOfReferrables(new Referrable(), j); }
public Referrable vectorOfReferrables(Referrable obj, int j) { int o = __offset(74); return o != 0 ? obj.__assign(__indirect(__vector(o) + j * 4), bb) : null; }
public MyGame.Example.Referrable vectorOfReferrables(int j) { return vectorOfReferrables(new MyGame.Example.Referrable(), j); }
public MyGame.Example.Referrable vectorOfReferrables(MyGame.Example.Referrable obj, int j) { int o = __offset(74); return o != 0 ? obj.__assign(__indirect(__vector(o) + j * 4), bb) : null; }
public int vectorOfReferrablesLength() { int o = __offset(74); return o != 0 ? __vector_len(o) : 0; }
public Referrable vectorOfReferrablesByKey(long key) { int o = __offset(74); return o != 0 ? Referrable.__lookup_by_key(null, __vector(o), key, bb) : null; }
public Referrable vectorOfReferrablesByKey(Referrable obj, long key) { int o = __offset(74); return o != 0 ? Referrable.__lookup_by_key(obj, __vector(o), key, bb) : null; }
public MyGame.Example.Referrable vectorOfReferrablesByKey(long key) { int o = __offset(74); return o != 0 ? MyGame.Example.Referrable.__lookup_by_key(null, __vector(o), key, bb) : null; }
public MyGame.Example.Referrable vectorOfReferrablesByKey(MyGame.Example.Referrable obj, long key) { int o = __offset(74); return o != 0 ? MyGame.Example.Referrable.__lookup_by_key(obj, __vector(o), key, bb) : null; }
public long singleWeakReference() { int o = __offset(76); return o != 0 ? bb.getLong(o + bb_pos) : 0L; }
public boolean mutateSingleWeakReference(long single_weak_reference) { int o = __offset(76); if (o != 0) { bb.putLong(o + bb_pos, single_weak_reference); return true; } else { return false; } }
public long vectorOfWeakReferences(int j) { int o = __offset(78); return o != 0 ? bb.getLong(__vector(o) + j * 8) : 0; }
public int vectorOfWeakReferencesLength() { int o = __offset(78); return o != 0 ? __vector_len(o) : 0; }
public ByteBuffer vectorOfWeakReferencesAsByteBuffer() { return __vector_as_bytebuffer(78, 8); }
public ByteBuffer vectorOfWeakReferencesInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 78, 8); }
public boolean mutateVectorOfWeakReferences(int j, long vector_of_weak_references) { int o = __offset(78); if (o != 0) { bb.putLong(__vector(o) + j * 8, vector_of_weak_references); return true; } else { return false; } }
public Referrable vectorOfStrongReferrables(int j) { return vectorOfStrongReferrables(new Referrable(), j); }
public Referrable vectorOfStrongReferrables(Referrable obj, int j) { int o = __offset(80); return o != 0 ? obj.__assign(__indirect(__vector(o) + j * 4), bb) : null; }
public MyGame.Example.Referrable vectorOfStrongReferrables(int j) { return vectorOfStrongReferrables(new MyGame.Example.Referrable(), j); }
public MyGame.Example.Referrable vectorOfStrongReferrables(MyGame.Example.Referrable obj, int j) { int o = __offset(80); return o != 0 ? obj.__assign(__indirect(__vector(o) + j * 4), bb) : null; }
public int vectorOfStrongReferrablesLength() { int o = __offset(80); return o != 0 ? __vector_len(o) : 0; }
public Referrable vectorOfStrongReferrablesByKey(long key) { int o = __offset(80); return o != 0 ? Referrable.__lookup_by_key(null, __vector(o), key, bb) : null; }
public Referrable vectorOfStrongReferrablesByKey(Referrable obj, long key) { int o = __offset(80); return o != 0 ? Referrable.__lookup_by_key(obj, __vector(o), key, bb) : null; }
public MyGame.Example.Referrable vectorOfStrongReferrablesByKey(long key) { int o = __offset(80); return o != 0 ? MyGame.Example.Referrable.__lookup_by_key(null, __vector(o), key, bb) : null; }
public MyGame.Example.Referrable vectorOfStrongReferrablesByKey(MyGame.Example.Referrable obj, long key) { int o = __offset(80); return o != 0 ? MyGame.Example.Referrable.__lookup_by_key(obj, __vector(o), key, bb) : null; }
public long coOwningReference() { int o = __offset(82); return o != 0 ? bb.getLong(o + bb_pos) : 0L; }
public boolean mutateCoOwningReference(long co_owning_reference) { int o = __offset(82); if (o != 0) { bb.putLong(o + bb_pos, co_owning_reference); return true; } else { return false; } }
public long vectorOfCoOwningReferences(int j) { int o = __offset(84); return o != 0 ? bb.getLong(__vector(o) + j * 8) : 0; }
Expand Down
6 changes: 3 additions & 3 deletions tests/MyGame/Example/Referrable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public struct Referrable : IFlatbufferObject
public ulong Id { get { int o = __p.__offset(4); return o != 0 ? __p.bb.GetUlong(o + __p.bb_pos) : (ulong)0; } }
public bool MutateId(ulong id) { int o = __p.__offset(4); if (o != 0) { __p.bb.PutUlong(o + __p.bb_pos, id); return true; } else { return false; } }

public static Offset<Referrable> CreateReferrable(FlatBufferBuilder builder,
public static Offset<MyGame.Example.Referrable> CreateReferrable(FlatBufferBuilder builder,
ulong id = 0) {
builder.StartTable(1);
Referrable.AddId(builder, id);
Expand All @@ -29,9 +29,9 @@ public static Offset<Referrable> CreateReferrable(FlatBufferBuilder builder,

public static void StartReferrable(FlatBufferBuilder builder) { builder.StartTable(1); }
public static void AddId(FlatBufferBuilder builder, ulong id) { builder.AddUlong(0, id, 0); }
public static Offset<Referrable> EndReferrable(FlatBufferBuilder builder) {
public static Offset<MyGame.Example.Referrable> EndReferrable(FlatBufferBuilder builder) {
int o = builder.EndTable();
return new Offset<Referrable>(o);
return new Offset<MyGame.Example.Referrable>(o);
}

public static VectorOffset CreateSortedVectorOfReferrable(FlatBufferBuilder builder, Offset<Referrable>[] offsets) {
Expand Down
6 changes: 3 additions & 3 deletions tests/MyGame/Example/Stat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public struct Stat : IFlatbufferObject
public ushort Count { get { int o = __p.__offset(8); return o != 0 ? __p.bb.GetUshort(o + __p.bb_pos) : (ushort)0; } }
public bool MutateCount(ushort count) { int o = __p.__offset(8); if (o != 0) { __p.bb.PutUshort(o + __p.bb_pos, count); return true; } else { return false; } }

public static Offset<Stat> CreateStat(FlatBufferBuilder builder,
public static Offset<MyGame.Example.Stat> CreateStat(FlatBufferBuilder builder,
StringOffset idOffset = default(StringOffset),
long val = 0,
ushort count = 0) {
Expand All @@ -44,9 +44,9 @@ public static Offset<Stat> CreateStat(FlatBufferBuilder builder,
public static void AddId(FlatBufferBuilder builder, StringOffset idOffset) { builder.AddOffset(0, idOffset.Value, 0); }
public static void AddVal(FlatBufferBuilder builder, long val) { builder.AddLong(1, val, 0); }
public static void AddCount(FlatBufferBuilder builder, ushort count) { builder.AddUshort(2, count, 0); }
public static Offset<Stat> EndStat(FlatBufferBuilder builder) {
public static Offset<MyGame.Example.Stat> EndStat(FlatBufferBuilder builder) {
int o = builder.EndTable();
return new Offset<Stat>(o);
return new Offset<MyGame.Example.Stat>(o);
}
};

Expand Down
4 changes: 2 additions & 2 deletions tests/MyGame/Example/Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ public struct Test : IFlatbufferObject
public sbyte B { get { return __p.bb.GetSbyte(__p.bb_pos + 2); } }
public void MutateB(sbyte b) { __p.bb.PutSbyte(__p.bb_pos + 2, b); }

public static Offset<Test> CreateTest(FlatBufferBuilder builder, short A, sbyte B) {
public static Offset<MyGame.Example.Test> CreateTest(FlatBufferBuilder builder, short A, sbyte B) {
builder.Prep(2, 4);
builder.Pad(1);
builder.PutSbyte(B);
builder.PutShort(A);
return new Offset<Test>(builder.Offset);
return new Offset<MyGame.Example.Test>(builder.Offset);
}
};

Expand Down
Loading

0 comments on commit a80db85

Please sign in to comment.