samedi 4 juillet 2020

Constructing std::vector

Say that I have a bunch of classes to model text rendering; one of them manages font styling (in this case Baz), another is in charge of holding alignment and position info (Foo) and finally a class that itself renders the text using Pango, Cairo, Fontconfig, etc.

In the current iteration of this code, each instance of Bar creates its own PangoContext, PangoLayout, cairo_t, etc inside its constructor (wrapping the C APIs with smart pointers), and these are only used in the object's constructor.

A very reduced example of what I'm working with looks like this:

#include <memory>
#include <iostream>
#include <vector>

struct Baz {
    Baz();
    std::string aMember;
    int anotherMember;  
};

struct Bar {
    Bar(std::string const& _string, int const& _int);
};

struct Foo {
    Foo();
    void populateBar(std::vector<Baz> _bunchOfBaz);
    std::vector<std::unique_ptr<Bar>> vectorOfBar;
};

Bar::Bar(std::string const& _string, int const& _int) {
    // Use smart pointers to set-up a pango layout and render some stuff in a cairo context.
}

void Foo::populateBar(std::vector<Baz> _bunchOfBaz) {
    vectorOfBar.clear();
    for (Baz const& _baz: _bunchOfBaz) {
        vectorOfBar.push_back(
          std::make_unique<Bar>(
            _baz.aMember,
            _baz.anotherMember
          )
        );
    }
}

The question is: is it guaranteed that iterations in the loop inside populateBar will run sequentially, and that the loop will not move on until the Bar constructor returns? essentially, what I want to know is: Could I keep a single instance each of PangoLayout, PangoContext, cairo_t etc inside Foo and reuse them safely for each iteration of that loop?

Aucun commentaire:

Enregistrer un commentaire