mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-03 07:42:27 +00:00
Run clang-format -i **/*.cpp (#8865)
This commit is contained in:
@@ -15,25 +15,27 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <jni.h>
|
#include <jni.h>
|
||||||
#include <string>
|
|
||||||
#include <search.h>
|
#include <search.h>
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include "generated/animal_generated.h"
|
#include "generated/animal_generated.h"
|
||||||
|
|
||||||
using namespace com::fbs::app;
|
using namespace com::fbs::app;
|
||||||
using namespace flatbuffers;
|
using namespace flatbuffers;
|
||||||
|
|
||||||
extern "C" JNIEXPORT jbyteArray JNICALL Java_com_flatbuffers_app_MainActivity_createAnimalFromJNI(
|
extern "C" JNIEXPORT jbyteArray JNICALL
|
||||||
JNIEnv* env,
|
Java_com_flatbuffers_app_MainActivity_createAnimalFromJNI(JNIEnv* env,
|
||||||
jobject /* this */) {
|
jobject /* this */) {
|
||||||
// create a new animal flatbuffers
|
// create a new animal flatbuffers
|
||||||
auto fb = FlatBufferBuilder(1024);
|
auto fb = FlatBufferBuilder(1024);
|
||||||
auto tiger = CreateAnimalDirect(fb, "Tiger", "Roar", 300);
|
auto tiger = CreateAnimalDirect(fb, "Tiger", "Roar", 300);
|
||||||
fb.Finish(tiger);
|
fb.Finish(tiger);
|
||||||
|
|
||||||
// copies it to a Java byte array.
|
// copies it to a Java byte array.
|
||||||
auto buf = reinterpret_cast<jbyte*>(fb.GetBufferPointer());
|
auto buf = reinterpret_cast<jbyte*>(fb.GetBufferPointer());
|
||||||
int size = fb.GetSize();
|
int size = fb.GetSize();
|
||||||
auto ret = env->NewByteArray(size);
|
auto ret = env->NewByteArray(size);
|
||||||
env->SetByteArrayRegion (ret, 0, fb.GetSize(), buf);
|
env->SetByteArrayRegion(ret, 0, fb.GetSize(), buf);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,8 +5,8 @@
|
|||||||
#include "benchmarks/cpp/flatbuffers/fb_bench.h"
|
#include "benchmarks/cpp/flatbuffers/fb_bench.h"
|
||||||
#include "benchmarks/cpp/raw/raw_bench.h"
|
#include "benchmarks/cpp/raw/raw_bench.h"
|
||||||
|
|
||||||
static inline void Encode(benchmark::State &state,
|
static inline void Encode(benchmark::State& state,
|
||||||
std::unique_ptr<Bench> &bench, uint8_t *buffer) {
|
std::unique_ptr<Bench>& bench, uint8_t* buffer) {
|
||||||
int64_t length;
|
int64_t length;
|
||||||
for (auto _ : state) {
|
for (auto _ : state) {
|
||||||
bench->Encode(buffer, length);
|
bench->Encode(buffer, length);
|
||||||
@@ -14,31 +14,33 @@ static inline void Encode(benchmark::State &state,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void Decode(benchmark::State &state,
|
static inline void Decode(benchmark::State& state,
|
||||||
std::unique_ptr<Bench> &bench, uint8_t *buffer) {
|
std::unique_ptr<Bench>& bench, uint8_t* buffer) {
|
||||||
int64_t length;
|
int64_t length;
|
||||||
uint8_t *encoded = bench->Encode(buffer, length);
|
uint8_t* encoded = bench->Encode(buffer, length);
|
||||||
|
|
||||||
for (auto _ : state) {
|
for (auto _ : state) {
|
||||||
void *decoded = bench->Decode(encoded, length);
|
void* decoded = bench->Decode(encoded, length);
|
||||||
benchmark::DoNotOptimize(decoded);
|
benchmark::DoNotOptimize(decoded);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void Use(benchmark::State &state, std::unique_ptr<Bench> &bench,
|
static inline void Use(benchmark::State& state, std::unique_ptr<Bench>& bench,
|
||||||
uint8_t *buffer, int64_t check_sum) {
|
uint8_t* buffer, int64_t check_sum) {
|
||||||
int64_t length;
|
int64_t length;
|
||||||
uint8_t *encoded = bench->Encode(buffer, length);
|
uint8_t* encoded = bench->Encode(buffer, length);
|
||||||
void *decoded = bench->Decode(encoded, length);
|
void* decoded = bench->Decode(encoded, length);
|
||||||
|
|
||||||
int64_t sum = 0;
|
int64_t sum = 0;
|
||||||
|
|
||||||
for (auto _ : state) { sum = bench->Use(decoded); }
|
for (auto _ : state) {
|
||||||
|
sum = bench->Use(decoded);
|
||||||
|
}
|
||||||
|
|
||||||
EXPECT_EQ(sum, check_sum);
|
EXPECT_EQ(sum, check_sum);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void BM_Flatbuffers_Encode(benchmark::State &state) {
|
static void BM_Flatbuffers_Encode(benchmark::State& state) {
|
||||||
const int64_t kBufferLength = 1024;
|
const int64_t kBufferLength = 1024;
|
||||||
uint8_t buffer[kBufferLength];
|
uint8_t buffer[kBufferLength];
|
||||||
|
|
||||||
@@ -48,7 +50,7 @@ static void BM_Flatbuffers_Encode(benchmark::State &state) {
|
|||||||
}
|
}
|
||||||
BENCHMARK(BM_Flatbuffers_Encode);
|
BENCHMARK(BM_Flatbuffers_Encode);
|
||||||
|
|
||||||
static void BM_Flatbuffers_Decode(benchmark::State &state) {
|
static void BM_Flatbuffers_Decode(benchmark::State& state) {
|
||||||
const int64_t kBufferLength = 1024;
|
const int64_t kBufferLength = 1024;
|
||||||
uint8_t buffer[kBufferLength];
|
uint8_t buffer[kBufferLength];
|
||||||
|
|
||||||
@@ -58,7 +60,7 @@ static void BM_Flatbuffers_Decode(benchmark::State &state) {
|
|||||||
}
|
}
|
||||||
BENCHMARK(BM_Flatbuffers_Decode);
|
BENCHMARK(BM_Flatbuffers_Decode);
|
||||||
|
|
||||||
static void BM_Flatbuffers_Use(benchmark::State &state) {
|
static void BM_Flatbuffers_Use(benchmark::State& state) {
|
||||||
const int64_t kBufferLength = 1024;
|
const int64_t kBufferLength = 1024;
|
||||||
uint8_t buffer[kBufferLength];
|
uint8_t buffer[kBufferLength];
|
||||||
|
|
||||||
@@ -68,7 +70,7 @@ static void BM_Flatbuffers_Use(benchmark::State &state) {
|
|||||||
}
|
}
|
||||||
BENCHMARK(BM_Flatbuffers_Use);
|
BENCHMARK(BM_Flatbuffers_Use);
|
||||||
|
|
||||||
static void BM_Raw_Encode(benchmark::State &state) {
|
static void BM_Raw_Encode(benchmark::State& state) {
|
||||||
const int64_t kBufferLength = 1024;
|
const int64_t kBufferLength = 1024;
|
||||||
uint8_t buffer[kBufferLength];
|
uint8_t buffer[kBufferLength];
|
||||||
|
|
||||||
@@ -77,7 +79,7 @@ static void BM_Raw_Encode(benchmark::State &state) {
|
|||||||
}
|
}
|
||||||
BENCHMARK(BM_Raw_Encode);
|
BENCHMARK(BM_Raw_Encode);
|
||||||
|
|
||||||
static void BM_Raw_Decode(benchmark::State &state) {
|
static void BM_Raw_Decode(benchmark::State& state) {
|
||||||
const int64_t kBufferLength = 1024;
|
const int64_t kBufferLength = 1024;
|
||||||
uint8_t buffer[kBufferLength];
|
uint8_t buffer[kBufferLength];
|
||||||
|
|
||||||
@@ -86,7 +88,7 @@ static void BM_Raw_Decode(benchmark::State &state) {
|
|||||||
}
|
}
|
||||||
BENCHMARK(BM_Raw_Decode);
|
BENCHMARK(BM_Raw_Decode);
|
||||||
|
|
||||||
static void BM_Raw_Use(benchmark::State &state) {
|
static void BM_Raw_Use(benchmark::State& state) {
|
||||||
const int64_t kBufferLength = 1024;
|
const int64_t kBufferLength = 1024;
|
||||||
uint8_t buffer[kBufferLength];
|
uint8_t buffer[kBufferLength];
|
||||||
|
|
||||||
|
|||||||
@@ -13,10 +13,10 @@ using namespace benchmarks_flatbuffers;
|
|||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
struct FlatBufferBench : Bench {
|
struct FlatBufferBench : Bench {
|
||||||
explicit FlatBufferBench(int64_t initial_size, Allocator *allocator)
|
explicit FlatBufferBench(int64_t initial_size, Allocator* allocator)
|
||||||
: fbb(initial_size, allocator, false) {}
|
: fbb(initial_size, allocator, false) {}
|
||||||
|
|
||||||
uint8_t *Encode(void *, int64_t &len) override {
|
uint8_t* Encode(void*, int64_t& len) override {
|
||||||
fbb.Clear();
|
fbb.Clear();
|
||||||
|
|
||||||
const int kVectorLength = 3;
|
const int kVectorLength = 3;
|
||||||
@@ -40,7 +40,7 @@ struct FlatBufferBench : Bench {
|
|||||||
return fbb.GetBufferPointer();
|
return fbb.GetBufferPointer();
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t Use(void *decoded) override {
|
int64_t Use(void* decoded) override {
|
||||||
sum = 0;
|
sum = 0;
|
||||||
auto foobarcontainer = GetFooBarContainer(decoded);
|
auto foobarcontainer = GetFooBarContainer(decoded);
|
||||||
sum = 0;
|
sum = 0;
|
||||||
@@ -56,7 +56,7 @@ struct FlatBufferBench : Bench {
|
|||||||
Add(static_cast<int64_t>(bar->ratio()));
|
Add(static_cast<int64_t>(bar->ratio()));
|
||||||
Add(bar->size());
|
Add(bar->size());
|
||||||
Add(bar->time());
|
Add(bar->time());
|
||||||
auto &foo = bar->parent();
|
auto& foo = bar->parent();
|
||||||
Add(foo.count());
|
Add(foo.count());
|
||||||
Add(foo.id());
|
Add(foo.id());
|
||||||
Add(foo.length());
|
Add(foo.length());
|
||||||
@@ -65,8 +65,8 @@ struct FlatBufferBench : Bench {
|
|||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *Decode(void *buffer, int64_t) override { return buffer; }
|
void* Decode(void* buffer, int64_t) override { return buffer; }
|
||||||
void Dealloc(void *) override {};
|
void Dealloc(void*) override {};
|
||||||
|
|
||||||
FlatBufferBuilder fbb;
|
FlatBufferBuilder fbb;
|
||||||
};
|
};
|
||||||
@@ -74,7 +74,7 @@ struct FlatBufferBench : Bench {
|
|||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
std::unique_ptr<Bench> NewFlatBuffersBench(int64_t initial_size,
|
std::unique_ptr<Bench> NewFlatBuffersBench(int64_t initial_size,
|
||||||
Allocator *allocator) {
|
Allocator* allocator) {
|
||||||
return std::unique_ptr<FlatBufferBench>(
|
return std::unique_ptr<FlatBufferBench>(
|
||||||
new FlatBufferBench(initial_size, allocator));
|
new FlatBufferBench(initial_size, allocator));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,8 +45,8 @@ struct FooBarContainer {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct RawBench : Bench {
|
struct RawBench : Bench {
|
||||||
uint8_t *Encode(void *buf, int64_t &len) override {
|
uint8_t* Encode(void* buf, int64_t& len) override {
|
||||||
FooBarContainer *fbc = new (buf) FooBarContainer;
|
FooBarContainer* fbc = new (buf) FooBarContainer;
|
||||||
strcpy(fbc->location, "http://google.com/flatbuffers/"); // Unsafe eek!
|
strcpy(fbc->location, "http://google.com/flatbuffers/"); // Unsafe eek!
|
||||||
fbc->location_len = (int)strlen(fbc->location);
|
fbc->location_len = (int)strlen(fbc->location);
|
||||||
fbc->fruit = Bananas;
|
fbc->fruit = Bananas;
|
||||||
@@ -54,16 +54,16 @@ struct RawBench : Bench {
|
|||||||
for (int i = 0; i < kVectorLength; i++) {
|
for (int i = 0; i < kVectorLength; i++) {
|
||||||
// We add + i to not make these identical copies for a more realistic
|
// We add + i to not make these identical copies for a more realistic
|
||||||
// compression test.
|
// compression test.
|
||||||
auto &foobar = fbc->list[i];
|
auto& foobar = fbc->list[i];
|
||||||
foobar.rating = 3.1415432432445543543 + i;
|
foobar.rating = 3.1415432432445543543 + i;
|
||||||
foobar.postfix = '!' + i;
|
foobar.postfix = '!' + i;
|
||||||
strcpy(foobar.name, "Hello, World!");
|
strcpy(foobar.name, "Hello, World!");
|
||||||
foobar.name_len = (int)strlen(foobar.name);
|
foobar.name_len = (int)strlen(foobar.name);
|
||||||
auto &bar = foobar.sibling;
|
auto& bar = foobar.sibling;
|
||||||
bar.ratio = 3.14159f + i;
|
bar.ratio = 3.14159f + i;
|
||||||
bar.size = 10000 + i;
|
bar.size = 10000 + i;
|
||||||
bar.time = 123456 + i;
|
bar.time = 123456 + i;
|
||||||
auto &foo = bar.parent;
|
auto& foo = bar.parent;
|
||||||
foo.id = 0xABADCAFEABADCAFE + i;
|
foo.id = 0xABADCAFEABADCAFE + i;
|
||||||
foo.count = 10000 + i;
|
foo.count = 10000 + i;
|
||||||
foo.length = 1000000 + i;
|
foo.length = 1000000 + i;
|
||||||
@@ -71,11 +71,11 @@ struct RawBench : Bench {
|
|||||||
}
|
}
|
||||||
|
|
||||||
len = sizeof(FooBarContainer);
|
len = sizeof(FooBarContainer);
|
||||||
return reinterpret_cast<uint8_t *>(fbc);
|
return reinterpret_cast<uint8_t*>(fbc);
|
||||||
};
|
};
|
||||||
|
|
||||||
int64_t Use(void *decoded) override {
|
int64_t Use(void* decoded) override {
|
||||||
auto foobarcontainer = reinterpret_cast<FooBarContainer *>(decoded);
|
auto foobarcontainer = reinterpret_cast<FooBarContainer*>(decoded);
|
||||||
sum = 0;
|
sum = 0;
|
||||||
Add(foobarcontainer->initialized);
|
Add(foobarcontainer->initialized);
|
||||||
Add(foobarcontainer->location_len);
|
Add(foobarcontainer->location_len);
|
||||||
@@ -89,7 +89,7 @@ struct RawBench : Bench {
|
|||||||
Add(static_cast<int64_t>(bar->ratio));
|
Add(static_cast<int64_t>(bar->ratio));
|
||||||
Add(bar->size);
|
Add(bar->size);
|
||||||
Add(bar->time);
|
Add(bar->time);
|
||||||
auto &foo = bar->parent;
|
auto& foo = bar->parent;
|
||||||
Add(foo.count);
|
Add(foo.count);
|
||||||
Add(foo.id);
|
Add(foo.id);
|
||||||
Add(foo.length);
|
Add(foo.length);
|
||||||
@@ -98,8 +98,8 @@ struct RawBench : Bench {
|
|||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *Decode(void *buf, int64_t) override { return buf; }
|
void* Decode(void* buf, int64_t) override { return buf; }
|
||||||
void Dealloc(void *) override{};
|
void Dealloc(void*) override {};
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|||||||
@@ -1,18 +1,18 @@
|
|||||||
#include "greeter.grpc.fb.h"
|
|
||||||
#include "greeter_generated.h"
|
|
||||||
|
|
||||||
#include <grpcpp/grpcpp.h>
|
#include <grpcpp/grpcpp.h>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include "greeter.grpc.fb.h"
|
||||||
|
#include "greeter_generated.h"
|
||||||
|
|
||||||
class GreeterClient {
|
class GreeterClient {
|
||||||
public:
|
public:
|
||||||
GreeterClient(std::shared_ptr<grpc::Channel> channel)
|
GreeterClient(std::shared_ptr<grpc::Channel> channel)
|
||||||
: stub_(Greeter::NewStub(channel)) {}
|
: stub_(Greeter::NewStub(channel)) {}
|
||||||
|
|
||||||
std::string SayHello(const std::string &name) {
|
std::string SayHello(const std::string& name) {
|
||||||
flatbuffers::grpc::MessageBuilder mb;
|
flatbuffers::grpc::MessageBuilder mb;
|
||||||
auto name_offset = mb.CreateString(name);
|
auto name_offset = mb.CreateString(name);
|
||||||
auto request_offset = CreateHelloRequest(mb, name_offset);
|
auto request_offset = CreateHelloRequest(mb, name_offset);
|
||||||
@@ -25,7 +25,7 @@ class GreeterClient {
|
|||||||
|
|
||||||
auto status = stub_->SayHello(&context, request_msg, &response_msg);
|
auto status = stub_->SayHello(&context, request_msg, &response_msg);
|
||||||
if (status.ok()) {
|
if (status.ok()) {
|
||||||
const HelloReply *response = response_msg.GetRoot();
|
const HelloReply* response = response_msg.GetRoot();
|
||||||
return response->message()->str();
|
return response->message()->str();
|
||||||
} else {
|
} else {
|
||||||
std::cerr << status.error_code() << ": " << status.error_message()
|
std::cerr << status.error_code() << ": " << status.error_message()
|
||||||
@@ -34,8 +34,8 @@ class GreeterClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SayManyHellos(const std::string &name, int num_greetings,
|
void SayManyHellos(const std::string& name, int num_greetings,
|
||||||
std::function<void(const std::string &)> callback) {
|
std::function<void(const std::string&)> callback) {
|
||||||
flatbuffers::grpc::MessageBuilder mb;
|
flatbuffers::grpc::MessageBuilder mb;
|
||||||
auto name_offset = mb.CreateString(name);
|
auto name_offset = mb.CreateString(name);
|
||||||
auto request_offset =
|
auto request_offset =
|
||||||
@@ -49,7 +49,7 @@ class GreeterClient {
|
|||||||
|
|
||||||
auto stream = stub_->SayManyHellos(&context, request_msg);
|
auto stream = stub_->SayManyHellos(&context, request_msg);
|
||||||
while (stream->Read(&response_msg)) {
|
while (stream->Read(&response_msg)) {
|
||||||
const HelloReply *response = response_msg.GetRoot();
|
const HelloReply* response = response_msg.GetRoot();
|
||||||
callback(response->message()->str());
|
callback(response->message()->str());
|
||||||
}
|
}
|
||||||
auto status = stream->Finish();
|
auto status = stream->Finish();
|
||||||
@@ -64,7 +64,7 @@ class GreeterClient {
|
|||||||
std::unique_ptr<Greeter::Stub> stub_;
|
std::unique_ptr<Greeter::Stub> stub_;
|
||||||
};
|
};
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char** argv) {
|
||||||
std::string server_address("localhost:50051");
|
std::string server_address("localhost:50051");
|
||||||
|
|
||||||
auto channel =
|
auto channel =
|
||||||
@@ -77,7 +77,7 @@ int main(int argc, char **argv) {
|
|||||||
std::cerr << "Greeter received: " << message << std::endl;
|
std::cerr << "Greeter received: " << message << std::endl;
|
||||||
|
|
||||||
int num_greetings = 10;
|
int num_greetings = 10;
|
||||||
greeter.SayManyHellos(name, num_greetings, [](const std::string &message) {
|
greeter.SayManyHellos(name, num_greetings, [](const std::string& message) {
|
||||||
std::cerr << "Greeter received: " << message << std::endl;
|
std::cerr << "Greeter received: " << message << std::endl;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,25 +1,25 @@
|
|||||||
#include "greeter.grpc.fb.h"
|
|
||||||
#include "greeter_generated.h"
|
|
||||||
|
|
||||||
#include <grpcpp/grpcpp.h>
|
#include <grpcpp/grpcpp.h>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include "greeter.grpc.fb.h"
|
||||||
|
#include "greeter_generated.h"
|
||||||
|
|
||||||
class GreeterServiceImpl final : public Greeter::Service {
|
class GreeterServiceImpl final : public Greeter::Service {
|
||||||
virtual grpc::Status SayHello(
|
virtual grpc::Status SayHello(
|
||||||
grpc::ServerContext *context,
|
grpc::ServerContext* context,
|
||||||
const flatbuffers::grpc::Message<HelloRequest> *request_msg,
|
const flatbuffers::grpc::Message<HelloRequest>* request_msg,
|
||||||
flatbuffers::grpc::Message<HelloReply> *response_msg) override {
|
flatbuffers::grpc::Message<HelloReply>* response_msg) override {
|
||||||
flatbuffers::grpc::MessageBuilder mb_;
|
flatbuffers::grpc::MessageBuilder mb_;
|
||||||
|
|
||||||
// We call GetRoot to "parse" the message. Verification is already
|
// We call GetRoot to "parse" the message. Verification is already
|
||||||
// performed by default. See the notes below for more details.
|
// performed by default. See the notes below for more details.
|
||||||
const HelloRequest *request = request_msg->GetRoot();
|
const HelloRequest* request = request_msg->GetRoot();
|
||||||
|
|
||||||
// Fields are retrieved as usual with FlatBuffers
|
// Fields are retrieved as usual with FlatBuffers
|
||||||
const std::string &name = request->name()->str();
|
const std::string& name = request->name()->str();
|
||||||
|
|
||||||
// `flatbuffers::grpc::MessageBuilder` is a `FlatBufferBuilder` with a
|
// `flatbuffers::grpc::MessageBuilder` is a `FlatBufferBuilder` with a
|
||||||
// special allocator for efficient gRPC buffer transfer, but otherwise
|
// special allocator for efficient gRPC buffer transfer, but otherwise
|
||||||
@@ -39,14 +39,14 @@ class GreeterServiceImpl final : public Greeter::Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
virtual grpc::Status SayManyHellos(
|
virtual grpc::Status SayManyHellos(
|
||||||
grpc::ServerContext *context,
|
grpc::ServerContext* context,
|
||||||
const flatbuffers::grpc::Message<ManyHellosRequest> *request_msg,
|
const flatbuffers::grpc::Message<ManyHellosRequest>* request_msg,
|
||||||
grpc::ServerWriter<flatbuffers::grpc::Message<HelloReply>> *writer)
|
grpc::ServerWriter<flatbuffers::grpc::Message<HelloReply>>* writer)
|
||||||
override {
|
override {
|
||||||
// The streaming usage below is simply a combination of standard gRPC
|
// The streaming usage below is simply a combination of standard gRPC
|
||||||
// streaming with the FlatBuffers usage shown above.
|
// streaming with the FlatBuffers usage shown above.
|
||||||
const ManyHellosRequest *request = request_msg->GetRoot();
|
const ManyHellosRequest* request = request_msg->GetRoot();
|
||||||
const std::string &name = request->name()->str();
|
const std::string& name = request->name()->str();
|
||||||
int num_greetings = request->num_greetings();
|
int num_greetings = request->num_greetings();
|
||||||
|
|
||||||
for (int i = 0; i < num_greetings; i++) {
|
for (int i = 0; i < num_greetings; i++) {
|
||||||
@@ -75,7 +75,7 @@ void RunServer() {
|
|||||||
server->Wait();
|
server->Wait();
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, const char *argv[]) {
|
int main(int argc, const char* argv[]) {
|
||||||
RunServer();
|
RunServer();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,8 +42,8 @@ class Array {
|
|||||||
// If T is a non-pointer and a LE-scalar or a struct (!scalar_tag::value).
|
// If T is a non-pointer and a LE-scalar or a struct (!scalar_tag::value).
|
||||||
static FLATBUFFERS_CONSTEXPR bool is_span_observable =
|
static FLATBUFFERS_CONSTEXPR bool is_span_observable =
|
||||||
!std::is_pointer<T>::value &&
|
!std::is_pointer<T>::value &&
|
||||||
((scalar_tag::value && (FLATBUFFERS_LITTLEENDIAN || sizeof(T) == 1)) ||
|
((scalar_tag::value && (FLATBUFFERS_LITTLEENDIAN || sizeof(T) == 1)) ||
|
||||||
!scalar_tag::value);
|
!scalar_tag::value);
|
||||||
|
|
||||||
FLATBUFFERS_CONSTEXPR uint16_t size() const { return length; }
|
FLATBUFFERS_CONSTEXPR uint16_t size() const { return length; }
|
||||||
|
|
||||||
|
|||||||
@@ -469,8 +469,9 @@ class FlatBufferBuilderImpl {
|
|||||||
if (dedup_vtables_) {
|
if (dedup_vtables_) {
|
||||||
for (auto it = buf_.scratch_data(); it < buf_.scratch_end();
|
for (auto it = buf_.scratch_data(); it < buf_.scratch_end();
|
||||||
it += sizeof(uoffset_t)) {
|
it += sizeof(uoffset_t)) {
|
||||||
auto vt_offset_ptr = reinterpret_cast<uoffset_t *>(it);
|
auto vt_offset_ptr = reinterpret_cast<uoffset_t*>(it);
|
||||||
auto vt2 = reinterpret_cast<voffset_t *>(buf_.data_at(*vt_offset_ptr + length_of_64_bit_region_));
|
auto vt2 = reinterpret_cast<voffset_t*>(
|
||||||
|
buf_.data_at(*vt_offset_ptr + length_of_64_bit_region_));
|
||||||
auto vt2_size = ReadScalar<voffset_t>(vt2);
|
auto vt2_size = ReadScalar<voffset_t>(vt2);
|
||||||
if (vt1_size != vt2_size || 0 != memcmp(vt2, vt1, vt1_size)) continue;
|
if (vt1_size != vt2_size || 0 != memcmp(vt2, vt1, vt1_size)) continue;
|
||||||
vt_use = *vt_offset_ptr;
|
vt_use = *vt_offset_ptr;
|
||||||
|
|||||||
@@ -410,7 +410,6 @@ FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span<const U> make_structs_span(
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
// Represent a vector much like the template above, but in this case we
|
// Represent a vector much like the template above, but in this case we
|
||||||
// don't know what the element types are (used with reflection.h).
|
// don't know what the element types are (used with reflection.h).
|
||||||
class VectorOfAny {
|
class VectorOfAny {
|
||||||
|
|||||||
@@ -14,12 +14,12 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "flatbuffers/file_manager.h"
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "flatbuffers/file_manager.h"
|
|
||||||
|
|
||||||
namespace flatbuffers {
|
namespace flatbuffers {
|
||||||
|
|
||||||
bool RealFileSaver::SaveFile(const char* name, const char* buf, size_t len,
|
bool RealFileSaver::SaveFile(const char* name, const char* buf, size_t len,
|
||||||
|
|||||||
@@ -652,19 +652,23 @@ class CSharpGenerator : public BaseGenerator {
|
|||||||
// Get the getter for the key of the struct.
|
// Get the getter for the key of the struct.
|
||||||
if (IsString(key_field->value.type)) {
|
if (IsString(key_field->value.type)) {
|
||||||
return "string.CompareOrdinal(" +
|
return "string.CompareOrdinal(" +
|
||||||
GenGetterForLookupByKey(struct_def, key_field, "builder.DataBuffer",
|
GenGetterForLookupByKey(struct_def, key_field,
|
||||||
"builder.DataBuffer.Length - o1.Value") +
|
"builder.DataBuffer",
|
||||||
", " +
|
"builder.DataBuffer.Length - o1.Value") +
|
||||||
GenGetterForLookupByKey(struct_def, key_field, "builder.DataBuffer",
|
", " +
|
||||||
"builder.DataBuffer.Length - o2.Value") +
|
GenGetterForLookupByKey(struct_def, key_field,
|
||||||
")";
|
"builder.DataBuffer",
|
||||||
|
"builder.DataBuffer.Length - o2.Value") +
|
||||||
|
")";
|
||||||
} else {
|
} else {
|
||||||
return GenGetterForLookupByKey(struct_def, key_field, "builder.DataBuffer",
|
return GenGetterForLookupByKey(struct_def, key_field,
|
||||||
"builder.DataBuffer.Length - o1.Value") +
|
"builder.DataBuffer",
|
||||||
".CompareTo(" +
|
"builder.DataBuffer.Length - o1.Value") +
|
||||||
GenGetterForLookupByKey(struct_def, key_field, "builder.DataBuffer",
|
".CompareTo(" +
|
||||||
"builder.DataBuffer.Length - o2.Value") +
|
GenGetterForLookupByKey(struct_def, key_field,
|
||||||
")";
|
"builder.DataBuffer",
|
||||||
|
"builder.DataBuffer.Length - o2.Value") +
|
||||||
|
")";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1596,7 +1600,8 @@ class CSharpGenerator : public BaseGenerator {
|
|||||||
|
|
||||||
code += " obj_.__assign(tableOffset, bb);\n";
|
code += " obj_.__assign(tableOffset, bb);\n";
|
||||||
if (IsString(key_field->value.type)) {
|
if (IsString(key_field->value.type)) {
|
||||||
code += " int comp = string.CompareOrdinal(obj_." + name + ", key);\n";
|
code +=
|
||||||
|
" int comp = string.CompareOrdinal(obj_." + name + ", key);\n";
|
||||||
} else {
|
} else {
|
||||||
code += " int comp = obj_." + name + ".CompareTo(key);\n";
|
code += " int comp = obj_." + name + ".CompareTo(key);\n";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,16 +56,16 @@ static Namer::Config JavaDefaultConfig() {
|
|||||||
|
|
||||||
static std::set<std::string> JavaKeywords() {
|
static std::set<std::string> JavaKeywords() {
|
||||||
return {
|
return {
|
||||||
"abstract", "assert", "boolean", "break", "byte",
|
"abstract", "assert", "boolean", "break", "byte",
|
||||||
"case", "catch", "char", "class", "const",
|
"case", "catch", "char", "class", "const",
|
||||||
"continue", "default", "do", "double", "else",
|
"continue", "default", "do", "double", "else",
|
||||||
"enum", "extends", "final", "finally", "float",
|
"enum", "extends", "final", "finally", "float",
|
||||||
"for", "goto", "if", "implements","import",
|
"for", "goto", "if", "implements", "import",
|
||||||
"instanceof","int", "interface", "long", "native",
|
"instanceof", "int", "interface", "long", "native",
|
||||||
"new", "notify", "package", "private", "protected",
|
"new", "notify", "package", "private", "protected",
|
||||||
"public", "return", "short", "static", "strictfp",
|
"public", "return", "short", "static", "strictfp",
|
||||||
"super", "switch", "synchronized","this", "throw",
|
"super", "switch", "synchronized", "this", "throw",
|
||||||
"throws", "transient","try", "void", "volatile",
|
"throws", "transient", "try", "void", "volatile",
|
||||||
"while",
|
"while",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -1468,7 +1468,8 @@ class JavaGenerator : public BaseGenerator {
|
|||||||
" " + variable_name + "Type = " + field_name + "Type(" +
|
" " + variable_name + "Type = " + field_name + "Type(" +
|
||||||
type_params + ");\n";
|
type_params + ");\n";
|
||||||
code += indent + variable_name + ".setType(" + variable_name + "Type);\n";
|
code += indent + variable_name + ".setType(" + variable_name + "Type);\n";
|
||||||
code += indent + "com.google.flatbuffers.Table " + variable_name + "Value;\n";
|
code +=
|
||||||
|
indent + "com.google.flatbuffers.Table " + variable_name + "Value;\n";
|
||||||
code += indent + "switch (" + variable_name + "Type) {\n";
|
code += indent + "switch (" + variable_name + "Type) {\n";
|
||||||
for (auto eit = enum_def.Vals().begin(); eit != enum_def.Vals().end();
|
for (auto eit = enum_def.Vals().begin(); eit != enum_def.Vals().end();
|
||||||
++eit) {
|
++eit) {
|
||||||
|
|||||||
@@ -1216,8 +1216,8 @@ class KotlinGenerator : public BaseGenerator {
|
|||||||
// fun inventoryInByteBuffer(_bb: Bytebuffer):
|
// fun inventoryInByteBuffer(_bb: Bytebuffer):
|
||||||
// ByteBuffer? = __vector_as_bytebuffer(_bb, 14, 1)
|
// ByteBuffer? = __vector_as_bytebuffer(_bb, 14, 1)
|
||||||
GenerateFunOneLine(
|
GenerateFunOneLine(
|
||||||
writer, field_name + "InByteBuffer", "_bb: ByteBuffer",
|
writer, field_name + "InByteBuffer", "_bb: ByteBuffer", buffer_type,
|
||||||
buffer_type, [&]() {
|
[&]() {
|
||||||
writer.SetValue("end", end_idx);
|
writer.SetValue("end", end_idx);
|
||||||
writer += "__vector_in_bytebuffer(_bb, {{offset}}, {{end}})";
|
writer += "__vector_in_bytebuffer(_bb, {{offset}}, {{end}})";
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -2885,8 +2885,7 @@ class PythonGenerator : public BaseGenerator {
|
|||||||
EnsureDirExists(directories);
|
EnsureDirExists(directories);
|
||||||
|
|
||||||
for (size_t i = directories.find(kPathSeparator, path_.size());
|
for (size_t i = directories.find(kPathSeparator, path_.size());
|
||||||
i != std::string::npos;
|
i != std::string::npos; i = directories.find(kPathSeparator, i + 1)) {
|
||||||
i = directories.find(kPathSeparator, i + 1)) {
|
|
||||||
const std::string init_py =
|
const std::string init_py =
|
||||||
directories.substr(0, i) + kPathSeparator + "__init__.py";
|
directories.substr(0, i) + kPathSeparator + "__init__.py";
|
||||||
parser_.opts.file_saver->SaveFile(init_py.c_str(), "", false);
|
parser_.opts.file_saver->SaveFile(init_py.c_str(), "", false);
|
||||||
@@ -2904,7 +2903,7 @@ class PythonGenerator : public BaseGenerator {
|
|||||||
} // namespace python
|
} // namespace python
|
||||||
|
|
||||||
static const char* GeneratePython(const Parser& parser, const std::string& path,
|
static const char* GeneratePython(const Parser& parser, const std::string& path,
|
||||||
const std::string& file_name) {
|
const std::string& file_name) {
|
||||||
python::Version version{parser.opts.python_version};
|
python::Version version{parser.opts.python_version};
|
||||||
if (!version.IsValid()) return "The provided Python version is not valid";
|
if (!version.IsValid()) return "The provided Python version is not valid";
|
||||||
|
|
||||||
@@ -2913,7 +2912,8 @@ static const char* GeneratePython(const Parser& parser, const std::string& path,
|
|||||||
|
|
||||||
if (parser.opts.python_typing) {
|
if (parser.opts.python_typing) {
|
||||||
python::PythonStubGenerator stub_generator(parser, path, version);
|
python::PythonStubGenerator stub_generator(parser, path, version);
|
||||||
if (!stub_generator.Generate()) return "could not generate Python type stubs";
|
if (!stub_generator.Generate())
|
||||||
|
return "could not generate Python type stubs";
|
||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
@@ -2948,7 +2948,8 @@ class PythonCodeGenerator : public CodeGenerator {
|
|||||||
|
|
||||||
Status GenerateGrpcCode(const Parser& parser, const std::string& path,
|
Status GenerateGrpcCode(const Parser& parser, const std::string& path,
|
||||||
const std::string& filename) override {
|
const std::string& filename) override {
|
||||||
if (!GeneratePythonGRPC(parser, path, filename)) { // TODO add status GeneratePythonGRPC
|
if (!GeneratePythonGRPC(parser, path,
|
||||||
|
filename)) { // TODO add status GeneratePythonGRPC
|
||||||
return Status::ERROR;
|
return Status::ERROR;
|
||||||
}
|
}
|
||||||
return Status::OK;
|
return Status::OK;
|
||||||
|
|||||||
@@ -1347,7 +1347,8 @@ class SwiftGenerator : public BaseGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void GenerateObjectAPIStructConstructor(const StructDef& struct_def) {
|
void GenerateObjectAPIStructConstructor(const StructDef& struct_def) {
|
||||||
code_ += "{{ACCESS_TYPE}} init(_ _t: borrowing {{STRUCTNAME}}" + Mutable() + ") {";
|
code_ += "{{ACCESS_TYPE}} init(_ _t: borrowing {{STRUCTNAME}}" + Mutable() +
|
||||||
|
") {";
|
||||||
Indent();
|
Indent();
|
||||||
for (auto it = struct_def.fields.vec.begin();
|
for (auto it = struct_def.fields.vec.begin();
|
||||||
it != struct_def.fields.vec.end(); ++it) {
|
it != struct_def.fields.vec.end(); ++it) {
|
||||||
@@ -1393,8 +1394,9 @@ class SwiftGenerator : public BaseGenerator {
|
|||||||
base_constructor);
|
base_constructor);
|
||||||
}
|
}
|
||||||
code_ += "";
|
code_ += "";
|
||||||
BuildObjectConstructor(buffer_constructor,
|
BuildObjectConstructor(
|
||||||
"_ _t: borrowing " + namer_.NamespacedType(struct_def));
|
buffer_constructor,
|
||||||
|
"_ _t: borrowing " + namer_.NamespacedType(struct_def));
|
||||||
BuildObjectConstructor(base_constructor);
|
BuildObjectConstructor(base_constructor);
|
||||||
if (!struct_def.fixed)
|
if (!struct_def.fixed)
|
||||||
code_ +=
|
code_ +=
|
||||||
@@ -1960,7 +1962,7 @@ class SwiftGenerator : public BaseGenerator {
|
|||||||
std::string GenType(const Type& type,
|
std::string GenType(const Type& type,
|
||||||
const bool should_consider_suffix = false) const {
|
const bool should_consider_suffix = false) const {
|
||||||
return IsScalar(type.base_type) ? GenTypeBasic(type)
|
return IsScalar(type.base_type) ? GenTypeBasic(type)
|
||||||
: IsArray(type) ? GenType(type.VectorType())
|
: IsArray(type) ? GenType(type.VectorType())
|
||||||
: GenTypePointer(type, should_consider_suffix);
|
: GenTypePointer(type, should_consider_suffix);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -339,8 +339,7 @@ class TsGenerator : public BaseGenerator {
|
|||||||
for (auto it = dc.begin(); it != dc.end(); ++it) {
|
for (auto it = dc.begin(); it != dc.end(); ++it) {
|
||||||
if (indent) code += indent;
|
if (indent) code += indent;
|
||||||
std::string safe = *it;
|
std::string safe = *it;
|
||||||
for (size_t pos = 0;
|
for (size_t pos = 0; (pos = safe.find("*/", pos)) != std::string::npos;) {
|
||||||
(pos = safe.find("*/", pos)) != std::string::npos;) {
|
|
||||||
safe.replace(pos, 2, "*\\/");
|
safe.replace(pos, 2, "*\\/");
|
||||||
pos += 3;
|
pos += 3;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ void Offset64Test() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
const RootTable *root_table = GetRootTable(builder.GetBufferPointer());
|
const RootTable* root_table = GetRootTable(builder.GetBufferPointer());
|
||||||
|
|
||||||
// Expect the far vector to be properly sized.
|
// Expect the far vector to be properly sized.
|
||||||
TEST_EQ(root_table->far_vector()->size(), far_vector_size);
|
TEST_EQ(root_table->far_vector()->size(), far_vector_size);
|
||||||
@@ -130,12 +130,12 @@ void Offset64NestedFlatBuffer() {
|
|||||||
fbb.Finish(root_table_offset);
|
fbb.Finish(root_table_offset);
|
||||||
|
|
||||||
// Ensure the buffer is valid.
|
// Ensure the buffer is valid.
|
||||||
const RootTable *root_table = GetRootTable(fbb.GetBufferPointer());
|
const RootTable* root_table = GetRootTable(fbb.GetBufferPointer());
|
||||||
TEST_EQ_STR(root_table->near_string()->c_str(), "nested: some near string");
|
TEST_EQ_STR(root_table->near_string()->c_str(), "nested: some near string");
|
||||||
|
|
||||||
// Copy the data out of the builder.
|
// Copy the data out of the builder.
|
||||||
std::vector<uint8_t> nested_data{ fbb.GetBufferPointer(),
|
std::vector<uint8_t> nested_data{fbb.GetBufferPointer(),
|
||||||
fbb.GetBufferPointer() + fbb.GetSize() };
|
fbb.GetBufferPointer() + fbb.GetSize()};
|
||||||
|
|
||||||
{
|
{
|
||||||
// Clear so we can reuse the builder.
|
// Clear so we can reuse the builder.
|
||||||
@@ -167,7 +167,7 @@ void Offset64NestedFlatBuffer() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
const RootTable *root_table = GetRootTable(fbb.GetBufferPointer());
|
const RootTable* root_table = GetRootTable(fbb.GetBufferPointer());
|
||||||
|
|
||||||
// Test that the parent buffer field is ok.
|
// Test that the parent buffer field is ok.
|
||||||
TEST_EQ_STR(root_table->near_string()->c_str(), "some near string");
|
TEST_EQ_STR(root_table->near_string()->c_str(), "some near string");
|
||||||
@@ -185,7 +185,7 @@ void Offset64CreateDirect() {
|
|||||||
FlatBufferBuilder64 fbb;
|
FlatBufferBuilder64 fbb;
|
||||||
|
|
||||||
// Create a vector of some data
|
// Create a vector of some data
|
||||||
std::vector<uint8_t> data{ 0, 1, 2 };
|
std::vector<uint8_t> data{0, 1, 2};
|
||||||
|
|
||||||
// Call the "Direct" creation method to ensure that things are added to the
|
// Call the "Direct" creation method to ensure that things are added to the
|
||||||
// buffer in the correct order, Offset64 first followed by any Offsets.
|
// buffer in the correct order, Offset64 first followed by any Offsets.
|
||||||
@@ -205,7 +205,7 @@ void Offset64CreateDirect() {
|
|||||||
TEST_EQ(VerifyRootTableBuffer(verifier), true);
|
TEST_EQ(VerifyRootTableBuffer(verifier), true);
|
||||||
|
|
||||||
// Verify the data.
|
// Verify the data.
|
||||||
const RootTable *root_table = GetRootTable(fbb.GetBufferPointer());
|
const RootTable* root_table = GetRootTable(fbb.GetBufferPointer());
|
||||||
TEST_EQ(root_table->far_vector()->size(), data.size());
|
TEST_EQ(root_table->far_vector()->size(), data.size());
|
||||||
TEST_EQ(root_table->big_vector()->size(), data.size());
|
TEST_EQ(root_table->big_vector()->size(), data.size());
|
||||||
TEST_EQ_STR(root_table->far_string()->c_str(), "some far string");
|
TEST_EQ_STR(root_table->far_string()->c_str(), "some far string");
|
||||||
@@ -214,8 +214,8 @@ void Offset64CreateDirect() {
|
|||||||
|
|
||||||
void Offset64Evolution() {
|
void Offset64Evolution() {
|
||||||
// Some common data for the tests.
|
// Some common data for the tests.
|
||||||
const std::vector<uint8_t> data = { 1, 2, 3, 4 };
|
const std::vector<uint8_t> data = {1, 2, 3, 4};
|
||||||
const std::vector<uint8_t> big_data = { 6, 7, 8, 9, 10 };
|
const std::vector<uint8_t> big_data = {6, 7, 8, 9, 10};
|
||||||
|
|
||||||
// Built V1 read V2
|
// Built V1 read V2
|
||||||
{
|
{
|
||||||
@@ -301,13 +301,13 @@ void Offset64VectorOfStructs() {
|
|||||||
FlatBufferBuilder64 builder;
|
FlatBufferBuilder64 builder;
|
||||||
|
|
||||||
std::vector<LeafStruct> far_leaves;
|
std::vector<LeafStruct> far_leaves;
|
||||||
far_leaves.emplace_back(LeafStruct{ 123, 4.567 });
|
far_leaves.emplace_back(LeafStruct{123, 4.567});
|
||||||
far_leaves.emplace_back(LeafStruct{ 987, 6.543 });
|
far_leaves.emplace_back(LeafStruct{987, 6.543});
|
||||||
|
|
||||||
std::vector<LeafStruct> big_leaves;
|
std::vector<LeafStruct> big_leaves;
|
||||||
big_leaves.emplace_back(LeafStruct{ 72, 72.8 });
|
big_leaves.emplace_back(LeafStruct{72, 72.8});
|
||||||
big_leaves.emplace_back(LeafStruct{ 82, 82.8 });
|
big_leaves.emplace_back(LeafStruct{82, 82.8});
|
||||||
big_leaves.emplace_back(LeafStruct{ 92, 92.8 });
|
big_leaves.emplace_back(LeafStruct{92, 92.8});
|
||||||
|
|
||||||
// Add the two vectors of leaf structs.
|
// Add the two vectors of leaf structs.
|
||||||
const Offset<RootTable> root_table_offset =
|
const Offset<RootTable> root_table_offset =
|
||||||
@@ -327,7 +327,7 @@ void Offset64VectorOfStructs() {
|
|||||||
TEST_EQ(VerifyRootTableBuffer(verifier), true);
|
TEST_EQ(VerifyRootTableBuffer(verifier), true);
|
||||||
|
|
||||||
// Verify the data.
|
// Verify the data.
|
||||||
const RootTable *root_table = GetRootTable(builder.GetBufferPointer());
|
const RootTable* root_table = GetRootTable(builder.GetBufferPointer());
|
||||||
TEST_EQ(root_table->far_struct_vector()->size(), far_leaves.size());
|
TEST_EQ(root_table->far_struct_vector()->size(), far_leaves.size());
|
||||||
TEST_EQ(root_table->far_struct_vector()->Get(0)->a(), 123);
|
TEST_EQ(root_table->far_struct_vector()->Get(0)->a(), 123);
|
||||||
TEST_EQ(root_table->far_struct_vector()->Get(0)->b(), 4.567);
|
TEST_EQ(root_table->far_struct_vector()->Get(0)->b(), 4.567);
|
||||||
@@ -369,7 +369,7 @@ void Offset64SizePrefix() {
|
|||||||
|
|
||||||
TEST_EQ(VerifySizePrefixedRootTableBuffer(verifier), true);
|
TEST_EQ(VerifySizePrefixedRootTableBuffer(verifier), true);
|
||||||
|
|
||||||
const RootTable *root_table =
|
const RootTable* root_table =
|
||||||
GetSizePrefixedRootTable(builder.GetBufferPointer());
|
GetSizePrefixedRootTable(builder.GetBufferPointer());
|
||||||
|
|
||||||
// Verify the fields.
|
// Verify the fields.
|
||||||
@@ -432,7 +432,7 @@ void Offset64ManyVectors() {
|
|||||||
|
|
||||||
TEST_EQ(VerifyRootTableBuffer(verifier), true);
|
TEST_EQ(VerifyRootTableBuffer(verifier), true);
|
||||||
|
|
||||||
const RootTable *root_table = GetRootTable(builder.GetBufferPointer());
|
const RootTable* root_table = GetRootTable(builder.GetBufferPointer());
|
||||||
|
|
||||||
// Verify the fields.
|
// Verify the fields.
|
||||||
TEST_EQ_STR(root_table->far_string()->c_str(), "some far string");
|
TEST_EQ_STR(root_table->far_string()->c_str(), "some far string");
|
||||||
@@ -449,7 +449,7 @@ void Offset64ForceAlign() {
|
|||||||
|
|
||||||
// Setup some data to serialize that is less than the force_align size of 32
|
// Setup some data to serialize that is less than the force_align size of 32
|
||||||
// bytes.
|
// bytes.
|
||||||
std::vector<uint8_t> data{ 1, 2, 3 };
|
std::vector<uint8_t> data{1, 2, 3};
|
||||||
|
|
||||||
// Use the CreateDirect which calls the ForceVectorAlign
|
// Use the CreateDirect which calls the ForceVectorAlign
|
||||||
const auto root_table_offset =
|
const auto root_table_offset =
|
||||||
|
|||||||
@@ -48,22 +48,22 @@ using ::cpp17::MyGame::Example::Vec3;
|
|||||||
** Build some FB objects.
|
** Build some FB objects.
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
namespace {
|
namespace {
|
||||||
const Monster *BuildMonster(flatbuffers::FlatBufferBuilder &fbb) {
|
const Monster* BuildMonster(flatbuffers::FlatBufferBuilder& fbb) {
|
||||||
using ::cpp17::MyGame::Example::Color;
|
using ::cpp17::MyGame::Example::Color;
|
||||||
using ::cpp17::MyGame::Example::MonsterBuilder;
|
using ::cpp17::MyGame::Example::MonsterBuilder;
|
||||||
using ::cpp17::MyGame::Example::Test;
|
using ::cpp17::MyGame::Example::Test;
|
||||||
auto name = fbb.CreateString("my_monster");
|
auto name = fbb.CreateString("my_monster");
|
||||||
auto inventory = fbb.CreateVector(std::vector<uint8_t>{ 4, 5, 6, 7 });
|
auto inventory = fbb.CreateVector(std::vector<uint8_t>{4, 5, 6, 7});
|
||||||
MonsterBuilder builder(fbb);
|
MonsterBuilder builder(fbb);
|
||||||
auto vec3 = Vec3{ /*x=*/1.1f,
|
auto vec3 = Vec3{/*x=*/1.1f,
|
||||||
/*y=*/2.2f,
|
/*y=*/2.2f,
|
||||||
/*z=*/3.3f,
|
/*z=*/3.3f,
|
||||||
/*test1=*/6.6,
|
/*test1=*/6.6,
|
||||||
/*test2=*/Color::Green,
|
/*test2=*/Color::Green,
|
||||||
/*test3=*/
|
/*test3=*/
|
||||||
Test(
|
Test(
|
||||||
/*a=*/11,
|
/*a=*/11,
|
||||||
/*b=*/90) };
|
/*b=*/90)};
|
||||||
builder.add_pos(&vec3);
|
builder.add_pos(&vec3);
|
||||||
builder.add_name(name);
|
builder.add_name(name);
|
||||||
builder.add_mana(1);
|
builder.add_mana(1);
|
||||||
@@ -85,7 +85,7 @@ const Monster *BuildMonster(flatbuffers::FlatBufferBuilder &fbb) {
|
|||||||
builder.add_non_owning_reference(17);
|
builder.add_non_owning_reference(17);
|
||||||
builder.add_inventory(inventory);
|
builder.add_inventory(inventory);
|
||||||
fbb.Finish(builder.Finish());
|
fbb.Finish(builder.Finish());
|
||||||
const Monster *monster =
|
const Monster* monster =
|
||||||
flatbuffers::GetRoot<Monster>(fbb.GetBufferPointer());
|
flatbuffers::GetRoot<Monster>(fbb.GetBufferPointer());
|
||||||
return monster;
|
return monster;
|
||||||
}
|
}
|
||||||
@@ -103,7 +103,7 @@ void StringifyAnyFlatbuffersTypeTest() {
|
|||||||
flatbuffers::FlatBufferBuilder fbb;
|
flatbuffers::FlatBufferBuilder fbb;
|
||||||
// We are using a Monster here, but we could have used any type, because the
|
// We are using a Monster here, but we could have used any type, because the
|
||||||
// code that follows is totally generic!
|
// code that follows is totally generic!
|
||||||
const auto *monster = BuildMonster(fbb);
|
const auto* monster = BuildMonster(fbb);
|
||||||
|
|
||||||
std::string expected = R"(MyGame.Example.Monster{
|
std::string expected = R"(MyGame.Example.Monster{
|
||||||
pos = MyGame.Example.Vec3{
|
pos = MyGame.Example.Vec3{
|
||||||
@@ -179,13 +179,13 @@ void StringifyAnyFlatbuffersTypeTest() {
|
|||||||
** Test Traits::FieldType
|
** Test Traits::FieldType
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
using pos_type = Monster::Traits::FieldType<0>;
|
using pos_type = Monster::Traits::FieldType<0>;
|
||||||
static_assert(std::is_same_v<pos_type, const Vec3 *>);
|
static_assert(std::is_same_v<pos_type, const Vec3*>);
|
||||||
|
|
||||||
using mana_type = Monster::Traits::FieldType<1>;
|
using mana_type = Monster::Traits::FieldType<1>;
|
||||||
static_assert(std::is_same_v<mana_type, int16_t>);
|
static_assert(std::is_same_v<mana_type, int16_t>);
|
||||||
|
|
||||||
using name_type = Monster::Traits::FieldType<3>;
|
using name_type = Monster::Traits::FieldType<3>;
|
||||||
static_assert(std::is_same_v<name_type, const flatbuffers::String *>);
|
static_assert(std::is_same_v<name_type, const flatbuffers::String*>);
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
** Generic Create Function Test.
|
** Generic Create Function Test.
|
||||||
@@ -263,7 +263,7 @@ int FlatBufferCpp17Tests() {
|
|||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
int main(int /*argc*/, const char * /*argv*/[]) {
|
int main(int /*argc*/, const char* /*argv*/[]) {
|
||||||
InitTestEngine();
|
InitTestEngine();
|
||||||
|
|
||||||
FlatBufferCpp17Tests();
|
FlatBufferCpp17Tests();
|
||||||
|
|||||||
@@ -1,31 +1,32 @@
|
|||||||
#include <stddef.h>
|
#include <bfbs_gen_lua.h>
|
||||||
#include <stdint.h>
|
#include <bfbs_gen_nim.h>
|
||||||
#include <string>
|
|
||||||
#include "flatbuffers/idl.h" // For Parser and generation functions
|
|
||||||
#include "test_init.h"
|
|
||||||
#include <memory>
|
|
||||||
#include <flatbuffers/flatc.h>
|
#include <flatbuffers/flatc.h>
|
||||||
#include "idl_gen_cpp.h" // For C++ generator
|
|
||||||
#include <idl_gen_csharp.h>
|
|
||||||
#include <iostream>
|
|
||||||
#include "flatbuffers/code_generator.h"
|
|
||||||
#include <idl_gen_binary.h>
|
#include <idl_gen_binary.h>
|
||||||
|
#include <idl_gen_csharp.h>
|
||||||
#include <idl_gen_dart.h>
|
#include <idl_gen_dart.h>
|
||||||
#include <idl_gen_fbs.h>
|
#include <idl_gen_fbs.h>
|
||||||
#include <idl_gen_go.h>
|
#include <idl_gen_go.h>
|
||||||
#include <idl_gen_java.h>
|
#include <idl_gen_java.h>
|
||||||
#include <idl_gen_json_schema.h>
|
#include <idl_gen_json_schema.h>
|
||||||
#include <idl_gen_kotlin.h>
|
#include <idl_gen_kotlin.h>
|
||||||
#include <idl_gen_kotlin.h>
|
|
||||||
#include <idl_gen_lobster.h>
|
#include <idl_gen_lobster.h>
|
||||||
#include <bfbs_gen_lua.h>
|
|
||||||
#include <bfbs_gen_nim.h>
|
|
||||||
#include <idl_gen_python.h>
|
|
||||||
#include <idl_gen_php.h>
|
#include <idl_gen_php.h>
|
||||||
|
#include <idl_gen_python.h>
|
||||||
#include <idl_gen_rust.h>
|
#include <idl_gen_rust.h>
|
||||||
#include <idl_gen_text.h>
|
|
||||||
#include <idl_gen_swift.h>
|
#include <idl_gen_swift.h>
|
||||||
|
#include <idl_gen_text.h>
|
||||||
#include <idl_gen_ts.h>
|
#include <idl_gen_ts.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "flatbuffers/code_generator.h"
|
||||||
|
#include "flatbuffers/idl.h" // For Parser and generation functions
|
||||||
|
#include "idl_gen_cpp.h" // For C++ generator
|
||||||
|
#include "test_init.h"
|
||||||
|
|
||||||
static constexpr size_t kMinInputLength = 1;
|
static constexpr size_t kMinInputLength = 1;
|
||||||
static constexpr size_t kMaxInputLength = 16384;
|
static constexpr size_t kMaxInputLength = 16384;
|
||||||
@@ -98,8 +99,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
|
|||||||
if (parser.Parse(parse_input)) {
|
if (parser.Parse(parse_input)) {
|
||||||
parser.Serialize();
|
parser.Serialize();
|
||||||
const uint8_t* buf = parser.builder_.GetBufferPointer();
|
const uint8_t* buf = parser.builder_.GetBufferPointer();
|
||||||
flatbuffers::Verifier verifier(buf,
|
flatbuffers::Verifier verifier(buf, parser.builder_.GetSize());
|
||||||
parser.builder_.GetSize());
|
|
||||||
TEST_EQ(true, reflection::VerifySchemaBuffer(verifier));
|
TEST_EQ(true, reflection::VerifySchemaBuffer(verifier));
|
||||||
|
|
||||||
auto root = flatbuffers::GetRoot<flatbuffers::Table>(buf);
|
auto root = flatbuffers::GetRoot<flatbuffers::Table>(buf);
|
||||||
@@ -126,8 +126,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
|
|||||||
generators.emplace_back(flatbuffers::NewKotlinCodeGenerator());
|
generators.emplace_back(flatbuffers::NewKotlinCodeGenerator());
|
||||||
generators.emplace_back(flatbuffers::NewKotlinKMPCodeGenerator());
|
generators.emplace_back(flatbuffers::NewKotlinKMPCodeGenerator());
|
||||||
generators.emplace_back(flatbuffers::NewLobsterCodeGenerator());
|
generators.emplace_back(flatbuffers::NewLobsterCodeGenerator());
|
||||||
generators.emplace_back(flatbuffers::NewLuaBfbsGenerator(flatbuffers_version));
|
generators.emplace_back(
|
||||||
generators.emplace_back(flatbuffers::NewNimBfbsGenerator(flatbuffers_version));
|
flatbuffers::NewLuaBfbsGenerator(flatbuffers_version));
|
||||||
|
generators.emplace_back(
|
||||||
|
flatbuffers::NewNimBfbsGenerator(flatbuffers_version));
|
||||||
generators.emplace_back(flatbuffers::NewPythonCodeGenerator());
|
generators.emplace_back(flatbuffers::NewPythonCodeGenerator());
|
||||||
generators.emplace_back(flatbuffers::NewPhpCodeGenerator());
|
generators.emplace_back(flatbuffers::NewPhpCodeGenerator());
|
||||||
generators.emplace_back(flatbuffers::NewRustCodeGenerator());
|
generators.emplace_back(flatbuffers::NewRustCodeGenerator());
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
#include <iostream>
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
#include "flatbuffers/util.h"
|
#include "flatbuffers/util.h"
|
||||||
|
|
||||||
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
|
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
std::cerr << "Usage: monster_debug <path to fuzzer crash file>\n";
|
std::cerr << "Usage: monster_debug <path to fuzzer crash file>\n";
|
||||||
return 0;
|
return 0;
|
||||||
@@ -23,7 +24,7 @@ int main(int argc, char *argv[]) {
|
|||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
auto rc = LLVMFuzzerTestOneInput(
|
auto rc = LLVMFuzzerTestOneInput(
|
||||||
reinterpret_cast<const uint8_t *>(crash_file_data.data()),
|
reinterpret_cast<const uint8_t*>(crash_file_data.data()),
|
||||||
crash_file_data.size());
|
crash_file_data.size());
|
||||||
std::cout << "LLVMFuzzerTestOneInput finished with code " << rc << "\n\n";
|
std::cout << "LLVMFuzzerTestOneInput finished with code " << rc << "\n\n";
|
||||||
return rc;
|
return rc;
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "flatbuffers/util.h"
|
#include "flatbuffers/util.h"
|
||||||
|
|
||||||
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
|
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
std::cerr << "Usage: scalar_debug <path to fuzzer crash file>\n";
|
std::cerr << "Usage: scalar_debug <path to fuzzer crash file>\n";
|
||||||
return 0;
|
return 0;
|
||||||
@@ -21,7 +22,7 @@ int main(int argc, char *argv[]) {
|
|||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
auto rc = LLVMFuzzerTestOneInput(
|
auto rc = LLVMFuzzerTestOneInput(
|
||||||
reinterpret_cast<const uint8_t *>(crash_file_data.data()),
|
reinterpret_cast<const uint8_t*>(crash_file_data.data()),
|
||||||
crash_file_data.size());
|
crash_file_data.size());
|
||||||
std::cout << "LLVMFuzzerTestOneInput finished with code " << rc << "\n\n";
|
std::cout << "LLVMFuzzerTestOneInput finished with code " << rc << "\n\n";
|
||||||
return rc;
|
return rc;
|
||||||
|
|||||||
@@ -22,15 +22,19 @@ const Native::Vector3D UnPackVector3DAlt(const Geometry::Vector3DAlt& obj) {
|
|||||||
|
|
||||||
namespace Geometry {
|
namespace Geometry {
|
||||||
void Matrix::UnPackTo(
|
void Matrix::UnPackTo(
|
||||||
Native::Matrix *_o,
|
Native::Matrix* _o,
|
||||||
const ::flatbuffers::resolver_function_t *_resolver) const {
|
const ::flatbuffers::resolver_function_t* _resolver) const {
|
||||||
(void)_resolver;
|
(void)_resolver;
|
||||||
|
|
||||||
auto _rows = rows();
|
auto _rows = rows();
|
||||||
if (_rows) { _o->rows = _rows; }
|
if (_rows) {
|
||||||
|
_o->rows = _rows;
|
||||||
|
}
|
||||||
|
|
||||||
auto _columns = columns();
|
auto _columns = columns();
|
||||||
if (_columns) { _o->columns = _columns; }
|
if (_columns) {
|
||||||
|
_o->columns = _columns;
|
||||||
|
}
|
||||||
|
|
||||||
auto _values = values();
|
auto _values = values();
|
||||||
if (_values) {
|
if (_values) {
|
||||||
@@ -42,8 +46,8 @@ void Matrix::UnPackTo(
|
|||||||
}
|
}
|
||||||
|
|
||||||
::flatbuffers::Offset<Matrix> Matrix::Pack(
|
::flatbuffers::Offset<Matrix> Matrix::Pack(
|
||||||
::flatbuffers::FlatBufferBuilder &_fbb, const Native::Matrix *_o,
|
::flatbuffers::FlatBufferBuilder& _fbb, const Native::Matrix* _o,
|
||||||
const ::flatbuffers::rehasher_function_t *_rehasher) {
|
const ::flatbuffers::rehasher_function_t* _rehasher) {
|
||||||
(void)_rehasher;
|
(void)_rehasher;
|
||||||
|
|
||||||
return CreateMatrix(_fbb, _o->rows, _o->columns,
|
return CreateMatrix(_fbb, _o->rows, _o->columns,
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ struct Vector3D {
|
|||||||
this->z = _z;
|
this->z = _z;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator==(const Vector3D &other) const {
|
bool operator==(const Vector3D& other) const {
|
||||||
return (x == other.x) && (y == other.y) && (z == other.z);
|
return (x == other.x) && (y == other.y) && (z == other.z);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -38,7 +38,7 @@ struct Matrix {
|
|||||||
values.resize(_rows * _columns);
|
values.resize(_rows * _columns);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator==(const Matrix &other) const {
|
bool operator==(const Matrix& other) const {
|
||||||
return (rows == other.rows) && (columns == other.columns) &&
|
return (rows == other.rows) && (columns == other.columns) &&
|
||||||
(values == other.values);
|
(values == other.values);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -771,15 +771,15 @@ void FixedLengthArrayTest() {
|
|||||||
// set memory chunk of size ArrayStruct to 1's
|
// set memory chunk of size ArrayStruct to 1's
|
||||||
std::memset(static_cast<void*>(non_zero_memory), 1, arr_size);
|
std::memset(static_cast<void*>(non_zero_memory), 1, arr_size);
|
||||||
// after placement-new it should be all 0's
|
// after placement-new it should be all 0's
|
||||||
#if defined(FLATBUFFERS_MEMORY_LEAK_TRACKING) && \
|
#if defined(FLATBUFFERS_MEMORY_LEAK_TRACKING) && defined(_MSC_VER) && \
|
||||||
defined(_MSC_VER) && defined(_DEBUG)
|
defined(_DEBUG)
|
||||||
#undef new
|
#undef new
|
||||||
#endif
|
#endif
|
||||||
MyGame::Example::ArrayStruct* ap =
|
MyGame::Example::ArrayStruct* ap =
|
||||||
new (non_zero_memory) MyGame::Example::ArrayStruct;
|
new (non_zero_memory) MyGame::Example::ArrayStruct;
|
||||||
#if defined(FLATBUFFERS_MEMORY_LEAK_TRACKING) && \
|
#if defined(FLATBUFFERS_MEMORY_LEAK_TRACKING) && defined(_MSC_VER) && \
|
||||||
defined(_MSC_VER) && defined(_DEBUG)
|
defined(_DEBUG)
|
||||||
#define new DEBUG_NEW
|
#define new DEBUG_NEW
|
||||||
#endif
|
#endif
|
||||||
(void)ap;
|
(void)ap;
|
||||||
for (size_t i = 0; i < arr_size; ++i) {
|
for (size_t i = 0; i < arr_size; ++i) {
|
||||||
@@ -925,7 +925,8 @@ void NativeTypeTest() {
|
|||||||
src_data.matrix->values = {3, 4};
|
src_data.matrix->values = {3, 4};
|
||||||
|
|
||||||
for (int i = 0; i < N; ++i) {
|
for (int i = 0; i < N; ++i) {
|
||||||
src_data.matrices.push_back(std::unique_ptr<Native::Matrix>(new Native::Matrix(1, i)));
|
src_data.matrices.push_back(
|
||||||
|
std::unique_ptr<Native::Matrix>(new Native::Matrix(1, i)));
|
||||||
std::fill(src_data.matrices[i]->values.begin(),
|
std::fill(src_data.matrices[i]->values.begin(),
|
||||||
src_data.matrices[i]->values.end(), i + 0.5f);
|
src_data.matrices[i]->values.end(), i + 0.5f);
|
||||||
}
|
}
|
||||||
@@ -960,7 +961,7 @@ void NativeTypeTest() {
|
|||||||
TEST_EQ(dstDataT->matrix->values[1], 4);
|
TEST_EQ(dstDataT->matrix->values[1], 4);
|
||||||
|
|
||||||
for (int i = 0; i < N; ++i) {
|
for (int i = 0; i < N; ++i) {
|
||||||
const Native::Matrix &m = *dstDataT->matrices[i];
|
const Native::Matrix& m = *dstDataT->matrices[i];
|
||||||
TEST_EQ(m.rows, 1);
|
TEST_EQ(m.rows, 1);
|
||||||
TEST_EQ(m.columns, i);
|
TEST_EQ(m.columns, i);
|
||||||
for (int j = 0; j < i; ++j) {
|
for (int j = 0; j < i; ++j) {
|
||||||
|
|||||||
Reference in New Issue
Block a user