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