REC RPC library
rec_rpc_serialization_Serializable.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2011, REC Robotics Equipment Corporation GmbH, Planegg, Germany
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7 
8 - Redistributions of source code must retain the above copyright notice,
9  this list of conditions and the following disclaimer.
10 - Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation and/or
12  other materials provided with the distribution.
13 - Neither the name of the REC Robotics Equipment Corporation GmbH nor the names of
14  its contributors may be used to endorse or promote products derived from this software
15  without specific prior written permission.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
18 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
24 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26 
27 #ifndef _REC_RPC_SERIALIZATION_SERIALIZABLE_H_
28 #define _REC_RPC_SERIALIZATION_SERIALIZABLE_H_
29 
30 #include <QSharedPointer>
31 #include <QDataStream>
32 #include <QMetaType>
33 #include <typeinfo>
34 
35 #include "rec/rpc/defines.h"
36 
37 namespace rec
38 {
39  namespace rpc
40  {
41  namespace serialization
42  {
44  class Serializable;
45  typedef QSharedPointer< Serializable > SerializablePtr;
46  typedef QSharedPointer< const Serializable > SerializablePtrConst;
55  {
56  friend QDataStream& operator<<( QDataStream&, const Serializable& );
57  friend QDataStream& operator>>( QDataStream&, Serializable& );
58 
59  public:
61  static SerializablePtr empty;
62 
64  Serializable();
66  virtual ~Serializable();
67 
68  protected:
76  virtual void serialize( QDataStream& stream ) const;
77 
85  virtual void deserialize( QDataStream& stream );
86  };
87 
89  {
90  }
91 
93  {
94  }
95 
96  inline void Serializable::serialize( QDataStream& ) const
97  {
98  }
99 
100  inline void Serializable::deserialize( QDataStream& )
101  {
102  }
103 
112  inline QDataStream& operator<<( QDataStream& out, const Serializable& data )
113  {
114  data.serialize( out );
115  return out;
116  }
117 
126  inline QDataStream& operator>>( QDataStream& in, Serializable& data )
127  {
128  data.deserialize( in );
129  return in;
130  }
131  }
132  }
133 }
134 
135 Q_DECLARE_METATYPE( rec::rpc::serialization::SerializablePtr );
136 Q_DECLARE_METATYPE( rec::rpc::serialization::SerializablePtrConst );
137 
139 #define DEFINE_SERIALIZABLE( NAME, TYPE ) \
140  typedef TYPE NAME; \
141  typedef QSharedPointer< NAME > NAME##Ptr; \
142  typedef QSharedPointer< const NAME > NAME##PtrConst;
143 
150 #define DEFINE_EMPTY_PARAM( FUNCTIONNAME ) DEFINE_SERIALIZABLE( FUNCTIONNAME##Param, rec::rpc::serialization::Serializable );
151 
157 #define DEFINE_EMPTY_RESULT( FUNCTIONNAME ) DEFINE_SERIALIZABLE( FUNCTIONNAME##Result, rec::rpc::serialization::Serializable );
158 
164 #define DEFINE_EMPTY_TOPICDATA( TOPICNAME ) DEFINE_SERIALIZABLE( topic##TOPICNAME##Data, rec::rpc::serialization::Serializable );
165 
166 #endif //_REC_RPC_SERIALIZATION_SERIALIZABLE_H_
#define REC_RPC_EXPORT
Definition: defines.h:49
QDataStream & operator<<(QDataStream &out, const Serializable &data)
Serialize the data and write it into a data stream.
QDataStream & operator>>(QDataStream &in, Serializable &data)
Read data from a data stream and deserialize it.
virtual void serialize(QDataStream &stream) const