diff --git a/doc/release/master/remove_stream_operator_std_vector.md b/doc/release/master/remove_stream_operator_std_vector.md new file mode 100644 index 00000000000..03c4d4dcf96 --- /dev/null +++ b/doc/release/master/remove_stream_operator_std_vector.md @@ -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). diff --git a/example/os/log/log_test.cpp b/example/os/log/log_test.cpp index 7dd151bcbd1..91b84a6b4c0 100644 --- a/example/os/log/log_test.cpp +++ b/example/os/log/log_test.cpp @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; diff --git a/src/libYARP_os/src/yarp/os/LogStream.h b/src/libYARP_os/src/yarp/os/LogStream.h index 2c332e22179..697ba112079 100644 --- a/src/libYARP_os/src/yarp/os/LogStream.h +++ b/src/libYARP_os/src/yarp/os/LogStream.h @@ -24,12 +24,6 @@ #include #include - -namespace std { -template -std::ostream& operator<<(std::ostream& os, const std::vector& t); -} - namespace yarp { namespace os { @@ -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: @@ -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 inline LogStream& operator<<(const std::vector& t) { - stream->oss << t; - stream->oss << ' '; + bool nospace = stream->nospace; + stream->nospace = true; + stream->oss << '['; + for (typename std::vector::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 -inline std::ostream& std::operator<<(std::ostream& os, const std::vector& t) -{ - os << '['; - for (typename std::vector::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 diff --git a/src/libYARP_robotinterface/src/yarp/robotinterface/experimental/Action.cpp b/src/libYARP_robotinterface/src/yarp/robotinterface/experimental/Action.cpp index 7778702e66a..6128f1fc911 100644 --- a/src/libYARP_robotinterface/src/yarp/robotinterface/experimental/Action.cpp +++ b/src/libYARP_robotinterface/src/yarp/robotinterface/experimental/Action.cpp @@ -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; } diff --git a/src/libYARP_robotinterface/src/yarp/robotinterface/experimental/Action.h b/src/libYARP_robotinterface/src/yarp/robotinterface/experimental/Action.h index c0f0aa81dba..9bf831feb96 100644 --- a/src/libYARP_robotinterface/src/yarp/robotinterface/experimental/Action.h +++ b/src/libYARP_robotinterface/src/yarp/robotinterface/experimental/Action.h @@ -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); diff --git a/src/libYARP_robotinterface/src/yarp/robotinterface/experimental/Device.cpp b/src/libYARP_robotinterface/src/yarp/robotinterface/experimental/Device.cpp index a4645578cdb..c52a73ec3ac 100644 --- a/src/libYARP_robotinterface/src/yarp/robotinterface/experimental/Device.cpp +++ b/src/libYARP_robotinterface/src/yarp/robotinterface/experimental/Device.cpp @@ -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; } diff --git a/src/libYARP_robotinterface/src/yarp/robotinterface/experimental/Device.h b/src/libYARP_robotinterface/src/yarp/robotinterface/experimental/Device.h index 292c90db1eb..5d91cdbdfac 100644 --- a/src/libYARP_robotinterface/src/yarp/robotinterface/experimental/Device.h +++ b/src/libYARP_robotinterface/src/yarp/robotinterface/experimental/Device.h @@ -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); diff --git a/src/libYARP_robotinterface/src/yarp/robotinterface/experimental/Robot.cpp b/src/libYARP_robotinterface/src/yarp/robotinterface/experimental/Robot.cpp index fea58884205..19cd651f4a5 100644 --- a/src/libYARP_robotinterface/src/yarp/robotinterface/experimental/Robot.cpp +++ b/src/libYARP_robotinterface/src/yarp/robotinterface/experimental/Robot.cpp @@ -23,21 +23,21 @@ #include -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; } @@ -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)) { diff --git a/src/libYARP_robotinterface/src/yarp/robotinterface/experimental/Robot.h b/src/libYARP_robotinterface/src/yarp/robotinterface/experimental/Robot.h index f9270bb08b9..8b2e1c5ea0f 100644 --- a/src/libYARP_robotinterface/src/yarp/robotinterface/experimental/Robot.h +++ b/src/libYARP_robotinterface/src/yarp/robotinterface/experimental/Robot.h @@ -63,7 +63,6 @@ class YARP_robotinterface_API Robot } // namespace robotinterface } // namespace yarp -YARP_robotinterface_API std::ostringstream& operator<<(std::ostringstream& oss, const yarp::robotinterface::experimental::Robot& t); YARP_robotinterface_API yarp::os::LogStream operator<<(yarp::os::LogStream dbg, const yarp::robotinterface::experimental::Robot& t);