RCFProto
 All Classes Functions Typedefs
SerializationProtocol_Base.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2013, Delta V Software. All rights reserved.
6 // http://www.deltavsoft.com
7 //
8 // RCF is distributed under dual licenses - closed source or GPL.
9 // Consult your particular license for conditions of use.
10 //
11 // If you have not purchased a commercial license, you are using RCF
12 // under GPL terms.
13 //
14 // Version: 2.0
15 // Contact: support <at> deltavsoft.com
16 //
17 //******************************************************************************
18 
19 #ifndef INCLUDE_RCF_PROTOCOL_PROTOCOL_HPP
20 #define INCLUDE_RCF_PROTOCOL_PROTOCOL_HPP
21 
22 #include <boost/function.hpp>
23 
24 #include <RCF/Config.hpp>
25 #include <RCF/Exception.hpp>
26 #include <RCF/Tools.hpp>
27 
28 #include <RCF/MemStream.hpp>
29 
30 namespace RCF {
31 
32  enum SerializationProtocol
33  {
34 
35  Sp_SfBinary = 1,
36  Sp_SfText = 2,
37  Sp_BsBinary = 3,
38  Sp_BsText = 4,
39  Sp_BsXml = 5,
40 
41  // Legacy identifiers (RCF 1.1).
42  SfBinary = 1,
43  SfText = 2,
44  BsBinary = 3,
45  BsText = 4,
46  BsXml = 5
47 
48  };
49 
50  class SerializationProtocolIn;
51  class SerializationProtocolOut;
52 
53  extern const SerializationProtocol DefaultSerializationProtocol;
54 
55  enum MarshalingProtocol
56  {
57  Mp_Rcf = 1
58  };
59 
60  extern const MarshalingProtocol DefaultMarshalingProtocol;
61 
62  template<typename N>
63  class Protocol
64  {
65  public:
66 
67  static std::string getName()
68  {
69  return "";
70  }
71 
72  class In
73  {
74  public:
75  void bind(
76  MemIstream & is,
77  std::size_t archiveSize,
78  int runtimeVersion,
79  int archiveVersion,
80  SerializationProtocolIn & spIn)
81  {
82  RCF_UNUSED_VARIABLE(is);
83  RCF_UNUSED_VARIABLE(archiveSize);
84  RCF_UNUSED_VARIABLE(runtimeVersion);
85  RCF_UNUSED_VARIABLE(archiveVersion);
86  RCF_UNUSED_VARIABLE(spIn);
87  }
88 
89  void unbind()
90  {}
91 
92  template<typename T>
93  In &operator>>(T &t)
94  {
95  RCF_UNUSED_VARIABLE(t);
96  RCF_THROW(RCF::SerializationException(_RcfError_UnknownSerializationProtocol(N::value)))(N::value);
97  return *this;
98  }
99  };
100 
101  class Out
102  {
103  public:
104  void bind(
105  MemOstream & os,
106  int runtimeVersion,
107  int archiveVersion,
108  SerializationProtocolOut & spOut)
109  {
110  RCF_UNUSED_VARIABLE(os);
111  RCF_UNUSED_VARIABLE(runtimeVersion);
112  RCF_UNUSED_VARIABLE(archiveVersion);
113  RCF_UNUSED_VARIABLE(spOut);
114  }
115 
116  void unbind()
117  {}
118 
119  template<typename T>
120  Out &operator<<(const T &t)
121  {
122  RCF_UNUSED_VARIABLE(t);
123  RCF_THROW(RCF::SerializationException(_RcfError_UnknownSerializationProtocol(N::value)))(N::value);
124  return *this;
125  }
126  };
127  };
128 
129 
130  template<typename IS, typename OS>
131  class ProtocolImpl_SF
132  {
133  public:
134  class In
135  {
136  public:
137  void bind(
138  MemIstream & is,
139  std::size_t archiveSize,
140  int runtimeVersion,
141  int archiveVersion,
142  SerializationProtocolIn & spIn)
143  {
144  mAr.clearState();
145  mAr.setIs(is, archiveSize, runtimeVersion, archiveVersion);
146  mAr.setRemoteCallContext(&spIn);
147  }
148 
149  void unbind()
150  {}
151 
152  template<typename T> In &operator>>(T &t)
153  {
154  mAr >> t;
155  return *this;
156  }
157 
158  IS & getIStream()
159  {
160  return mAr;
161  }
162 
163  private:
164  IS mAr;
165 
166  };
167 
168  class Out
169  {
170  public:
171  void bind(
172  RCF::MemOstream &os,
173  int runtimeVersion,
174  int archiveVersion,
175  SerializationProtocolOut & spOut)
176  {
177  mAr.clearState();
178  mAr.setOs(os, runtimeVersion, archiveVersion);
179  mAr.setRemoteCallContext(&spOut);
180  mAr.suppressArchiveMetadata();
181  }
182 
183  void unbind()
184  {}
185 
186  template<typename T> Out &operator<<(const T &t)
187  {
188  mAr << t;
189  return *this;
190  }
191 
192  OS & getOStream() { return mAr; }
193 
194  private:
195  OS mAr;
196 
197  };
198  };
199 
200  template<typename IS, typename OS>
201  class ProtocolImpl_BSer
202  {
203  public:
204  class In
205  {
206  public:
207  void bind(
208  MemIstream & is,
209  std::size_t archiveSize,
210  int runtimeVersion,
211  int archiveVersion,
212  SerializationProtocolIn & spIn)
213  {
214  RCF_UNUSED_VARIABLE(archiveSize);
215  RCF_UNUSED_VARIABLE(runtimeVersion);
216  RCF_UNUSED_VARIABLE(archiveVersion);
217  RCF_UNUSED_VARIABLE(spIn);
218 
219  mAr.reset( new IS(is) );
220  invokeCustomizationCallback();
221  }
222 
223  void unbind()
224  {
225  mAr.reset();
226  }
227 
228  template<typename T> In &operator>>(T &t)
229  {
230  *mAr >> t;
231  return *this;
232  }
233 
234  typedef boost::function1<void, IS &> CustomizationCallback;
235 
236  void setCustomizationCallback(CustomizationCallback customizationCallback)
237  {
238  mCustomizationCallback = customizationCallback;
239  invokeCustomizationCallback();
240  }
241 
242  private:
243  std::auto_ptr<IS> mAr;
244 
245  CustomizationCallback mCustomizationCallback;
246 
247  void invokeCustomizationCallback()
248  {
249  if (mCustomizationCallback)
250  {
251  mCustomizationCallback(*mAr);
252  }
253  }
254 
255  };
256 
257  class Out
258  {
259  public:
260  void bind(
261  RCF::MemOstream &os,
262  int runtimeVersion,
263  int archiveVersion,
264  SerializationProtocolOut & spOut)
265  {
266  RCF_UNUSED_VARIABLE(runtimeVersion);
267  RCF_UNUSED_VARIABLE(archiveVersion);
268  RCF_UNUSED_VARIABLE(spOut);
269 
270  mAr.reset( new OS(os) );
271  invokeCustomizationCallback();
272  }
273 
274  void unbind()
275  {
276  mAr.reset();
277  }
278 
279  template<typename T> Out &operator<<(const T &t)
280  {
281  *mAr << t;
282  return *this;
283  }
284 
285  typedef boost::function1<void, OS &> CustomizationCallback;
286 
287  void setCustomizationCallback(CustomizationCallback customizationCallback)
288  {
289  mCustomizationCallback = customizationCallback;
290  invokeCustomizationCallback();
291  }
292 
293  private:
294  std::auto_ptr<OS> mAr;
295 
296  CustomizationCallback mCustomizationCallback;
297 
298  void invokeCustomizationCallback()
299  {
300  if (mCustomizationCallback)
301  {
302  mCustomizationCallback(*mAr);
303  }
304  }
305 
306  };
307  };
308 
309 } // namespace RCF
310 
311 #endif