-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
We kept the old API for backwards compatibility (for now...).
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -150,8 +150,17 @@ namespace roboptim | |
/// \{ | ||
|
||
/// \pre costfunction \f$\mathbb{R}^n \rightarrow \mathbb{R}\f$ | ||
This comment has been minimized.
Sorry, something went wrong. |
||
/// \brief Deprecated constructor taking a reference to a cost function. | ||
/// This legacy version meant that we simply kept a const reference to the | ||
/// cost function, which could reference stack variables... | ||
/// This prepares the transition to something safer (shared_ptr). | ||
/// \param cost cost function. | ||
explicit Problem (const function_t& cost); | ||
explicit Problem (const function_t& cost) ROBOPTIM_CORE_DEPRECATED; | ||
|
||
/// \pre costfunction \f$\mathbb{R}^n \rightarrow \mathbb{R}\f$ | ||
/// \brief Constructor taking a shared_ptr to a cost function. | ||
/// \param cost cost function. | ||
explicit Problem (const boost::shared_ptr<function_t>& cost); | ||
|
||
/// \brief Copy constructor. | ||
/// \param pb problem to copy. | ||
|
@@ -285,11 +294,25 @@ namespace roboptim | |
/// \return output stream | ||
std::ostream& print (std::ostream& o) const; | ||
|
||
private: | ||
/// \brief Custom deleter that does not delete anything. | ||
/// This can be used when creating a shared_ptr from a reference, although | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
bchretien
Member
|
||
/// this should be used with **great** care... | ||
struct NoopDeleter | ||
{ | ||
inline void operator() (function_t*) const {} | ||
}; | ||
|
||
private: | ||
/// \brief Objective function. | ||
const function_t& function_; | ||
/// Note: do not give access to this shared_ptr, since for now the legacy | ||
/// API relying on a simple reference prevents any proper memory | ||
/// management. | ||
const boost::shared_ptr<function_t> function_; | ||
|
||
/// \brief Starting point. | ||
startingPoint_t startingPoint_; | ||
|
||
/// \brief Vector of constraints. | ||
constraints_t constraints_; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,7 +38,10 @@ namespace roboptim | |
// | ||
template <typename T> | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
bchretien
Member
|
||
Problem<T>::Problem (const function_t& f) | ||
: function_ (f), | ||
// Note: kids, don't do this at home! Until now, we kept a reference to | ||
// the function passed, which is just as bad. This prepares the transition | ||
// to the safer shared_ptr version. | ||
: function_ (const_cast<function_t*> (&f), NoopDeleter ()), | ||
startingPoint_ (), | ||
constraints_ (), | ||
boundsVect_ (), | ||
|
@@ -54,6 +57,24 @@ namespace roboptim | |
std::fill (argumentScaling_.begin (), argumentScaling_.end (), 1.); | ||
} | ||
|
||
template <typename T> | ||
Problem<T>::Problem (const boost::shared_ptr<function_t>& f) | ||
: function_ (f), | ||
startingPoint_ (), | ||
constraints_ (), | ||
boundsVect_ (), | ||
argumentBounds_ (static_cast<std::size_t> (f->inputSize ())), | ||
scalingVect_ (), | ||
argumentScaling_ (static_cast<std::size_t> (f->inputSize ())), | ||
argumentNames_ () | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
{ | ||
// Initialize bound. | ||
std::fill (argumentBounds_.begin (), argumentBounds_.end (), | ||
function_t::makeInfiniteInterval ()); | ||
// Initialize scaling. | ||
This comment has been minimized.
Sorry, something went wrong.
thomas-moulard
Member
|
||
std::fill (argumentScaling_.begin (), argumentScaling_.end (), 1.); | ||
} | ||
|
||
template <typename T> | ||
Problem<T>::~Problem () | ||
{ | ||
|
@@ -77,7 +98,7 @@ namespace roboptim | |
const typename Problem<T>::function_t& | ||
Problem<T>::function () const | ||
{ | ||
return function_; | ||
return *function_; | ||
} | ||
|
||
template <typename T> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,7 +47,9 @@ BOOST_AUTO_TEST_CASE_TEMPLATE (problem, T, functionTypes_t) | |
typename constantFunction_t::vector_t v (2); | ||
v.setZero (); | ||
|
||
constantFunction_t f (v); | ||
// For shared_ptr constructor | ||
boost::shared_ptr<constantFunction_t> | ||
f = boost::make_shared<constantFunction_t> (v); | ||
|
||
problem_t pb_fail (f); | ||
|
||
|
@@ -114,6 +116,11 @@ BOOST_AUTO_TEST_CASE_TEMPLATE (problem, T, functionTypes_t) | |
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" | ||
This comment has been minimized.
Sorry, something went wrong.
thomas-moulard
Member
|
||
BOOST_CHECK (pb.argumentScales () == pb.argumentScaling ()); | ||
BOOST_CHECK (pb.scalesVector () == pb.scalingVector ()); | ||
|
||
// For legacy const ref constructor | ||
constantFunction_t fRef (v); | ||
problem_t pbRef (fRef); | ||
(*output) << pbRef << std::endl; | ||
#pragma GCC diagnostic pop | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
// Test a problem with multiple types of constraints. | ||
|
Remove duplicated comment?