• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "test_builder.h"
2 
3 #include "flatbuffers/stl_emulation.h"
4 #include "monster_test_generated.h"
5 
6 using namespace MyGame::Example;
7 
8 struct OwnedAllocator : public flatbuffers::DefaultAllocator {};
9 
10 class TestHeapBuilder : public flatbuffers::FlatBufferBuilder {
11  private:
12   // clang-format off
13   #if !defined(FLATBUFFERS_CPP98_STL)
14   TestHeapBuilder(const TestHeapBuilder &);
15   TestHeapBuilder &operator=(const TestHeapBuilder &);
16   #endif  // !defined(FLATBUFFERS_CPP98_STL)
17   // clang-format on
18 
19  public:
TestHeapBuilder()20   TestHeapBuilder()
21       : flatbuffers::FlatBufferBuilder(2048, new OwnedAllocator(), true) {}
22 
23   // clang-format off
24   #if !defined(FLATBUFFERS_CPP98_STL)
25   // clang-format on
TestHeapBuilder(TestHeapBuilder && other)26   TestHeapBuilder(TestHeapBuilder &&other)
27       : FlatBufferBuilder(std::move(other)) {}
28 
operator =(TestHeapBuilder && other)29   TestHeapBuilder &operator=(TestHeapBuilder &&other) {
30     FlatBufferBuilder::operator=(std::move(other));
31     return *this;
32   }
33   // clang-format off
34   #endif  // !defined(FLATBUFFERS_CPP98_STL)
35   // clang-format on
36 };
37 
38 // This class simulates flatbuffers::grpc::detail::SliceAllocatorMember
39 struct AllocatorMember {
40   flatbuffers::DefaultAllocator member_allocator_;
41 };
42 
43 struct GrpcLikeMessageBuilder : private AllocatorMember,
44                                 public flatbuffers::FlatBufferBuilder {
45  private:
46   GrpcLikeMessageBuilder(const GrpcLikeMessageBuilder &);
47   GrpcLikeMessageBuilder &operator=(const GrpcLikeMessageBuilder &);
48 
49  public:
GrpcLikeMessageBuilderGrpcLikeMessageBuilder50   GrpcLikeMessageBuilder()
51       : flatbuffers::FlatBufferBuilder(1024, &member_allocator_, false) {}
52 
GrpcLikeMessageBuilderGrpcLikeMessageBuilder53   GrpcLikeMessageBuilder(GrpcLikeMessageBuilder &&other)
54       : FlatBufferBuilder(1024, &member_allocator_, false) {
55     // Default construct and swap idiom.
56     Swap(other);
57   }
58 
59   // clang-format off
60   #if !defined(FLATBUFFERS_CPP98_STL)
61   // clang-format on
operator =GrpcLikeMessageBuilder62   GrpcLikeMessageBuilder &operator=(GrpcLikeMessageBuilder &&other) {
63     // Construct temporary and swap idiom
64     GrpcLikeMessageBuilder temp(std::move(other));
65     Swap(temp);
66     return *this;
67   }
68   // clang-format off
69   #endif  // !defined(FLATBUFFERS_CPP98_STL)
70   // clang-format on
71 
SwapGrpcLikeMessageBuilder72   void Swap(GrpcLikeMessageBuilder &other) {
73     // No need to swap member_allocator_ because it's stateless.
74     FlatBufferBuilder::Swap(other);
75     // After swapping the FlatBufferBuilder, we swap back the allocator, which
76     // restores the original allocator back in place. This is necessary because
77     // MessageBuilder's allocator is its own member (SliceAllocatorMember). The
78     // allocator passed to FlatBufferBuilder::vector_downward must point to this
79     // member.
80     buf_.swap_allocator(other.buf_);
81   }
82 };
83 
populate1(flatbuffers::FlatBufferBuilder & builder)84 flatbuffers::Offset<Monster> populate1(
85     flatbuffers::FlatBufferBuilder &builder) {
86   auto name_offset = builder.CreateString(m1_name());
87   return CreateMonster(builder, nullptr, 0, 0, name_offset, 0, m1_color());
88 }
89 
populate2(flatbuffers::FlatBufferBuilder & builder)90 flatbuffers::Offset<Monster> populate2(
91     flatbuffers::FlatBufferBuilder &builder) {
92   auto name_offset = builder.CreateString(m2_name());
93   return CreateMonster(builder, nullptr, 0, 0, name_offset, 0, m2_color());
94 }
95 
release_raw_base(flatbuffers::FlatBufferBuilder & fbb,size_t & size,size_t & offset)96 uint8_t *release_raw_base(flatbuffers::FlatBufferBuilder &fbb, size_t &size,
97                           size_t &offset) {
98   return fbb.ReleaseRaw(size, offset);
99 }
100 
free_raw(flatbuffers::grpc::MessageBuilder &,uint8_t *)101 void free_raw(flatbuffers::grpc::MessageBuilder &, uint8_t *) {
102   // release_raw_base calls FlatBufferBuilder::ReleaseRaw on the argument
103   // MessageBuilder. It's semantically wrong as MessageBuilder has its own
104   // ReleaseRaw member function that takes three arguments. In such cases
105   // though, ~MessageBuilder() invokes ~SliceAllocator() that takes care of
106   // deleting memory as it calls grpc_slice_unref. Obviously, this behavior is
107   // very surprising as the pointer returned by FlatBufferBuilder::ReleaseRaw is
108   // not valid as soon as MessageBuilder goes out of scope. This problem does
109   // not occur with FlatBufferBuilder.
110 }
111 
free_raw(flatbuffers::FlatBufferBuilder &,uint8_t * buf)112 void free_raw(flatbuffers::FlatBufferBuilder &, uint8_t *buf) {
113   flatbuffers::DefaultAllocator().deallocate(buf, 0);
114 }
115 
verify(const flatbuffers::DetachedBuffer & buf,const std::string & expected_name,Color color)116 bool verify(const flatbuffers::DetachedBuffer &buf,
117             const std::string &expected_name, Color color) {
118   const Monster *monster = flatbuffers::GetRoot<Monster>(buf.data());
119   return (monster->name()->str() == expected_name) &&
120          (monster->color() == color);
121 }
122 
verify(const uint8_t * buf,size_t offset,const std::string & expected_name,Color color)123 bool verify(const uint8_t *buf, size_t offset, const std::string &expected_name,
124             Color color) {
125   const Monster *monster = flatbuffers::GetRoot<Monster>(buf + offset);
126   return (monster->name()->str() == expected_name) &&
127          (monster->color() == color);
128 }
129 
release_n_verify(flatbuffers::FlatBufferBuilder & fbb,const std::string & expected_name,Color color)130 bool release_n_verify(flatbuffers::FlatBufferBuilder &fbb,
131                       const std::string &expected_name, Color color) {
132   flatbuffers::DetachedBuffer buf = fbb.Release();
133   return verify(buf, expected_name, color);
134 }
135 
FlatBufferBuilderTest()136 void FlatBufferBuilderTest() {
137   using flatbuffers::FlatBufferBuilder;
138 
139   BuilderTests<FlatBufferBuilder>::all_tests();
140   BuilderTests<TestHeapBuilder>::all_tests();
141   BuilderTests<GrpcLikeMessageBuilder>::all_tests();
142 
143   BuilderReuseTestSelector tests[4] = {
144     REUSABLE_AFTER_RELEASE, REUSABLE_AFTER_RELEASE_RAW,
145     REUSABLE_AFTER_RELEASE_AND_MOVE_ASSIGN,
146     REUSABLE_AFTER_RELEASE_RAW_AND_MOVE_ASSIGN
147   };
148 
149   BuilderReuseTests<FlatBufferBuilder, FlatBufferBuilder>::run_tests(
150       TestSelector(tests, tests + 4));
151   BuilderReuseTests<TestHeapBuilder, TestHeapBuilder>::run_tests(
152       TestSelector(tests, tests + 4));
153   BuilderReuseTests<GrpcLikeMessageBuilder, GrpcLikeMessageBuilder>::run_tests(
154       TestSelector(tests, tests + 4));
155 }
156 
157 // Link-time check using pointer type.
CheckTestGeneratedIsValid(const MyGame::Example::Color &)158 void CheckTestGeneratedIsValid(const MyGame::Example::Color &) {}