//****************************************************************************** // 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 deltavsoft.com // //****************************************************************************** #ifndef INCLUDE_SF_STREAM_HPP #define INCLUDE_SF_STREAM_HPP #include #include #include #include #include #include #include namespace RCF { class SerializationProtocolIn; class SerializationProtocolOut; class MemIstream; class MemOstream; } namespace SF { //************************************************** // Encoding of object data template 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 > mNidToIdMap; std::unique_ptr > > 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 > 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 IStream & operator>>(T &t); /// Deserialize an object from the stream. template 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 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 #include namespace SF { template IStream & IStream::operator>>(T &t) { Archive ar(Archive::READ, this); ar & t; return *this; } template IStream & IStream::operator>>(const T &t) { Archive ar(Archive::READ, this); ar & t; return *this; } template OStream & OStream::operator<<(const T &t) { Archive ar(Archive::WRITE, this); ar & t; return *this; } } // namespace SF #endif // !INCLUDE_SF_STREAM_HPP