An external API I'm using requires C-style array of objects:
// Some api function
void doStuff(const Foo* objects, size_t length);
Actually, the API uses int
for length but that just makes it even worse. When creating the array of objects, I don't know how many will I have, because some turn out wrong:
void ObjManager::sendObjectsToApi(const std::list<const std::string>& names)
{
// Create the most suitable type of connection
std::????<Foo> objects;
// Loop names, try to create object for every one of them
for( auto i=names.begin(), l=names.end(); i<l; i++ ) {
Foo obj = createFooWithName(*i);
if( obj.is_valid() ) {
objects.addToCollection( obj );
}
}
// Convert collection to C style array
size_t length = objects.size();
Foo* c_objects = objects.toC_StyleArray();
API::doStuff(c_objects, length);
}