| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- //******************************************************************************
- // RCF - Remote Call Framework
- //
- // Copyright (c) 2005 - 2020, Delta V Software. All rights reserved.
- // http://www.deltavsoft.com
- //
- // RCF is distributed under dual licenses - closed source or GPL.
- // Consult your particular license for conditions of use.
- //
- // If you have not purchased a commercial license, you are using RCF
- // under GPL terms.
- //
- // Version: 3.2
- // Contact: support <at> deltavsoft.com
- //
- //******************************************************************************
- #ifndef INCLUDE_SF_STREAM_HPP
- #define INCLUDE_SF_STREAM_HPP
- #include <map>
- #include <string>
- #include <RCF/Export.hpp>
- #include <SF/DataPtr.hpp>
- #include <SF/Encoding.hpp>
- #include <SF/I_Stream.hpp>
- #include <iosfwd>
- namespace RCF {
- class SerializationProtocolIn;
- class SerializationProtocolOut;
- class MemIstream;
- class MemOstream;
- }
- namespace SF {
- //**************************************************
- // Encoding of object data
- template<typename E>
- class Encoding : public I_Encoding
- {
- public:
- virtual UInt32 getCount(
- DataPtr & data,
- const std::type_info & type)
- {
- return countElements( (E *) 0, data, type);
- }
- virtual void toData(
- DataPtr & data,
- void * pvObject,
- const std::type_info & type,
- int nCount)
- {
- encodeElements( (E *) 0, data, pvObject, type, nCount);
- }
- virtual void toObject(
- DataPtr & data,
- void * pvObject,
- const std::type_info & type,
- int nCount)
- {
- decodeElements( (E *) 0, data, pvObject, type, nCount);
- }
- };
- //**************************************************
- // Context handling
- class RCF_EXPORT ContextRead
- {
- public:
- ContextRead();
- ~ContextRead();
- void add(SF::UInt32 nid, const ObjectId &id);
- void add(void *ptr, const std::type_info &objType, void *pObj);
- bool query(SF::UInt32 nid, ObjectId &id);
- bool query(void *ptr, const std::type_info &objType, void *&pObj);
- void clear();
- void setEnabled(bool enabled);
- bool getEnabled() const;
- private:
- bool mEnabled;
- std::unique_ptr<std::map<UInt32, ObjectId> > mNidToIdMap;
- std::unique_ptr<std::map<std::string, std::map< void *, void * > > > mTypeToObjMap;
- };
- class RCF_EXPORT ContextWrite
- {
- public:
- ContextWrite();
- ~ContextWrite();
- void setEnabled(bool enabled);
- bool getEnabled() const;
- void add(const ObjectId &id, UInt32 &nid);
- bool query(const ObjectId &id, UInt32 &nid);
- void clear();
- private:
- bool mEnabled;
- UInt32 mCurrentId;
- std::unique_ptr<std::map<ObjectId, UInt32> > mIdToNidMap;
- };
- //**************************************************
- // Stream local storage
- class RCF_EXPORT LocalStorage : Noncopyable
- {
- public:
- LocalStorage();
- ~LocalStorage();
- void setNode(Node *);
- Node *getNode();
- private:
- Node * mpNode;
- };
- //****************************************************
- // Base stream classes
- class Node;
- class SerializerBase;
- /// Base class for input streams using SF serialization. Use operator >>() to deserialize objects from the stream.
- class RCF_EXPORT IStream : Noncopyable
- {
- public:
- IStream();
- IStream(
- RCF::MemIstream & is,
- std::size_t archiveSize = 0,
- int runtimeVersion = 0,
- int archiveVersion = 0);
- /// Constructs an IStream from a std::istream. Serialized data will be read from the std::istream.
- IStream(
- std::istream & is,
- std::size_t archiveSize = 0,
- int runtimeVersion = 0,
- int archiveVersion = 0);
- virtual ~IStream();
- void setIs(
- std::istream & is,
- std::size_t archiveSize = 0,
- int runtimeVersion = 0,
- int archiveVersion = 0);
- void clearState();
- UInt32 read(Byte8 *pBytes, UInt32 nLength);
- bool verifyAgainstArchiveSize(std::size_t bytesToRead);
- bool begin(Node &node);
- bool get(DataPtr &value);
- void end();
- UInt32 read_int(UInt32 &n);
- UInt32 read_byte(Byte8 &byte);
- void putback_byte(Byte8 byte);
- std::size_t tell() const;
- void seek(std::size_t newPos);
- virtual I_Encoding &
- getEncoding() = 0;
- /// Gets the RCF runtime version associated with this stream.
- int getRuntimeVersion();
- /// Gets the archive version associated with this stream.
- int getArchiveVersion();
- /// Sets the archive version associated with this stream.
- void setArchiveVersion(int archiveVersion);
- /// Sets the RCF runtime version associated with this stream.
- void setRuntimeVersion(int runtimeVersion);
- void ignoreVersionStamp(bool ignore = true);
- void setRemoteCallContext(
- RCF::SerializationProtocolIn * pSerializationProtocolIn);
- RCF::SerializationProtocolIn *
- getRemoteCallContext();
- ContextRead & getTrackingContext();
- const ContextRead & getTrackingContext() const;
- LocalStorage & getLocalStorage();
- void setEnablePointerTracking(bool enable);
- bool getEnablePointerTracking() const;
- // Streaming operators.
- /// Deserialize an object from the stream.
- template<typename T>
- IStream & operator>>(T &t);
- /// Deserialize an object from the stream.
- template<typename T>
- IStream & operator>>(const T &t);
- private:
- ContextRead mContextRead;
- LocalStorage mLocalStorage;
- std::istream * mpIs;
- std::size_t mArchiveSize;
- int mRuntimeVersion;
- int mArchiveVersion;
- bool mIgnoreVersionStamp;
- RCF::SerializationProtocolIn * mpSerializationProtocolIn;
- };
- /// Base class for output streams using SF serialization. Use operator <<() to serialize objects into the stream.
- class RCF_EXPORT OStream : Noncopyable
- {
- public:
- OStream();
- OStream(
- RCF::MemOstream & os,
- int runtimeVersion = 0,
- int archiveVersion = 0);
- /// Constructs an OStream from a std::ostream. Serialized data will be written to the std::ostream.
- OStream(
- std::ostream & os,
- int runtimeVersion = 0,
- int archiveVersion = 0);
- virtual ~OStream();
- void setOs(
- std::ostream & os,
- int runtimeVersion = 0,
- int archiveVersion = 0);
- void clearState();
- UInt32 writeRaw(const Byte8 *pBytes, UInt32 nLength);
- void begin(const Node &node);
- void put(const DataPtr &value);
- void end();
- UInt32 write_int(UInt32 n);
- UInt32 write_byte(Byte8 byte);
- UInt32 write(const Byte8 *pBytes, UInt32 nLength);
- virtual I_Encoding &
- getEncoding() = 0;
- /// Gets the RCF runtime version associated with this stream.
- int getRuntimeVersion();
- /// Gets the archive version associated with this stream.
- int getArchiveVersion();
- /// Sets the archive version associated with this stream.
- void setArchiveVersion(int archiveVersion);
- /// Sets the RCF runtime version associated with this stream.
- void setRuntimeVersion(int runtimeVersion);
- void suppressArchiveMetadata(bool suppress = true);
- void setRemoteCallContext(
- RCF::SerializationProtocolOut * pSerializationProtocolOut);
- RCF::SerializationProtocolOut *
- getRemoteCallContext();
- ContextWrite & getTrackingContext();
- const ContextWrite & getTrackingContext() const;
- LocalStorage & getLocalStorage();
- void setEnablePointerTracking(bool enable);
- bool getEnablePointerTracking() const;
- // Streaming operator.
- /// Serialize an object to the stream.
- template<typename T>
- OStream & operator<<(const T &t);
- private:
- void writeArchiveMetadata();
- ContextWrite mContextWrite;
- LocalStorage mLocalStorage;
- std::ostream * mpOs;
- int mRuntimeVersion;
- int mArchiveVersion;
- bool mSuppressArchiveMetadata;
- bool mArchiveMetadataWritten;
- RCF::SerializationProtocolOut * mpSerializationProtocolOut;
- };
- } // namespace SF
- #include <SF/Archive.hpp>
- #include <SF/Serializer.hpp>
- namespace SF {
- template<typename T>
- IStream & IStream::operator>>(T &t)
- {
- Archive ar(Archive::READ, this);
- ar & t;
- return *this;
- }
- template<typename T>
- IStream & IStream::operator>>(const T &t)
- {
- Archive ar(Archive::READ, this);
- ar & t;
- return *this;
- }
- template<typename T>
- OStream & OStream::operator<<(const T &t)
- {
- Archive ar(Archive::WRITE, this);
- ar & t;
- return *this;
- }
- } // namespace SF
- #endif // !INCLUDE_SF_STREAM_HPP
|