Skip to content

Commit 4df2ec3

Browse files
author
Tan Li Boon
committed
Reduce amount of stringstream misuse
1 parent 4a03a6b commit 4df2ec3

3 files changed

Lines changed: 22 additions & 42 deletions

File tree

src/binpickingtask.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,9 +1771,7 @@ void BinPickingTaskResource::_HeartbeatMonitorThread(const double reinitializeti
17711771
socket->set(zmq::sockopt::tcp_keepalive_idle, 2); // the interval between the last data packet sent (simple ACKs are not considered data) and the first keepalive probe; after the connection is marked to need keepalive, this counter is not used any further
17721772
socket->set(zmq::sockopt::tcp_keepalive_intvl, 2); // the interval between subsequential keepalive probes, regardless of what the connection has exchanged in the meantime
17731773
socket->set(zmq::sockopt::tcp_keepalive_cnt, 2); // the number of unacknowledged probes to send before considering the connection dead and notifying the application layer
1774-
std::stringstream ss; ss << std::setprecision(std::numeric_limits<double>::digits10+1);
1775-
ss << _heartbeatPort;
1776-
socket->connect("tcp://"+ _mujinControllerIp+":"+ss.str());
1774+
socket->connect("tcp://" + _mujinControllerIp + ":" + std::to_string(_heartbeatPort));
17771775
socket->set(zmq::sockopt::subscribe, "");
17781776

17791777
zmq::pollitem_t pollitem;
@@ -1882,8 +1880,7 @@ std::string GetValueForSmallestSlaveRequestId(const std::string& heartbeat, cons
18821880
{
18831881

18841882
rapidjson::Document pt(rapidjson::kObjectType);
1885-
std::stringstream ss(heartbeat);
1886-
ParseJson(pt, ss.str());
1883+
ParseJson(pt, heartbeat);
18871884
try {
18881885
const std::string slavereqid = FindSmallestSlaveRequestId(pt);
18891886
std::string result;
@@ -1905,8 +1902,7 @@ std::string mujinclient::utils::GetScenePkFromHeatbeat(const std::string& heartb
19051902

19061903
std::string utils::GetSlaveRequestIdFromHeatbeat(const std::string& heartbeat) {
19071904
rapidjson::Document pt;
1908-
std::stringstream ss(heartbeat);
1909-
ParseJson(pt, ss.str());
1905+
ParseJson(pt, heartbeat);
19101906
try {
19111907
static const std::string prefix("slaverequestid-");
19121908
return FindSmallestSlaveRequestId(pt).substr(prefix.length());

src/binpickingtaskzmq.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,7 @@ void BinPickingTaskZmqResource::_HeartbeatMonitorThread(const double reinitializ
271271
socket->set(zmq::sockopt::tcp_keepalive_idle, 2); // the interval between the last data packet sent (simple ACKs are not considered data) and the first keepalive probe; after the connection is marked to need keepalive, this counter is not used any further
272272
socket->set(zmq::sockopt::tcp_keepalive_intvl, 2); // the interval between subsequential keepalive probes, regardless of what the connection has exchanged in the meantime
273273
socket->set(zmq::sockopt::tcp_keepalive_cnt, 2); // the number of unacknowledged probes to send before considering the connection dead and notifying the application layer
274-
std::stringstream ss; ss << std::setprecision(std::numeric_limits<double>::digits10+1);
275-
ss << _heartbeatPort;
276-
socket->connect("tcp://"+ _mujinControllerIp+":"+ss.str());
274+
socket->connect("tcp://" + _mujinControllerIp + ":" + std::to_string(_heartbeatPort));
277275
socket->set(zmq::sockopt::subscribe, "");
278276

279277
zmq::pollitem_t pollitem;
@@ -288,8 +286,7 @@ void BinPickingTaskZmqResource::_HeartbeatMonitorThread(const double reinitializ
288286
socket->recv(reply);
289287
rapidjson::Document pt(rapidjson::kObjectType);
290288
try{
291-
std::stringstream replystring_ss(reply.to_string());
292-
ParseJson(pt, replystring_ss.str());
289+
ParseJson(pt, reply.to_string());
293290
heartbeat.Parse(pt);
294291
{
295292
boost::mutex::scoped_lock lock(_mutexTaskState);

src/mujinzmq.cpp

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,7 @@ void ZmqSubscriber::_InitializeSocket(boost::shared_ptr<zmq::context_t> context)
157157
_socket->set(zmq::sockopt::tcp_keepalive_cnt, 2); // the number of unacknowledged probes to send before considering the connection dead and notifying the application layer
158158
_socket->set(zmq::sockopt::sndhwm, 2);
159159
_socket->set(zmq::sockopt::linger, 100); // ms
160-
std::ostringstream port_stream;
161-
port_stream << "tcp://" << _host << ':' << _port;
162-
_socket->connect(port_stream.str());
160+
_socket->connect("tcp://" + _host + ':' + std::to_string(_port));
163161
_socket->set(zmq::sockopt::subscribe, "");
164162
}
165163

@@ -209,9 +207,7 @@ void ZmqPublisher::_InitializeSocket(boost::shared_ptr<zmq::context_t> context)
209207
_socket->set(zmq::sockopt::tcp_keepalive_cnt, 2); // the number of unacknowledged probes to send before considering the connection dead and notifying the application layer
210208
_socket->set(zmq::sockopt::sndhwm, 2);
211209
_socket->set(zmq::sockopt::linger, 100); // ms
212-
std::ostringstream port_stream;
213-
port_stream << "tcp://*:" << _port;
214-
_socket->bind(port_stream.str());
210+
_socket->bind("tcp://*:" + std::to_string(_port));
215211
}
216212

217213
void ZmqPublisher::_DestroySocket()
@@ -277,10 +273,9 @@ std::string ZmqClient::Call(const std::string& msg, const double timeout, const
277273
_InitializeSocket(_context);
278274
recreatedonce = true;
279275
} else{
280-
std::stringstream ss;
281-
ss << "Failed to send request after re-creating socket.";
282-
MUJIN_LOG_ERROR(ss.str());
283-
throw MujinException(ss.str(), MEC_Failed);
276+
std::string ss = "Failed to send request after re-creating socket.";
277+
MUJIN_LOG_ERROR(ss);
278+
throw MujinException(ss, MEC_Failed);
284279
}
285280
}
286281
if( !!_preemptfn ) {
@@ -289,15 +284,14 @@ std::string ZmqClient::Call(const std::string& msg, const double timeout, const
289284

290285
}
291286
if (GetMilliTime() - starttime > timeout*1000.0) {
292-
std::stringstream ss;
293-
ss << "Timed out trying to send request.";
294-
MUJIN_LOG_ERROR(ss.str());
287+
std::string ss = "Timed out trying to send request.";
288+
MUJIN_LOG_ERROR(ss);
295289
if (msg.length() > 1000) {
296290
MUJIN_LOG_INFO(msg.substr(0,1000) << "...");
297291
} else {
298292
MUJIN_LOG_INFO(msg);
299293
}
300-
throw MujinException(ss.str(), MEC_Timeout);
294+
throw MujinException(ss, MEC_Timeout);
301295
}
302296
//recv
303297
recreatedonce = false;
@@ -377,15 +371,14 @@ std::string ZmqClient::Call(const std::string& msg, const double timeout, const
377371
}
378372
}
379373
if (GetMilliTime() - starttime > timeout*1000.0) {
380-
std::stringstream ss;
381-
ss << "timed out trying to receive request";
382-
MUJIN_LOG_ERROR(ss.str());
374+
std::string ss = "timed out trying to receive request";
375+
MUJIN_LOG_ERROR(ss);
383376
if (msg.length() > 1000) {
384377
MUJIN_LOG_INFO(msg.substr(0,1000) << "...");
385378
} else {
386379
MUJIN_LOG_INFO(msg);
387380
}
388-
throw MujinException(ss.str(), MEC_Failed);
381+
throw MujinException(ss, MEC_Failed);
389382
}
390383

391384
return "";
@@ -405,12 +398,9 @@ void ZmqClient::_InitializeSocket(boost::shared_ptr<zmq::context_t> context)
405398
_socket->set(zmq::sockopt::tcp_keepalive_idle, 2); // the interval between the last data packet sent (simple ACKs are not considered data) and the first keepalive probe; after the connection is marked to need keepalive, this counter is not used any further
406399
_socket->set(zmq::sockopt::tcp_keepalive_intvl, 2); // the interval between subsequential keepalive probes, regardless of what the connection has exchanged in the meantime
407400
_socket->set(zmq::sockopt::tcp_keepalive_cnt, 2); // the number of unacknowledged probes to send before considering the connection dead and notifying the application layer
408-
std::ostringstream port_stream;
409-
port_stream << "tcp://" << _host << ':' << _port;
410-
std::stringstream ss;
411-
ss << "connecting to socket at " << _host << ":" << _port;
412-
MUJIN_LOG_INFO(ss.str());
413-
_socket->connect(port_stream.str());
401+
std::string endpoint = "tcp://" + _host + ':' + std::to_string(_port);
402+
MUJIN_LOG_INFO("connecting to socket at " + endpoint);
403+
_socket->connect(endpoint);
414404
}
415405

416406
void ZmqClient::_DestroySocket()
@@ -481,12 +471,9 @@ void ZmqServer::_InitializeSocket(boost::shared_ptr<zmq::context_t> context)
481471
_pollitem.socket = _socket->operator void*();
482472
_pollitem.events = ZMQ_POLLIN;
483473

484-
std::ostringstream endpoint;
485-
endpoint << "tcp://*:" << _port;
486-
_socket->bind(endpoint.str());
487-
std::stringstream ss;
488-
ss << "binded to " << endpoint.str();
489-
MUJIN_LOG_INFO(ss.str());
474+
std::string endpoint = "tcp://*:" + std::to_string(_port);
475+
_socket->bind(endpoint);
476+
MUJIN_LOG_INFO("binded to " + endpoint);
490477
}
491478

492479
void ZmqServer::_DestroySocket()

0 commit comments

Comments
 (0)