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