+ Reply to Thread
Results 1 to 1 of 1

Thread: Sending containers through C++ message classes

  1. #1
    Young's Avatar
    Young is offline Junior Member
    Join Date
    March 5th, 2008
    Location
    Toronto, Canada
    Posts
    58
    Blog Entries
    1
    Downloads
    7
    Uploads
    0

    Default Sending containers through C++ message classes

    It is often the case that you want to send a container such as Vector through C++ message class. Unlike Java or .NET that has container implementing serializable interface, you need to manually serialize your container in onSerialize and onDeserialize methods of your message class.

    Here is the sample snippet of code.

    Code:
    void MyMessage::onSerialize(OutputStreamPtr &stream) throw (SoamException)
    {
        stream->write(m_int);
        stream->write(m_isSync);
        stream->write(m_vectorSize);
        for (int i=0; i<m_vectorSize; i++)
        {
            stream->write(m_inputVector.at(i));
        }
    }
    
    void MyMessage::onDeserialize(InputStreamPtr &stream) throw (SoamException)
    {
        stream->read(m_int);
        stream->read(m_isSync);
        stream->read(m_vectorSize);
        for (int i=0; i<m_vectorSize; i++)
        {
            std::string f;
    
            stream->read(f);
            m_inputVector.push_back(f);
        }
    }
    
    Make sure you write the size of you container to the output stream. Also, make sure you write each element of the container one by one to the output stream. In the same way, read the size and each element of your container from the input stream.
    If you fail to serialize and deserialize the size of your container, it will result in core dump.
    Last edited by Young; March 13th, 2008 at 03:19 PM.

+ Reply to Thread

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts