Skip to content

Commit

Permalink
os: Remove operator<< for std::ostream and std::vector from LogStream.
Browse files Browse the repository at this point in the history
This was causing a conflict with Casadi

Fixes robotology#2067
Replaces robotology#2421
  • Loading branch information
drdanz committed Apr 19, 2021
1 parent 34ae034 commit 0b9f606
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 102 deletions.
11 changes: 11 additions & 0 deletions doc/release/master/remove_stream_operator_std_vector.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
remove_stream_operator_std_vector {#master}
---------------------------------

## Libraries

### `os`

#### `LogStream`

* The `operator<<` for `std::ostream` and `std::vector` which caused conflicts
with Casadi was removed (#2067).
10 changes: 5 additions & 5 deletions example/os/log/log_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ int main(int argc, char *argv[])

yTrace();
yTrace() << "This is" << "another" << "trace" << i;
yTrace() << v;
yTrace() << v << "and there should be a space before \"and\" and after" << "the first \"after\"";
yTrace() << "The end of line is removed from this trace\n";
yCTrace(LOG_COMPONENT);
yCTrace(LOG_COMPONENT) << "This is" << "another" << "trace" << i;
Expand All @@ -70,7 +70,7 @@ int main(int argc, char *argv[])

yDebug();
yDebug() << "This is" << "another" << "debug" << i;
yDebug() << v;
yDebug() << v << "and there should be a space before \"and\" and after" << "the first \"after\"";
yDebug() << "The end of line is removed from this debug\n";
yCDebug(LOG_COMPONENT);
yCDebug(LOG_COMPONENT) << "This is" << "another" << "debug" << i;
Expand All @@ -88,7 +88,7 @@ int main(int argc, char *argv[])

yInfo();
yInfo() << "This is" << "more" << "info" << i;
yInfo() << v;
yInfo() << v << "and there should be a space before \"and\" and after" << "the first \"after\"";
yInfo() << "The end of line is removed from this info\n";
yCInfo(LOG_COMPONENT);
yCInfo(LOG_COMPONENT) << "This is" << "more" << "info" << i;
Expand All @@ -106,7 +106,7 @@ int main(int argc, char *argv[])

yWarning();
yWarning() << "This is" << "another" << "warning" << i;
yWarning() << v;
yWarning() << v << "and there should be a space before \"and\" and after" << "the first \"after\"";
yWarning() << "The end of line is removed from this warning\n";
yCWarning(LOG_COMPONENT);
yCWarning(LOG_COMPONENT) << "This is" << "another" << "warning" << i;
Expand All @@ -124,7 +124,7 @@ int main(int argc, char *argv[])

yError();
yError() << "This is" << "another" << "error" << i;
yError() << v;
yError() << v << "and there should be a space before \"and\" and after" << "the first \"after\"";
yError() << "The end of line is removed from this error\n";
yCError(LOG_COMPONENT);
yCError(LOG_COMPONENT) << "This is" << "another" << "error" << i;
Expand Down
103 changes: 63 additions & 40 deletions src/libYARP_os/src/yarp/os/LogStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@
#include <string>
#include <vector>


namespace std {
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& t);
}

namespace yarp {
namespace os {

Expand Down Expand Up @@ -67,6 +61,7 @@ class YARP_os_API LogStream
const yarp::os::Log::Predicate pred;
const LogComponent& comp;
int ref;
bool nospace {false};
} * stream;

public:
Expand Down Expand Up @@ -132,121 +127,149 @@ class YARP_os_API LogStream
inline LogStream& operator<<(bool t)
{
stream->oss << (t ? "true" : "false");
stream->oss << ' ';
if (!stream->nospace) {
stream->oss << ' ';
}
return *this;
}
inline LogStream& operator<<(char t)
{
stream->oss << t;
stream->oss << ' ';
if (!stream->nospace) {
stream->oss << ' ';
}
return *this;
}
inline LogStream& operator<<(signed short t)
{
stream->oss << t;
stream->oss << ' ';
if (!stream->nospace) {
stream->oss << ' ';
}
return *this;
}
inline LogStream& operator<<(unsigned short t)
{
stream->oss << t;
stream->oss << ' ';
if (!stream->nospace) {
stream->oss << ' ';
}
return *this;
}
inline LogStream& operator<<(signed int t)
{
stream->oss << t;
stream->oss << ' ';
if (!stream->nospace) {
stream->oss << ' ';
}
return *this;
}
inline LogStream& operator<<(unsigned int t)
{
stream->oss << t;
stream->oss << ' ';
if (!stream->nospace) {
stream->oss << ' ';
}
return *this;
}
inline LogStream& operator<<(signed long t)
{
stream->oss << t;
stream->oss << ' ';
if (!stream->nospace) {
stream->oss << ' ';
}
return *this;
}
inline LogStream& operator<<(unsigned long t)
{
stream->oss << t;
stream->oss << ' ';
if (!stream->nospace) {
stream->oss << ' ';
}
return *this;
}
inline LogStream& operator<<(signed long long t)
{
stream->oss << t;
stream->oss << ' ';
if (!stream->nospace) {
stream->oss << ' ';
}
return *this;
}
inline LogStream& operator<<(unsigned long long t)
{
stream->oss << t;
stream->oss << ' ';
if (!stream->nospace) {
stream->oss << ' ';
}
return *this;
}
inline LogStream& operator<<(float t)
{
stream->oss << t;
stream->oss << ' ';
if (!stream->nospace) {
stream->oss << ' ';
}
return *this;
}
inline LogStream& operator<<(double t)
{
stream->oss << t;
stream->oss << ' ';
if (!stream->nospace) {
stream->oss << ' ';
}
return *this;
}
inline LogStream& operator<<(const char* t)
{
stream->oss << t;
stream->oss << ' ';
if (!stream->nospace) {
stream->oss << ' ';
}
return *this;
}
inline LogStream& operator<<(const void* t)
{
stream->oss << t;
stream->oss << ' ';
if (!stream->nospace) {
stream->oss << ' ';
}
return *this;
}

inline LogStream& operator<<(const std::string& t)
{
stream->oss << t.c_str();
stream->oss << ' ';
if (!stream->nospace) {
stream->oss << ' ';
}
return *this;
}

template <typename T>
inline LogStream& operator<<(const std::vector<T>& t)
{
stream->oss << t;
stream->oss << ' ';
bool nospace = stream->nospace;
stream->nospace = true;
stream->oss << '[';
for (typename std::vector<T>::const_iterator it = t.begin(); it != t.end(); ++it) {
const T& p = *it;
if (it != t.begin()) {
stream->oss << ", ";
}
*this << p;
}
stream->oss << ']';
stream->nospace = nospace;
if (!stream->nospace) {
stream->oss << ' ';
}
return *this;
}
}

}; // class LogStream

} // namespace os
} // namespace yarp


