Skip to content

The unique_ptr release

Compare
Choose a tag to compare
@sebjameswml sebjameswml released this 10 Feb 11:15
· 1437 commits to main since this release
c63e3f4

This release has a significant improvement to the way in which morph::VisualModel memory is managed within a morph::Visual scene. Previously, you used the new keyword to allocate memory for each VisualModel, then you had to trust that the morphologica Visual object would deallocate that memory as it went out of scope. This scheme is retained, but made explicit now by the use of std::unique_ptr.

If you have a morph::Visual scene like this:

morph::Visual v (1600, 1000, "Example scene");

Where you would previously have added a morph::VisualModel by writing (using GraphVisual as an example of a VisualModel):

morph::GraphVisual<double>* gptr = new morph::GraphVisual<double> (v.shaderprog, v.tshaderprog, morph::vec<float>({0,0,0}));

You now write the slightly neater:

auto g_uptr = std::make_unique<morph::GraphVisual<double>> (v.shaders, morph::vec<float>({0,0,0}));

this returns an object of type std::unique_ptr<GraphVisual<double>>. You then add this to the morph::Visual like before:

auto gptr = v.addVisualModel (g_uptr);

where gptr is of type morph::GraphVisual<double>*. The key thing is that by adding the unique_ptr into the Visual scene, v, 'v takes ownership of the memory of the GraphVisual object, but you can go on interacting with the GraphVisual object by using the non-owning pointer, gptr. Note also that the boilerplate v.shaderprog and v.tshaderprog ID values have been collected into the single object v.shaders.

In this release, there has also been some refactoring, significantly of morph::Vector -> morph::vec and morph::vVector -> morph::vvec. These mathematical vector classes also have some nice new functionality, such as 1D convolutions, smoothing and differentiation.