Skip to content

Commit

Permalink
Add std::make_unique<T>(...) that was omitted from C++11
Browse files Browse the repository at this point in the history
This function can be replace by the standard std::make_unique<T>(...) when we migrate to using C++14.
  • Loading branch information
jslee02 committed Mar 20, 2016
1 parent 786d79c commit c5541ee
Show file tree
Hide file tree
Showing 17 changed files with 55 additions and 48 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Expand Up @@ -368,9 +368,9 @@ endif()

# BulletCollision
if(UNIX)
pkg_check_modules(BULLET bullet>=2.82 QUIET)
pkg_check_modules(BULLET bullet>=2.81 QUIET)
if(NOT BULLET_FOUND)
pkg_check_modules(BULLET bullet2.82>=2.82 QUIET)
pkg_check_modules(BULLET bullet2.81>=2.81 QUIET)
endif()
else()
find_package(Bullet COMPONENTS BulletMath BulletCollision QUIET)
Expand Down
3 changes: 1 addition & 2 deletions apps/rigidShapes/Main.cpp
Expand Up @@ -55,8 +55,7 @@ int main(int argc, char* argv[])
assert(myWorld != NULL);
#ifndef FCL_DART5
myWorld->getConstraintSolver()->setCollisionDetector(
std::unique_ptr<dart::collision::CollisionDetector>(
new dart::collision::FCLMeshCollisionDetector()));
std::make_unique<dart::collision::FCLMeshCollisionDetector>());
#endif
// create a window and link it to the world
MyWindow window;
Expand Down
17 changes: 17 additions & 0 deletions dart/common/StlHelpers.h
Expand Up @@ -41,9 +41,26 @@
#include <cstddef>
#include <vector>

namespace std {

//==============================================================================
template<typename T, typename... Args>
unique_ptr<T> make_unique(Args&&... args)
{
return unique_ptr<T>(new T(forward<Args>(args)...));
}
// TODO(JS): This is a stopgap solution as it was omitted from C++11 as "partly
// an oversight". This can be replaced by std::make_unique<T> of the standard
// library when we migrate to using C++14.

} // namespace std



