Remote Call Framework 3.0
Stream.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2018, 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.0
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  virtual I_Encoding &
179  getEncoding() = 0;
180 
182  int getRuntimeVersion();
183 
185  int getArchiveVersion();
186 
188  void setArchiveVersion(int archiveVersion);
189 
191  void setRuntimeVersion(int runtimeVersion);
192 
193  void ignoreVersionStamp(bool ignore = true);
194 
195  void setRemoteCallContext(
196  RCF::SerializationProtocolIn * pSerializationProtocolIn);
197 
198  RCF::SerializationProtocolIn *
199  getRemoteCallContext();
200 
201  ContextRead & getTrackingContext();
202  const ContextRead & getTrackingContext() const;
203  LocalStorage & getLocalStorage();
204 
205  void setEnablePointerTracking(bool enable);
206  bool getEnablePointerTracking() const;
207 
208  // Streaming operators.
209 
211  template<typename T>
212  IStream & operator>>(T &t);
213 
215  template<typename T>
216  IStream & operator>>(const T &t);
217 
218  private:
219 
220  ContextRead mContextRead;
221  LocalStorage mLocalStorage;
222 
223  std::istream * mpIs;
224  std::size_t mArchiveSize;
225  int mRuntimeVersion;
226  int mArchiveVersion;
227  bool mIgnoreVersionStamp;
228 
229  RCF::SerializationProtocolIn * mpSerializationProtocolIn;
230  };
231 
233  class RCF_EXPORT OStream : Noncopyable
234  {
235  public:
236  OStream();
237 
238  OStream(
239  RCF::MemOstream & os,
240  int runtimeVersion = 0,
241  int archiveVersion = 0);
242 
244  OStream(
245  std::ostream & os,
246  int runtimeVersion = 0,
247  int archiveVersion = 0);
248 
249  virtual ~OStream();
250 
251  void setOs(
252  std::ostream & os,
253  int runtimeVersion = 0,
254  int archiveVersion = 0);
255 
256  void clearState();
257 
258  UInt32 writeRaw(const Byte8 *pBytes, UInt32 nLength);
259 
260  void begin(const Node &node);
261  void put(const DataPtr &value);
262  void end();
263  UInt32 write_int(UInt32 n);
264  UInt32 write_byte(Byte8 byte);
265  UInt32 write(const Byte8 *pBytes, UInt32 nLength);
266 
267  virtual I_Encoding &
268  getEncoding() = 0;
269 
271  int getRuntimeVersion();
272 
274  int getArchiveVersion();
275 
277  void setArchiveVersion(int archiveVersion);
278 
280  void setRuntimeVersion(int runtimeVersion);
281 
282  void suppressArchiveMetadata(bool suppress = true);
283 
284  void setRemoteCallContext(
285  RCF::SerializationProtocolOut * pSerializationProtocolOut);
286 
287  RCF::SerializationProtocolOut *
288  getRemoteCallContext();
289 
290  ContextWrite & getTrackingContext();
291  const ContextWrite & getTrackingContext() const;
292 
293  LocalStorage & getLocalStorage();
294 
295  void setEnablePointerTracking(bool enable);
296  bool getEnablePointerTracking() const;
297 
298  // Streaming operator.
299 
301  template<typename T>
302  OStream & operator<<(const T &t);
303 
304  private:
305 
306  void writeArchiveMetadata();
307 
308  ContextWrite mContextWrite;
309  LocalStorage mLocalStorage;
310 
311  std::ostream * mpOs;
312  int mRuntimeVersion;
313  int mArchiveVersion;
314  bool mSuppressArchiveMetadata;
315  bool mArchiveMetadataWritten;
316 
317  RCF::SerializationProtocolOut * mpSerializationProtocolOut;
318  };
319 
320 } // namespace SF
321 
322 #include <SF/Archive.hpp>
323 #include <SF/Serializer.hpp>
324 
325 namespace SF {
326 
327  template<typename T>
328  IStream & IStream::operator>>(T &t)
329  {
330  Archive ar(Archive::READ, this);
331  ar & t;
332  return *this;
333  }
334 
335  template<typename T>
336  IStream & IStream::operator>>(const T &t)
337  {
338  Archive ar(Archive::READ, this);
339  ar & t;
340  return *this;
341  }
342 
343  template<typename T>
344  OStream & OStream::operator<<(const T &t)
345  {
346  Archive ar(Archive::WRITE, this);
347  ar & t;
348  return *this;
349  }
350 
351 } // namespace SF
352 
353 #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:233
Definition: AmiIoHandler.hpp:24
Base class for input streams using SF serialization. Use operator >>() to deserialize objects from th...
Definition: Stream.hpp:138