REC RPC library
rec_rpc_serialization_Primitive.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_PRIMITIVE_H_
28 #define _REC_RPC_SERIALIZATION_PRIMITIVE_H_
29 
31 
32 namespace rec
33 {
34  namespace rpc
35  {
36  namespace serialization
37  {
45  template< typename T >
47  {
48  public:
50  Primitive();
52  Primitive( const T& v );
53 
59  T value() const;
60 
66  void setValue( const T& value );
67 
73  const T& ref() const;
74 
80  T& ref();
81 
83  operator T&();
85  operator const T&() const;
86 
88  protected:
89  T _value;
92  private:
93  void serialize( QDataStream& ) const;
94  void deserialize( QDataStream& );
95  };
96 
97  template< typename T >
99  : _value( T() )
100  {
101  }
102 
103  template< typename T >
105  : _value( v )
106  {
107  }
108 
109  template< typename T >
111  {
112  return _value;
113  }
114 
115  template< typename T >
117  {
118  _value = value;
119  }
120 
121  template< typename T >
122  const T& Primitive< T >::ref() const
123  {
124  return _value;
125  }
126 
127  template< typename T >
129  {
130  return _value;
131  }
132 
133  template< typename T >
135  {
136  return _value;
137  }
138 
139  template< typename T >
140  Primitive< T >::operator const T&() const
141  {
142  return _value;
143  }
144 
145  template< typename T >
146  void Primitive< T >::serialize( QDataStream& stream ) const
147  {
148  stream << _value;
149  }
150 
151  template< typename T >
152  void Primitive< T >::deserialize( QDataStream& stream )
153  {
154  stream >> _value;
155  }
156 
158  template< typename T >
159  struct PrimitivePtr
160  {
161  typedef QSharedPointer< Primitive< T > > Type;
162  };
164  }
165  }
166 }
167 
179 #define DECLARE_PRIMITIVE_MEMBER( DATATYPE, NAME ) \
180  private: \
181  rec::rpc::serialization::PrimitivePtr< DATATYPE >::Type _##NAME; \
182  void create_##NAME() { _##NAME = rec::rpc::serialization::PrimitivePtr< DATATYPE >::Type( new rec::rpc::serialization::Primitive< DATATYPE > ); } \
183  public: \
184  DATATYPE& NAME() { return *_##NAME; } \
185  const DATATYPE& NAME() const { return *_##NAME; } \
186  void set##NAME( const DATATYPE& value ) { _##NAME->setValue( value ); }
187 
194 #define DEFINE_PRIMITIVE_PARAM( FUNCTIONNAME, DATATYPE ) DEFINE_SERIALIZABLE( FUNCTIONNAME##Param, rec::rpc::serialization::Primitive< DATATYPE > );
195 
202 #define DEFINE_PRIMITIVE_RESULT( FUNCTIONNAME, DATATYPE ) DEFINE_SERIALIZABLE( FUNCTIONNAME##Result, rec::rpc::serialization::Primitive< DATATYPE > );
203 
210 #define DEFINE_PRIMITIVE_TOPICDATA( TOPICNAME, DATATYPE ) DEFINE_SERIALIZABLE( topic##TOPICNAME##Data, rec::rpc::serialization::Primitive< DATATYPE > );
211 
212 #endif //_REC_RPC_SERIALIZATION_PRIMITIVE_H_
void setValue(const T &value)
Modify the stored value.
const T & ref() const
Access the stored value.
#define REC_RPC_VISIBILITY_DEFAULT
Definition: defines.h:50
T value() const
Retrieve the stored value.
Serialization class for primitive objects and values.