namespace dart {
namespace common {

//==============================================================================
template <typename T>
static T getVectorObjectIfAvailable(size_t index, const std::vector<T>& vec)
{
Expand Down
5 changes: 3 additions & 2 deletions dart/common/detail/AddonWithVersion.h
Expand Up @@ -38,6 +38,7 @@
#define DART_COMMON_DETAIL_ADDONWITHVERSION_H_

#include "dart/common/Addon.h"
#include "dart/common/StlHelpers.h"

namespace dart {
namespace common {
Expand Down Expand Up @@ -236,7 +237,7 @@ std::unique_ptr<Addon>
AddonWithState<BaseT, DerivedT, StateData, ManagerT, updateState>::
cloneAddon(AddonManager* newManager) const
{
return std::unique_ptr<Derived>(new Derived(newManager, mState));
return std::make_unique<Derived>(newManager, mState);
}

//==============================================================================
Expand Down Expand Up @@ -315,7 +316,7 @@ AddonWithVersionedProperties<BaseT, DerivedT, PropertiesData,
ManagerT, updateProperties>::
cloneAddon(AddonManager* newManager) const
{
return std::unique_ptr<Derived>(new Derived(newManager, mProperties));
return std::make_unique<Derived>(newManager, mProperties);
}

//==============================================================================
Expand Down
6 changes: 3 additions & 3 deletions dart/common/detail/Extensible.h
Expand Up @@ -38,6 +38,7 @@
#define DART_COMMON_DETAIL_EXTENSIBLE_H_

#include "dart/common/Extensible.h"
#include "dart/common/StlHelpers.h"

namespace dart {
namespace common {
Expand Down Expand Up @@ -129,7 +130,7 @@ ExtensibleMixer<T, Mixin>& ExtensibleMixer<T, Mixin>::operator=(
template <class T, class Mixin>
std::unique_ptr<T> ExtensibleMixer<T, Mixin>::clone() const
{
return std::unique_ptr<T>(new ExtensibleMixer<T, Mixin>(*this));
return std::make_unique<ExtensibleMixer<T, Mixin>>(*this);
}

//==============================================================================
Expand Down Expand Up @@ -289,8 +290,7 @@ std::unique_ptr< ExtensibleVector<T> > ExtensibleVector<T>::clone() const
for(const T& entry : mVector)
clonedVector.push_back(entry->clone());

return std::unique_ptr< ExtensibleVector<T> >(
new ExtensibleVector<T>(std::move(clonedVector)) );
return std::make_unique< ExtensibleVector<T> >( std::move(clonedVector) );
}

//==============================================================================
Expand Down
4 changes: 2 additions & 2 deletions dart/dynamics/BodyNode.cpp
Expand Up @@ -286,8 +286,8 @@ BodyNode::NodeProperties BodyNode::getAttachedNodeProperties() const
vec.push_back(std::move(prop));
}

nodeProperties[entry.first] = std::unique_ptr<NodePropertiesVector>(
new NodePropertiesVector(std::move(vec)));
nodeProperties[entry.first] = std::make_unique<NodePropertiesVector>(
std::move(vec));
}

return nodeProperties;
Expand Down
6 changes: 2 additions & 4 deletions dart/dynamics/EndEffector.cpp
Expand Up @@ -187,8 +187,7 @@ void EndEffector::setNodeState(const Node::State& otherState)
//==============================================================================
std::unique_ptr<Node::State> EndEffector::getNodeState() const
{
return std::unique_ptr<EndEffector::State>(
new EndEffector::State(getEndEffectorState()));
return std::make_unique<EndEffector::State>(getEndEffectorState());
}

//==============================================================================
Expand Down Expand Up @@ -217,8 +216,7 @@ void EndEffector::setNodeProperties(const Node::Properties& otherProperties)
//==============================================================================
std::unique_ptr<Node::Properties> EndEffector::getNodeProperties() const
{
return std::unique_ptr<EndEffector::Properties>(
new EndEffector::Properties(getEndEffectorProperties()));
return std::make_unique<EndEffector::Properties>(getEndEffectorProperties());
}

//==============================================================================
Expand Down
11 changes: 5 additions & 6 deletions dart/dynamics/InverseKinematics.cpp
Expand Up @@ -432,8 +432,8 @@ InverseKinematics::TaskSpaceRegion::TaskSpaceRegion(
std::unique_ptr<InverseKinematics::ErrorMethod>
InverseKinematics::TaskSpaceRegion::clone(InverseKinematics* _newIK) const
{
return std::unique_ptr<ErrorMethod>(
new TaskSpaceRegion(_newIK, getTaskSpaceRegionProperties()));
return std::make_unique<TaskSpaceRegion>(
_newIK, getTaskSpaceRegionProperties());
}

//==============================================================================
Expand Down Expand Up @@ -743,8 +743,7 @@ InverseKinematics::JacobianDLS::JacobianDLS(
std::unique_ptr<InverseKinematics::GradientMethod>
InverseKinematics::JacobianDLS::clone(InverseKinematics* _newIK) const
{
return std::unique_ptr<GradientMethod>(
new JacobianDLS(_newIK, getJacobianDLSProperties()));
return std::make_unique<JacobianDLS>(_newIK, getJacobianDLSProperties());
}

//==============================================================================
Expand Down Expand Up @@ -802,8 +801,8 @@ InverseKinematics::JacobianTranspose::JacobianTranspose(
std::unique_ptr<InverseKinematics::GradientMethod>
InverseKinematics::JacobianTranspose::clone(InverseKinematics* _newIK) const
{
return std::unique_ptr<GradientMethod>(
new JacobianTranspose(_newIK, getGradientMethodProperties()));
return std::make_unique<JacobianTranspose>(
_newIK, getGradientMethodProperties());
}

//==============================================================================
Expand Down
3 changes: 2 additions & 1 deletion dart/optimizer/nlopt/NloptSolver.cpp
Expand Up @@ -41,6 +41,7 @@
#include <Eigen/Dense>

#include "dart/common/Console.h"
#include "dart/common/StlHelpers.h"
#include "dart/optimizer/Problem.h"
#include "dart/optimizer/Function.h"

Expand Down Expand Up @@ -100,7 +101,7 @@ bool NloptSolver::solve()
|| mOpt->get_dimension() != dimension
|| mOpt->get_algorithm() != mAlg)
{
mOpt = std::unique_ptr<nlopt::opt>(new nlopt::opt(mAlg, dimension));
mOpt = std::make_unique<nlopt::opt>(mAlg, dimension);
}
else
{
Expand Down
8 changes: 2 additions & 6 deletions osgDart/InteractiveFrame.cpp
Expand Up @@ -111,9 +111,7 @@ const InteractiveFrame* InteractiveTool::getInteractiveFrame() const
//==============================================================================
dart::dynamics::SimpleFrame* InteractiveTool::addShapeFrame(const dart::dynamics::ShapePtr& shape)
{
mSimpleFrames.push_back(
std::unique_ptr<dart::dynamics::SimpleFrame>(
new dart::dynamics::SimpleFrame(this)));
mSimpleFrames.push_back(std::make_unique<dart::dynamics::SimpleFrame>(this));

auto shapeFrame = mSimpleFrames.back().get();
shapeFrame->setShape(shape);
Expand Down Expand Up @@ -226,9 +224,7 @@ const InteractiveTool* InteractiveFrame::getTool(
dart::dynamics::SimpleFrame* InteractiveFrame::addShapeFrame(
const dart::dynamics::ShapePtr& shape)
{
mSimpleFrames.push_back(
std::unique_ptr<dart::dynamics::SimpleFrame>(
new dart::dynamics::SimpleFrame(this)));
mSimpleFrames.push_back(std::make_unique<dart::dynamics::SimpleFrame>(this));

auto shapeFrame = mSimpleFrames.back().get();
shapeFrame->setShape(shape);
Expand Down
4 changes: 2 additions & 2 deletions osgDart/examples/osgHuboPuppet.cpp
Expand Up @@ -189,7 +189,7 @@ class HuboArmIK : public InverseKinematics::Analytical

std::unique_ptr<GradientMethod> clone(InverseKinematics* _newIK) const override
{
return std::unique_ptr<GradientMethod>(new HuboArmIK(_newIK, mBaseLinkName, getAnalyticalProperties()));
return std::make_unique<HuboArmIK>(_newIK, mBaseLinkName, getAnalyticalProperties());
}

const std::vector<Solution>& computeSolutions(
Expand Down Expand Up @@ -478,7 +478,7 @@ class HuboLegIK : public InverseKinematics::Analytical

std::unique_ptr<GradientMethod> clone(InverseKinematics* _newIK) const override
{
return std::unique_ptr<GradientMethod>(new HuboLegIK(_newIK, mBaseLinkName, getAnalyticalProperties()));
return std::make_unique<HuboLegIK>(_newIK, mBaseLinkName, getAnalyticalProperties());
}

const std::vector<Solution>& computeSolutions(
Expand Down
6 changes: 2 additions & 4 deletions tutorials/tutorialBiped-Finished.cpp
Expand Up @@ -223,8 +223,7 @@ class MyWindow : public SimWindow
{
setWorld(world);

mController = std::unique_ptr<Controller>
(new Controller(mWorld->getSkeleton("biped")));
mController = std::make_unique<Controller>(mWorld->getSkeleton("biped"));
}

/// Handle keyboard input
Expand Down Expand Up @@ -473,8 +472,7 @@ int main(int argc, char* argv[])

#ifdef HAVE_BULLET_COLLISION
world->getConstraintSolver()->setCollisionDetector(
std::unique_ptr<dart::collision::BulletCollisionDetector>(
new dart::collision::BulletCollisionDetector()));
std::make_unique<dart::collision::BulletCollisionDetector>());
#endif

world->addSkeleton(floor);
Expand Down
6 changes: 2 additions & 4 deletions tutorials/tutorialBiped.cpp
Expand Up @@ -157,8 +157,7 @@ class MyWindow : public SimWindow
{
setWorld(world);

mController = std::unique_ptr<Controller>
(new Controller(mWorld->getSkeleton("biped")));
mController = std::make_unique<Controller>(mWorld->getSkeleton("biped"));
}

/// Handle keyboard input
Expand Down Expand Up @@ -311,8 +310,7 @@ int main(int argc, char* argv[])

#ifdef HAVE_BULLET_COLLISION
world->getConstraintSolver()->setCollisionDetector(
std::unique_ptr<dart::collision::BulletCollisionDetector>(
new dart::collision::BulletCollisionDetector()));
std::make_unique<dart::collision::BulletCollisionDetector>());
#endif

world->addSkeleton(floor);
Expand Down
4 changes: 2 additions & 2 deletions tutorials/tutorialDominoes-Finished.cpp
Expand Up @@ -235,8 +235,8 @@ class MyWindow : public dart::gui::SimWindow
mFirstDomino = world->getSkeleton("domino");
mFloor = world->getSkeleton("floor");

mController = std::unique_ptr<Controller>(
new Controller(world->getSkeleton("manipulator"), mFirstDomino));
mController = std::make_unique<Controller>(
world->getSkeleton("manipulator"), mFirstDomino);
}

// Attempt to create a new domino. If the new domino would be in collision
Expand Down
4 changes: 2 additions & 2 deletions tutorials/tutorialDominoes.cpp
Expand Up @@ -149,8 +149,8 @@ class MyWindow : public dart::gui::SimWindow
mFirstDomino = world->getSkeleton("domino");
mFloor = world->getSkeleton("floor");

mController = std::unique_ptr<Controller>(
new Controller(world->getSkeleton("manipulator"), mFirstDomino));
mController = std::make_unique<Controller>(
world->getSkeleton("manipulator"), mFirstDomino);
}

// Attempt to create a new domino. If the new domino would be in collision
Expand Down
10 changes: 5 additions & 5 deletions unittests/testAddon.cpp
Expand Up @@ -84,7 +84,7 @@ class GenericAddon : public Addon, public Subject

std::unique_ptr<Addon> cloneAddon(AddonManager* newManager) const override final
{
return std::unique_ptr<GenericAddon>(new GenericAddon(newManager));
return std::make_unique<GenericAddon>(newManager);
}

};
Expand All @@ -101,7 +101,7 @@ class SpecializedAddon : public Addon, public Subject

std::unique_ptr<Addon> cloneAddon(AddonManager* newManager) const override final
{
return std::unique_ptr<SpecializedAddon>(new SpecializedAddon(newManager));
return std::make_unique<SpecializedAddon>(newManager);
}
};

Expand All @@ -124,7 +124,7 @@ class StatefulAddon : public Addon, public Subject

std::unique_ptr<Addon::State> clone() const override
{
return std::unique_ptr<Addon::State>(new State(*this));
return std::make_unique<State>(*this);
}

void copy(const Addon::State& anotherState) override
Expand All @@ -147,7 +147,7 @@ class StatefulAddon : public Addon, public Subject

std::unique_ptr<Addon::Properties> clone() const override
{
return std::unique_ptr<Addon::Properties>(new Properties(*this));
return std::make_unique<Properties>(*this);
}

void copy(const Addon::Properties& otherProperties) override
Expand Down Expand Up @@ -184,7 +184,7 @@ class StatefulAddon : public Addon, public Subject

std::unique_ptr<Addon> cloneAddon(AddonManager* newManager) const override final
{
return std::unique_ptr<Addon>(new StatefulAddon(newManager, *this));
return std::make_unique<StatefulAddon>(newManager, *this);
}

void setAddonState(const Addon::State& otherState) override
Expand Down
2 changes: 1 addition & 1 deletion unittests/testConstraint.cpp
Expand Up @@ -126,7 +126,7 @@ void ConstraintTest::SingleContactTest(const std::string& /*_fileName*/)
world->setGravity(Vector3d(0.0, -10.00, 0.0));
world->setTimeStep(0.001);
world->getConstraintSolver()->setCollisionDetector(
std::unique_ptr<CollisionDetector>(new DARTCollisionDetector()));
std::make_unique<DARTCollisionDetector>());

SkeletonPtr sphereSkel = createSphere(0.05, Vector3d(0.0, 1.0, 0.0));
BodyNode* sphere = sphereSkel->getBodyNode(0);
Expand Down

0 comments on commit c5541ee

Please sign in to comment.