Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use STL in place of Boost #53

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion bondcpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@ catkin_package(
DEPENDS Boost
)

# ROS Melodic: http://www.ros.org/reps/rep-0003.html
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()

add_library(${PROJECT_NAME}
src/timeout.cpp
src/bond.cpp
src/BondSM_sm.cpp
src/timeout.cpp
)
target_link_libraries(${PROJECT_NAME} ${UUID_LIBRARIES} ${catkin_LIBRARIES} ${BOOST_LIBRARIES})

Expand Down
27 changes: 13 additions & 14 deletions bondcpp/include/bondcpp/bond.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@
#ifndef BONDCPP__BOND_H_
#define BONDCPP__BOND_H_

#include <boost/scoped_ptr.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition.hpp>

#include <ros/ros.h>
#include <ros/macros.h>

Expand All @@ -44,6 +40,9 @@
#include <bond/Status.h>
#include "BondSM_sm.h"

#include <condition_variable>
#include <functional>
#include <mutex>
#include <string>
#include <vector>

Expand Down Expand Up @@ -77,8 +76,8 @@ class BONDCPP_DECL Bond
* \param on_formed callback that will be called when the bond is formed.
*/
Bond(const std::string &topic, const std::string &id,
boost::function<void(void)> on_broken = boost::function<void(void)>(),
boost::function<void(void)> on_formed = boost::function<void(void)>());
std::function<void(void)> on_broken = nullptr,
std::function<void(void)> on_formed = nullptr);

/** \brief Destructs the object, breaking the bond if it is still formed.
*/
Expand All @@ -101,11 +100,11 @@ class BONDCPP_DECL Bond

/** \brief Sets the formed callback.
*/
void setFormedCallback(boost::function<void(void)> on_formed);
void setFormedCallback(const std::function<void(void)>& on_formed);

/** \brief Sets the broken callback
*/
void setBrokenCallback(boost::function<void(void)> on_broken);
void setBrokenCallback(const std::function<void(void)>& on_broken);

