This commit is contained in:
Derek Bailey
2021-11-19 17:01:48 -08:00
committed by GitHub
parent 256ab3798d
commit e89de4411d
16 changed files with 212 additions and 252 deletions

View File

@@ -1,5 +1,5 @@
#ifndef FLATBUFFERS_BASE_H_
# define FLATBUFFERS_BASE_H_
#define FLATBUFFERS_BASE_H_
// clang-format off

View File

@@ -17,6 +17,8 @@
#ifndef FLATBUFFERS_FLATBUFFER_BUILDER_H_
#define FLATBUFFERS_FLATBUFFER_BUILDER_H_
#include <functional>
#include "flatbuffers/allocator.h"
#include "flatbuffers/array.h"
#include "flatbuffers/base.h"
@@ -31,8 +33,6 @@
#include "flatbuffers/vector_downward.h"
#include "flatbuffers/verifier.h"
#include <functional>
namespace flatbuffers {
// Converts a Field ID to a virtual table offset.
@@ -96,18 +96,19 @@ class FlatBufferBuilder {
/// @brief Move constructor for FlatBufferBuilder.
FlatBufferBuilder(FlatBufferBuilder &&other)
: buf_(1024, nullptr, false, AlignOf<largest_scalar_t>()),
num_field_loc(0),
max_voffset_(0),
nested(false),
finished(false),
minalign_(1),
force_defaults_(false),
dedup_vtables_(true),
string_pool(nullptr) {
: buf_(1024, nullptr, false, AlignOf<largest_scalar_t>()),
num_field_loc(0),
max_voffset_(0),
nested(false),
finished(false),
minalign_(1),
force_defaults_(false),
dedup_vtables_(true),
string_pool(nullptr) {
EndianCheck();
// Default construct and swap idiom.
// Lack of delegating constructors in vs2010 makes it more verbose than needed.
// Lack of delegating constructors in vs2010 makes it more verbose than
// needed.
Swap(other);
}
@@ -676,8 +677,9 @@ class FlatBufferBuilder {
/// returns any type that you can construct a FlatBuffers vector out of.
/// @return Returns a typed `Offset` into the serialized data indicating
/// where the vector is stored.
template<typename T> Offset<Vector<T>> CreateVector(size_t vector_size,
const std::function<T (size_t i)> &f) {
template<typename T>
Offset<Vector<T>> CreateVector(size_t vector_size,
const std::function<T(size_t i)> &f) {
FLATBUFFERS_ASSERT(FLATBUFFERS_GENERAL_HEAP_ALLOC_OK);
std::vector<T> elems(vector_size);
for (size_t i = 0; i < vector_size; i++) elems[i] = f(i);
@@ -795,15 +797,16 @@ class FlatBufferBuilder {
/// @brief Serialize an array of structs into a FlatBuffer `vector`.
/// @tparam T The data type of the struct array elements.
/// @param[in] filler A function that takes the current iteration 0..vector_size-1
/// and a pointer to the struct that must be filled.
/// @param[in] filler A function that takes the current iteration
/// 0..vector_size-1 and a pointer to the struct that must be filled.
/// @return Returns a typed `Offset` into the serialized data indicating
/// where the vector is stored.
/// This is mostly useful when flatbuffers are generated with mutation
/// accessors.
template<typename T> Offset<Vector<const T *>> CreateVectorOfStructs(
template<typename T>
Offset<Vector<const T *>> CreateVectorOfStructs(
size_t vector_size, const std::function<void(size_t i, T *)> &filler) {
T* structs = StartVectorOfStructs<T>(vector_size);
T *structs = StartVectorOfStructs<T>(vector_size);
for (size_t i = 0; i < vector_size; i++) {
filler(i, structs);
structs++;

View File

@@ -92,9 +92,9 @@ struct NativeTable {};
/// if you wish. The resolver does the opposite lookup, for when the object
/// is being serialized again.
typedef uint64_t hash_value_t;
typedef std::function<void (void **pointer_adr, hash_value_t hash)>
resolver_function_t;
typedef std::function<hash_value_t (void *pointer)> rehasher_function_t;
typedef std::function<void(void **pointer_adr, hash_value_t hash)>
resolver_function_t;
typedef std::function<hash_value_t(void *pointer)> rehasher_function_t;
// Helper function to test if a field is present, using any of the field
// enums in the generated code.

View File

@@ -1132,23 +1132,23 @@ class Builder FLATBUFFERS_FINAL_CLASS {
// sorted fashion.
// std::sort is typically already a lot faster on sorted data though.
auto dict = reinterpret_cast<TwoValue *>(stack_.data() + start);
std::sort(dict, dict + len,
[&](const TwoValue &a, const TwoValue &b) -> bool {
auto as = reinterpret_cast<const char *>(buf_.data() + a.key.u_);
auto bs = reinterpret_cast<const char *>(buf_.data() + b.key.u_);
auto comp = strcmp(as, bs);
// We want to disallow duplicate keys, since this results in a
// map where values cannot be found.
// But we can't assert here (since we don't want to fail on
// random JSON input) or have an error mechanism.
// Instead, we set has_duplicate_keys_ in the builder to
// signal this.
// TODO: Have to check for pointer equality, as some sort
// implementation apparently call this function with the same
// element?? Why?
if (!comp && &a != &b) has_duplicate_keys_ = true;
return comp < 0;
});
std::sort(
dict, dict + len, [&](const TwoValue &a, const TwoValue &b) -> bool {
auto as = reinterpret_cast<const char *>(buf_.data() + a.key.u_);
auto bs = reinterpret_cast<const char *>(buf_.data() + b.key.u_);
auto comp = strcmp(as, bs);
// We want to disallow duplicate keys, since this results in a
// map where values cannot be found.
// But we can't assert here (since we don't want to fail on
// random JSON input) or have an error mechanism.
// Instead, we set has_duplicate_keys_ in the builder to
// signal this.
// TODO: Have to check for pointer equality, as some sort
// implementation apparently call this function with the same
// element?? Why?
if (!comp && &a != &b) has_duplicate_keys_ = true;
return comp < 0;
});
// First create a vector out of all keys.
// TODO(wvo): if kBuilderFlagShareKeyVectors is true, see if we can share
// the first vector.
@@ -1404,12 +1404,10 @@ class Builder FLATBUFFERS_FINAL_CLASS {
template<typename T> static Type GetScalarType() {
static_assert(flatbuffers::is_scalar<T>::value, "Unrelated types");
return flatbuffers::is_floating_point<T>::value
? FBT_FLOAT
: flatbuffers::is_same<T, bool>::value
? FBT_BOOL
: (flatbuffers::is_unsigned<T>::value ? FBT_UINT
: FBT_INT);
return flatbuffers::is_floating_point<T>::value ? FBT_FLOAT
: flatbuffers::is_same<T, bool>::value
? FBT_BOOL
: (flatbuffers::is_unsigned<T>::value ? FBT_UINT : FBT_INT);
}
public:

View File

@@ -17,6 +17,7 @@
#ifndef FLATBUFFERS_IDL_H_
#define FLATBUFFERS_IDL_H_
#include <functional>
#include <map>
#include <memory>
#include <stack>
@@ -27,8 +28,6 @@
#include "flatbuffers/hash.h"
#include "flatbuffers/reflection.h"
#include <functional>
// This file defines the data types representing a parsed IDL (Interface
// Definition Language) / schema file.