Skip to content

Commit

Permalink
improved code by replacing const char* with const string&
Browse files Browse the repository at this point in the history
  • Loading branch information
randaz81 committed Aug 28, 2024
1 parent e0dee96 commit fa08aa3
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 80 deletions.
17 changes: 7 additions & 10 deletions src/libYARP_manager/src/yarp/manager/broker.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,13 @@ class Broker {
virtual bool start() = 0;
virtual bool stop() = 0;
virtual bool kill() = 0;
virtual bool connect(const char* from, const char* to,
const char* carrier, bool persist=false) = 0;
virtual bool disconnect(const char* from, const char* to,
const char* carrier) = 0;
virtual bool connect(const std::string& from, const std::string& to, const std::string& carrier, bool persist = false) = 0;
virtual bool disconnect(const std::string& from, const std::string& to, const std::string& carrier) = 0;
virtual int running() = 0; // 0 if is not running and 1 if is running; otherwise -1.
virtual bool exists(const char* port) = 0;
virtual std::string requestRpc(const char* szport, const char* request, double timeout=0.0) = 0;
virtual bool connected(const char* from, const char* to,
const char* carrier) = 0;
virtual const char* error() = 0;
virtual bool exists(const std::string& port) = 0;
virtual std::string requestRpc(const std::string& szport, const std::string& request, double timeout = 0.0) = 0;
virtual bool connected(const std::string& from, const std::string& to, const std::string& carrier) = 0;
virtual std::string error() = 0;
virtual bool initialized() = 0;
virtual bool attachStdout() = 0;
virtual void detachStdout() = 0;
Expand All @@ -62,7 +59,7 @@ class Broker {
bool hasWatchDog() { return bWithWatchDog; }
void setDisplay(const char* szDisplay) { if(szDisplay) { strDisplay = szDisplay; } }

const char* getDisplay() const {return strDisplay.c_str(); }
std::string getDisplay() const {return strDisplay; }
protected:
unsigned int UNIQUEID;
BrokerEventSink* eventSink;
Expand Down
10 changes: 5 additions & 5 deletions src/libYARP_manager/src/yarp/manager/execstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ void Ready::startModule()
{
OSTRINGSTREAM msg;
msg<<"cannot run "<<executable->getCommand()<<" on "<<executable->getHost();
if (executable->getBroker()->error()) {
if (!executable->getBroker()->error().empty()) {
msg << " : " << executable->getBroker()->error();
}
logger->addError(msg);
Expand Down Expand Up @@ -305,7 +305,7 @@ void Connecting::connectAllPorts()
{
OSTRINGSTREAM msg;
msg<<"cannot connect "<<(*itr).from() <<" to "<<(*itr).to();
if (executable->getBroker()->error()) {
if (!executable->getBroker()->error().empty()) {
msg << " : " << executable->getBroker()->error();
}
logger->addError(msg);
Expand Down Expand Up @@ -418,7 +418,7 @@ void Dying::stopModule()
{
OSTRINGSTREAM msg;
msg<<"cannot stop "<<executable->getCommand()<<" on "<<executable->getHost();
if (executable->getBroker()->error()) {
if (!executable->getBroker()->error().empty()) {
msg << " : " << executable->getBroker()->error();
}
logger->addError(msg);
Expand All @@ -439,7 +439,7 @@ void Dying::killModule()
{
OSTRINGSTREAM msg;
msg<<"cannot kill "<<executable->getCommand()<<" on "<<executable->getHost();
if (executable->getBroker()->error()) {
if (!executable->getBroker()->error().empty()) {
msg << " : " << executable->getBroker()->error();
}
logger->addError(msg);
Expand Down Expand Up @@ -467,7 +467,7 @@ void Dying::disconnectAllPorts()
{
OSTRINGSTREAM msg;
msg<<"cannot disconnect "<<(*itr).from() <<" to "<<(*itr).to();
if (executable->getBroker()->error()) {
if (!executable->getBroker()->error().empty()) {
msg << " : " << executable->getBroker()->error();
}
logger->addError(msg);
Expand Down
2 changes: 1 addition & 1 deletion src/libYARP_manager/src/yarp/manager/executable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ bool Executable::initialize()
{
OSTRINGSTREAM msg;
msg<<"cannot initialize broker. : ";
if (broker->error()) {
if (!broker->error().empty()) {
msg << broker->error();
}
logger->addError(msg);
Expand Down
25 changes: 12 additions & 13 deletions src/libYARP_manager/src/yarp/manager/localbroker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,17 +355,16 @@ int LocalBroker::running()
/**
* connection broker
*/
bool LocalBroker::connect(const char* from, const char* to,
const char* carrier, bool persist)
bool LocalBroker::connect(const std::string& from, const std::string& to, const std::string& carrier, bool persist)
{

if(!from)
if(from.empty())
{
strError = "no source port is introduced.";
return false;
}

if(!to)
if (to.empty())
{
strError = "no destination port is introduced.";
return false;
Expand Down Expand Up @@ -395,16 +394,16 @@ bool LocalBroker::connect(const char* from, const char* to,
return true;
}

bool LocalBroker::disconnect(const char* from, const char* to, const char *carrier)
bool LocalBroker::disconnect(const std::string& from, const std::string& to, const std::string& carrier)
{

if(!from)
if (from.empty())
{
strError = "no source port is introduced.";
return false;
}

if(!to)
if (to.empty())
{
strError = "no destination port is introduced.";
return false;
Expand Down Expand Up @@ -439,15 +438,15 @@ bool LocalBroker::disconnect(const char* from, const char* to, const char *carri

}

bool LocalBroker::exists(const char* port)
bool LocalBroker::exists(const std::string& port)
{
return NetworkBase::exists(port);
}


std::string LocalBroker::requestRpc(const char* szport, const char* request, double timeout)
std::string LocalBroker::requestRpc(const std::string& szport, const std::string& request, double timeout)
{
if ((szport == nullptr) || (request == nullptr)) {
if (szport.empty() || request.empty()) {
return {};
}

Expand Down Expand Up @@ -492,7 +491,7 @@ std::string LocalBroker::requestRpc(const char* szport, const char* request, dou
return response.toString().c_str();
}

bool LocalBroker::connected(const char* from, const char* to, const char* carrier)
bool LocalBroker::connected(const std::string& from, const std::string& to, const std::string& carrier)
{
if (!exists(from) || !exists(to)) {
return false;
Expand All @@ -501,9 +500,9 @@ bool LocalBroker::connected(const char* from, const char* to, const char* carrie
}


const char* LocalBroker::error()
std::string LocalBroker::error()
{
return strError.c_str();
return strError;
}

bool LocalBroker::attachStdout()
Expand Down
15 changes: 6 additions & 9 deletions src/libYARP_manager/src/yarp/manager/localbroker.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,13 @@ class LocalBroker: public Broker, public yarp::os::Thread {
bool start() override;
bool stop() override;
bool kill() override;
bool connect(const char* from, const char* to,
const char* carrier, bool persist=false) override;
bool disconnect(const char* from, const char* to,
const char *carrier) override;
bool connect(const std::string& from, const std::string& to, const std::string& carrier, bool persist = false) override;
bool disconnect(const std::string& from, const std::string& to, const std::string& carrier) override;
int running() override;
bool exists(const char* port) override;
std::string requestRpc(const char* szport, const char* request, double timeout) override;
bool connected(const char* from, const char* to,
const char* carrier) override;
const char* error() override;
bool exists(const std::string& port) override;
std::string requestRpc(const std::string& szport, const std::string& request, double timeout) override;
bool connected(const std::string& from, const std::string& to, const std::string& carrier) override;
std::string error() override;
bool initialized() override { return bInitialized;}
bool attachStdout() override;
void detachStdout() override;
Expand Down
4 changes: 2 additions & 2 deletions src/libYARP_manager/src/yarp/manager/scriptbroker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ bool ScriptLocalBroker::init(const char* szcmd, const char* szparam,
}


bool ScriptYarprunBroker::whichFile(const char* server, const char* filename, std::string& filenameWithPath)
bool ScriptYarprunBroker::whichFile(const std::string& server, const std::string& filename, std::string& filenameWithPath)
{
if (!strlen(server)) {
if (server.empty()) {
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions src/libYARP_manager/src/yarp/manager/scriptbroker.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class ScriptYarprunBroker: public YarpBroker
const char* szhost, const char* szstdio,
const char* szworkdir, const char* szenv) override;
private:
bool whichFile(const char* server, const char* filename, std::string& filenameWithPath);
std::string script;
bool whichFile(const std::string& server, const std::string& filename, std::string& filenameWithPath);
std::string script;
};

} // namespace yarp::manager
Expand Down
50 changes: 25 additions & 25 deletions src/libYARP_manager/src/yarp/manager/yarpbroker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,16 +408,15 @@ Property& YarpBroker::runProperty()
/**
* connection broker
*/
bool YarpBroker::connect(const char* from, const char* to,
const char* carrier, bool persist)
bool YarpBroker::connect(const std::string& from, const std::string& to, const std::string& carrier, bool persist)
{
if(!from)
if(from.empty())
{
strError = "no source port is introduced.";
return false;
}

if(!to)
if(to.empty())
{
strError = "no destination port is introduced.";
return false;
Expand Down Expand Up @@ -471,16 +470,16 @@ bool YarpBroker::connect(const char* from, const char* to,
return true;
}

bool YarpBroker::disconnect(const char* from, const char* to, const char* carrier)
bool YarpBroker::disconnect(const std::string& from, const std::string& to, const std::string& carrier)
{

if(!from)
if(from.empty())
{
strError = "no source port is introduced.";
return false;
}

if(!to)
if(to.empty())
{
strError = "no destination port is introduced.";
return false;
Expand Down Expand Up @@ -521,17 +520,17 @@ bool YarpBroker::disconnect(const char* from, const char* to, const char* carrie

}

bool YarpBroker::exists(const char* szport)
bool YarpBroker::exists(const std::string& szport)
{
ContactStyle style;
style.quiet = true;
style.timeout = CONNECTION_TIMEOUT;
return NetworkBase::exists(szport, style);
}

std::string YarpBroker::requestRpc(const char* szport, const char* request, double timeout)
std::string YarpBroker::requestRpc(const std::string& szport, const std::string& request, double timeout)
{
if ((szport == nullptr) || (request == nullptr)) {
if (szport.empty() || request.empty()) {
return {};
}

Expand Down Expand Up @@ -576,7 +575,7 @@ std::string YarpBroker::requestRpc(const char* szport, const char* request, doub
return response.toString().c_str();
}

bool YarpBroker::connected(const char* from, const char* to, const char* carrier)
bool YarpBroker::connected(const std::string& from, const std::string& to, const std::string& carrier)
{
if (!exists(from) || !exists(to)) {
return false;
Expand All @@ -588,9 +587,9 @@ bool YarpBroker::connected(const char* from, const char* to, const char* carrier
return NetworkBase::isConnected(from, to, style);
}

bool YarpBroker::getSystemInfo(const char* server, SystemInfoSerializer& info)
bool YarpBroker::getSystemInfo(const std::string& server, SystemInfoSerializer& info)
{
if (!strlen(server)) {
if (server.empty()) {
return false;
}
if (!semParam.check()) {
Expand Down Expand Up @@ -681,10 +680,10 @@ bool YarpBroker::getAllPorts(std::vector<std::string> &ports)
return true;
}

bool YarpBroker::getAllProcesses(const char* server,
bool YarpBroker::getAllProcesses(const std::string& server,
ProcessContainer& processes)
{
if (!strlen(server)) {
if (server.empty()) {
return false;
}

Expand Down Expand Up @@ -729,9 +728,9 @@ bool YarpBroker::getAllProcesses(const char* server,
}


bool YarpBroker::rmconnect(const char* from, const char* to)
bool YarpBroker::rmconnect(const std::string& from, const std::string& to)
{
std::string topic = std::string(from) + std::string(to);
std::string topic = from + to;
Bottle cmd, reply;
cmd.addString("untopic");
cmd.addString(topic.c_str());
Expand All @@ -743,23 +742,23 @@ bool YarpBroker::rmconnect(const char* from, const char* to)
CONNECTION_TIMEOUT);
}

bool YarpBroker::setQos(const char* from, const char *to,
const char *qosFrom, const char *qosTo) {
bool YarpBroker::setQos(const std::string& from, const std::string& to, const std::string& qosFrom, const std::string& qosTo)
{
strError.clear();

if (qosFrom && qosTo && !strlen(qosFrom) && !strlen(qosTo)) {
if (qosFrom.empty() && qosTo.empty()) {
return true;
}

QosStyle styleFrom;
QosStyle styleTo;
if(qosFrom != nullptr && strlen(qosFrom)) {
if (qosFrom.empty() == false) {
if(!getQosFromString(qosFrom, styleFrom)) {
strError = "Error in parsing Qos properties of " + std::string(from);
return false;
}
}
if (qosTo != nullptr && strlen(qosTo)) {
if (qosTo.empty() == false) {
if(!getQosFromString(qosTo, styleTo)) {
strError = "Error in parsing Qos properties of " + std::string(to);
return false;
Expand All @@ -768,7 +767,8 @@ bool YarpBroker::setQos(const char* from, const char *to,
return NetworkBase::setConnectionQos(from, to, styleFrom, styleTo, true);
}

bool YarpBroker::getQosFromString(const char* qos, yarp::os::QosStyle& style) {
bool YarpBroker::getQosFromString(const std::string& qos, yarp::os::QosStyle& style)
{
std::string strQos(qos);
transform(strQos.begin(), strQos.end(), strQos.begin(),
(int(*)(int))toupper);
Expand Down Expand Up @@ -804,9 +804,9 @@ bool YarpBroker::getQosFromString(const char* qos, yarp::os::QosStyle& style) {
return true;
}

const char* YarpBroker::error()
std::string YarpBroker::error()
{
return strError.c_str();
return strError;
}


Expand Down
Loading

0 comments on commit fa08aa3

Please sign in to comment.