Main MRPT website > C++ reference
MRPT logo
CMessage.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef CMessage_H
29 #define CMessage_H
30 
31 #include <mrpt/utils/utils_defs.h>
33 
34 /*---------------------------------------------------------------
35  Class
36  ---------------------------------------------------------------*/
37 namespace mrpt
38 {
39  namespace utils
40  {
41  /** A class that contain generic messages, that can be sent and received from a "CClientTCPSocket" object.
42  * A message consists of a "header" (or type), and a "body" (or content).
43  * Apart from arbitrary data, specific methods are provided for easing the serialization of MRPT's "CSerializable" objects.
44  * This class is also used for passing data to hardware interfaces (see )
45  * \sa CClientTCPSocket
46  * \ingroup mrpt_base_grp
47  */
49  {
50  public:
51  /** An identifier of the message type.
52  */
53  uint32_t type;
54 
55  /** The contents of the message (memory is automatically handled by the std::vector object)
56  */
57  std::vector<unsigned char> content;
58 
59  /** A method for serializing a MRPT's object into the content.
60  * Any modification to data in "content" after this will corrupt the object serialization.
61  * Member "type" is unmodified in this method.
62  */
63  void serializeObject( CSerializable *obj );
64 
65  /** A method that parse the data in the message into an existing object.
66  * Note that the class of the object must be known and must match the one of the serialized object.
67  * \except std::exception On corrupt data, unknown serialized objects, unknown serialized object version, non-matching classes,...
68  */
69  void deserializeIntoExistingObject( CSerializable *obj );
70 
71  /** A method that parse the data in the message into a new object of (a priori) unknown class.
72  * The pointer will contain on return a copy of the reconstructed object. Deleting this object when
73  * no longer required is the responsability of the user. Note that previous contents of the pointer
74  * will be ignored (it should be NULL).
75  * \except std::exception On corrupt data, unknown serialized objects, unknown serialized object version,...
76  */
77  void deserializeIntoNewObject( CSerializablePtr &obj );
78 
79  /** Sets the contents of the message from a string
80  * \sa getContentAsString
81  */
82  void setContentFromString( const std::string &str );
83 
84  /** Gets the contents of the message as a string
85  * \sa setContentFromString
86  */
87  void getContentAsString( std::string &str );
88 
89  /** Sets the contents of the message from a "void*" (the pointer itself becomes the message) - This is intended for inter-thread comms only.
90  * \sa getContentAsPointer
91  */
92  void setContentFromPointer( void * ptr );
93 
94  /** Gets the contents of the message as a "void*" (the pointer itself is the message) - This is intended for inter-thread comms only.
95  * \sa setContentFromPointer
96  */
97  void * getContentAsPointer() const;
98 
99  /** Sets the contents of the message from an arbitary structure - This is intended for inter-thread comms only, the message will be not cross-platform.
100  * \sa getContentAsStruct
101  */
102  template <class T>
103  void setContentFromStruct( const T &data )
104  {
105  content.resize( sizeof(data) );
106  T * ptr = reinterpret_cast< T* >( &content[0] );
107  *ptr = data;
108  }
109 
110  /** Gets the contents of the message as an arbitary structure - This is intended for inter-thread comms only, the message will be not cross-platform.
111  * \sa setContentFromStruct
112  */
113  template <class T>
114  void getContentAsStruct( T &data ) const
115  {
116  MRPT_START
117  ASSERT_(content.size() == sizeof(data) );
118  data = * reinterpret_cast< T* >( &content[0] );
119  MRPT_END
120  }
121 
122 
123  }; // End of class
124 
125  } // End of namespace
126 } // End of namespace
127 
128 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013