1

In the Chat example of the library, I see that there is a "chat_message" header declaring a class. In that class, I can see the following functions -among others-:

const char* body() const
  {
    return data_ + header_length;
  }

  char* body()
  {
    return data_ + header_length;
  }

  std::size_t body_length() const
  {
    return body_length_;
  }

I only see accessors here, no mutators. However, in the "chat_session" class, the handlers make use of this class even if it does not have any mutators. However, I guess that the member variables of "chat_message" are modified somehow. See this piece of code from "chat_session":

  void handle_read_header(const boost::system::error_code& error)
  {
    if (!error && read_msg_.decode_header())
    {
      boost::asio::async_read(socket_,
          boost::asio::buffer(read_msg_.body(), read_msg_.body_length()),
          boost::bind(&chat_session::handle_read_body, shared_from_this(),
            boost::asio::placeholders::error));
    }
    else
    {
      room_.leave(shared_from_this());
    }
  }

  void handle_read_body(const boost::system::error_code& error)
  {
    if (!error)
    {
      room_.deliver(read_msg_);
      boost::asio::async_read(socket_,
          boost::asio::buffer(read_msg_.data(), chat_message::header_length),
          boost::bind(&chat_session::handle_read_header, shared_from_this(),
            boost::asio::placeholders::error));
    }
    else
    {
      room_.leave(shared_from_this());
    }
  }

I guess that in the "async_read"s the received data is passed somehow to the object, but I don't understand how.

Could you explain to me how is data passed from the "async_read" to the object?

Thank you very much indeed.

Tanner Sansbury
  • 51,153
  • 9
  • 112
  • 169
MikelAlejoBR
  • 302
  • 5
  • 15
  • "I only see accessors here, no mutators." -- So what's `char* body()`? That gives you a pointer that lets you modify the data array. Same with `char* data()`. There's also `void body_length(std::size_t new_length)` to change the length. – Dan Mašek May 13 '16 at 08:51
  • 1
    Then in the `handle_*` functions, a `boost::asio::buffer` instance is created wrapping the internal array of `chat_message`, exposed using the non-const `body()` or `data()` members and either body length given by `body_length()` or the constant `header_length`. – Dan Mašek May 13 '16 at 09:01
  • @DanMašek oh, I didn't realize about that. I am kind of new with the language. I have been reading about the const functions vs the non-const functions and seen the basic differences between them. However, I don't understand why do we have const and non-const funcions. I would undertand using non-const functions to modify the member variables, and const ones to read them. Is this right,? BTW: Thanks for answering so fast. – MikelAlejoBR May 13 '16 at 11:03
  • To clarify: "However, I don't understand why do we have const and non-const functions HERE". Meaning this class. – MikelAlejoBR May 13 '16 at 11:36
  • 1
    "non-const to modify, const to read" -- yes, that's correct. It's about [const correctness](http://stackoverflow.com/questions/136880/sell-me-on-const-correctness). This way in `chat_client`, we can have method `void write(const chat_message& msg)` taking a const reference to a chat message. That signature tells me "I will only observe the values of the message, but I will NOT modify them.", and the compiler will enforce that (assuming all the code is correctly written -- of course you can shoot yourself in the foot if you desire :) ). – Dan Mašek May 13 '16 at 18:26
  • @DanMašek Thank you very much for your answers, now everything is clear :) – MikelAlejoBR May 13 '16 at 18:29

0 Answers0