Commit 573815da authored by Simon Giesecke's avatar Simon Giesecke

Problem: protected data member in encoder_base_t

Solution: encapsulate data member properly
parent 088fd65b
...@@ -54,14 +54,14 @@ namespace zmq ...@@ -54,14 +54,14 @@ namespace zmq
template <typename T> class encoder_base_t : public i_encoder template <typename T> class encoder_base_t : public i_encoder
{ {
public: public:
inline encoder_base_t (size_t bufsize_) : inline explicit encoder_base_t (size_t bufsize_) :
_write_pos (0), _write_pos (0),
_to_write (0), _to_write (0),
_next (NULL), _next (NULL),
_new_msg_flag (false), _new_msg_flag (false),
_buf_size (bufsize_), _buf_size (bufsize_),
_buf (static_cast<unsigned char *> (malloc (bufsize_))), _buf (static_cast<unsigned char *> (malloc (bufsize_))),
in_progress (NULL) _in_progress (NULL)
{ {
alloc_assert (_buf); alloc_assert (_buf);
} }
...@@ -78,7 +78,7 @@ template <typename T> class encoder_base_t : public i_encoder ...@@ -78,7 +78,7 @@ template <typename T> class encoder_base_t : public i_encoder
unsigned char *buffer = !*data_ ? _buf : *data_; unsigned char *buffer = !*data_ ? _buf : *data_;
size_t buffersize = !*data_ ? _buf_size : size_; size_t buffersize = !*data_ ? _buf_size : size_;
if (in_progress == NULL) if (in_progress () == NULL)
return 0; return 0;
size_t pos = 0; size_t pos = 0;
...@@ -88,11 +88,11 @@ template <typename T> class encoder_base_t : public i_encoder ...@@ -88,11 +88,11 @@ template <typename T> class encoder_base_t : public i_encoder
// in the buffer. // in the buffer.
if (!_to_write) { if (!_to_write) {
if (_new_msg_flag) { if (_new_msg_flag) {
int rc = in_progress->close (); int rc = _in_progress->close ();
errno_assert (rc == 0); errno_assert (rc == 0);
rc = in_progress->init (); rc = _in_progress->init ();
errno_assert (rc == 0); errno_assert (rc == 0);
in_progress = NULL; _in_progress = NULL;
break; break;
} }
(static_cast<T *> (this)->*_next) (); (static_cast<T *> (this)->*_next) ();
...@@ -130,8 +130,8 @@ template <typename T> class encoder_base_t : public i_encoder ...@@ -130,8 +130,8 @@ template <typename T> class encoder_base_t : public i_encoder
void load_msg (msg_t *msg_) void load_msg (msg_t *msg_)
{ {
zmq_assert (in_progress == NULL); zmq_assert (in_progress () == NULL);
in_progress = msg_; _in_progress = msg_;
(static_cast<T *> (this)->*_next) (); (static_cast<T *> (this)->*_next) ();
} }
...@@ -152,6 +152,8 @@ template <typename T> class encoder_base_t : public i_encoder ...@@ -152,6 +152,8 @@ template <typename T> class encoder_base_t : public i_encoder
_new_msg_flag = new_msg_flag_; _new_msg_flag = new_msg_flag_;
} }
msg_t *in_progress () { return _in_progress; }
private: private:
// Where to get the data to write from. // Where to get the data to write from.
unsigned char *_write_pos; unsigned char *_write_pos;
...@@ -172,8 +174,7 @@ template <typename T> class encoder_base_t : public i_encoder ...@@ -172,8 +174,7 @@ template <typename T> class encoder_base_t : public i_encoder
encoder_base_t (const encoder_base_t &); encoder_base_t (const encoder_base_t &);
void operator= (const encoder_base_t &); void operator= (const encoder_base_t &);
protected: msg_t *_in_progress;
msg_t *in_progress;
}; };
} }
......
...@@ -45,6 +45,6 @@ zmq::raw_encoder_t::~raw_encoder_t () ...@@ -45,6 +45,6 @@ zmq::raw_encoder_t::~raw_encoder_t ()
void zmq::raw_encoder_t::raw_message_ready () void zmq::raw_encoder_t::raw_message_ready ()
{ {
next_step (in_progress->data (), in_progress->size (), next_step (in_progress ()->data (), in_progress ()->size (),
&raw_encoder_t::raw_message_ready, true); &raw_encoder_t::raw_message_ready, true);
} }
...@@ -49,14 +49,14 @@ zmq::v1_encoder_t::~v1_encoder_t () ...@@ -49,14 +49,14 @@ zmq::v1_encoder_t::~v1_encoder_t ()
void zmq::v1_encoder_t::size_ready () void zmq::v1_encoder_t::size_ready ()
{ {
// Write message body into the buffer. // Write message body into the buffer.
next_step (in_progress->data (), in_progress->size (), next_step (in_progress ()->data (), in_progress ()->size (),
&v1_encoder_t::message_ready, true); &v1_encoder_t::message_ready, true);
} }
void zmq::v1_encoder_t::message_ready () void zmq::v1_encoder_t::message_ready ()
{ {
// Get the message size. // Get the message size.
size_t size = in_progress->size (); size_t size = in_progress ()->size ();
// Account for the 'flags' byte. // Account for the 'flags' byte.
size++; size++;
...@@ -66,12 +66,12 @@ void zmq::v1_encoder_t::message_ready () ...@@ -66,12 +66,12 @@ void zmq::v1_encoder_t::message_ready ()
// message size. In both cases 'flags' field follows. // message size. In both cases 'flags' field follows.
if (size < UCHAR_MAX) { if (size < UCHAR_MAX) {
_tmpbuf[0] = static_cast<unsigned char> (size); _tmpbuf[0] = static_cast<unsigned char> (size);
_tmpbuf[1] = (in_progress->flags () & msg_t::more); _tmpbuf[1] = (in_progress ()->flags () & msg_t::more);
next_step (_tmpbuf, 2, &v1_encoder_t::size_ready, false); next_step (_tmpbuf, 2, &v1_encoder_t::size_ready, false);
} else { } else {
_tmpbuf[0] = UCHAR_MAX; _tmpbuf[0] = UCHAR_MAX;
put_uint64 (_tmpbuf + 1, size); put_uint64 (_tmpbuf + 1, size);
_tmpbuf[9] = (in_progress->flags () & msg_t::more); _tmpbuf[9] = (in_progress ()->flags () & msg_t::more);
next_step (_tmpbuf, 10, &v1_encoder_t::size_ready, false); next_step (_tmpbuf, 10, &v1_encoder_t::size_ready, false);
} }
} }
...@@ -52,17 +52,17 @@ void zmq::v2_encoder_t::message_ready () ...@@ -52,17 +52,17 @@ void zmq::v2_encoder_t::message_ready ()
// Encode flags. // Encode flags.
unsigned char &protocol_flags = _tmp_buf[0]; unsigned char &protocol_flags = _tmp_buf[0];
protocol_flags = 0; protocol_flags = 0;
if (in_progress->flags () & msg_t::more) if (in_progress ()->flags () & msg_t::more)
protocol_flags |= v2_protocol_t::more_flag; protocol_flags |= v2_protocol_t::more_flag;
if (in_progress->size () > UCHAR_MAX) if (in_progress ()->size () > UCHAR_MAX)
protocol_flags |= v2_protocol_t::large_flag; protocol_flags |= v2_protocol_t::large_flag;
if (in_progress->flags () & msg_t::command) if (in_progress ()->flags () & msg_t::command)
protocol_flags |= v2_protocol_t::command_flag; protocol_flags |= v2_protocol_t::command_flag;
// Encode the message length. For messages less then 256 bytes, // Encode the message length. For messages less then 256 bytes,
// the length is encoded as 8-bit unsigned integer. For larger // the length is encoded as 8-bit unsigned integer. For larger
// messages, 64-bit unsigned integer in network byte order is used. // messages, 64-bit unsigned integer in network byte order is used.
const size_t size = in_progress->size (); const size_t size = in_progress ()->size ();
if (unlikely (size > UCHAR_MAX)) { if (unlikely (size > UCHAR_MAX)) {
put_uint64 (_tmp_buf + 1, size); put_uint64 (_tmp_buf + 1, size);
next_step (_tmp_buf, 9, &v2_encoder_t::size_ready, false); next_step (_tmp_buf, 9, &v2_encoder_t::size_ready, false);
...@@ -75,6 +75,6 @@ void zmq::v2_encoder_t::message_ready () ...@@ -75,6 +75,6 @@ void zmq::v2_encoder_t::message_ready ()
void zmq::v2_encoder_t::size_ready () void zmq::v2_encoder_t::size_ready ()
{ {
// Write message body into the buffer. // Write message body into the buffer.
next_step (in_progress->data (), in_progress->size (), next_step (in_progress ()->data (), in_progress ()->size (),
&v2_encoder_t::message_ready, true); &v2_encoder_t::message_ready, true);
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment