Skip to content

Commit 2b9591f

Browse files
authored
Replace boost features with C++11 equivalents (#58)
* Replace boost features with C++11 equivalents Signed-off-by: Jacob Perron <jacob@openrobotics.org> * Include <cmath> in util.h Needed for std::abs Signed-off-by: Jacob Perron <jacob@openrobotics.org>
1 parent 850b011 commit 2b9591f

17 files changed

Lines changed: 95 additions & 87 deletions

include/create/create.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ POSSIBILITY OF SUCH DAMAGE.
3232
#ifndef CREATE_H
3333
#define CREATE_H
3434

35-
#include <boost/shared_ptr.hpp>
3635
#include <boost/numeric/ublas/matrix.hpp>
3736
#include <chrono>
37+
#include <memory>
3838
#include <string>
3939
#include <unistd.h>
4040

@@ -96,8 +96,8 @@ namespace create {
9696
bool updateLEDs();
9797

9898
protected:
99-
boost::shared_ptr<create::Data> data;
100-
boost::shared_ptr<create::Serial> serial;
99+
std::shared_ptr<create::Data> data;
100+
std::shared_ptr<create::Serial> serial;
101101

102102
public:
103103
/**

include/create/data.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ POSSIBILITY OF SUCH DAMAGE.
3232
#ifndef CREATE_DATA_H
3333
#define CREATE_DATA_H
3434

35-
#include <boost/shared_ptr.hpp>
36-
#include <boost/make_shared.hpp>
3735
#include <map>
36+
#include <memory>
3837
#include <vector>
3938

4039
#include "create/packet.h"
@@ -43,7 +42,7 @@ POSSIBILITY OF SUCH DAMAGE.
4342
namespace create {
4443
class Data {
4544
private:
46-
std::map<uint8_t, boost::shared_ptr<Packet> > packets;
45+
std::map<uint8_t, std::shared_ptr<Packet> > packets;
4746
uint32_t totalDataBytes;
4847
std::vector<uint8_t> ids;
4948

@@ -52,7 +51,7 @@ namespace create {
5251
~Data();
5352

5453
bool isValidPacketID(const uint8_t id) const;
55-
boost::shared_ptr<Packet> getPacket(const uint8_t id);
54+
std::shared_ptr<Packet> getPacket(const uint8_t id);
5655
void validateAll();
5756
uint32_t getTotalDataBytes() const;
5857
uint8_t getNumPackets() const;

include/create/packet.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ POSSIBILITY OF SUCH DAMAGE.
3131
#ifndef CREATE_PACKET_H
3232
#define CREATE_PACKET_H
3333

34-
#include <boost/thread/mutex.hpp>
34+
#include <mutex>
3535

3636
namespace create {
3737
class Packet {
3838
private:
3939
uint16_t data;
4040
uint16_t tmpData;
41-
mutable boost::mutex dataMutex;
42-
mutable boost::mutex tmpDataMutex;
41+
mutable std::mutex dataMutex;
42+
mutable std::mutex tmpDataMutex;
4343

4444
protected:
4545
// Thread safe

include/create/serial.h

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,30 @@ POSSIBILITY OF SUCH DAMAGE.
3535
#ifndef CREATE_SERIAL_H
3636
#define CREATE_SERIAL_H
3737

38+
#include <condition_variable>
39+
#include <functional>
40+
#include <memory>
41+
#include <mutex>
42+
#include <thread>
43+
3844
#include <boost/asio.hpp>
39-
#include <boost/thread.hpp>
40-
#include <boost/thread/condition_variable.hpp>
41-
#include <boost/function.hpp>
42-
#include <boost/shared_ptr.hpp>
43-
#include <boost/enable_shared_from_this.hpp>
4445

4546
#include "create/data.h"
4647
#include "create/types.h"
4748
#include "create/util.h"
4849

4950
namespace create {
50-
class Serial : public boost::enable_shared_from_this<Serial> {
51+
class Serial : public std::enable_shared_from_this<Serial> {
5152

5253
protected:
5354
boost::asio::io_service io;
5455
boost::asio::signal_set signals;
5556
boost::asio::serial_port port;
5657

5758
private:
58-
boost::thread ioThread;
59-
boost::condition_variable dataReadyCond;
60-
boost::mutex dataReadyMut;
59+
std::thread ioThread;
60+
std::condition_variable dataReadyCond;
61+
std::mutex dataReadyMut;
6162
bool dataReady;
6263
bool isReading;
6364
bool firstRead;
@@ -66,13 +67,13 @@ namespace create {
6667
// Callback executed when data arrives from Create
6768
void onData(const boost::system::error_code& e, const std::size_t& size);
6869
// Callback to execute once data arrives
69-
boost::function<void()> callback;
70+
std::function<void()> callback;
7071
// Start and stop reading data from Create
7172
bool startReading();
7273
void stopReading();
7374

7475
protected:
75-
boost::shared_ptr<Data> data;
76+
std::shared_ptr<Data> data;
7677
// These are for possible diagnostics
7778
uint64_t corruptPackets;
7879
uint64_t totalPackets;
@@ -85,9 +86,9 @@ namespace create {
8586
void notifyDataReady();
8687

8788
public:
88-
Serial(boost::shared_ptr<Data> data);
89+
Serial(std::shared_ptr<Data> data);
8990
~Serial();
90-
bool connect(const std::string& port, const int& baud = 115200, boost::function<void()> cb = 0);
91+
bool connect(const std::string& port, const int& baud = 115200, std::function<void()> cb = 0);
9192
void disconnect();
9293
inline bool connected() const { return port.is_open(); };
9394
bool send(const uint8_t* bytes, const uint32_t numBytes);

include/create/serial_query.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,9 @@ POSSIBILITY OF SUCH DAMAGE.
3636
#ifndef CREATE_SERIAL_QUERY_H
3737
#define CREATE_SERIAL_QUERY_H
3838

39+
#include <memory>
40+
3941
#include <boost/asio.hpp>
40-
#include <boost/thread.hpp>
41-
#include <boost/thread/condition_variable.hpp>
42-
#include <boost/function.hpp>
43-
#include <boost/shared_ptr.hpp>
4442

4543
#include "create/data.h"
4644
#include "create/types.h"
@@ -69,7 +67,7 @@ namespace create {
6967
void processByte(uint8_t byteRead);
7068

7169
public:
72-
SerialQuery(boost::shared_ptr<Data> data);
70+
SerialQuery(std::shared_ptr<Data> data);
7371
};
7472
} // namespace create
7573

include/create/serial_stream.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,7 @@ POSSIBILITY OF SUCH DAMAGE.
3535
#ifndef CREATE_SERIAL_STREAM_H
3636
#define CREATE_SERIAL_STREAM_H
3737

38-
#include <boost/asio.hpp>
39-
#include <boost/thread.hpp>
40-
#include <boost/thread/condition_variable.hpp>
41-
#include <boost/function.hpp>
42-
#include <boost/shared_ptr.hpp>
38+
#include <memory>
4339

4440
#include "create/data.h"
4541
#include "create/types.h"
@@ -73,7 +69,7 @@ namespace create {
7369
void processByte(uint8_t byteRead);
7470

7571
public:
76-
SerialStream(boost::shared_ptr<Data> data, const uint8_t& header = create::util::STREAM_HEADER);
72+
SerialStream(std::shared_ptr<Data> data, const uint8_t& header = create::util::STREAM_HEADER);
7773

7874
};
7975
} // namespace create

include/create/util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ POSSIBILITY OF SUCH DAMAGE.
3232
#ifndef CREATE_UTIL_H
3333
#define CREATE_UTIL_H
3434

35+
#include <cmath>
36+
3537
#define COUT(prefix,msg) (std::cout<<prefix<<msg<<std::endl)
3638
#define CERR(prefix,msg) (std::cerr<<prefix<<msg<<std::endl)
3739

src/create.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
#include <boost/bind.hpp>
2-
#include <boost/make_shared.hpp>
31
#include <iostream>
42
#include <cmath>
53
#include <ctime>
4+
#include <memory>
65
#include <assert.h>
76

87
#include "create/create.h"
@@ -42,11 +41,11 @@ namespace create {
4241
poseCovar = Matrix(3, 3, 0.0);
4342
requestedLeftVel = 0;
4443
requestedRightVel = 0;
45-
data = boost::shared_ptr<Data>(new Data(model.getVersion()));
44+
data = std::shared_ptr<Data>(new Data(model.getVersion()));
4645
if (model.getVersion() == V_1) {
47-
serial = boost::make_shared<SerialQuery>(data);
46+
serial = std::make_shared<SerialQuery>(data);
4847
} else {
49-
serial = boost::make_shared<SerialStream>(data);
48+
serial = std::make_shared<SerialStream>(data);
5049
}
5150
}
5251

@@ -273,7 +272,7 @@ namespace create {
273272
float maxWait = 30; // seconds
274273
float retryInterval = 5; //seconds
275274
time(&start);
276-
while (!serial->connect(port, baud, boost::bind(&Create::onData, this)) && !timeout) {
275+
while (!serial->connect(port, baud, std::bind(&Create::onData, this)) && !timeout) {
277276
time(&now);
278277
if (difftime(now, start) > maxWait) {
279278
timeout = true;

src/data.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "create/data.h"
22

3-
#define ADD_PACKET(id,nbytes,info,enabledVersion) if ((enabledVersion) & version) packets[id]=boost::make_shared<Packet>(nbytes,info)
3+
#define ADD_PACKET(id,nbytes,info,enabledVersion) if ((enabledVersion) & version) packets[id]=std::make_shared<Packet>(nbytes,info)
44

55
namespace create {
66

@@ -45,7 +45,7 @@ namespace create {
4545
ADD_PACKET(ID_STASIS, 1, "stasis", V_3);
4646

4747
totalDataBytes = 0;
48-
for (std::map<uint8_t, boost::shared_ptr<Packet> >::iterator it = packets.begin();
48+
for (std::map<uint8_t, std::shared_ptr<Packet> >::iterator it = packets.begin();
4949
it != packets.end();
5050
++it) {
5151
ids.push_back(it->first);
@@ -62,15 +62,15 @@ namespace create {
6262
return false;
6363
}
6464

65-
boost::shared_ptr<Packet> Data::getPacket(uint8_t id) {
65+
std::shared_ptr<Packet> Data::getPacket(uint8_t id) {
6666
if (isValidPacketID(id)) {
6767
return packets[id];
6868
}
69-
return boost::shared_ptr<Packet>();
69+
return std::shared_ptr<Packet>();
7070
}
7171

7272
void Data::validateAll() {
73-
for (std::map<uint8_t, boost::shared_ptr<Packet> >::iterator it = packets.begin();
73+
for (std::map<uint8_t, std::shared_ptr<Packet> >::iterator it = packets.begin();
7474
it != packets.end();
7575
++it) {
7676
it->second->validate();

src/packet.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <memory>
2+
13
#include "create/packet.h"
24

35
namespace create {
@@ -11,22 +13,22 @@ namespace create {
1113
Packet::~Packet() { }
1214

1315
void Packet::setDataToValidate(const uint16_t& tmp) {
14-
boost::mutex::scoped_lock lock(tmpDataMutex);
16+
std::lock_guard<std::mutex> lock(tmpDataMutex);
1517
tmpData = tmp;
1618
}
1719

1820
void Packet::validate() {
19-
boost::mutex::scoped_lock lock(tmpDataMutex);
21+
std::lock_guard<std::mutex> lock(tmpDataMutex);
2022
setData(tmpData);
2123
}
2224

2325
void Packet::setData(const uint16_t& d) {
24-
boost::mutex::scoped_lock lock(dataMutex);
26+
std::lock_guard<std::mutex> lock(dataMutex);
2527
data = d;
2628
}
2729

2830
uint16_t Packet::getData() const {
29-
boost::mutex::scoped_lock lock(dataMutex);
31+
std::lock_guard<std::mutex> lock(dataMutex);
3032
return data;
3133
}
3234

0 commit comments

Comments
 (0)