flatbuffers icon indicating copy to clipboard operation
flatbuffers copied to clipboard

[vector] Allow to iterate with mutables

Open ArnaudD-FR opened this issue 2 years ago • 1 comments

When using mutable buffers it is currently impossible to deference iterators without having a compiler error due to struct IndirectHelper::Read returning return_type instead of mutable_return_type:

struct VectorIterator {
  ...
  // compiler error: failed to convert const value (IndirectHelper<T>::return_type)
  // to non const value (IndirectHelper<T>::mutable_return_type, aka IT)
  IT operator*() const { return IndirectHelper<T>::Read(data_, 0); }
  IT operator->() const { return IndirectHelper<T>::Read(data_, 0); }
  ...
};

The main idea behind this PR is to have an VectorIterator using uint8_t * and VectorConstIterator using const uint8_t * as data to use the correct overload of IndirectHelper<T>::Read

So VectorIterator has a new template parameter to switch from const_iterator to iterator. Side effect, as array was also using VectorIterator it is now using VectorConstIterator.

This will fix issue #4674

ArnaudD-FR avatar May 06 '22 11:05 ArnaudD-FR

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

For more information, open the CLA check for this pull request.

google-cla[bot] avatar May 06 '22 11:05 google-cla[bot]

@ArnaudD-FR Do you plan on addressing the comments?

dbaileychess avatar Aug 06 '22 05:08 dbaileychess

@dbaileychess Do you have a place to put the sample test code?

ArnaudD-FR avatar Aug 11 '22 16:08 ArnaudD-FR

@dbaileychess Do you have a place to put the sample test code?

@ArnaudD-FR I would just add something to tests/test.cpp.

dbaileychess avatar Aug 13 '22 05:08 dbaileychess

@dbaileychess Why was this closed? Lack of tests? I spent the last few hours trying to debug obscure template errors caused by this and been ripping my hair out, would love to see this fixed.

Is there any known workaround?

dbechrd avatar Sep 30 '22 06:09 dbechrd

@dbaileychess , sorry for delay, many changes on my professional life those last months. Can you reopen this issue? I provided the test. Just tell me if this is what you expect.

adesmier-viceversa avatar Oct 03 '22 08:10 adesmier-viceversa

Just open a new PR. I close PRs that don't get any activity.

dbaileychess avatar Oct 03 '22 22:10 dbaileychess