Skip to content

cmMakefile: Automatic cmTarget lifetime management

Motivation

cmTarget isn't used outside of CMake, so there's no real pressure to make its implementation abstract. We use cmTarget * all over the place, if we make cmTarget pimpl based, we end up with pointers to pointers to structs. If we use std::unique_ptr instead to manage cmTarget instaces we stay with pointers to structs and don't need to copy/move construct either.

Changes

The goal of this topic is to use C++'s built in automatic utilities to manage cmTarget instance lifetime and ensure we don't make copies of it by accident. To achieve this, we

  • make cmTarget non-copyable and non-movable
  • use std::unique_ptr<cmTarget> to manage cmTarget lifetime
  • use cm::make_unique<cmTarget> to create cmTarget instances

cmTarget instances are owned by cmMakefile instances, therefore this adapts the ownership concepts for cmTarget within that class. All cmTarget instances owned by a cmMakefile instances are managed by a single std::vector<std::unique_ptr<cmTarget>> which guarantees automatic destruction.

Merge request reports