Remote Call Framework 3.1
SerializationProtocol_Base.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2019, 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: 3.1
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 <functional>
23 
24 #include <RCF/Config.hpp>
25 #include <RCF/Enums.hpp>
26 #include <RCF/ErrorMsg.hpp>
27 #include <RCF/MemStream.hpp>
28 #include <RCF/Tools.hpp>
29 
30 namespace RCF {
31 
32  class SerializationProtocolIn;
33  class SerializationProtocolOut;
34 
35  extern const SerializationProtocol DefaultSerializationProtocol;
36 
37  template<typename N>
38  class Protocol
39  {
40  public:
41 
42  static std::string getName()
43  {
44  return "";
45  }
46 
47  class In
48  {
49  public:
50  void bind(
51  MemIstream & is,
52  std::size_t archiveSize,
53  int runtimeVersion,
54  int archiveVersion,
55  SerializationProtocolIn & spIn)
56  {
57  RCF_UNUSED_VARIABLE(is);
58  RCF_UNUSED_VARIABLE(archiveSize);
59  RCF_UNUSED_VARIABLE(runtimeVersion);
60  RCF_UNUSED_VARIABLE(archiveVersion);
61  RCF_UNUSED_VARIABLE(spIn);
62  }
63 
64  void unbind()
65  {}
66 
67  template<typename T>
68  In &operator>>(T &t)
69  {
70  RCF_UNUSED_VARIABLE(t);
71  RCF_THROW(RCF::Exception(
72  RcfError_UnknownSerializationProtocol,
73  N::value));
74  return *this;
75  }
76  };
77 
78  class Out
79  {
80  public:
81  void bind(
82  MemOstream & os,
83  int runtimeVersion,
84  int archiveVersion,
85  SerializationProtocolOut & spOut)
86  {
87  RCF_UNUSED_VARIABLE(os);
88  RCF_UNUSED_VARIABLE(runtimeVersion);
89  RCF_UNUSED_VARIABLE(archiveVersion);
90  RCF_UNUSED_VARIABLE(spOut);
91  }
92 
93  void unbind()
94  {}
95 
96  template<typename T>
97  Out &operator<<(const T &t)
98  {
99  RCF_UNUSED_VARIABLE(t);
100  RCF_THROW(RCF::Exception(
101  RcfError_UnknownSerializationProtocol,
102  N::value));
103  return *this;
104  }
105  };
106  };
107 
108 
109  template<typename IS, typename OS>
110  class ProtocolImpl_SF
111  {
112  public:
113  class In
114  {
115  public:
116  void bind(
117  MemIstream & is,
118  std::size_t archiveSize,
119  int runtimeVersion,
120  int archiveVersion,
121  SerializationProtocolIn & spIn)
122  {
123  mAr.clearState();
124  mAr.setIs(is, archiveSize, runtimeVersion, archiveVersion);
125  mAr.setRemoteCallContext(&spIn);
126  }
127 
128  void unbind()
129  {}
130 
131  template<typename T> In &operator>>(T &t)
132  {
133  mAr >> t;
134  return *this;
135  }
136 
137  IS & getIStream()
138  {
139  return mAr;
140  }
141 
142  private:
143  IS mAr;
144 
145  };
146 
147  class Out
148  {
149  public:
150  void bind(
151  RCF::MemOstream &os,
152  int runtimeVersion,
153  int archiveVersion,
154  SerializationProtocolOut & spOut)
155  {
156  mAr.clearState();
157  mAr.setOs(os, runtimeVersion, archiveVersion);
158  mAr.setRemoteCallContext(&spOut);
159  mAr.suppressArchiveMetadata();
160  }
161 
162  void unbind()
163  {}
164 
165  template<typename T> Out &operator<<(const T &t)
166  {
167  mAr << t;
168  return *this;
169  }
170 
171  OS & getOStream() { return mAr; }
172 
173  private:
174  OS mAr;
175 
176  };
177  };
178 
179  template<typename IS, typename OS>
180  class ProtocolImpl_BSer
181  {
182  public:
183  class In
184  {
185  public:
186  void bind(
187  MemIstream & is,
188  std::size_t archiveSize,
189  int runtimeVersion,
190  int archiveVersion,
191  SerializationProtocolIn & spIn)
192  {
193  RCF_UNUSED_VARIABLE(archiveSize);
194  RCF_UNUSED_VARIABLE(runtimeVersion);
195  RCF_UNUSED_VARIABLE(archiveVersion);
196  RCF_UNUSED_VARIABLE(spIn);
197 
198  mAr.reset( new IS(is) );
199  invokeCustomizationCallback();
200  }
201 
202  void unbind()
203  {
204  mAr.reset();
205  }
206 
207  template<typename T> In &operator>>(T &t)
208  {
209  *mAr >> t;
210  return *this;
211  }
212 
213  typedef std::function<void(IS &)> CustomizationCallback;
214 
215  void setCustomizationCallback(CustomizationCallback customizationCallback)
216  {
217  mCustomizationCallback = customizationCallback;
218  invokeCustomizationCallback();
219  }
220 
221  private:
222  std::unique_ptr<IS> mAr;
223 
224  CustomizationCallback mCustomizationCallback;
225 
226  void invokeCustomizationCallback()
227  {
228  if (mCustomizationCallback)
229  {
230  mCustomizationCallback(*mAr);
231  }
232  }
233 
234  };
235 
236  class Out
237  {
238  public:
239  void bind(
240  RCF::MemOstream &os,
241  int runtimeVersion,
242  int archiveVersion,
243  SerializationProtocolOut & spOut)
244  {
245  RCF_UNUSED_VARIABLE(runtimeVersion);
246  RCF_UNUSED_VARIABLE(archiveVersion);
247  RCF_UNUSED_VARIABLE(spOut);
248 
249  mAr.reset( new OS(os) );
250  invokeCustomizationCallback();
251  }
252 
253  void unbind()
254  {
255  mAr.reset();
256  }
257 
258  template<typename T> Out &operator<<(const T &t)
259  {
260  *mAr << t;
261  return *this;
262  }
263 
264  typedef std::function<void(OS &)> CustomizationCallback;
265 
266  void setCustomizationCallback(CustomizationCallback customizationCallback)
267  {
268  mCustomizationCallback = customizationCallback;
269  invokeCustomizationCallback();
270  }
271 
272  private:
273  std::unique_ptr<OS> mAr;
274 
275  CustomizationCallback mCustomizationCallback;
276 
277  void invokeCustomizationCallback()
278  {
279  if (mCustomizationCallback)
280  {
281  mCustomizationCallback(*mAr);
282  }
283  }
284 
285  };
286  };
287 
288 } // namespace RCF
289 
290 #endif
SerializationProtocol
Describes which serialization implementation to use when serializing data structures for a remote cal...
Definition: Enums.hpp:167
Base class for all RCF exceptions.
Definition: Exception.hpp:64
Definition: AmiIoHandler.hpp:24