Skip to content

Commit

Permalink
phase-out boost::function,bind
Browse files Browse the repository at this point in the history
- use std::function instead of boost
- use c++11 lambda instead of boost::bind in most cases
- use std::bind in few cases where this deems more readable
- use move semantics for passing of function objects
  • Loading branch information
wfjm committed Dec 16, 2018
1 parent 11e6c81 commit 1620ee3
Show file tree
Hide file tree
Showing 67 changed files with 526 additions and 533 deletions.
2 changes: 2 additions & 0 deletions doc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ The full set of tests is only run for tagged releases.
- replace boost with std
- use std::unique_ptr instead of boost::scoped_ptr
- use std::shared_ptr instead of boost
- use std::function instead of boost
- use std::bind or in most cases a lambda instead of boost::bind
- reduce usage of pointers in APIs
- add HasPort/HasVirt(); Port() and Virt() return reference
- rw11/shell.tcl: add workaround for tclreadline and `after` interference
Expand Down
9 changes: 4 additions & 5 deletions tools/src/librlink/ReventLoop.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// $Id: ReventLoop.cpp 1066 2018-11-10 11:21:53Z mueller $
// $Id: ReventLoop.cpp 1083 2018-12-15 19:19:16Z mueller $
//
// Copyright 2013-2018 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
//
Expand All @@ -13,6 +13,7 @@
//
// Revision History:
// Date Rev Version Comment
// 2018-12-15 1083 1.2.3 AddPollHandler(): use rval ref and move
// 2018-11-09 1066 1.2.2 use emplace_back
// 2017-04-07 868 1.2.1 Dump(): add detail arg
// 2015-04-04 662 1.2 BUGFIX: fix race in Stop(), add UnStop,StopPending
Expand All @@ -32,7 +33,6 @@
#include <poll.h>
#include <errno.h>

#include "boost/bind.hpp"
#include "boost/thread/locks.hpp"

#include "librtools/Rexception.hpp"
Expand Down Expand Up @@ -78,8 +78,7 @@ ReventLoop::~ReventLoop()
// by default handlers should start with:
// if (pfd.revents & (~pfd.events)) return -1;

void ReventLoop::AddPollHandler(const pollhdl_t& pollhdl,
int fd, short events)
void ReventLoop::AddPollHandler(pollhdl_t&& pollhdl, int fd, short events)
{
boost::lock_guard<boost::mutex> lock(fPollDscMutex);

Expand All @@ -91,7 +90,7 @@ void ReventLoop::AddPollHandler(const pollhdl_t& pollhdl,
}
}

fPollDsc.emplace_back(PollDsc(pollhdl,fd,events));
fPollDsc.emplace_back(move(pollhdl),fd,events);
fUpdatePoll = true;