template <typename T>
inline std::ostream& std::operator<<(std::ostream& os, const std::vector<T>& t)
{
os << '[';
for (typename std::vector<T>::const_iterator it = t.begin(); it != t.end(); ++it) {
const T& p = *it;
if (it != t.begin()) {
os << ", ";
}
os << p;
}
os << ']';
return os;
}

#endif // YARP_OS_LOGSTREAM_H
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,15 @@ class yarp::robotinterface::experimental::Action::Private
ParamList params;
};

std::ostream& std::operator<<(std::ostream& oss, const yarp::robotinterface::experimental::Action& t)
yarp::os::LogStream operator<<(yarp::os::LogStream dbg, const yarp::robotinterface::experimental::Action& t)
{
oss << "(\"" << ActionPhaseToString(t.phase()) << ":" << ActionTypeToString(t.type()) << ":" << t.level() << "\"";
dbg << "(\"" << ActionPhaseToString(t.phase()) << ":" << ActionTypeToString(t.type()) << ":" << t.level() << "\"";
if (!t.params().empty()) {
oss << ", params = [";
oss << t.params();
oss << "]";
dbg << ", params = [";
dbg << t.params();
dbg << "]";
}
oss << ")";
return oss;
}


yarp::os::LogStream operator<<(yarp::os::LogStream dbg, const yarp::robotinterface::experimental::Action& t)
{
std::ostringstream oss;
oss << t;
dbg << oss.str();
dbg << ")";
return dbg;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ class YARP_robotinterface_API Action
} // namespace yarp


