Skip to content

Vector builder API for c++? #8930

@dpcollins-google

Description

@dpcollins-google

I think its almost possible today to write an API like the following to make it easier to efficiently build vectors of tables. The only problem is that CreateUninitializedVector asserts that the passed type is a scalar.

Are there any other issues I'm not seeing with such an API? Do the maintainers think something with this shape would be helpful to add to avoid needing out of band storage for vectors of tables?

template <typename T>
class VectorBuilder {
  using VecOffset = Offset<Vector<Offset<T>>;
 public:
  VectorBuilder(FlatBufferBuilder& fbb, size_t size) : size_(size), vector_(fbb.CreateUninitializedVector(size_, &start_)) {}

  void Push(Offset<T> elt) {
    assert(written_ < size_);
    *(start + written_) = elt;
    ++written_;
  }

  VecOffset Finish() {
    assert(written_ == size_);
    return vector_;
  }
  
 private:
  const size_t size_;
  Offset<T>* start_;
  const VecOffset vector_;

  size_t written_ = 0;
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions