Remote Call Framework 3.1
Stream.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_SF_STREAM_HPP
20 #define INCLUDE_SF_STREAM_HPP
21 
22 #include <map>
23 #include <string>
24 
25 #include <RCF/Export.hpp>
26 
27 #include <SF/DataPtr.hpp>
28 #include <SF/Encoding.hpp>
29 #include <SF/I_Stream.hpp>
30 
31 #include <iosfwd>
32 
33 namespace RCF {
34 
35  class SerializationProtocolIn;
36  class SerializationProtocolOut;
37 
38  class MemIstream;
39  class MemOstream;
40 
41 }
42 namespace SF {
43 
44  //**************************************************
45  // Encoding of object data
46 
47  template<typename E>
48  class Encoding : public I_Encoding
49  {
50  public:
51  virtual UInt32 getCount(
52  DataPtr & data,
53  const std::type_info & type)
54  {
55  return countElements( (E *) 0, data, type);
56  }
57 
58  virtual void toData(
59  DataPtr & data,
60  void * pvObject,
61  const std::type_info & type,
62  int nCount)
63  {
64  encodeElements( (E *) 0, data, pvObject, type, nCount);
65  }
66 
67  virtual void toObject(
68  DataPtr & data,
69  void * pvObject,
70  const std::type_info & type,
71  int nCount)
72  {
73  decodeElements( (E *) 0, data, pvObject, type, nCount);
74  }
75  };
76 
77  //**************************************************
78  // Context handling
79 
80  class RCF_EXPORT ContextRead
81  {
82  public:
83  ContextRead();
84  ~ContextRead();
85  void add(SF::UInt32 nid, const ObjectId &id);
86  void add(void *ptr, const std::type_info &objType, void *pObj);
87  bool query(SF::UInt32 nid, ObjectId &id);
88  bool query(void *ptr, const std::type_info &objType, void *&pObj);
89  void clear();
90 
91  void setEnabled(bool enabled);
92  bool getEnabled() const;
93 
94  private:
95  bool mEnabled;
96  std::unique_ptr<std::map<UInt32, ObjectId> > mNidToIdMap;
97  std::unique_ptr<std::map<std::string, std::map< void *, void * > > > mTypeToObjMap;
98  };
99 
100  class RCF_EXPORT ContextWrite
101  {
102  public:
103  ContextWrite();
104  ~ContextWrite();
105  void setEnabled(bool enabled);
106  bool getEnabled() const;
107  void add(const ObjectId &id, UInt32 &nid);
108  bool query(const ObjectId &id, UInt32 &nid);
109  void clear();
110  private:
111  bool mEnabled;
112  UInt32 mCurrentId;
113  std::unique_ptr<std::map<ObjectId, UInt32> > mIdToNidMap;
114  };
115 
116  //**************************************************
117  // Stream local storage
118 
119  class RCF_EXPORT LocalStorage : Noncopyable
120  {
121  public:
122  LocalStorage();
123  ~LocalStorage();
124  void setNode(Node *);
125  Node *getNode();
126 
127  private:
128  Node * mpNode;
129  };
130 
131  //****************************************************
132  // Base stream classes
133 
134  class Node;
135  class SerializerBase;
136 
138  class RCF_EXPORT IStream : Noncopyable
139  {
140  public:
141 
142  IStream();
143 
144  IStream(
145  RCF::MemIstream & is,
146  std::size_t archiveSize = 0,
147  int runtimeVersion = 0,
148  int archiveVersion = 0);
149 
151  IStream(
152  std::istream & is,
153  std::size_t archiveSize = 0,
154  int runtimeVersion = 0,
155  int archiveVersion = 0);
156 
157  virtual ~IStream();
158 
159  void setIs(
160  std::istream & is,
161  std::size_t archiveSize = 0,
162  int runtimeVersion = 0,
163  int archiveVersion = 0);
164 
165  void clearState();
166 
167  UInt32 read(Byte8 *pBytes, UInt32 nLength);
168 
169  bool verifyAgainstArchiveSize(std::size_t bytesToRead);
170 
171  bool begin(Node &node);
172  bool get(DataPtr &value);
173  void end();
174  UInt32 read_int(UInt32 &n);
175  UInt32 read_byte(Byte8 &byte);
176  void putback_byte(Byte8 byte);
177 
178  std::size_t tell() const;
179  void seek(std::size_t newPos);
180 
181  virtual I_Encoding &
182  getEncoding() = 0;
183 
185  int getRuntimeVersion();
186 
188  int getArchiveVersion();
189 
191  void setArchiveVersion(int archiveVersion);
192 
194  void setRuntimeVersion(int runtimeVersion);
195 
196  void ignoreVersionStamp(bool ignore = true);
197 
198  void setRemoteCallContext(
199  RCF::SerializationProtocolIn * pSerializationProtocolIn);
200 
201  RCF::SerializationProtocolIn *
202  getRemoteCallContext();
203 
204  ContextRead & getTrackingContext();
205  const ContextRead & getTrackingContext() const;
206  LocalStorage & getLocalStorage();
207 
208  void setEnablePointerTracking(bool enable);
209  bool getEnablePointerTracking() const;
210 
211  // Streaming operators.
212 
214  template<typename T>
215  IStream & operator>>(T &t);
216 
218  template<typename T>
219  IStream & operator>>(const T &t);
220 
221  private:
222 
223  ContextRead mContextRead;
224  LocalStorage mLocalStorage;
225 
226  std::istream * mpIs;
227  std::size_t mArchiveSize;
228  int mRuntimeVersion;
229  int mArchiveVersion;
230  bool mIgnoreVersionStamp;
231 
232  RCF::SerializationProtocolIn * mpSerializationProtocolIn;
233  };
234 
236  class RCF_EXPORT OStream : Noncopyable
237  {
238  public:
239  OStream();
240 
241  OStream(
242  RCF::MemOstream & os,
243  int runtimeVersion = 0,
244  int archiveVersion = 0);
245 
247  OStream(
248  std::ostream & os,
249  int runtimeVersion = 0,
250  int archiveVersion = 0);
251 
252  virtual ~OStream();
253 
254  void setOs(
255  std::ostream & os,
256  int runtimeVersion = 0,
257  int archiveVersion = 0);
258 
259  void clearState();
260 
261  UInt32 writeRaw(const Byte8 *pBytes, UInt32 nLength);
262 
263  void begin(const Node &node);
264  void put(const DataPtr &value);
265  void end();
266  UInt32 write_int(UInt32 n);
267  UInt32 write_byte(Byte8 byte);
268  UInt32 write(const Byte8 *pBytes, UInt32 nLength);
269 
270  virtual I_Encoding &
271  getEncoding() = 0;
272 
274  int getRuntimeVersion();
275 
277  int getArchiveVersion();
278 
280  void setArchiveVersion(int archiveVersion);
281 
283  void setRuntimeVersion(int runtimeVersion);
284 
285  void suppressArchiveMetadata(bool suppress = true);
286 
287  void setRemoteCallContext(
288  RCF::SerializationProtocolOut * pSerializationProtocolOut);
289 
290  RCF::SerializationProtocolOut *
291  getRemoteCallContext();
292 
293  ContextWrite & getTrackingContext();
294  const ContextWrite & getTrackingContext() const;
295 
296  LocalStorage & getLocalStorage();
297 
298  void setEnablePointerTracking(bool enable);
299  bool getEnablePointerTracking() const;
300 
301  // Streaming operator.
302 
304  template<typename T>
305  OStream & operator<<(const T &t);
306 
307  private:
308 
309  void writeArchiveMetadata();
310 
311  ContextWrite mContextWrite;
312  LocalStorage mLocalStorage;
313 
314  std::ostream * mpOs;
315  int mRuntimeVersion;
316  int mArchiveVersion;
317  bool mSuppressArchiveMetadata;
318  bool mArchiveMetadataWritten;
319 
320  RCF::SerializationProtocolOut * mpSerializationProtocolOut;
321  };
322 
323 } // namespace SF
324 
325 #include <SF/Archive.hpp>
326 #include <SF/Serializer.hpp>
327 
328 namespace SF {
329 
330  template<typename T>
331  IStream & IStream::operator>>(T &t)
332  {
333  Archive ar(Archive::READ, this);
334  ar & t;
335  return *this;
336  }
337 
338  template<typename T>
339  IStream & IStream::operator>>(const T &t)
340  {
341  Archive ar(Archive::READ, this);
342  ar & t;
343  return *this;
344  }
345 
346  template<typename T>
347  OStream & OStream::operator<<(const T &t)
348  {
349  Archive ar(Archive::WRITE, this);
350  ar & t;
351  return *this;
352  }
353 
354 } // namespace SF
355 
356 #endif // !INCLUDE_SF_STREAM_HPP
Represents an archive, in which serialized objects are stored.
Definition: Archive.hpp:32
Definition: ByteBuffer.hpp:189
Base class for output streams using SF serialization. Use operator <<() to serialize objects into the...
Definition: Stream.hpp:236
Definition: AmiIoHandler.hpp:24
Base class for input streams using SF serialization. Use operator >>() to deserialize objects from th...
Definition: Stream.hpp:138