blob: a17205a68eda0a1e5364f406d9cb1664afd96d74 [file] [log] [blame]
Austin Schuh272c6132020-11-14 16:37:52 -08001/*
2 * Copyright 2014 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// This is a sandbox for modeling C++17 code generator.
James Kuszmaul8e62b022022-03-22 09:33:25 -070018// C++17 code generator: "flatc --cpp-std c++17".
Austin Schuh272c6132020-11-14 16:37:52 -080019// Warning:
20// This is an experimental feature and could change at any time.
21
22#include "flatbuffers/flatbuffers.h"
23#include "flatbuffers/flexbuffers.h"
24#include "flatbuffers/idl.h"
25#include "flatbuffers/minireflect.h"
26#include "flatbuffers/registry.h"
27#include "flatbuffers/util.h"
James Kuszmaul8e62b022022-03-22 09:33:25 -070028#include "stringify_util.h"
Austin Schuh272c6132020-11-14 16:37:52 -080029#include "test_assert.h"
30
31// Embed generated code into an isolated namespace.
32namespace cpp17 {
33#include "generated_cpp17/monster_test_generated.h"
34#include "generated_cpp17/optional_scalars_generated.h"
James Kuszmaul8e62b022022-03-22 09:33:25 -070035#include "generated_cpp17/union_vector_generated.h"
Austin Schuh272c6132020-11-14 16:37:52 -080036} // namespace cpp17
37
38namespace cpp11 {
39#include "../monster_test_generated.h"
40#include "../optional_scalars_generated.h"
41} // namespace cpp11
42
James Kuszmaul8e62b022022-03-22 09:33:25 -070043using ::cpp17::MyGame::Example::Monster;
44using ::cpp17::MyGame::Example::Vec3;
45
46/*******************************************************************************
47** Build some FB objects.
48*******************************************************************************/
49const Monster *BuildMonster(flatbuffers::FlatBufferBuilder &fbb) {
50 using ::cpp17::MyGame::Example::Color;
51 using ::cpp17::MyGame::Example::MonsterBuilder;
52 using ::cpp17::MyGame::Example::Test;
53 auto name = fbb.CreateString("my_monster");
54 auto inventory = fbb.CreateVector(std::vector<uint8_t>{ 4, 5, 6, 7 });
55 MonsterBuilder builder(fbb);
56 auto vec3 = Vec3{ /*x=*/1.1f,
57 /*y=*/2.2f,
58 /*z=*/3.3f,
59 /*test1=*/6.6,
60 /*test2=*/Color::Green,
61 /*test3=*/
62 Test(
63 /*a=*/11,
64 /*b=*/90) };
65 builder.add_pos(&vec3);
66 builder.add_name(name);
67 builder.add_mana(1);
68 builder.add_hp(2);
69 builder.add_testbool(true);
70 builder.add_testhashs32_fnv1(4);
71 builder.add_testhashu32_fnv1(5);
72 builder.add_testhashs64_fnv1(6);
73 builder.add_testhashu64_fnv1(7);
74 builder.add_testhashs32_fnv1a(8);
75 builder.add_testhashu32_fnv1a(9);
76 builder.add_testhashs64_fnv1a(10);
77 builder.add_testhashu64_fnv1a(11);
78 builder.add_testf(12.1f);
79 builder.add_testf2(13.1f);
80 builder.add_testf3(14.1f);
81 builder.add_single_weak_reference(15);
82 builder.add_co_owning_reference(16);
83 builder.add_non_owning_reference(17);
84 builder.add_inventory(inventory);
85 fbb.Finish(builder.Finish());
86 const Monster *monster =
87 flatbuffers::GetRoot<Monster>(fbb.GetBufferPointer());
88 return monster;
89}
90
91/*******************************************************************************
92** Test Case: Static Field Reflection Traits for Table & Structs.
93*******************************************************************************/
94// This test tests & demonstrates the power of the static reflection. Using it,
95// we can given any Flatbuffer type to a generic function and it will be able to
96// produce is full recursive string representation of it.
97//
98// This test covers all types: primitive types, structs, tables, Vectors, etc.
99//
100void StringifyAnyFlatbuffersTypeTest() {
101 flatbuffers::FlatBufferBuilder fbb;
102 // We are using a Monster here, but we could have used any type, because the
103 // code that follows is totally generic!
104 const auto *monster = BuildMonster(fbb);
105
106 std::string expected = R"(MyGame.Example.Monster{
107 pos = MyGame.Example.Vec3{
108 x = 1.1
109 y = 2.2
110 z = 3.3
111 test1 = 6.6
112 test2 = 2
113 test3 = MyGame.Example.Test{
114 a = 11
115 b = 90
116 }
117 }
118 mana = 1
119 hp = 2
120 name = "my_monster"
121 inventory = [
122 4,
123 5,
124 6,
125 7
126 ]
127 color = 8
128 test_type = 0
129 testbool = 1
130 testhashs32_fnv1 = 4
131 testhashu32_fnv1 = 5
132 testhashs64_fnv1 = 6
133 testhashu64_fnv1 = 7
134 testhashs32_fnv1a = 8
135 testhashu32_fnv1a = 9
136 testhashs64_fnv1a = 10
137 testhashu64_fnv1a = 11
138 testf = 12.1
139 testf2 = 13.1
140 testf3 = 14.1
141 single_weak_reference = 15
142 co_owning_reference = 16
143 non_owning_reference = 17
144 any_unique_type = 0
145 any_ambiguous_type = 0
146 signed_enum = -1
147 })";
148
149 // Call a generic function that has no specific knowledge of the flatbuffer we
150 // are passing in; it should use only static reflection to produce a string
151 // representations of the field names and values recursively. We give it an
152 // initial indentation so that the result can be compared with our raw string
153 // above, which we wanted to indent so that it will look nicer in this code.
154 //
155 // A note about JSON: as can be seen from the string above, this produces a
156 // JSON-like notation, but we are not using any of Flatbuffers' JSON infra to
157 // produce this! It is produced entirely using compile-time reflection, and
158 // thus does not require any runtime access to the *.fbs definition files!
159 std::optional<std::string> result =
160 cpp17::StringifyFlatbufferValue(*monster, /*indent=*/" ");
161
162 TEST_ASSERT(result.has_value());
163 TEST_EQ_STR(expected.c_str(), result->c_str());
164}
165
166/*******************************************************************************
167** Test Traits::FieldType
168*******************************************************************************/
169using pos_type = Monster::Traits::FieldType<0>;
170static_assert(std::is_same_v<pos_type, const Vec3*>);
171
172using mana_type = Monster::Traits::FieldType<1>;
173static_assert(std::is_same_v<mana_type, int16_t>);
174
175using name_type = Monster::Traits::FieldType<3>;
176static_assert(std::is_same_v<name_type, const flatbuffers::String*>);
177
178/*******************************************************************************
179** Generic Create Function Test.
180*******************************************************************************/
Austin Schuh272c6132020-11-14 16:37:52 -0800181void CreateTableByTypeTest() {
182 flatbuffers::FlatBufferBuilder builder;
183
184 // We will create an object of this type using only the type.
185 using type_to_create_t = cpp17::MyGame::Example::Stat;
186
187 [&builder] {
188 auto id_str = builder.CreateString("my_id");
189 auto table = type_to_create_t::Traits::Create(builder, id_str, 42, 7);
190 // Be sure that the correct return type was inferred.
191 static_assert(
192 std::is_same_v<decltype(table), flatbuffers::Offset<type_to_create_t>>);
193 builder.Finish(table);
194 }();
195
196 // Access it.
197 auto stat =
198 flatbuffers::GetRoot<type_to_create_t>(builder.GetBufferPointer());
199 TEST_EQ_STR(stat->id()->c_str(), "my_id");
200 TEST_EQ(stat->val(), 42);
201 TEST_EQ(stat->count(), 7);
202}
203
204void OptionalScalarsTest() {
James Kuszmaul8e62b022022-03-22 09:33:25 -0700205 static_assert(
206 std::is_same<flatbuffers::Optional<float>, std::optional<float>>::value);
Austin Schuh272c6132020-11-14 16:37:52 -0800207 static_assert(std::is_same<flatbuffers::nullopt_t, std::nullopt_t>::value);
208
209 // test C++ nullable
210 flatbuffers::FlatBufferBuilder fbb;
211 FinishScalarStuffBuffer(fbb, cpp17::optional_scalars::CreateScalarStuff(
212 fbb, 1, static_cast<int8_t>(2)));
213 auto opts =
214 cpp17::optional_scalars::GetMutableScalarStuff(fbb.GetBufferPointer());
215 TEST_ASSERT(!opts->maybe_bool());
216 TEST_ASSERT(!opts->maybe_f32().has_value());
217 TEST_ASSERT(opts->maybe_i8().has_value());
218 TEST_EQ(opts->maybe_i8().value(), 2);
219 TEST_ASSERT(opts->mutate_maybe_i8(3));
220 TEST_ASSERT(opts->maybe_i8().has_value());
221 TEST_EQ(opts->maybe_i8().value(), 3);
222 TEST_ASSERT(!opts->mutate_maybe_i16(-10));
223
224 cpp17::optional_scalars::ScalarStuffT obj;
225 opts->UnPackTo(&obj);
226 TEST_ASSERT(!obj.maybe_bool);
227 TEST_ASSERT(!obj.maybe_f32.has_value());
228 TEST_ASSERT(obj.maybe_i8.has_value() && obj.maybe_i8.value() == 3);
229 TEST_ASSERT(obj.maybe_i8 && *obj.maybe_i8 == 3);
230 obj.maybe_i32 = -1;
231
232 fbb.Clear();
233 FinishScalarStuffBuffer(
234 fbb, cpp17::optional_scalars::ScalarStuff::Pack(fbb, &obj));
235 opts = cpp17::optional_scalars::GetMutableScalarStuff(fbb.GetBufferPointer());
236 TEST_ASSERT(opts->maybe_i8().has_value());
237 TEST_EQ(opts->maybe_i8().value(), 3);
238 TEST_ASSERT(opts->maybe_i32().has_value());
239 TEST_EQ(opts->maybe_i32().value(), -1);
240
241 TEST_EQ(std::optional<int32_t>(opts->maybe_i32()).value(), -1);
242 TEST_EQ(std::optional<int64_t>(opts->maybe_i32()).value(), -1);
243 TEST_ASSERT(opts->maybe_i32() == std::optional<int64_t>(-1));
244}
245
246int FlatBufferCpp17Tests() {
247 CreateTableByTypeTest();
248 OptionalScalarsTest();
James Kuszmaul8e62b022022-03-22 09:33:25 -0700249 StringifyAnyFlatbuffersTypeTest();
Austin Schuh272c6132020-11-14 16:37:52 -0800250 return 0;
251}
252
253int main(int /*argc*/, const char * /*argv*/[]) {
254 InitTestEngine();
255
256 FlatBufferCpp17Tests();
257
258 if (!testing_fails) {
259 TEST_OUTPUT_LINE("C++17: ALL TESTS PASSED");
260 } else {
261 TEST_OUTPUT_LINE("C++17: %d FAILED TESTS", testing_fails);
262 }
263 return CloseTestEngine();
264}