Skip to content

Commit 84ab7a0

Browse files
authored
Merge pull request #187 from cowo78/master
std::string conversion for zmq::message_t
2 parents a96e0de + 0d1c20e commit 84ab7a0

File tree

2 files changed

+76
-24
lines changed

2 files changed

+76
-24
lines changed

zmq.hpp

Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727
#define __ZMQ_HPP_INCLUDED__
2828

2929
#if (__cplusplus >= 201103L)
30-
#define ZMQ_CPP11
31-
#define ZMQ_NOTHROW noexcept
32-
#define ZMQ_EXPLICIT explicit
33-
#elif (defined(_MSC_VER) && (_MSC_VER >= 1900))
34-
#define ZMQ_CPP11
35-
#define ZMQ_NOTHROW noexcept
36-
#define ZMQ_EXPLICIT explicit
30+
#define ZMQ_CPP11
31+
#define ZMQ_NOTHROW noexcept
32+
#define ZMQ_EXPLICIT explicit
33+
#elif (defined(_MSC_VER) && (_MSC_VER >= 1900))
34+
#define ZMQ_CPP11
35+
#define ZMQ_NOTHROW noexcept
36+
#define ZMQ_EXPLICIT explicit
3737
#else
3838
#define ZMQ_CPP03
3939
#define ZMQ_NOTHROW
@@ -42,18 +42,22 @@
4242

4343
#include <zmq.h>
4444

45-
#include <algorithm>
4645
#include <cassert>
4746
#include <cstring>
48-
#include <string>
47+
48+
#include <algorithm>
4949
#include <exception>
50-
#include <vector>
50+
#include <iomanip>
5151
#include <iterator>
52+
#include <sstream>
53+
#include <string>
54+
#include <vector>
55+
5256

5357
#ifdef ZMQ_CPP11
54-
#include <chrono>
55-
#include <tuple>
56-
#include <functional>
58+
#include <chrono>
59+
#include <tuple>
60+
#include <functional>
5761
#endif
5862

5963
// Detect whether the compiler supports C++11 rvalue references.
@@ -83,11 +87,11 @@
8387
#endif
8488

8589
#if ZMQ_VERSION >= ZMQ_MAKE_VERSION(3, 3, 0)
86-
#define ZMQ_NEW_MONITOR_EVENT_LAYOUT
90+
#define ZMQ_NEW_MONITOR_EVENT_LAYOUT
8791
#endif
8892

8993
#if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 1, 0)
90-
#define ZMQ_HAS_PROXY_STEERABLE
94+
#define ZMQ_HAS_PROXY_STEERABLE
9195
/* Socket event data */
9296
typedef struct {
9397
uint16_t event; // id of the event as bitfield
@@ -378,6 +382,42 @@ namespace zmq
378382
return value;
379383
}
380384
#endif
385+
/** Dump content to string. Ascii chars are readable, the rest is printed as hex.
386+
* Probably ridiculously slow.
387+
*/
388+
inline std::string str() const
389+
{
390+
// Partly mutuated from the same method in zmq::multipart_t
391+
std::stringstream os;
392+
393+
const unsigned char* msg_data = this->data<unsigned char>();
394+
unsigned char byte;
395+
size_t size = this->size();
396+
int is_ascii[2] = {0, 0};
397+
398+
os << "zmq::message_t [size " << std::dec << std::setw(3) << std::setfill('0') << size << "] (";
399+
// Totally arbitrary
400+
if (size >= 1000) {
401+
os << "... too big to print)";
402+
} else {
403+
while (size--) {
404+
byte = *msg_data++;
405+
406+
is_ascii[1] = (byte >= 33 && byte < 127);
407+
if (is_ascii[1] != is_ascii[0])
408+
os << " "; // Separate text/non text
409+
410+
if (is_ascii[1]) {
411+
os << byte;
412+
} else {
413+
os << std::hex << std::uppercase << std::setw(2) << std::setfill('0') << static_cast<short>(byte);
414+
}
415+
is_ascii[0] = is_ascii[1];
416+
}
417+
os << ")";
418+
}
419+
return os.str();
420+
}
381421

382422
private:
383423
// The underlying message
@@ -927,7 +967,7 @@ namespace zmq
927967
#elif ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 1)
928968
virtual void on_event_handshake_failed(const zmq_event_t &event_, const char* addr_) { (void) event_; (void) addr_; }
929969
virtual void on_event_handshake_succeed(const zmq_event_t &event_, const char* addr_) { (void) event_; (void) addr_; }
930-
#endif
970+
#endif
931971
virtual void on_event_unknown(const zmq_event_t &event_, const char* addr_) { (void)event_; (void)addr_; }
932972
private:
933973

@@ -984,9 +1024,9 @@ namespace zmq
9841024

9851025
#if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 3)
9861026
if (zmq_errno () == EAGAIN)
987-
#else
1027+
#else
9881028
if (zmq_errno () == ETIMEDOUT)
989-
#endif
1029+
#endif
9901030
return false;
9911031

9921032
throw error_t ();
@@ -995,9 +1035,15 @@ namespace zmq
9951035
private:
9961036
void *poller_ptr;
9971037
std::vector<zmq_poller_event_t> poller_events;
998-
};
1038+
}; // class poller_t
9991039
#endif // defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11) && defined(ZMQ_HAVE_POLLER)
10001040

1041+
1042+
inline std::ostream& operator<<(std::ostream& os, const message_t& msg)
1043+
{
1044+
return os << msg.str();
10011045
}
10021046

1003-
#endif
1047+
} // namespace zmq
1048+
1049+
#endif // __ZMQ_HPP_INCLUDED__

zmq_addon.hpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#define __ZMQ_ADDON_HPP_INCLUDED__
2626

2727
#include <zmq.hpp>
28+
2829
#include <deque>
2930
#include <iomanip>
3031
#include <sstream>
@@ -328,7 +329,7 @@ class multipart_t
328329
{
329330
return &m_parts[index];
330331
}
331-
332+
332333
// Get a string copy of a specific message part
333334
std::string peekstr(size_t index) const
334335
{
@@ -582,10 +583,15 @@ class multipart_t
582583
// Disable implicit copying (moving is more efficient)
583584
multipart_t(const multipart_t& other) ZMQ_DELETED_FUNCTION;
584585
void operator=(const multipart_t& other) ZMQ_DELETED_FUNCTION;
585-
};
586+
}; // class multipart_t
586587

587-
#endif
588+
#endif // ZMQ_HAS_RVALUE_REFS
588589

590+
inline std::ostream& operator<<(std::ostream& os, const multipart_t& msg)
591+
{
592+
return os << msg.str();
589593
}
590594

591-
#endif
595+
} // namespace zmq
596+
597+
#endif // __ZMQ_ADDON_HPP_INCLUDED__

0 commit comments

Comments
 (0)