Veda/PackResource.h

Go to the documentation of this file.
00001 /*! \file 
00002     \author victorien ferry & www.m4nkind.com
00003     \brief This file applies the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1 , read file COPYING.
00004 */
00005 #ifndef COM_M4NKIND_PackResource_H
00006 #define COM_M4NKIND_PackResource_H
00007 
00008 #include "PackStruct.h"
00009 #include "PackULong_Flags.h"
00010 #include "PackString.h"
00011 
00012 /*!
00013     \class  PackResource
00014     \ingroup    BaseSerializableClass
00015     \brief   Base type managing a file as a resource. the file can be external, on the disk,
00016             or internal, which means linked into the serialized context.
00017             In all compilation modes, You get the resource with GetBinary(). In editable mode,
00018             you can set a binary file 
00019             You Set() and Get() a file name relative to the executable path,
00020             and choose if it should be linked while serialized, or just read from the file at inition.
00021             In all cases, you get the binary with GetBinary() 
00022             It is used for the automated serialization.
00023 
00024 */
00025 
00026 class PackResource : public PackStruct
00027 {
00028 /*==================================================================
00029                                 PUBLIC
00030 ==================================================================*/
00031 public:
00032     /*!
00033         \brief  Constructor. 
00034     */
00035     PackResource(void);
00036 
00037 #ifdef _ENGINE_EDITABLE_
00038     /*!
00039         \brief  Destructor. 
00040     */
00041     virtual ~PackResource(void);
00042 #endif
00043     /*!
00044         \brief  Read the object description from a byte chunk. Could crash if chunk not valid.
00045         \param  _pDescriptionChunk the objet description chunk. 
00046         \return the end of the chunk written, possibly unlocated. Don't use this if you don't need it.
00047     */
00048     virtual const unsigned char * Serialize_In(const unsigned char * _pDescriptionChunk);
00049 
00050 #ifdef _ENGINE_EDITABLE_
00051     /*!
00052         \brief  get the size of the whole byte chunk that will be written by Serialize_Out().
00053         \return byte size of the serialisation to do.(1,2,3, or4)
00054     */
00055     virtual unsigned int  GetSerializedDescriptionSize(void);
00056 #endif
00057 #ifdef _ENGINE_EDITABLE_
00058     /*!
00059         \brief  write the Current object definition to a Chunk using private packed types, recursively.
00060         \param  _pDescriptionChunkToFill the chunk where to write the objet description chunk. 
00061         \return the end of the chunk written, possibly unlocated. Don't use this if you don't need it.
00062     */
00063     virtual unsigned char * Serialize_Out(unsigned char * _pDescriptionChunkToFill);
00064 #endif
00065 #ifdef _ENGINE_EDITABLE_
00066     /*!
00067         \brief  Each BaseType's inherited classes must explicit an ID for their
00068                 class, or let use one of the super class at least through this virtual method.
00069                 This is needed by GUIs to detect the types of each sub-members, and shape
00070                 an interface for each Object according to their member list.
00071         \return a const character string, that must be unique and unchanged for all serializable base type.
00072     */
00073     virtual const char *GetClassID() const { return "PackStruct::PackResource"; };
00074 #endif      
00075     /*!
00076         \brief Get the image of the file as a binary. if _pBinary is 0L, no binary.
00077                 This is the main Get accessor method that should be used for a PackResource.
00078         \param _pBinary return a pointer to the binary, as a char table.
00079         \param _BinaryLength return the exact byte length of the binary.
00080     */
00081     unsigned int GetByteLength();
00082 
00083     /*!
00084         \brief Get the image of the file or linked chunk as a binary.
00085                 if _pBinary is 0L, no binary, or file problem.
00086                 This is the main Get accessor method that should be used for a PackResource.
00087                 You can then read that binary chunk within your function, but don't store a
00088                 pointer to it: It could be destroyed in the future.
00089                 (Note it is destroyed by a next call.)
00090                 This class allows streaming with parameters _ByteOffset and _maximumLengthToGet, in File mode or in Linked mode.
00091 
00092         \param _pBinary return a pointer to the binary, as a char table.
00093         \param _BinaryLength return the exact byte length of the binary in _pBinary.
00094         \param  _ByteOffset
00095     */
00096     void GetBinaryAtOffset( const unsigned char *&_pBinary , unsigned int &_BinaryLengthReturned, 
00097                     unsigned int _byteOffset=0UL,const unsigned int _maximumLengthToGet=0xffffffffUL
00098         );
00099 
00100 #ifdef _ENGINE_EDITABLE_
00101     /*!
00102         \brief  At init in edition mode, propose the file extension that this resource object manage.
00103         \param  _pFileExtenbsion without the dot, "mpg" , "jpg" , ...
00104     */
00105     void    SetFileExtension(const char *_pFileExtenbsion);
00106 #endif
00107 #ifdef _ENGINE_EDITABLE_
00108     /*!
00109         \brief  get the file extension that this resource object manage.
00110                 can return null (0L) if not specified.
00111         \return     _pFileExtenbsion
00112     */
00113     inline const char *GetFileExtension() const { return m_FileExtension.Get(); };
00114 #endif
00115 #ifdef _ENGINE_EDITABLE_
00116     /*!
00117         \brief  set the origin file.
00118         \param  _pFilePath
00119     */
00120     void    SetFile(const char *_pFilePath);
00121 #endif
00122 #ifdef _ENGINE_EDITABLE_
00123     /*!
00124         \brief  access to the origin file path, even in link mode.
00125                 can return 0L or invalid path.
00126         \return File Path.
00127     */
00128     inline const char *GetFilePath() const { return mSer_FilePath.Get(); };
00129 #endif
00130 #ifdef _ENGINE_EDITABLE_
00131     /*!
00132         \brief  Load and link the file if true,
00133                 declare the resource is a file outside the object if false.
00134         \param  _isLink true: is linked, false, is in file.
00135     */
00136     void    SetLinkMode(bool _isLink);
00137 #endif
00138 #ifdef _ENGINE_EDITABLE_
00139     /*!
00140         \brief  get Link mode.
00141         \return true is linked, false, is in file.
00142     */
00143     bool    GetLinkMode();
00144 #endif
00145 #ifdef _ENGINE_EDITABLE_
00146     /*!
00147         \brief Give a binary Form: in external file mode, it is saved, in link mode, it is kept in memory.
00148         \param  _pBinaryToSet
00149         \param  _BinaryLength byte size of _pBinaryToSet
00150         \param  _append if true, chunk is added to the existing chunk or file.
00151         \return true if OK, false if file mode write error.
00152     */
00153     bool SetBinary( const char *_pBinaryToSet , unsigned int _BinaryLength, bool _append=false );
00154 #endif
00155 #ifdef _ENGINE_EDITABLE_
00156     /*!
00157         \brief  set or append a 2 bytes unsigned integer in Little endian.
00158                 By default, it is added at the end of the file.
00159         \param  _short 2 bytes unsigned integer
00160         \param  _append if true, chunk is added to the existing chunk or file.
00161         \return true if OK, false if file mode write error.
00162     */
00163     inline bool SetLE_UnsignedShort( unsigned short _short, bool _append=true )
00164     {
00165         char bb;
00166         bb =(char) _short;
00167         if(!SetBinary( &bb , 1, _append)) return false;
00168         bb = _short>>8;
00169         if(!SetBinary( &bb , 1, true)) return false;
00170     }
00171 #endif
00172 #ifdef _ENGINE_EDITABLE_
00173     /*!
00174         \brief  set or append a 4 bytes unsigned integer in Little endian.
00175                 By default, it is added at the end of the file.
00176         \param  _value 4 bytes unsigned integer
00177         \param  _append if true, chunk is added to the existing chunk or file.
00178         \return true if OK, false if file mode write error.
00179     */
00180     inline bool SetLE_UnsignedInt( unsigned int _value, bool _append=true )
00181     {
00182         char bb;
00183         //bool res;
00184         bb = _value;
00185         if(!SetBinary( &bb , 1, _append)) return false;
00186         bb = _value>>8;
00187         SetBinary( &bb , 1, true);
00188         bb = _value>>16;
00189         SetBinary( &bb , 1, true);
00190         bb = _value>>24;
00191         if(!SetBinary( &bb , 1, true)) return false;
00192     }
00193 #endif
00194 
00195     /*!
00196         \brief read Little Endian Unsigned short at fileOffset, and increment fileOffset after it.
00197         \param  _short the value read and returned.
00198         \return if false, _short invalid.
00199     */
00200     inline  bool    GetLE_UnsignedShort( unsigned short &_short,unsigned int &_fileOffset)
00201     {
00202         const unsigned char *pBinary;
00203         unsigned int    lengthReturned;
00204         GetBinaryAtOffset(pBinary,lengthReturned,_fileOffset,2);
00205         if(lengthReturned==2)
00206         {
00207             _fileOffset+=2;
00208             _short = (pBinary[1]<<8)|pBinary[0];
00209             return true;
00210         } else return false;
00211     };
00212     /*!
00213         \brief read Little Endian Signed short at fileOffset, and increment fileOffset after it.
00214         \param  _short the value read and returned.
00215         \return if false, _short invalid.
00216     */
00217     inline  bool    GetLE_SignedShort( signed short &_short,unsigned int &_fileOffset)
00218         { return GetLE_UnsignedShort(((unsigned short &)_short),_fileOffset); };
00219     /*!
00220         \brief read Little Endian Unsigned 4 byte int at fileOffset, and increment fileOffset after it.
00221         \param  _short the value read and returned.
00222         \return if false, _short invalid.
00223     */
00224     inline  bool    GetLE_UnsignedInt( unsigned int &_int,unsigned int &_fileOffset)
00225     {
00226         const unsigned char *pBinary;
00227         unsigned int    lengthReturned;
00228         GetBinaryAtOffset(pBinary,lengthReturned,_fileOffset,4);
00229         if(lengthReturned==4)
00230         {
00231             _fileOffset+=4;
00232             _int = (pBinary[3]<<24)|(pBinary[2]<<16)|(pBinary[1]<<8)|pBinary[0];
00233             return true;
00234         } else return false;
00235     };
00236     /*!
00237         \brief read Little Endian Unsigned 4 byte int at fileOffset, and increment fileOffset after it.
00238         \param  _short the value read and returned.
00239         \return if false, _short invalid.
00240     */
00241     inline  bool    GetLE_SignedInt( signed int &_int,unsigned int &_fileOffset)
00242         { return GetLE_UnsignedInt(((unsigned int &)_int),_fileOffset); };
00243 #ifdef _ENGINE_EDITABLE_
00244     /*!
00245         \brief  Set the kind of class of object that we will be able to refer. Is only possible in editable mode.
00246                 In read-only mode, the referable type is implicit. It can only be done once.
00247         \return pointer on a pointer
00248     */
00249     inline PackResource **GetPointerOnNextResourceInContextList(){ return( &m_pNextResourceInContextList ); };
00250 #endif
00251 #ifdef _ENGINE_EDITABLE_
00252     /*!
00253         \brief  The Object this one belong to as a member. Should only be used at init.
00254         \param _pManager    BaseObject
00255     */
00256     virtual void    SetObjectThatManagesThis(BaseObject *_pManager);
00257 #endif
00258 /*==================================================================
00259                                 PROTECTED
00260 ==================================================================*/
00261 protected:
00262     //! the file path in file mode, empty or origin file path in linked mode. Editable.
00263     PackString          mSer_FilePath;
00264     //! the total linked length of the binary currently open.0 means filemode. computed, serialized but not editable.
00265     PackULong           mSer_BinarySerializedLength;
00266     //! the binary currently opened. The chunk is managed by the object. In Linked mode, the whole binary start here. In File mode, it start a chunk that can be any part (or the whole) of the file.
00267     unsigned char   *m_pBinary;
00268     //! means the current byte size of m_pBinary. Can be different from the resource byte size.
00269     unsigned int    m_CurrentChunkSize;
00270     //! true if link, false is in file.
00271     bool            m_isLinkWith;
00272 #ifdef _ENGINE_EDITABLE_
00273     //! propose a file extension for the resource managed.
00274     PackString          m_FileExtension;
00275 #endif
00276 #ifdef _ENGINE_EDITABLE_
00277     //! chain list which start is in BaseContext, and list all resources in a context.
00278     PackResource    *m_pNextResourceInContextList;
00279 #endif
00280 
00281 
00282 #ifdef _ENGINE_EDITABLE_
00283     /*!
00284         \brief delete if was allocated.
00285     */
00286     virtual void FreeResource();
00287 #endif
00288 };
00289 
00290 /*!
00291     \def    REGISTER_MEMBER_RESOURCE
00292 
00293     \brief  This macro is used to register a serializable member in a class constructor.
00294             For editable mode, it uses _MemberName to explicit the use of the member, in order
00295             to display it in a GUI for example. 
00296     \param  _object
00297     \param  _MemberName string to display on editor, 0L mean serialized but not edited.
00298     \param  _FileExtension
00299 */
00300 #ifdef _ENGINE_EDITABLE_
00301 #define     REGISTER_MEMBER_RESOURCE(_object,_MemberName,_FileExtension) \
00302 RegisterSerializableMember(_object);\
00303 _object.SetMemberName( _MemberName );\
00304 _object.SetFileExtension(_FileExtension);
00305 #else
00306 #define     REGISTER_MEMBER_RESOURCE(_object,_MemberName,_DefaultVal) \
00307 RegisterSerializableMember(_object);
00308 #endif
00309 
00310 // end of file.
00311 #endif

      /\/\        4         N         k         !         N         D
                      _______  _ __ ___  _____            ___ _ _  ____
     ___________  __//___   /________  |/    / ___________\_______/    \
    /   _   _   \/   _     /    _   /      _/_/____/    _       __     /
   /    /   /       /     /    /    \      \/     /    /    \   \     /
  \\___/___/___/    ¯    _____/_____/       ______\___/_____/\________\\
               \________/_ ___ __ l____\      /elD!  
                 http://www.m4nkind.com \____/