00001 /* 00002 * The information in this file is 00003 * Copyright(c) 2007 Ball Aerospace & Technologies Corporation 00004 * and is subject to the terms and conditions of the 00005 * GNU Lesser General Public License Version 2.1 00006 * The license text is available from 00007 * http://www.gnu.org/licenses/lgpl.html 00008 */ 00009 #ifndef SESSION_ITEM_SERIALIZER_H 00010 #define SESSION_ITEM_SERIALIZER_H 00011 00012 class XMLWriter; 00013 00014 #include "AppConfig.h" 00015 #include <vector> 00016 00017 /** 00018 * Used by SessionItems during session serialization to save their state in 00019 * the session. The data stored via the SessionItemSerializer will be restored 00020 * to the SessionItem on loading a session via the SessionItemDeserializer. 00021 * If a SessionItem needs to be restored, but does not need to save any state 00022 * information, it should call serialize(NULL,0) to ensure that it will be 00023 * recreated on session load. If a SessionItem does not do at least this on 00024 * serialization, it will not be recreated on session restore. Conversely, if 00025 * a SessionItem does not need to be recreated on session load, it should not 00026 * call any of the serialize methods on the SessionItemSerializer. 00027 * 00028 * @see SessionItem 00029 * @see SessionItemDeserializer 00030 */ 00031 class SessionItemSerializer 00032 { 00033 public: 00034 /** 00035 * Reserves a specified number of bytes in the session. 00036 * 00037 * This method is called by a SessionItem during session serialization 00038 * to specify the total number of bytes that the SessionItem will be saving 00039 * to the session. If serialize() will only be called once, this method 00040 * need not be called since the size can be inferred from the data - this 00041 * method need only be called if serialize() will be called more than once. 00042 * If this method is called more than once, only the first call will be 00043 * applied. When using multiple blocks, reserve() should be used to reserve 00044 * the space needed for each block, not for then entire SessionItem. 00045 * 00046 * @param size 00047 * The total number of bytes that will be serialized to this 00048 * object. 00049 * 00050 * @see endBlock() 00051 */ 00052 virtual void reserve(int64_t size) = 0; 00053 00054 /** 00055 * Saves the specified data in the session file. 00056 * 00057 * This method is called by a SessionItem during session serialization 00058 * to save the state of the SessionItem. The data provided to this method 00059 * will be retrieved upon deserialization to recreate the SessionItem. If 00060 * this method is to be called more than once, the reserve() method must be 00061 * called to specify the total amount of space to reserve. 00062 * 00063 * @param data 00064 * The data to be saved in the session. It can be in any format. 00065 * 00066 * @return True if the data is successfully saved, or false otherwise 00067 */ 00068 virtual bool serialize(const std::vector<unsigned char> &data) = 0; 00069 00070 /** 00071 * Saves the specified data in the session file. 00072 * 00073 * This method is called by a SessionItem during session serialization 00074 * to save the state of the SessionItem. The data provided to this method 00075 * will be retrieved upon deserialization to recreate the SessionItem. 00076 * 00077 * If the size is 0, this method will return true and the current block will be 00078 * created. This can be used to force the creation of a size 0 block. 00079 * 00080 * @param pData 00081 * A pointer to the data to be written. The method will return 00082 * false if this pointer is NULL and \b size is not 0. 00083 * 00084 * @param size 00085 * The size of the data in bytes 00086 * 00087 * @return True if the data is successfully saved, or false otherwise 00088 */ 00089 virtual bool serialize(const void *pData, int64_t size) = 0; 00090 00091 /** 00092 * Saves the specified data in the session file. 00093 * 00094 * This method is called by a SessionItem during session serialization 00095 * to save the state of the SessionItem. The data provided to this method 00096 * will be retrieved upon deserialization to recreate the SessionItem. This 00097 * method may not be called more than once. This is a convenience method 00098 * for saving session information in XML format. 00099 * 00100 * @param writer 00101 * An XMLWriter in which the state of the SessionItem has been 00102 * written. 00103 * 00104 * @return True if the data is successfully saved, or false otherwise 00105 */ 00106 virtual bool serialize(XMLWriter &writer) = 0; 00107 00108 /** 00109 * Creates a new session item block. 00110 * 00111 * Typically, SessionItem data can be saved as a single block of data either with 00112 * a single call to serialize() or a call to reserve() followed by multiple calls 00113 * to serialize(). It may be desirable in certain cases to block the data for a single 00114 * SessionItem. The first block may contain an XML header, for example, and further 00115 * blocks contain raw binary data. In this case, the XML is more easily separable 00116 * from the binary data and can be more easily passed to an XML parser without the 00117 * raw binary data interfering or slowing down the parse. 00118 * 00119 * A SessionItem wishing to use this feature should call serialize() once, or reserve() 00120 * followed by serialize() to write the first block. When finished with the block, call 00121 * endBlock() to close the block and prepare the SessionItemSerializer for the next block. 00122 * Call serialize() or reserve() followed by serialize() for the second block. Repeat this 00123 * for additional blocks of data. The final block will implicitly call endBlock(). 00124 * 00125 * @note Different blocks will not necessarily be stored adjacent to each other. Different 00126 * session serialization implementations may locate blocks in different files, or in 00127 * different segments of the same file. SessionItem serialization should treat different 00128 * blocks as discreet items. You can not, for example, write out a data cube with each 00129 * band stored in a different block and expect that the entire data cube is contiguous 00130 * on the disk. 00131 */ 00132 virtual void endBlock() = 0; 00133 00134 protected: 00135 /** 00136 * Destroys the SessionItemSerializer object. 00137 * 00138 * The SessionItemSerializer object is automatically deleted by 00139 * SessionManager. Plug-ins do not need to destroy it. 00140 */ 00141 virtual ~SessionItemSerializer() {} 00142 }; 00143 00144 #endif