Skip to content

Commit

Permalink
Fix dereference operator of VectorIterator to structures
Browse files Browse the repository at this point in the history
For Vector or Array of structures the dereference operator of an
iterator returns the pointer to the structure. However, IndirectHelper,
which is used in the implementation of this operator, is instantiated
in the way that the IndirectHelper::Read returns structure by value.

This is because, Vector and Array instantiate IndirectHelper with
const T*, but VectorIterator instantiates IndirectHelper with T. There
are three IndirectHelper template definition: first for T, second for
Offset<T> and the last one for const T*. Those have different
IndirectHelper:Read implementations and (more importantly) return type.
This is the reason of mismatch in VectorIterator::operator* between
return type declaration and what was exactly returned.

That is, for Array<T,...> where T is scalar the VectorIterator is
instantiated as VectorIterator<T, T>, dereference operator returns T
and its implementation uses IndirectHelper<T> which Read function
returns T.
When T is not scalar, then VectorIterator is instantiated as
VectorIterator<T, const T *>, dereference operator returns const T * and
its implementation uses IndirectHelper<T> which Read function returns T.

The fix is done as follows:
* implement type trait is_specialization_of_Offset and
 is_specialization_of_Offset64,
* change partial specialization of IndirectHelper with const T * that
 it is instantiated by T and enabled only if T is not scalar and not
 specialization of Offset or Offset64,
* remove type differentiation (due to scalar) from Array..

The above makes the IndirectHelper able to correctly instantiate itself
basing only on T. Thus, the instantiation in VectorIterator correctly
instantiate IndirectHelper::Read function, especially the return type.
  • Loading branch information
admo committed Mar 12, 2024
1 parent 67eb95d commit f26f762
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
7 changes: 2 additions & 5 deletions include/flatbuffers/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,10 @@ template<typename T, uint16_t length> class Array {
// Array<T> can carry only POD data types (scalars or structs).
typedef typename flatbuffers::bool_constant<flatbuffers::is_scalar<T>::value>
scalar_tag;
typedef
typename flatbuffers::conditional<scalar_tag::value, T, const T *>::type
IndirectHelperType;

public:
typedef uint16_t size_type;
typedef typename IndirectHelper<IndirectHelperType>::return_type return_type;
typedef typename IndirectHelper<T>::return_type return_type;
typedef VectorConstIterator<T, return_type, uoffset_t> const_iterator;
typedef VectorReverseIterator<const_iterator> const_reverse_iterator;

Expand All @@ -50,7 +47,7 @@ template<typename T, uint16_t length> class Array {

return_type Get(uoffset_t i) const {
FLATBUFFERS_ASSERT(i < size());
return IndirectHelper<IndirectHelperType>::Read(Data(), i);
return IndirectHelper<T>::Read(Data(), i);
}

return_type operator[](uoffset_t i) const { return Get(i); }
Expand Down
29 changes: 24 additions & 5 deletions include/flatbuffers/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <algorithm>

#include "flatbuffers/base.h"
#include "flatbuffers/stl_emulation.h"

namespace flatbuffers {

Expand All @@ -36,6 +37,10 @@ template<typename T = void> struct Offset {
bool IsNull() const { return !o; }
};

template<typename T> struct is_specialisation_of_Offset : false_type {};
template<typename T>
struct is_specialisation_of_Offset<Offset<T>> : true_type {};

// Wrapper for uoffset64_t Offsets.
template<typename T = void> struct Offset64 {
// The type of offset to use.
Expand All @@ -48,6 +53,10 @@ template<typename T = void> struct Offset64 {
bool IsNull() const { return !o; }
};

template<typename T> struct is_specialisation_of_Offset64 : false_type {};
template<typename T>
struct is_specialisation_of_Offset64<Offset64<T>> : true_type {};

// Litmus check for ensuring the Offsets are the expected size.
static_assert(sizeof(Offset<>) == 4, "Offset has wrong size");
static_assert(sizeof(Offset64<>) == 8, "Offset64 has wrong size");
Expand Down Expand Up @@ -90,7 +99,7 @@ static inline bool StringLessThan(const char *a_data, uoffset_t a_size,
// return type like this.
// The typedef is for the convenience of callers of this function
// (avoiding the need for a trailing return decltype)
template<typename T> struct IndirectHelper {
template<typename T, typename Enable = void> struct IndirectHelper {
typedef T return_type;
typedef T mutable_return_type;
static const size_t element_stride = sizeof(T);
Expand Down Expand Up @@ -135,10 +144,20 @@ struct IndirectHelper<OffsetT<T>> {
};

// For vector of structs.
template<typename T> struct IndirectHelper<const T *> {
typedef const T *return_type;
typedef T *mutable_return_type;
static const size_t element_stride = sizeof(T);
template<typename T>
struct IndirectHelper<
T, typename std::enable_if<
!std::is_scalar<typename std::remove_pointer<T>::type>::value &&
!is_specialisation_of_Offset<T>::value &&
!is_specialisation_of_Offset64<T>::value>::type> {
private:
typedef typename std::remove_pointer<typename std::remove_cv<T>::type>::type
pointee_type;

public:
typedef const pointee_type *return_type;
typedef pointee_type *mutable_return_type;
static const size_t element_stride = sizeof(pointee_type);

static return_type Read(const uint8_t *const p, const size_t i) {
// Structs are stored inline, relative to the first struct pointer.
Expand Down
18 changes: 18 additions & 0 deletions tests/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,24 @@ void FixedLengthArrayConstructorTest() {
TEST_EQ(arr_struct.e(), 10);
TEST_EQ(arr_struct.f()->Get(0), -2);
TEST_EQ(arr_struct.f()->Get(1), -1);

// Test for each loop over NestedStruct entries
for (auto i : *arr_struct.d()) {
for (auto a : *i->a()) {
TEST_EQ(a, 1);
break; // one iteration is enough, just testing compilation
}
TEST_EQ(i->b(), MyGame::Example::TestEnum::B);
for (auto c : *i->c()) {
TEST_EQ(c, MyGame::Example::TestEnum::A);
break; // one iteration is enough, just testing compilation
}
for (auto d : *i->d()) {
TEST_EQ(d, -2);
break; // one iteration is enough, just testing compilation
}
break; // one iteration is enough, just testing compilation
}
}
#else
void FixedLengthArrayConstructorTest() {}
Expand Down

0 comments on commit f26f762

Please sign in to comment.