I want to avoid the intermediate std::vector required by CreateVector(const std::vector<T> &v). Therefore I thought I'd use CreateUninitializedVector instead, but I am having trouble filling the buffer correctly.
The use case differs from the examples I found, because the elements are of type Offset. From the sources I concluded I need to apply ReferTo() and then EndianScalar() to my elements before assigning them to the buffer. The following is not an exact copy from my code, but it shows the problem.
Vector<Offset<MyType>> SaveElements(const std::vector<MyAppType>& elements)
{
Offset<MyType>* pbegin = nullptr;
auto offset = builder_.CreateUninitializedVector(nr_elements, sizeof(Offset<MyType>), (uint8_t**)&pbegin);
auto* pcurrent = pbegin;
for ( const auto& element : elements )
{
Offset<MyType> element_offset = SaveElement(element);
// How do I correctly write an Offset in the uninitialized buffer?
*pcurrent++ = EndianScalar(builder_.ReferTo(element_offset));
}
return offset;
}
Any help is greatly appreciated.
Patrick