📄 object.h
字号:
) const { return Compare(obj) == LessThan; } /** Compare the two objects. @return TRUE if objects are greater than. */ BOOL operator>( const PObject & obj // Object to compare against. ) const { return Compare(obj) == GreaterThan; } /** Compare the two objects. @return TRUE if objects are less than or equal. */ BOOL operator<=( const PObject & obj // Object to compare against. ) const { return Compare(obj) != GreaterThan; } /** Compare the two objects. @return TRUE if objects are greater than or equal. */ BOOL operator>=( const PObject & obj // Object to compare against. ) const { return Compare(obj) != LessThan; } //@} /**@name I/O functions */ //@{ /** Output the contents of the object to the stream. The exact output is dependent on the exact semantics of the descendent class. This is primarily used by the standard #operator<<# function. The default behaviour is to print the class name. */ virtual void PrintOn( ostream &strm // Stream to print the object into. ) const; /** Input the contents of the object from the stream. The exact input is dependent on the exact semantics of the descendent class. This is primarily used by the standard #operator>># function. The default behaviour is to do nothing. */ virtual void ReadFrom( istream &strm // Stream to read the objects contents from. ); /** Global function for using the standard << operator on objects descended from PObject. This simply calls the objects #PrintOn()# function. @return the #strm# parameter. */ inline friend ostream & operator<<( ostream &strm, // Stream to print the object into. const PObject & obj // Object to print to the stream. ) { obj.PrintOn(strm); return strm; } /** Global function for using the standard >> operator on objects descended from PObject. This simply calls the objects #ReadFrom()# function. @return the #strm# parameter. */ inline friend istream & operator>>( istream &strm, // Stream to read the objects contents from. PObject & obj // Object to read inormation into. ) { obj.ReadFrom(strm); return strm; } /** This function is used to determine the size of the object and all other objects it contains. The actual size is dependent on the exact semantics of the descendent object. For example the #PString# class would return the length of the string plus one, while the #PList# class would return the sum of the sizes of all of the objects in the list plus the size of an integer for the number of objects. This in only required by the #PBinarySerialiser# class which serialises the objects into a binary file. The #PTextSerialiser# class which serialises into a text stream does not use this function. Note serialisation requires the use of the #PDECLARE_SERIAL# and #PIMPLEMENT_SERIAL# macros. @return size in bytes of object. */ virtual PINDEX PreSerialise( PSerialiser & strm // Serialiser stream to serialise object into. ); /** Serialise the object into the specified stream. This is similar to the #PrintOn()# function that outputs the contents of the object to a stream, but where #PrintOn()# usually produces a human readable form of the object, this function outputs enough data so that it can be reconstructed by the #PUnSerialiser# class. When the user implements this function they will usually be doing it for one of either the text of binary output versions. In some circumstances, eg libraries, both need be supported so the #IsDscendent()# function should be used on the #strm# parameter to determine whether it is a #PBinarySerialiser# class or a #PTextSerialiser# class and do the appropriate output. To a large extent, if only the #<<# operator is used on the #PSerialiser# instance, the text and binary version can be made identical. */ virtual void Serialise( PSerialiser & strm // Serialiser stream to serialise object into. ); /** Un-serialise the object from the specified stream. This is similar to the #ReadFrom()# function that inputs the contents of the object from a stream, but where #ReadFrom()# usually intrerprets a human readable form of the object, this function inputs enough data so that it can be reconstructed from the data provided by the #Serialise()# function. When the user implements this function they will usually be doing it for one of either the text of binary input versions. In some circumstances, eg libraries, both need be supported so the #IsDscendent()# function should be used on the #strm# parameter to determine whether it is a #PBinarySerialiser# class or a #PTextSerialiser# class and do the appropriate input. To a large extent, if only the >> operator is used on the #PUnSerialiser# instance, the text and binary version can be made identical. */ virtual void UnSerialise( PUnSerialiser & strm // Serialiser stream to serialise object into. ); //@} /**@name Miscellaneous functions */ //@{ /** Create a copy of the class on the heap. The exact semantics of the descendent class determine what is required to make a duplicate of the instance. Not all classes can even {\bf do} a clone operation. The main user of the clone function is the #PDictionary# class as it requires copies of the dictionary keys. The default behaviour is for this function to assert. @return pointer to new copy of the class instance. */ virtual PObject * Clone() const; /** This function yields a hash value required by the #PDictionary# class. A descendent class that is required to be the key of a dictionary should override this function. The precise values returned is dependent on the semantics of the class. For example, the #PString# class overrides it to provide a hash function for distinguishing text strings. The default behaviour is to return the value zero. @return hash function value for class instance. */ virtual PINDEX HashFunction() const; //@}};///////////////////////////////////////////////////////////////////////////////// Serialisationclass PUnSerialiser;/** Registration class for persistent object serialisation/unserialisation.This class is for registration of object classes that will be serialised andun-serialised.As objects are un-serialised, the objects need to be constructed. For the#PUnSerialiser# instance to know what constructor to call, aregistration of functions that call the appropriate constructor.The #PDECLARE_SERIAL# macro creates a single instance of this class toregister the class with the serialiser.Even though this class implements a hash table it does {\bf not} use thestandard #PHashTable# or #PDictionary# classes due to recursivedefinition problems. Those classes need to register themselves with thisclass before they can be used!*/class PSerialRegistration { public: /** This type is a pointer to a function to create objects during the un-serialisation operation. */ typedef PObject * (*CreatorFunction)(PUnSerialiser * serial); /** Create a serialiser class registration. This is unversally called by static member variables in the #PDECLARE_SERIAL# and #PIMPLEMENT_SERIAL# macros. */ PSerialRegistration( const char * clsNam, // Name of class to register. CreatorFunction func // Constructor function for the class. ); /** Get the creator function for the class name specified. @return function to construct objects. */ static CreatorFunction GetCreator( const char * clsNam // Name of class to get the creator function for. ); // Constant for size of hash table. enum { HashTableSize = 41 }; protected: /** Calculate the bucket for the hash table lookup. @return Has index for class. */ static PINDEX HashFunction( const char * className // Class name to calculate hash function for. ); /// This serialiser registrations class const char * className; /** This serialiser registrations creator function - the function that will make a new object of the classes type and construct it with an instance of the #PSerialiser# class. */ CreatorFunction creator; /// Pointer to next registration when a hash clash occurs. PSerialRegistration * clash; /// A static dictionary of class names to creator functions. static PSerialRegistration * creatorHashTable[HashTableSize];};/** This class allows the serialisation of objects to an output stream. This packages up objects so that they can be reconstructed by an instance of the #PUnSerialiser# class. The stream they are sent to can be any stream; file, string, pipe, socket etc. Serialisation can be done in two manners: binary or text. This depends on the serialiser instance that was constructed. Each objects #PObject::Serialise()# function is called and it is up to that function to output in binary or text. To a large extent, if only the << operator is used on the #PSerialser# instance, the text and binary versions of the #PObject::Serialise()# function can be made identical. This class is an abstract class and descendents of #PTextSerialiser# or #PBinarySerialiser# should be created. */class PSerialiser : public PObject{ PCLASSINFO(PSerialiser, PObject); public: /// Construct a serialiser. PSerialiser( ostream & strm // Stream to output serialisation to. ); /// Output char to serial stream. virtual PSerialiser & operator<<(char) = 0; /// Output unsigned char to serial stream. virtual PSerialiser & operator<<(unsigned char) = 0; /// Output signed char to serial stream. virtual PSerialiser & operator<<(signed char) = 0; /// Output short to serial stream. virtual PSerialiser & operator<<(short) = 0; /// Output unsigned short to serial stream. virtual PSerialiser & operator<<(unsigned short) = 0; /// Output int to serial stream. virtual PSerialiser & operator<<(int) = 0; /// Output unsigned int to serial stream. virtual PSerialiser & operator<<(unsigned int) = 0; /// Output long to serial stream. virtual PSerialiser & operator<<(long) = 0; /// Output unsigned long to serial stream. virtual PSerialiser & operator<<(unsigned long) = 0; /// Output float to serial stream. virtual PSerialiser & operator<<(float) = 0; /// Output double to serial stream. virtual PSerialiser & operator<<(double) = 0; /// Output long double to serial stream. virtual PSerialiser & operator<<(long double) = 0; /// Output C string to serial stream. virtual PSerialiser & operator<<(const char *) = 0; /// Output C string to serial stream. virtual PSerialiser & operator<<(const unsigned char *) = 0; /// Output C string to serial stream. virtual PSerialiser & operator<<(const signed char *) = 0; /** Output the data to the serialiser object. When the operator is executed on a #PObject# descendent then that objects #PObject::Serialise()# function is called. */ virtual PSerialiser & operator<<(PObject &); protected: /// Stream to output serial data to. ostream & stream;};/** This class allows the un-serialisation of objects from an input stream. This reconstruct objects that where packaged earlier by an instance of the #PSerialise# class. The stream they are received from can be any stream; file, string, pipe, socket etc. Serialisation can be done in two manners: binary or text. This depends on the serialiser instance that was constructed. Each objects #PObject::Serialise()# function is called and it is up to that function to output in binary or text. To a large extent, if only the #<<# operator is used on the #PSerialser# instance, the text and binary versions of the #PObject::Serialise()# function can be made identical. This class is an abstract class and descendents of #PTextSerialiser# or #PBinarySerialiser# should be created. */class PUnSerialiser : public PObject{ PCLASSINFO(PUnSerialiser, PObject); public: /// Construct an un-serialiser. PUnSerialiser( istream & strm // Stream to read the serialised objects from. ); /// Input primitive from stream. virtual PUnSerialiser & operator>>(char &) = 0; /// Input primitive from stream. virtual PUnSerialiser & operator>>(unsigned char &) = 0; /// Input primitive from stream. virtual PUnSerialiser & operator>>(signed char &) = 0; /// Input primitive from stream. virtual PUnSerialiser & operator>>(short &) = 0; /// Input primitive from stream. virtual PUnSerialiser & operator>>(unsigned short &) = 0; /// Input primitive from stream. virtual PUnSerialiser & operator>>(int &) = 0; /// Input primitive from stream. virtual PUnSerialiser & operator>>(unsigned int &) = 0; /// Input primitive from stream. virtual PUnSerialiser & operator>>(long &) = 0; /// Input primitive from stream. virtual PUnSerialiser & operator>>(unsigned long &) = 0; /// Input primitive from stream. virtual PUnSerialiser & operator>>(float &) = 0; /// Input primitive from stream. virtual PUnSerialiser & operator>>(double &) = 0; /// Input primitive from stream. virtual PUnSerialiser & operator>>(long double &) = 0; /// Input primitive from stream.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -