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