mirror of
https://github.com/zeromq/cppzmq.git
synced 2025-05-19 12:49:09 +02:00
Problem: zmq_addon.hpp not follow clang-format
This commit is contained in:
parent
5031278f18
commit
cd72eef3fd
238
zmq_addon.hpp
238
zmq_addon.hpp
@ -31,8 +31,8 @@
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace zmq {
|
||||
|
||||
namespace zmq
|
||||
{
|
||||
#ifdef ZMQ_HAS_RVALUE_REFS
|
||||
|
||||
/*
|
||||
@ -48,46 +48,30 @@ private:
|
||||
std::deque<message_t> m_parts;
|
||||
|
||||
public:
|
||||
|
||||
typedef std::deque<message_t>::iterator iterator;
|
||||
typedef std::deque<message_t>::const_iterator const_iterator;
|
||||
|
||||
typedef std::deque<message_t>::reverse_iterator reverse_iterator;
|
||||
typedef std::deque<message_t>::const_reverse_iterator const_reverse_iterator;
|
||||
typedef std::deque<message_t>::const_reverse_iterator
|
||||
const_reverse_iterator;
|
||||
|
||||
// Default constructor
|
||||
multipart_t()
|
||||
{}
|
||||
multipart_t () {}
|
||||
|
||||
// Construct from socket receive
|
||||
multipart_t(socket_t& socket)
|
||||
{
|
||||
recv(socket);
|
||||
}
|
||||
multipart_t (socket_t &socket) { recv (socket); }
|
||||
|
||||
// Construct from memory block
|
||||
multipart_t(const void *src, size_t size)
|
||||
{
|
||||
addmem(src, size);
|
||||
}
|
||||
multipart_t (const void *src, size_t size) { addmem (src, size); }
|
||||
|
||||
// Construct from string
|
||||
multipart_t(const std::string& string)
|
||||
{
|
||||
addstr(string);
|
||||
}
|
||||
multipart_t (const std::string &string) { addstr (string); }
|
||||
|
||||
// Construct from message part
|
||||
multipart_t(message_t&& message)
|
||||
{
|
||||
add(std::move(message));
|
||||
}
|
||||
multipart_t (message_t &&message) { add (std::move (message)); }
|
||||
|
||||
// Move constructor
|
||||
multipart_t(multipart_t&& other)
|
||||
{
|
||||
m_parts = std::move(other.m_parts);
|
||||
}
|
||||
multipart_t (multipart_t &&other) { m_parts = std::move (other.m_parts); }
|
||||
|
||||
// Move assignment operator
|
||||
multipart_t &operator= (multipart_t &&other)
|
||||
@ -97,106 +81,51 @@ public:
|
||||
}
|
||||
|
||||
// Destructor
|
||||
virtual ~multipart_t()
|
||||
{
|
||||
clear();
|
||||
}
|
||||
virtual ~multipart_t () { clear (); }
|
||||
|
||||
message_t& operator[] (size_t n)
|
||||
{
|
||||
return m_parts[n];
|
||||
}
|
||||
message_t &operator[] (size_t n) { return m_parts[n]; }
|
||||
|
||||
const message_t& operator[] (size_t n) const
|
||||
{
|
||||
return m_parts[n];
|
||||
}
|
||||
const message_t &operator[] (size_t n) const { return m_parts[n]; }
|
||||
|
||||
message_t& at (size_t n)
|
||||
{
|
||||
return m_parts.at(n);
|
||||
}
|
||||
message_t &at (size_t n) { return m_parts.at (n); }
|
||||
|
||||
const message_t& at (size_t n) const
|
||||
{
|
||||
return m_parts.at(n);
|
||||
}
|
||||
const message_t &at (size_t n) const { return m_parts.at (n); }
|
||||
|
||||
iterator begin()
|
||||
{
|
||||
return m_parts.begin();
|
||||
}
|
||||
iterator begin () { return m_parts.begin (); }
|
||||
|
||||
const_iterator begin() const
|
||||
{
|
||||
return m_parts.begin();
|
||||
}
|
||||
const_iterator begin () const { return m_parts.begin (); }
|
||||
|
||||
const_iterator cbegin() const
|
||||
{
|
||||
return m_parts.cbegin();
|
||||
}
|
||||
const_iterator cbegin () const { return m_parts.cbegin (); }
|
||||
|
||||
reverse_iterator rbegin()
|
||||
{
|
||||
return m_parts.rbegin();
|
||||
}
|
||||
reverse_iterator rbegin () { return m_parts.rbegin (); }
|
||||
|
||||
const_reverse_iterator rbegin() const
|
||||
{
|
||||
return m_parts.rbegin();
|
||||
}
|
||||
const_reverse_iterator rbegin () const { return m_parts.rbegin (); }
|
||||
|
||||
iterator end()
|
||||
{
|
||||
return m_parts.end();
|
||||
}
|
||||
iterator end () { return m_parts.end (); }
|
||||
|
||||
const_iterator end() const
|
||||
{
|
||||
return m_parts.end();
|
||||
}
|
||||
const_iterator end () const { return m_parts.end (); }
|
||||
|
||||
const_iterator cend() const
|
||||
{
|
||||
return m_parts.cend();
|
||||
}
|
||||
const_iterator cend () const { return m_parts.cend (); }
|
||||
|
||||
reverse_iterator rend()
|
||||
{
|
||||
return m_parts.rend();
|
||||
}
|
||||
reverse_iterator rend () { return m_parts.rend (); }
|
||||
|
||||
const_reverse_iterator rend() const
|
||||
{
|
||||
return m_parts.rend();
|
||||
}
|
||||
const_reverse_iterator rend () const { return m_parts.rend (); }
|
||||
|
||||
// Delete all parts
|
||||
void clear()
|
||||
{
|
||||
m_parts.clear();
|
||||
}
|
||||
void clear () { m_parts.clear (); }
|
||||
|
||||
// Get number of parts
|
||||
size_t size() const
|
||||
{
|
||||
return m_parts.size();
|
||||
}
|
||||
size_t size () const { return m_parts.size (); }
|
||||
|
||||
// Check if number of parts is zero
|
||||
bool empty() const
|
||||
{
|
||||
return m_parts.empty();
|
||||
}
|
||||
bool empty () const { return m_parts.empty (); }
|
||||
|
||||
// Receive multipart message from socket
|
||||
bool recv (socket_t &socket, int flags = 0)
|
||||
{
|
||||
clear ();
|
||||
bool more = true;
|
||||
while (more)
|
||||
{
|
||||
while (more) {
|
||||
message_t message;
|
||||
if (!socket.recv (&message, flags))
|
||||
return false;
|
||||
@ -211,8 +140,7 @@ public:
|
||||
{
|
||||
flags &= ~(ZMQ_SNDMORE);
|
||||
bool more = size () > 0;
|
||||
while (more)
|
||||
{
|
||||
while (more) {
|
||||
message_t message = pop ();
|
||||
more = size () > 0;
|
||||
if (!socket.send (message, (more ? ZMQ_SNDMORE : 0) | flags))
|
||||
@ -261,18 +189,18 @@ public:
|
||||
}
|
||||
|
||||
// Push type (fixed-size) to front
|
||||
template<typename T>
|
||||
void pushtyp(const T& type)
|
||||
template <typename T> void pushtyp (const T &type)
|
||||
{
|
||||
static_assert(!std::is_same<T, std::string>::value, "Use pushstr() instead of pushtyp<std::string>()");
|
||||
static_assert (!std::is_same<T, std::string>::value,
|
||||
"Use pushstr() instead of pushtyp<std::string>()");
|
||||
m_parts.push_front (message_t (&type, sizeof (type)));
|
||||
}
|
||||
|
||||
// Push type (fixed-size) to back
|
||||
template<typename T>
|
||||
void addtyp(const T& type)
|
||||
template <typename T> void addtyp (const T &type)
|
||||
{
|
||||
static_assert(!std::is_same<T, std::string>::value, "Use addstr() instead of addtyp<std::string>()");
|
||||
static_assert (!std::is_same<T, std::string>::value,
|
||||
"Use addstr() instead of addtyp<std::string>()");
|
||||
m_parts.push_back (message_t (&type, sizeof (type)));
|
||||
}
|
||||
|
||||
@ -283,26 +211,25 @@ public:
|
||||
}
|
||||
|
||||
// Push message part to back
|
||||
void add(message_t&& message)
|
||||
{
|
||||
m_parts.push_back(std::move(message));
|
||||
}
|
||||
void add (message_t &&message) { m_parts.push_back (std::move (message)); }
|
||||
|
||||
// Pop string from front
|
||||
std::string popstr ()
|
||||
{
|
||||
std::string string(m_parts.front().data<char>(), m_parts.front().size());
|
||||
std::string string (m_parts.front ().data<char> (),
|
||||
m_parts.front ().size ());
|
||||
m_parts.pop_front ();
|
||||
return string;
|
||||
}
|
||||
|
||||
// Pop type (fixed-size) from front
|
||||
template<typename T>
|
||||
T poptyp()
|
||||
template <typename T> T poptyp ()
|
||||
{
|
||||
static_assert(!std::is_same<T, std::string>::value, "Use popstr() instead of poptyp<std::string>()");
|
||||
static_assert (!std::is_same<T, std::string>::value,
|
||||
"Use popstr() instead of poptyp<std::string>()");
|
||||
if (sizeof (T) != m_parts.front ().size ())
|
||||
throw std::runtime_error("Invalid type, size does not match the message size");
|
||||
throw std::runtime_error (
|
||||
"Invalid type, size does not match the message size");
|
||||
T type = *m_parts.front ().data<T> ();
|
||||
m_parts.pop_front ();
|
||||
return type;
|
||||
@ -325,32 +252,30 @@ public:
|
||||
}
|
||||
|
||||
// Get pointer to a specific message part
|
||||
const message_t* peek(size_t index) const
|
||||
{
|
||||
return &m_parts[index];
|
||||
}
|
||||
const message_t *peek (size_t index) const { return &m_parts[index]; }
|
||||
|
||||
// Get a string copy of a specific message part
|
||||
std::string peekstr (size_t index) const
|
||||
{
|
||||
std::string string(m_parts[index].data<char>(), m_parts[index].size());
|
||||
std::string string (m_parts[index].data<char> (),
|
||||
m_parts[index].size ());
|
||||
return string;
|
||||
}
|
||||
|
||||
// Peek type (fixed-size) from front
|
||||
template<typename T>
|
||||
T peektyp(size_t index) const
|
||||
template <typename T> T peektyp (size_t index) const
|
||||
{
|
||||
static_assert(!std::is_same<T, std::string>::value, "Use peekstr() instead of peektyp<std::string>()");
|
||||
static_assert (!std::is_same<T, std::string>::value,
|
||||
"Use peekstr() instead of peektyp<std::string>()");
|
||||
if (sizeof (T) != m_parts[index].size ())
|
||||
throw std::runtime_error("Invalid type, size does not match the message size");
|
||||
throw std::runtime_error (
|
||||
"Invalid type, size does not match the message size");
|
||||
T type = *m_parts[index].data<T> ();
|
||||
return type;
|
||||
}
|
||||
|
||||
// Create multipart from type (fixed-size)
|
||||
template<typename T>
|
||||
static multipart_t create(const T& type)
|
||||
template <typename T> static multipart_t create (const T &type)
|
||||
{
|
||||
multipart_t multipart;
|
||||
multipart.addtyp (type);
|
||||
@ -370,33 +295,30 @@ public:
|
||||
std::string str () const
|
||||
{
|
||||
std::stringstream ss;
|
||||
for (size_t i = 0; i < m_parts.size(); i++)
|
||||
{
|
||||
for (size_t i = 0; i < m_parts.size (); i++) {
|
||||
const unsigned char *data = m_parts[i].data<unsigned char> ();
|
||||
size_t size = m_parts[i].size ();
|
||||
|
||||
// Dump the message as text or binary
|
||||
bool isText = true;
|
||||
for (size_t j = 0; j < size; j++)
|
||||
{
|
||||
if (data[j] < 32 || data[j] > 127)
|
||||
{
|
||||
for (size_t j = 0; j < size; j++) {
|
||||
if (data[j] < 32 || data[j] > 127) {
|
||||
isText = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
ss << "\n[" << std::dec << std::setw(3) << std::setfill('0') << size << "] ";
|
||||
if (size >= 1000)
|
||||
{
|
||||
ss << "\n[" << std::dec << std::setw (3) << std::setfill ('0')
|
||||
<< size << "] ";
|
||||
if (size >= 1000) {
|
||||
ss << "... (to big to print)";
|
||||
continue;
|
||||
}
|
||||
for (size_t j = 0; j < size; j++)
|
||||
{
|
||||
for (size_t j = 0; j < size; j++) {
|
||||
if (isText)
|
||||
ss << static_cast<char> (data[j]);
|
||||
else
|
||||
ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<short>(data[j]);
|
||||
ss << std::hex << std::setw (2) << std::setfill ('0')
|
||||
<< static_cast<short> (data[j]);
|
||||
}
|
||||
}
|
||||
return ss.str ();
|
||||
@ -426,7 +348,8 @@ inline std::ostream& operator<<(std::ostream& os, const multipart_t& msg)
|
||||
|
||||
#endif // ZMQ_HAS_RVALUE_REFS
|
||||
|
||||
#if defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11) && defined(ZMQ_HAVE_POLLER)
|
||||
#if defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11) \
|
||||
&& defined(ZMQ_HAVE_POLLER)
|
||||
class active_poller_t
|
||||
{
|
||||
public:
|
||||
@ -445,17 +368,18 @@ inline std::ostream& operator<<(std::ostream& os, const multipart_t& msg)
|
||||
{
|
||||
auto it = decltype (handlers)::iterator{};
|
||||
auto inserted = bool{};
|
||||
std::tie(it, inserted) = handlers.emplace (static_cast<void*>(socket), std::make_shared<handler_t> (std::move (handler)));
|
||||
try
|
||||
{
|
||||
base_poller.add (socket, events, inserted && *(it->second) ? it->second.get() : nullptr);
|
||||
std::tie (it, inserted) =
|
||||
handlers.emplace (static_cast<void *> (socket),
|
||||
std::make_shared<handler_t> (std::move (handler)));
|
||||
try {
|
||||
base_poller.add (socket, events,
|
||||
inserted && *(it->second) ? it->second.get ()
|
||||
: nullptr);
|
||||
need_rebuild |= inserted;
|
||||
}
|
||||
catch (const zmq::error_t&)
|
||||
{
|
||||
catch (const zmq::error_t &) {
|
||||
// rollback
|
||||
if (inserted)
|
||||
{
|
||||
if (inserted) {
|
||||
handlers.erase (static_cast<void *> (socket));
|
||||
}
|
||||
throw;
|
||||
@ -487,24 +411,20 @@ inline std::ostream& operator<<(std::ostream& os, const multipart_t& msg)
|
||||
}
|
||||
const int count = base_poller.wait_all (poller_events, timeout);
|
||||
if (count != 0) {
|
||||
std::for_each (poller_events.begin (), poller_events.begin () + count,
|
||||
std::for_each (poller_events.begin (),
|
||||
poller_events.begin () + count,
|
||||
[](zmq_poller_event_t &event) {
|
||||
if (event.user_data != NULL)
|
||||
(*reinterpret_cast<handler_t*> (event.user_data)) (event.events);
|
||||
(*reinterpret_cast<handler_t *> (
|
||||
event.user_data)) (event.events);
|
||||
});
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
bool empty () const
|
||||
{
|
||||
return handlers.empty ();
|
||||
}
|
||||
bool empty () const { return handlers.empty (); }
|
||||
|
||||
size_t size () const
|
||||
{
|
||||
return handlers.size ();
|
||||
}
|
||||
size_t size () const { return handlers.size (); }
|
||||
|
||||
private:
|
||||
bool need_rebuild{false};
|
||||
|
Loading…
x
Reference in New Issue
Block a user