if (fspLog && fTraceLevel >= 1) {
Expand Down
10 changes: 6 additions & 4 deletions tools/src/librlink/ReventLoop.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// $Id: ReventLoop.hpp 1078 2018-12-08 14:19:03Z mueller $
// $Id: ReventLoop.hpp 1083 2018-12-15 19:19:16Z mueller $
//
// Copyright 2013-2018 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
//
Expand All @@ -13,6 +13,8 @@
//
// Revision History:
// Date Rev Version Comment
// 2018-12-15 1083 1.2.4 AddPollHandler(): use rval ref and move
// 2018-12-14 1081 1.2.3 use std::function instead of boost
// 2018-12-07 1078 1.2.2 use std::shared_ptr instead of boost
// 2017-04-07 868 1.2.1 Dump(): add detail arg
// 2015-04-04 662 1.2 BUGFIX: fix race in Stop(), add UnStop,StopPending
Expand All @@ -35,9 +37,9 @@
#include <cstdint>
#include <vector>
#include <memory>
#include <functional>

#include "boost/utility.hpp"
#include "boost/function.hpp"
#include "boost/thread/mutex.hpp"

#include "librtools/RlogFile.hpp"
Expand All @@ -46,12 +48,12 @@ namespace Retro {

class ReventLoop : private boost::noncopyable {
public:
typedef boost::function<int(const pollfd&)> pollhdl_t;
typedef std::function<int(const pollfd&)> pollhdl_t;

ReventLoop();
virtual ~ReventLoop();

void AddPollHandler(const pollhdl_t& pollhdl,
void AddPollHandler(pollhdl_t&& pollhdl,
int fd, short events=POLLIN);
bool TestPollHandler(int fd, short events=POLLIN);
void RemovePollHandler(int fd, short events, bool nothrow=false);
Expand Down
5 changes: 3 additions & 2 deletions tools/src/librlink/RlinkPortCuff.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// $Id: RlinkPortCuff.cpp 1066 2018-11-10 11:21:53Z mueller $
// $Id: RlinkPortCuff.cpp 1081 2018-12-14 22:29:42Z mueller $
//
// Copyright 2012-2018 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
//
Expand All @@ -13,6 +13,7 @@
//
// Revision History:
// Date Rev Version Comment
// 2018-12-14 1081 1.1.8 use std::bind instead of boost
// 2018-11-09 1066 1.1.7 use auto
// 2018-10-27 1059 1.1.6 coverity fixup (uncaught exception in dtor)
// 2017-04-15 875 1.1.5 Open(): set default scheme
Expand Down Expand Up @@ -243,7 +244,7 @@ bool RlinkPortCuff::Open(const std::string& url, RerrMsg& emsg)
libusb_set_pollfd_notifiers(fpUsbContext, ThunkPollfdAdd,
ThunkPollfdRemove, this);

fDriverThread = boost::thread(boost::bind(&RlinkPortCuff::Driver, this));
fDriverThread = boost::thread(std::bind(&RlinkPortCuff::Driver, this));

fIsOpen = true;

Expand Down
28 changes: 15 additions & 13 deletions tools/src/librlink/RlinkServer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// $Id: RlinkServer.cpp 1079 2018-12-09 10:56:59Z mueller $
// $Id: RlinkServer.cpp 1083 2018-12-15 19:19:16Z mueller $
//
// Copyright 2013-2018 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
//
Expand All @@ -13,6 +13,8 @@
//
// Revision History:
// Date Rev Version Comment
// 2018-12-15 1083 2.2.7 for std::function setups: use rval ref and move
// 2018-12-14 1081 2.2.6 use std::bind instead of boost
// 2018-12-07 1078 2.2.5 use std::shared_ptr instead of boost
// 2018-10-27 1059 2.2.4 coverity fixup (uncaught exception in dtor)
// 2017-04-07 868 2.2.3 Dump(): add detail arg
Expand All @@ -34,7 +36,6 @@
*/

#include "boost/thread/locks.hpp"
#include "boost/bind.hpp"

#include "librtools/Rexception.hpp"
#include "librtools/RosFill.hpp"
Expand Down Expand Up @@ -76,7 +77,8 @@ RlinkServer::RlinkServer()
RlinkCommand::kStat_M_RbNak |
RlinkCommand::kStat_M_RbErr);

fELoop.AddPollHandler(boost::bind(&RlinkServer::WakeupHandler, this, _1),
fELoop.AddPollHandler([this](const pollfd& pfd)
{ return WakeupHandler(pfd); },
fWakeupEvent, POLLIN);

// Statistic setup
Expand Down Expand Up @@ -135,7 +137,7 @@ void RlinkServer::SetConnect(const std::shared_ptr<RlinkConnect>& spconn)
//------------------------------------------+-----------------------------------
//! FIXME_docs

void RlinkServer::AddAttnHandler(const attnhdl_t& attnhdl, uint16_t mask,
void RlinkServer::AddAttnHandler(attnhdl_t&& attnhdl, uint16_t mask,
void* cdata)
{
if (mask == 0)
Expand All @@ -150,7 +152,7 @@ void RlinkServer::AddAttnHandler(const attnhdl_t& attnhdl, uint16_t mask,
"Bad args: duplicate handler");
}
}
fAttnDsc.push_back(AttnDsc(attnhdl, id));
fAttnDsc.emplace_back(move(attnhdl), id);

return;
}
Expand Down Expand Up @@ -205,22 +207,21 @@ void RlinkServer::RemoveAttnHandler(uint16_t mask, void* cdata)
//------------------------------------------+-----------------------------------
//! FIXME_docs

void RlinkServer::QueueAction(const actnhdl_t& actnhdl)
void RlinkServer::QueueAction(actnhdl_t&& actnhdl)
{
boost::lock_guard<RlinkConnect> lock(*fspConn);
fActnList.push_back(actnhdl);
fActnList.push_back(move(actnhdl));
if (IsActiveOutside()) Wakeup();
return;
}

//------------------------------------------+-----------------------------------
//! FIXME_docs

void RlinkServer::AddPollHandler(const pollhdl_t& pollhdl,
int fd, short events)
void RlinkServer::AddPollHandler(pollhdl_t&& pollhdl, int fd, short events)
{
boost::lock_guard<RlinkConnect> lock(*fspConn);
fELoop.AddPollHandler(pollhdl, fd, events);
fELoop.AddPollHandler(move(pollhdl), fd, events);
if (IsActiveOutside()) Wakeup();
return;
}
Expand Down Expand Up @@ -415,13 +416,14 @@ void RlinkServer::StartOrResume(bool resume)
// setup poll handler for Rlink traffic
int rlinkfd = fspConn->Port().FdRead();
if (!fELoop.TestPollHandler(rlinkfd, POLLIN))
fELoop.AddPollHandler(boost::bind(&RlinkServer::RlinkHandler, this, _1),
fELoop.AddPollHandler([this](const pollfd& pfd)
{ return RlinkHandler(pfd); },
rlinkfd, POLLIN);

// and start server thread
fELoop.UnStop();
fServerThread = boost::thread(boost::bind(&RlinkServerEventLoop::EventLoop,
&fELoop));
fServerThread = boost::thread(std::bind(&RlinkServerEventLoop::EventLoop,
&fELoop));

if (resume) {
RerrMsg emsg;
Expand Down
19 changes: 11 additions & 8 deletions tools/src/librlink/RlinkServer.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// $Id: RlinkServer.hpp 1078 2018-12-08 14:19:03Z mueller $
// $Id: RlinkServer.hpp 1083 2018-12-15 19:19:16Z mueller $
//
// Copyright 2013-2018 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
//
Expand All @@ -13,6 +13,8 @@
//
// Revision History:
// Date Rev Version Comment
// 2018-12-15 1083 2.2.4 for std::function setups: use rval ref and move
// 2018-12-14 1081 2.2.3 use std::function instead of boost
// 2018-12-07 1078 2.2.2 use std::shared_ptr instead of boost
// 2017-04-07 868 2.2.1 Dump(): add detail arg
// 2015-01-10 632 2.2 Exec() without emsg now void, will throw
Expand All @@ -38,6 +40,7 @@
#include <vector>
#include <list>
#include <memory>
#include <functional>

#include "boost/utility.hpp"
#include "boost/thread/thread.hpp"
Expand All @@ -63,9 +66,9 @@ namespace Retro {
AttnArgs(uint16_t apatt, uint16_t amask);
};

typedef ReventLoop::pollhdl_t pollhdl_t;
typedef boost::function<int(AttnArgs&)> attnhdl_t;
typedef boost::function<int()> actnhdl_t;
typedef ReventLoop::pollhdl_t pollhdl_t;
typedef std::function<int(AttnArgs&)> attnhdl_t;
typedef std::function<int()> actnhdl_t;

explicit RlinkServer();
virtual ~RlinkServer();
Expand All @@ -79,15 +82,15 @@ namespace Retro {
bool Exec(RlinkCommandList& clist, RerrMsg& emsg);
void Exec(RlinkCommandList& clist);

void AddAttnHandler(const attnhdl_t& attnhdl, uint16_t mask,
void AddAttnHandler(attnhdl_t&& attnhdl, uint16_t mask,
void* cdata = nullptr);
void RemoveAttnHandler(uint16_t mask, void* cdata = nullptr);
void GetAttnInfo(AttnArgs& args, RlinkCommandList& clist);
void GetAttnInfo(AttnArgs& args);

void QueueAction(const actnhdl_t& actnhdl);
void QueueAction(actnhdl_t&& actnhdl);

void AddPollHandler(const pollhdl_t& pollhdl,
void AddPollHandler(pollhdl_t&& pollhdl,
int fd, short events=POLLIN);
bool TestPollHandler(int fd, short events=POLLIN);
void RemovePollHandler(int fd, short events, bool nothrow=false);
Expand Down Expand Up @@ -164,7 +167,7 @@ namespace Retro {
attnhdl_t fHandler;
AttnId fId;
AttnDsc();
AttnDsc(attnhdl_t hdl, const AttnId& id);
AttnDsc(attnhdl_t&& hdl, const AttnId& id);
};

std::shared_ptr<RlinkConnect> fspConn;
Expand Down
7 changes: 4 additions & 3 deletions tools/src/librlink/RlinkServer.ipp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// $Id: RlinkServer.ipp 1078 2018-12-08 14:19:03Z mueller $
// $Id: RlinkServer.ipp 1083 2018-12-15 19:19:16Z mueller $
//
// Copyright 2013-2018 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
//
Expand All @@ -13,6 +13,7 @@
//
// Revision History:
// Date Rev Version Comment
// 2018-12-15 1083 2.2.2 for std::function setups: use rval ref and move
// 2018-12-07 1078 2.2.1 use std::shared_ptr instead of boost
// 2015-01-10 632 2.2 Exec() without emsg now void, will throw
// 2014-12-30 625 2.1 adopt to Rlink V4 attn logic
Expand Down Expand Up @@ -190,8 +191,8 @@ inline RlinkServer::AttnDsc::AttnDsc()
//------------------------------------------+-----------------------------------
//! Constructor

inline RlinkServer::AttnDsc::AttnDsc(attnhdl_t hdl, const AttnId& id)
: fHandler(hdl),
inline RlinkServer::AttnDsc::AttnDsc(attnhdl_t&& hdl, const AttnId& id)
: fHandler(move(hdl)),
fId(id)
{}

Expand Down
8 changes: 4 additions & 4 deletions tools/src/librlinktpp/RtclAttnShuttle.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// $Id: RtclAttnShuttle.cpp 1059 2018-10-27 10:34:16Z mueller $
// $Id: RtclAttnShuttle.cpp 1082 2018-12-15 13:56:20Z mueller $
//
// Copyright 2013-2018 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
//
Expand All @@ -13,6 +13,7 @@
//
// Revision History:
// Date Rev Version Comment
// 2018-12-15 1082 1.1.2 use lambda instead of bind
// 2018-10-27 1059 1.1.1 coverity fixup (uncaught exception in dtor)
// 2014-12-30 625 1.1 adopt to Rlink V4 attn logic
// 2014-11-08 602 1.0.3 cast int first to ptrdiff_t, than to ClientData
Expand All @@ -29,8 +30,6 @@

#include <errno.h>

#include "boost/bind.hpp"

#include "librtools/Rexception.hpp"
#include "librtools/Rtools.hpp"

Expand Down Expand Up @@ -82,7 +81,8 @@ RtclAttnShuttle::~RtclAttnShuttle()
void RtclAttnShuttle::Add(RlinkServer* pserv, Tcl_Interp* interp)
{
// connect to RlinkServer
pserv->AddAttnHandler(boost::bind(&RtclAttnShuttle::AttnHandler, this, _1),
pserv->AddAttnHandler([this](RlinkServer::AttnArgs& args)
{ return AttnHandler(args); },
fMask, (void*)this);
fpServ = pserv;

Expand Down
Loading

0 comments on commit 1620ee3

Please sign in to comment.