/** \brief Blocks until the bond is formed for at most 'duration'.
*
Expand Down Expand Up @@ -152,20 +151,20 @@ class BONDCPP_DECL Bond
friend class ::BondSM;

ros::NodeHandle nh_;
boost::scoped_ptr<BondSM> bondsm_;
std::unique_ptr<BondSM> bondsm_;
BondSMContext sm_;

std::string topic_;
std::string id_;
std::string instance_id_;
std::string sister_instance_id_;
boost::function<void(void)> on_broken_;
boost::function<void(void)> on_formed_;
std::function<void(void)> on_broken_;
std::function<void(void)> on_formed_;
bool sisterDiedFirst_;
bool started_;

boost::mutex mutex_;
boost::condition condition_;
std::mutex mutex_;
std::condition_variable condition_;

double connect_timeout_;
double heartbeat_timeout_;
Expand All @@ -189,7 +188,7 @@ class BONDCPP_DECL Bond
void doPublishing(const ros::SteadyTimerEvent &e);
void publishStatus(bool active);

std::vector<boost::function<void(void)> > pending_callbacks_;
std::vector<std::function<void(void)> > pending_callbacks_;
void flushPendingCallbacks();
};

Expand Down
12 changes: 5 additions & 7 deletions bondcpp/include/bondcpp/timeout.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,17 @@
#ifndef BONDCPP__TIMEOUT_H_
#define BONDCPP__TIMEOUT_H_

#include <functional>

#include <ros/ros.h>

namespace bond {

class Timeout
{
public:
Timeout(
const ros::Duration &d,
boost::function<void(void)> on_timeout = boost::function<void(void)>());
Timeout(
const ros::WallDuration &d,
boost::function<void(void)> on_timeout = boost::function<void(void)>());
Timeout(const ros::Duration &d, std::function<void(void)> on_timeout = nullptr);
Timeout(const ros::WallDuration &d, std::function<void(void)> on_timeout = nullptr);
~Timeout();

// Undefined what these do to a running timeout
Expand All @@ -58,7 +56,7 @@ class Timeout
ros::SteadyTimer timer_;
ros::SteadyTime deadline_;
ros::WallDuration duration_;
boost::function<void(void)> on_timeout_;
std::function<void(void)> on_timeout_;

void timerCallback(const ros::SteadyTimerEvent &e);
};
Expand Down
60 changes: 30 additions & 30 deletions bondcpp/src/bond.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
// Author: Stuart Glaser

#include <bondcpp/bond.h>
#include <boost/thread/thread_time.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>

#ifdef _WIN32
#include <Rpc.h>
Expand All @@ -40,6 +38,10 @@
#endif

#include <algorithm>
#include <chrono>
#include <condition_variable>
#include <functional>
#include <mutex>
#include <string>
#include <vector>

Expand All @@ -65,11 +67,11 @@ static std::string makeUUID()
}

Bond::Bond(const std::string &topic, const std::string &id,
boost::function<void(void)> on_broken,
boost::function<void(void)> on_formed)
std::function<void(void)> on_broken,
std::function<void(void)> on_formed)
:

bondsm_(new BondSM(this)),
bondsm_(std::make_unique<BondSM>(this)),
sm_(*bondsm_),
topic_(topic),
id_(id),
Expand All @@ -79,9 +81,9 @@ Bond::Bond(const std::string &topic, const std::string &id,
sisterDiedFirst_(false),
started_(false),

connect_timer_(ros::WallDuration(), boost::bind(&Bond::onConnectTimeout, this)),
heartbeat_timer_(ros::WallDuration(), boost::bind(&Bond::onHeartbeatTimeout, this)),
disconnect_timer_(ros::WallDuration(), boost::bind(&Bond::onDisconnectTimeout, this))
connect_timer_(ros::WallDuration(), std::bind(&Bond::onConnectTimeout, this)),
heartbeat_timer_(ros::WallDuration(), std::bind(&Bond::onHeartbeatTimeout, this)),
disconnect_timer_(ros::WallDuration(), std::bind(&Bond::onDisconnectTimeout, this))
{
setConnectTimeout(bond::Constants::DEFAULT_CONNECT_TIMEOUT);
setDisconnectTimeout(bond::Constants::DEFAULT_DISCONNECT_TIMEOUT);
Expand Down Expand Up @@ -112,7 +114,7 @@ Bond::~Bond()
heartbeat_timer_.cancel();
disconnect_timer_.cancel();

boost::mutex::scoped_lock lock(mutex_);
std::lock_guard<std::mutex> lock(mutex_);
pub_.shutdown();
}

Expand Down Expand Up @@ -173,25 +175,25 @@ void Bond::setCallbackQueue(ros::CallbackQueueInterface *queue)

void Bond::start()
{
boost::mutex::scoped_lock lock(mutex_);
std::lock_guard<std::mutex> lock(mutex_);
connect_timer_.reset();
pub_ = nh_.advertise<bond::Status>(topic_, 5);
sub_ = nh_.subscribe<bond::Status>(topic_, 30, boost::bind(&Bond::bondStatusCB, this, _1));
sub_ = nh_.subscribe<bond::Status>(topic_, 30, std::bind(&Bond::bondStatusCB, this, std::placeholders::_1));

publishingTimer_ = nh_.createSteadyTimer(
ros::WallDuration(heartbeat_period_), &Bond::doPublishing, this);
started_ = true;
}

void Bond::setFormedCallback(boost::function<void(void)> on_formed)
void Bond::setFormedCallback(const std::function<void(void)>& on_formed)
{
boost::mutex::scoped_lock lock(mutex_);
std::lock_guard<std::mutex> lock(mutex_);
on_formed_ = on_formed;
}

void Bond::setBrokenCallback(boost::function<void(void)> on_broken)
void Bond::setBrokenCallback(const std::function<void(void)>& on_broken)
{
boost::mutex::scoped_lock lock(mutex_);
std::lock_guard<std::mutex> lock(mutex_);
on_broken_ = on_broken;
}

Expand All @@ -201,7 +203,7 @@ bool Bond::waitUntilFormed(ros::Duration timeout)
}
bool Bond::waitUntilFormed(ros::WallDuration timeout)
{
boost::mutex::scoped_lock lock(mutex_);
std::unique_lock<std::mutex> lock(mutex_);
ros::SteadyTime deadline(ros::SteadyTime::now() + timeout);

while (sm_.getState().getId() == SM::WaitingForSister.getId()) {
Expand All @@ -218,8 +220,7 @@ bool Bond::waitUntilFormed(ros::WallDuration timeout)
break; // The deadline has expired
}

condition_.timed_wait(mutex_, boost::posix_time::milliseconds(
static_cast<int64_t>(wait_time.toSec() * 1000.0f)));
condition_.wait_for(lock, std::chrono::duration<double, std::ratio<1>>(wait_time.toSec()));
}
return sm_.getState().getId() != SM::WaitingForSister.getId();
}
Expand All @@ -230,7 +231,7 @@ bool Bond::waitUntilBroken(ros::Duration timeout)
}
bool Bond::waitUntilBroken(ros::WallDuration timeout)
{
boost::mutex::scoped_lock lock(mutex_);
std::unique_lock<std::mutex> lock(mutex_);
ros::SteadyTime deadline(ros::SteadyTime::now() + timeout);

while (sm_.getState().getId() != SM::Dead.getId()) {
Expand All @@ -247,22 +248,21 @@ bool Bond::waitUntilBroken(ros::WallDuration timeout)
break; // The deadline has expired
}

condition_.timed_wait(mutex_, boost::posix_time::milliseconds(
static_cast<int64_t>(wait_time.toSec() * 1000.0f)));
condition_.wait_for(lock, std::chrono::duration<double, std::ratio<1>>(wait_time.toSec()));
}
return sm_.getState().getId() == SM::Dead.getId();
}

bool Bond::isBroken()
{
boost::mutex::scoped_lock lock(mutex_);
std::lock_guard<std::mutex> lock(mutex_);
return sm_.getState().getId() == SM::Dead.getId();
}

void Bond::breakBond()
{
{
boost::mutex::scoped_lock lock(mutex_);
std::lock_guard<std::mutex> lock(mutex_);
if (sm_.getState().getId() != SM::Dead.getId()) {
sm_.Die();
publishStatus(false);
Expand All @@ -275,7 +275,7 @@ void Bond::breakBond()
void Bond::onConnectTimeout()
{
{
boost::mutex::scoped_lock lock(mutex_);
std::lock_guard<std::mutex> lock(mutex_);
sm_.ConnectTimeout();
}
flushPendingCallbacks();
Expand All @@ -292,15 +292,15 @@ void Bond::onHeartbeatTimeout()
}

{
boost::mutex::scoped_lock lock(mutex_);
std::lock_guard<std::mutex> lock(mutex_);
sm_.HeartbeatTimeout();
}
flushPendingCallbacks();
}
void Bond::onDisconnectTimeout()
{
{
boost::mutex::scoped_lock lock(mutex_);
std::lock_guard<std::mutex> lock(mutex_);
sm_.DisconnectTimeout();
}
flushPendingCallbacks();
Expand All @@ -311,7 +311,7 @@ void Bond::bondStatusCB(const bond::Status::ConstPtr &msg)
// Filters out messages from other bonds and messages from ourself
if (msg->id == id_ && msg->instance_id != instance_id_) {
{
boost::mutex::scoped_lock lock(mutex_);
std::lock_guard<std::mutex> lock(mutex_);

if (sister_instance_id_.empty()) {
sister_instance_id_ = msg->instance_id;
Expand Down Expand Up @@ -341,7 +341,7 @@ void Bond::bondStatusCB(const bond::Status::ConstPtr &msg)

void Bond::doPublishing(const ros::SteadyTimerEvent &)
{
boost::mutex::scoped_lock lock(mutex_);
std::lock_guard<std::mutex> lock(mutex_);
if (sm_.getState().getId() == SM::WaitingForSister.getId() ||
sm_.getState().getId() == SM::Alive.getId()) {
publishStatus(true);
Expand All @@ -367,9 +367,9 @@ void Bond::publishStatus(bool active)

void Bond::flushPendingCallbacks()
{
std::vector<boost::function<void(void)> > callbacks;
std::vector<std::function<void(void)> > callbacks;
{
boost::mutex::scoped_lock lock(mutex_);
std::lock_guard<std::mutex> lock(mutex_);
callbacks = pending_callbacks_;
pending_callbacks_.clear();
}
Expand Down
7 changes: 3 additions & 4 deletions bondcpp/src/timeout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,16 @@
#include "bondcpp/timeout.h"

#include <algorithm>
#include <functional>

namespace bond {

Timeout::Timeout(const ros::Duration &d,
boost::function<void(void)> on_timeout)
Timeout::Timeout(const ros::Duration &d, std::function<void(void)> on_timeout)
: duration_(d.sec, d.nsec), on_timeout_(on_timeout)
{
}

Timeout::Timeout(const ros::WallDuration &d,
boost::function<void(void)> on_timeout)
Timeout::Timeout(const ros::WallDuration &d, std::function<void(void)> on_timeout)
: duration_(d), on_timeout_(on_timeout)
{
}
Expand Down