From a4de6de7000e03011fa916f67da1c98a8b4aa591 Mon Sep 17 00:00:00 2001 From: Jon Simantov Date: Wed, 22 Jul 2015 02:01:56 -0700 Subject: [PATCH 1/3] Add optional root table name to SetString and ResizeVector. This allows you to use these functions with a flatbuffer whose root table type does't correspond with the root table type of the schema. If you don't specify the table name, it will use the root table from the schema by default (mimicing the current behavior). --- include/flatbuffers/reflection.h | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/include/flatbuffers/reflection.h b/include/flatbuffers/reflection.h index 1530a2ca0..e37603763 100644 --- a/include/flatbuffers/reflection.h +++ b/include/flatbuffers/reflection.h @@ -271,10 +271,13 @@ inline const reflection::Object &GetUnionType( // "delta" may be negative (shrinking). // Unless "delta" is a multiple of the largest alignment, you'll create a small // amount of garbage space in the buffer (usually 0..7 bytes). +// If your FlatBuffer's root table is not the schema's root table, you should +// pass in the name of the root table in root_table_name. class ResizeContext { public: ResizeContext(const reflection::Schema &schema, uoffset_t start, int delta, - std::vector *flatbuf) + std::vector *flatbuf, + const char* root_table_name = nullptr) : schema_(schema), startptr_(flatbuf->data() + start), delta_(delta), buf_(*flatbuf), dag_check_(flatbuf->size() / sizeof(uoffset_t), false) { @@ -284,7 +287,10 @@ class ResizeContext { // Now change all the offsets by delta_. auto root = GetAnyRoot(buf_.data()); Straddle(buf_.data(), root, buf_.data()); - ResizeTable(*schema.root_table(), root); + ResizeTable(root_table_name + ? *schema.objects()->LookupByKey(root_table_name) + : *schema.root_table(), + root); // We can now add or remove bytes at start. if (delta_ > 0) buf_.insert(buf_.begin() + start, delta_, 0); else buf_.erase(buf_.begin() + start, buf_.begin() + start - delta_); @@ -396,14 +402,15 @@ class ResizeContext { // live inside a std::vector so we can resize the buffer if needed. // "str" must live inside "flatbuf" and may be invalidated after this call. inline void SetString(const reflection::Schema &schema, const std::string &val, - const String *str, std::vector *flatbuf) { + const String *str, std::vector *flatbuf, + const char* root_table_name) { auto delta = static_cast(val.size()) - static_cast(str->Length()); auto start = static_cast(reinterpret_cast(str) - flatbuf->data() + sizeof(uoffset_t)); if (delta) { // Different size, we must expand (or contract). - ResizeContext(schema, start, delta, flatbuf); + ResizeContext(schema, start, delta, flatbuf, root_table_name); if (delta < 0) { // Clear the old string, since we don't want parts of it remaining. memset(flatbuf->data() + start, 0, str->Length()); @@ -416,17 +423,20 @@ inline void SetString(const reflection::Schema &schema, const std::string &val, // Resizes a flatbuffers::Vector inside a FlatBuffer. FlatBuffer must // live inside a std::vector so we can resize the buffer if needed. // "vec" must live inside "flatbuf" and may be invalidated after this call. +// If your FlatBuffer's root table is not the schema's root table, you should +// pass in the name of the root table in "root_table_name". template void ResizeVector(const reflection::Schema &schema, uoffset_t newsize, T val, const Vector *vec, - std::vector *flatbuf) { + std::vector *flatbuf, + const char* root_table_name = nullptr) { auto delta_elem = static_cast(newsize) - static_cast(vec->size()); auto delta_bytes = delta_elem * static_cast(sizeof(T)); auto vec_start = reinterpret_cast(vec) - flatbuf->data(); auto start = static_cast(vec_start + sizeof(uoffset_t) + sizeof(T) * vec->size()); if (delta_bytes) { - ResizeContext(schema, start, delta_bytes, flatbuf); + ResizeContext(schema, start, delta_bytes, flatbuf, root_table_name); WriteScalar(flatbuf->data() + vec_start, newsize); // Length field. // Set new elements to "val". for (int i = 0; i < delta_elem; i++) { From 6e160f4c5963c33cabc975edaf8b4212aba7da72 Mon Sep 17 00:00:00 2001 From: Jon Simantov Date: Wed, 22 Jul 2015 10:56:41 -0700 Subject: [PATCH 2/3] Use Object* for optional root_table in SetString/ResizeVector. Was previously using table name, but no reason not to just let the calling code worry about getting the object. --- include/flatbuffers/reflection.h | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/include/flatbuffers/reflection.h b/include/flatbuffers/reflection.h index e37603763..66352a80a 100644 --- a/include/flatbuffers/reflection.h +++ b/include/flatbuffers/reflection.h @@ -272,12 +272,12 @@ inline const reflection::Object &GetUnionType( // Unless "delta" is a multiple of the largest alignment, you'll create a small // amount of garbage space in the buffer (usually 0..7 bytes). // If your FlatBuffer's root table is not the schema's root table, you should -// pass in the name of the root table in root_table_name. +// pass in your root_table type as well. class ResizeContext { public: ResizeContext(const reflection::Schema &schema, uoffset_t start, int delta, std::vector *flatbuf, - const char* root_table_name = nullptr) + const reflection::Object* root_table = nullptr) : schema_(schema), startptr_(flatbuf->data() + start), delta_(delta), buf_(*flatbuf), dag_check_(flatbuf->size() / sizeof(uoffset_t), false) { @@ -287,10 +287,7 @@ class ResizeContext { // Now change all the offsets by delta_. auto root = GetAnyRoot(buf_.data()); Straddle(buf_.data(), root, buf_.data()); - ResizeTable(root_table_name - ? *schema.objects()->LookupByKey(root_table_name) - : *schema.root_table(), - root); + ResizeTable(root_table ? *root_table : *schema.root_table(), root); // We can now add or remove bytes at start. if (delta_ > 0) buf_.insert(buf_.begin() + start, delta_, 0); else buf_.erase(buf_.begin() + start, buf_.begin() + start - delta_); @@ -401,16 +398,18 @@ class ResizeContext { // Changes the contents of a string inside a FlatBuffer. FlatBuffer must // live inside a std::vector so we can resize the buffer if needed. // "str" must live inside "flatbuf" and may be invalidated after this call. +// If your FlatBuffer's root table is not the schema's root table, you should +// pass in your root_table type as well. inline void SetString(const reflection::Schema &schema, const std::string &val, const String *str, std::vector *flatbuf, - const char* root_table_name) { + const reflection::Object* root_table = nullptr) { auto delta = static_cast(val.size()) - static_cast(str->Length()); auto start = static_cast(reinterpret_cast(str) - flatbuf->data() + sizeof(uoffset_t)); if (delta) { // Different size, we must expand (or contract). - ResizeContext(schema, start, delta, flatbuf, root_table_name); + ResizeContext(schema, start, delta, flatbuf, root_table); if (delta < 0) { // Clear the old string, since we don't want parts of it remaining. memset(flatbuf->data() + start, 0, str->Length()); @@ -424,19 +423,18 @@ inline void SetString(const reflection::Schema &schema, const std::string &val, // live inside a std::vector so we can resize the buffer if needed. // "vec" must live inside "flatbuf" and may be invalidated after this call. // If your FlatBuffer's root table is not the schema's root table, you should -// pass in the name of the root table in "root_table_name". -template void ResizeVector(const reflection::Schema &schema, - uoffset_t newsize, T val, - const Vector *vec, - std::vector *flatbuf, - const char* root_table_name = nullptr) { +// pass in your root_table type as well. +template +void ResizeVector(const reflection::Schema &schema, uoffset_t newsize, T val, + const Vector *vec, std::vector *flatbuf, + const reflection::Object *root_table = nullptr) { auto delta_elem = static_cast(newsize) - static_cast(vec->size()); auto delta_bytes = delta_elem * static_cast(sizeof(T)); auto vec_start = reinterpret_cast(vec) - flatbuf->data(); auto start = static_cast(vec_start + sizeof(uoffset_t) + sizeof(T) * vec->size()); if (delta_bytes) { - ResizeContext(schema, start, delta_bytes, flatbuf, root_table_name); + ResizeContext(schema, start, delta_bytes, flatbuf, root_table); WriteScalar(flatbuf->data() + vec_start, newsize); // Length field. // Set new elements to "val". for (int i = 0; i < delta_elem; i++) { From b56020ad3b8e1482b599d721144f635b489e0477 Mon Sep 17 00:00:00 2001 From: Jon Simantov Date: Wed, 22 Jul 2015 11:46:14 -0700 Subject: [PATCH 3/3] Updated pointer syntax to be consistent in SetString, etc. --- include/flatbuffers/reflection.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/flatbuffers/reflection.h b/include/flatbuffers/reflection.h index 66352a80a..53112f214 100644 --- a/include/flatbuffers/reflection.h +++ b/include/flatbuffers/reflection.h @@ -277,7 +277,7 @@ class ResizeContext { public: ResizeContext(const reflection::Schema &schema, uoffset_t start, int delta, std::vector *flatbuf, - const reflection::Object* root_table = nullptr) + const reflection::Object *root_table = nullptr) : schema_(schema), startptr_(flatbuf->data() + start), delta_(delta), buf_(*flatbuf), dag_check_(flatbuf->size() / sizeof(uoffset_t), false) { @@ -402,7 +402,7 @@ class ResizeContext { // pass in your root_table type as well. inline void SetString(const reflection::Schema &schema, const std::string &val, const String *str, std::vector *flatbuf, - const reflection::Object* root_table = nullptr) { + const reflection::Object *root_table = nullptr) { auto delta = static_cast(val.size()) - static_cast(str->Length()); auto start = static_cast(reinterpret_cast(str) - flatbuf->data() +