GradientProblem & related classes use std::unique_ptr

Previously these classes in analogy with ceres::Problem's interface
had interfaces to allow bare pointers as well as unique_ptrs. This
CL changes the API to always use unique_ptr, this is less error prone
and makes the default ownership semantics clearer.

Change-Id: I7577a90761f341c7e009c248c820f0fec2e6f32d
This commit is contained in:
Sameer Agarwal
2024-08-14 11:15:23 -07:00
parent 487ce37fa7
commit 8f1b6123ad
14 changed files with 83 additions and 101 deletions
@@ -90,12 +90,13 @@ namespace ceres {
// first order function with central differences used for computing the
// derivative can be constructed as follows.
//
// FirstOrderFunction* function
// = new NumericDiffFirstOrderFunction<MyScalarCostFunctor, CENTRAL, 4>(
// new QuadraticCostFunctor(1.0)); ^ ^ ^
// | | |
// Finite Differencing Scheme -+ | |
// Dimension of xy ------------------------+
// std::unique_ptr<FirstOrderFunction> function
// = std::make_unique<
// NumericDiffFirstOrderFunction<MyScalarCostFunctor, CENTRAL, 4>>(
// std::make_unique<QuadraticCostFunctor>(1.0)); ^ ^
// | |
// Finite Differencing Scheme -----+ |
// Dimension of xy ----------------------+
//
//
// In the instantiation above, the template parameters following
@@ -106,9 +107,10 @@ namespace ceres {
// If the size of the parameter vector is not known at compile time, then an
// alternate construction syntax can be used:
//
// FirstOrderFunction* function
// = new NumericDiffFirstOrderFunction<MyScalarCostFunctor, CENTRAL>(
// new QuadraticCostFunctor(1.0), 4);
// std::unique_ptr<FirstOrderFunction> function
// = std::make_unique<NumericDiffFirstOrderFunction<MyScalarCostFunctor,
// CENTRAL>>(
// std::make_unique<QuadraticCostFunctor>(1.0), 4);
//
// Note that instead of passing 4 as a template argument, it is now passed as
// the second argument to the constructor.
@@ -117,15 +119,6 @@ template <typename FirstOrderFunctor,
int kNumParameters = DYNAMIC>
class NumericDiffFirstOrderFunction final : public FirstOrderFunction {
public:
template <class... Args,
bool kIsDynamic = kNumParameters == DYNAMIC,
std::enable_if_t<!kIsDynamic &&
std::is_constructible_v<FirstOrderFunctor,
Args&&...>>* = nullptr>
explicit NumericDiffFirstOrderFunction(Args&&... args)
: NumericDiffFirstOrderFunction{std::make_unique<FirstOrderFunction>(
std::forward<Args>(args)...)} {}
NumericDiffFirstOrderFunction(const NumericDiffFirstOrderFunction&) = delete;
NumericDiffFirstOrderFunction& operator=(
const NumericDiffFirstOrderFunction&) = delete;
@@ -134,37 +127,23 @@ class NumericDiffFirstOrderFunction final : public FirstOrderFunction {
NumericDiffFirstOrderFunction& operator=(
NumericDiffFirstOrderFunction&& other) noexcept = default;
// Constructor for the case where the parameter size is known at compile time.
explicit NumericDiffFirstOrderFunction(
FirstOrderFunctor* functor,
Ownership ownership = TAKE_OWNERSHIP,
const NumericDiffOptions& options = NumericDiffOptions())
: NumericDiffFirstOrderFunction{
std::unique_ptr<FirstOrderFunctor>{functor},
kNumParameters,
ownership,
options,
FIXED_INIT} {}
// Constructor for the case where the parameter size is known at compile time.
explicit NumericDiffFirstOrderFunction(
std::unique_ptr<FirstOrderFunctor> functor,
const NumericDiffOptions& options = NumericDiffOptions())
: NumericDiffFirstOrderFunction{std::move(functor),
kNumParameters,
TAKE_OWNERSHIP,
options,
FIXED_INIT} {}
template <class... Args,
bool kIsDynamic = kNumParameters == DYNAMIC,
std::enable_if_t<!kIsDynamic &&
std::is_constructible_v<FirstOrderFunctor,
Args&&...>>* = nullptr>
explicit NumericDiffFirstOrderFunction(Args&&... args)
: NumericDiffFirstOrderFunction{
std::move(functor), kNumParameters, TAKE_OWNERSHIP, FIXED_INIT} {}
// Constructor for the case where the parameter size is specified at run time.
explicit NumericDiffFirstOrderFunction(
FirstOrderFunctor* functor,
int num_parameters,
Ownership ownership = TAKE_OWNERSHIP,
const NumericDiffOptions& options = NumericDiffOptions())
: NumericDiffFirstOrderFunction{
std::unique_ptr<FirstOrderFunctor>{functor},
num_parameters,
ownership,
options,
DYNAMIC_INIT} {}
std::make_unique<FirstOrderFunctor>(std::forward<Args>(args)...)} {}
// Constructor for the case where the parameter size is specified at run time.
explicit NumericDiffFirstOrderFunction(