Skip to content

Commit

Permalink
Replace raw pointers => smart pointers (mavlink#314)
Browse files Browse the repository at this point in the history
Smart pointers provide automatic resource management. In other words,
they implement "Resource Acquisition Is Initialialization programming idiom".
The main goal of this idiom is to ensure that resource acquisition occurs
at the same time that the object is initialized, so that all resources
for the object are created and made ready in one line of code.

`std::unique_ptr` is a smart pointer that owns and manages another object
through a pointer and disposes of that object when the unique_ptr goes out of scope.

Ref 1: https://docs.microsoft.com/en-us/cpp/cpp/smart-pointers-modern-cpp
Ref 2: http://en.cppreference.com/w/cpp/memory/unique_ptr
  • Loading branch information
Shakthi Prashanth M authored and JonasVautherin committed Mar 14, 2018
1 parent 807fc9d commit 96f91d8
Show file tree
Hide file tree
Showing 18 changed files with 33 additions and 36 deletions.
5 changes: 1 addition & 4 deletions core/dronecore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@
namespace dronecore {

DroneCore::DroneCore() :
_impl(nullptr)
_impl { new DroneCoreImpl() }
{
_impl = new DroneCoreImpl();
}

DroneCore::~DroneCore()
{
delete _impl;
_impl = nullptr;
}

ConnectionResult DroneCore::add_any_connection(const std::string &connection_url)
Expand Down
3 changes: 2 additions & 1 deletion core/dronecore.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <string>
#include <memory>
#include <vector>
#include <functional>

Expand Down Expand Up @@ -174,7 +175,7 @@ class DroneCore

private:
/* @private. */
DroneCoreImpl *_impl;
std::unique_ptr<DroneCoreImpl> _impl;

// Non-copyable
DroneCore(const DroneCore &) = delete;
Expand Down
5 changes: 2 additions & 3 deletions plugins/action/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
namespace dronecore {

Action::Action(Device &device) :
PluginBase()
PluginBase(),
_impl { new ActionImpl(device) }
{
_impl = new ActionImpl(device);
}

Action::~Action()
{
delete _impl;
}

Action::Result Action::arm() const
Expand Down
3 changes: 2 additions & 1 deletion plugins/action/action.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <functional>
#include <memory>
#include "plugin_base.h"

namespace dronecore {
Expand Down Expand Up @@ -281,7 +282,7 @@ class Action : public PluginBase

private:
/** @private Underlying implementation, set at instantiation */
ActionImpl *_impl;
std::unique_ptr<ActionImpl> _impl;
};

} // namespace dronecore
5 changes: 2 additions & 3 deletions plugins/follow_me/follow_me.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
namespace dronecore {

FollowMe::FollowMe(Device &device) :
PluginBase()
PluginBase(),
_impl { new FollowMeImpl(device) }
{
_impl = new FollowMeImpl(device);
}

FollowMe::~FollowMe()
{
delete _impl;
}

const FollowMe::Config &FollowMe::get_config() const
Expand Down
3 changes: 2 additions & 1 deletion plugins/follow_me/follow_me.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <string>
#include <memory>
#include <iostream>
#include <cmath>
#include <functional>
Expand Down Expand Up @@ -188,7 +189,7 @@ class FollowMe : public PluginBase

private:
// Underlying implementation, set at instantiation
FollowMeImpl *_impl;
std::unique_ptr<FollowMeImpl> _impl;
};

} // namespace dronecore
5 changes: 2 additions & 3 deletions plugins/gimbal/gimbal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
namespace dronecore {

Gimbal::Gimbal(Device &device) :
PluginBase()
PluginBase(),
_impl { new GimbalImpl(device) }
{
_impl = new GimbalImpl(device);
}

Gimbal::~Gimbal()
{
delete _impl;
}

Gimbal::Result Gimbal::set_pitch_and_yaw(float pitch_deg, float yaw_deg)
Expand Down
3 changes: 2 additions & 1 deletion plugins/gimbal/gimbal.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <functional>
#include <memory>
#include "plugin_base.h"

namespace dronecore {
Expand Down Expand Up @@ -95,7 +96,7 @@ class Gimbal : public PluginBase

private:
/** @private Underlying implementation, set at instantiation */
GimbalImpl *_impl;
std::unique_ptr<GimbalImpl> _impl;
};

} // namespace dronecore
5 changes: 2 additions & 3 deletions plugins/info/info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
namespace dronecore {

Info::Info(Device &device) :
PluginBase()
PluginBase(),
_impl { new InfoImpl(device) }
{
_impl = new InfoImpl(device);
}

Info::~Info()
{
delete _impl;
}

uint64_t Info::uuid() const
Expand Down
3 changes: 2 additions & 1 deletion plugins/info/info.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <cstdint>
#include <memory>
#include "plugin_base.h"

namespace dronecore {
Expand Down Expand Up @@ -108,7 +109,7 @@ class Info : public PluginBase

private:
/** @private Underlying implementation, set at instantiation */
InfoImpl *_impl;
std::unique_ptr<InfoImpl> _impl;
};

} // namespace dronecore
5 changes: 2 additions & 3 deletions plugins/logging/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
namespace dronecore {

Logging::Logging(Device &device) :
PluginBase()
PluginBase(),
_impl { new LoggingImpl(device) }
{
_impl = new LoggingImpl(device);
}

Logging::~Logging()
{
delete _impl;
}

Logging::Result Logging::start_logging() const
Expand Down
3 changes: 2 additions & 1 deletion plugins/logging/logging.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <functional>
#include <memory>
#include "plugin_base.h"

namespace dronecore {
Expand Down Expand Up @@ -108,7 +109,7 @@ class Logging : public PluginBase

private:
/** @private Underlying implementation, set at instantiation */
LoggingImpl *_impl;
std::unique_ptr<LoggingImpl> _impl;
};

} // namespace dronecore
5 changes: 2 additions & 3 deletions plugins/mission/mission.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
namespace dronecore {

Mission::Mission(Device &device) :
PluginBase()
PluginBase(),
_impl { new MissionImpl(device) }
{
_impl = new MissionImpl(device);
}

Mission::~Mission()
{
delete _impl;
}


Expand Down
2 changes: 1 addition & 1 deletion plugins/mission/mission.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class Mission : public PluginBase

private:
/** @private Underlying implementation, set at instantiation */
MissionImpl *_impl;
std::unique_ptr<MissionImpl> _impl;
};

} // namespace dronecore
3 changes: 1 addition & 2 deletions plugins/mission/mission_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
namespace dronecore {

MissionItem::MissionItem() :
_impl(new MissionItemImpl())
_impl { new MissionItemImpl() }
{
}

MissionItem::~MissionItem()
{
delete _impl;
}


Expand Down
3 changes: 2 additions & 1 deletion plugins/mission/mission_item.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include <string>
#include <memory>

namespace dronecore {

Expand Down Expand Up @@ -197,7 +198,7 @@ class MissionItem

private:
/** @private Underlying implementation, set at instantiation */
MissionItemImpl *_impl;
std::unique_ptr<MissionItemImpl> _impl;
};

} // namespace dronecore
5 changes: 2 additions & 3 deletions plugins/offboard/offboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
namespace dronecore {

Offboard::Offboard(Device &device) :
PluginBase()
PluginBase(),
_impl { new OffboardImpl(device) }
{
_impl = new OffboardImpl(device);
}

Offboard::~Offboard()
{
delete _impl;
}

Offboard::Result Offboard::start()
Expand Down
3 changes: 2 additions & 1 deletion plugins/offboard/offboard.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <functional>
#include <memory>
#include "plugin_base.h"

namespace dronecore {
Expand Down Expand Up @@ -163,7 +164,7 @@ class Offboard : public PluginBase

private:
/** @private Underlying implementation, set at instantiation */
OffboardImpl *_impl;
std::unique_ptr<OffboardImpl> _impl;
};

} // namespace dronecore

0 comments on commit 96f91d8

Please sign in to comment.