0
0
mirror of https://github.com/zeromq/libzmq.git synced 2024-12-27 15:41:05 +08:00

Problem: protected data member in encoder_base_t

Solution: encapsulate data member properly
This commit is contained in:
Simon Giesecke 2018-05-30 09:57:46 +02:00
parent 088fd65bf2
commit 573815da83
4 changed files with 21 additions and 20 deletions

View File

@ -54,14 +54,14 @@ namespace zmq
template <typename T> class encoder_base_t : public i_encoder
{
public:
inline encoder_base_t (size_t bufsize_) :
inline explicit encoder_base_t (size_t bufsize_) :
_write_pos (0),
_to_write (0),
_next (NULL),
_new_msg_flag (false),
_buf_size (bufsize_),
_buf (static_cast<unsigned char *> (malloc (bufsize_))),
in_progress (NULL)
_in_progress (NULL)
{
alloc_assert (_buf);
}
@ -78,7 +78,7 @@ template <typename T> class encoder_base_t : public i_encoder
unsigned char *buffer = !*data_ ? _buf : *data_;
size_t buffersize = !*data_ ? _buf_size : size_;
if (in_progress == NULL)
if (in_progress () == NULL)
return 0;
size_t pos = 0;
@ -88,11 +88,11 @@ template <typename T> class encoder_base_t : public i_encoder
// in the buffer.
if (!_to_write) {
if (_new_msg_flag) {
int rc = in_progress->close ();
int rc = _in_progress->close ();
errno_assert (rc == 0);
rc = in_progress->init ();
rc = _in_progress->init ();
errno_assert (rc == 0);
in_progress = NULL;
_in_progress = NULL;
break;
}
(static_cast<T *> (this)->*_next) ();
@ -130,8 +130,8 @@ template <typename T> class encoder_base_t : public i_encoder
void load_msg (msg_t *msg_)
{
zmq_assert (in_progress == NULL);
in_progress = msg_;
zmq_assert (in_progress () == NULL);
_in_progress = msg_;
(static_cast<T *> (this)->*_next) ();
}
@ -152,6 +152,8 @@ template <typename T> class encoder_base_t : public i_encoder
_new_msg_flag = new_msg_flag_;
}
msg_t *in_progress () { return _in_progress; }
private:
// Where to get the data to write from.
unsigned char *_write_pos;
@ -172,8 +174,7 @@ template <typename T> class encoder_base_t : public i_encoder
encoder_base_t (const encoder_base_t &);
void operator= (const encoder_base_t &);
protected:
msg_t *in_progress;
msg_t *_in_progress;
};
}

View File

@ -45,6 +45,6 @@ zmq::raw_encoder_t::~raw_encoder_t ()
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);
}

View File

@ -49,14 +49,14 @@ zmq::v1_encoder_t::~v1_encoder_t ()
void zmq::v1_encoder_t::size_ready ()
{
// 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);
}
void zmq::v1_encoder_t::message_ready ()
{
// Get the message size.
size_t size = in_progress->size ();
size_t size = in_progress ()->size ();
// Account for the 'flags' byte.
size++;
@ -66,12 +66,12 @@ void zmq::v1_encoder_t::message_ready ()
// message size. In both cases 'flags' field follows.
if (size < UCHAR_MAX) {
_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);
} else {
_tmpbuf[0] = UCHAR_MAX;
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);
}
}

View File

@ -52,17 +52,17 @@ void zmq::v2_encoder_t::message_ready ()
// Encode flags.
unsigned char &protocol_flags = _tmp_buf[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;
if (in_progress->size () > UCHAR_MAX)
if (in_progress ()->size () > UCHAR_MAX)
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;
// Encode the message length. For messages less then 256 bytes,
// the length is encoded as 8-bit unsigned integer. For larger
// 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)) {
put_uint64 (_tmp_buf + 1, size);
next_step (_tmp_buf, 9, &v2_encoder_t::size_ready, false);
@ -75,6 +75,6 @@ void zmq::v2_encoder_t::message_ready ()
void zmq::v2_encoder_t::size_ready ()
{
// 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);
}