namespace std {
YARP_robotinterface_API std::ostream& operator<<(std::ostream& oss, const yarp::robotinterface::experimental::Action& t);
}
YARP_robotinterface_API yarp::os::LogStream operator<<(yarp::os::LogStream dbg, const yarp::robotinterface::experimental::Action& t);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,28 +187,20 @@ class yarp::robotinterface::experimental::Device::Private
Driver* driver;
};

std::ostream& std::operator<<(std::ostream& oss, const yarp::robotinterface::experimental::Device& t)
yarp::os::LogStream operator<<(yarp::os::LogStream dbg, const yarp::robotinterface::experimental::Device& t)
{
oss << "(name = \"" << t.name() << "\", type = \"" << t.type() << "\"";
dbg << "(name = \"" << t.name() << "\", type = \"" << t.type() << "\"";
if (!t.params().empty()) {
oss << ", params = [";
oss << t.params();
oss << "]";
dbg << ", params = [";
dbg << t.params();
dbg << "]";
}
if (!t.actions().empty()) {
oss << ", actions = [";
oss << t.actions();
oss << "]";
dbg << ", actions = [";
dbg << t.actions();
dbg << "]";
}
oss << ")";
return oss;
}

yarp::os::LogStream operator<<(yarp::os::LogStream dbg, const yarp::robotinterface::experimental::Device& t)
{
std::ostringstream oss;
oss << t;
dbg << oss.str();
dbg << ")";
return dbg;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ class YARP_robotinterface_API Device
} // namespace robotinterface
} // namespace yarp

namespace std {
YARP_robotinterface_API std::ostream& operator<<(std::ostream& oss, const yarp::robotinterface::experimental::Device& t);
}
YARP_robotinterface_API yarp::os::LogStream operator<<(yarp::os::LogStream dbg, const yarp::robotinterface::experimental::Device& t);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@
#include <unordered_set>


std::ostringstream& operator<<(std::ostringstream& oss, const yarp::robotinterface::experimental::Robot& t)
yarp::os::LogStream operator<<(yarp::os::LogStream dbg, const yarp::robotinterface::experimental::Robot& t)
{
oss << "(name = \"" << t.name() << "\"";
dbg << "(name = \"" << t.name() << "\"";
if (!t.params().empty()) {
oss << ", params = [";
oss << t.params();
oss << "]";
dbg << ", params = [";
dbg << t.params();
dbg << "]";
}
if (!t.devices().empty()) {
oss << ", devices = [";
oss << t.devices();
oss << "]";
dbg << ", devices = [";
dbg << t.devices();
dbg << "]";
}
oss << ")";
return oss;
dbg << ")";
return dbg;
}


Expand Down Expand Up @@ -420,14 +420,6 @@ bool yarp::robotinterface::experimental::Robot::Private::custom(const yarp::robo
return true;
}

yarp::os::LogStream operator<<(yarp::os::LogStream dbg, const yarp::robotinterface::experimental::Robot& t)
{
std::ostringstream oss;
oss << t;
dbg << oss.str();
return dbg;
}

yarp::robotinterface::experimental::Robot::Robot() :
mPriv(new Private(this))
{
Expand Down
Loading

0 comments on commit 0b9f606

Please sign in to comment.