klvobject.h

Go to the documentation of this file.
00001 
00009 /*
00010  *  Copyright (c) 2003, Matt Beard
00011  *  Portions Copyright (c) 2003, Metaglue Corporation
00012  *
00013  *  This software is provided 'as-is', without any express or implied warranty.
00014  *  In no event will the authors be held liable for any damages arising from
00015  *  the use of this software.
00016  *
00017  *  Permission is granted to anyone to use this software for any purpose,
00018  *  including commercial applications, and to alter it and redistribute it
00019  *  freely, subject to the following restrictions:
00020  *
00021  *    1. The origin of this software must not be misrepresented; you must
00022  *       not claim that you wrote the original software. If you use this
00023  *       software in a product, an acknowledgment in the product
00024  *       documentation would be appreciated but is not required.
00025  *  
00026  *    2. Altered source versions must be plainly marked as such, and must
00027  *       not be misrepresented as being the original software.
00028  *  
00029  *    3. This notice may not be removed or altered from any source
00030  *       distribution.
00031  */
00032 #ifndef MXFLIB__KLVOBJECT_H
00033 #define MXFLIB__KLVOBJECT_H
00034 
00035 
00036 // STL Includes
00037 #include <string>
00038 #include <list>
00039 #include <map>
00040 
00041 
00042 // Define some enums
00043 namespace mxflib
00044 {
00045     enum KeyFormat {
00046         KEY_NONE = 0,
00047         KEY_1_BYTE = 1,
00048         KEY_2_BYTE = 2,
00049         KEY_4_BYTE = 4,
00050         KEY_AUTO = 3
00051     };
00052 
00053     enum LenFormat
00054     {
00055         LEN_NONE = 0,
00056         LEN_1_BYTE = 1,
00057         LEN_2_BYTE = 2,
00058         LEN_4_BYTE = 4,
00059         LEN_BER = 3
00060     };
00061 }
00062 
00063 
00064 namespace mxflib
00065 {
00066     // Forward declare so the class can include pointers to itself
00067     class KLVObject;
00068 
00070     typedef SmartPtr<KLVObject> KLVObjectPtr;
00071 
00073     typedef std::list<KLVObjectPtr> KLVObjectList;
00074 
00075     typedef std::map<std::string, KLVObjectPtr> KLVObjectMap;
00076 }
00077 
00078 
00079 namespace mxflib
00080 {
00082 
00084     class KLVReadHandler_Base : public RefCount<KLVReadHandler_Base>
00085     {
00086     public:
00088         virtual ~KLVReadHandler_Base();
00089 
00091 
00099         virtual size_t ReadData(DataChunk &Buffer, KLVObjectPtr Object, Position Start = 0, size_t Size = static_cast<size_t>(-1)) = 0;
00100 
00101 //      //! Read the key and length of the KLVObject
00102 //      virtual Int32 ReadKL(KLVObjectPtr Object) { return -1;}
00103     };
00104 
00106     typedef SmartPtr<KLVReadHandler_Base> KLVReadHandlerPtr;
00107 
00108 
00109 //  //! Base class for KLVObject write handlers
00110 //  /*! \note Classes derived from this class <b>must not</b> include their own RefCount<> derivation
00111 //   */
00112 //  class KLVWriteHandler_Base : public RefCount<KLVWriteHandler_Base>
00113 //  {
00114 //  public:
00115 //      //! Base destructor
00116 //      virtual ~KLVReadHandler_Base();
00117 //
00118 //      //! Write data from the KLVObject to the destination
00119 //      /*! \param Object KLVObject that is the data source
00120 //       *  \param Buffer Location of the data to write
00121 //       *  \param Start Offset from the start of the KLV value of the first byte to be written
00122 //       *  \param Size Number of bytes to be written
00123 //       *  \return The count of bytes written
00124 //       */
00125 //      virtual size_t WriteData(KLVObjectPtr Object, const UInt8 *Buffer, Position Start = 0, size_t Size = static_cast<size_t>(-1)) = 0;
00126 //  };
00127 //
00128 //  //! Smart pointer for the base KLVObject read handler
00129 //  typedef SmartPtr<KLVWriteHandler_Base> KLVWriteHandlerPtr;
00130 
00131 
00133 
00143     class KLVObject : public RefCount<KLVObject>
00144     {
00145     protected:
00146         class KLVInfo
00147         {
00148         private:
00149             KLVInfo(KLVInfo &);             
00150 
00151         public:
00152             MXFFilePtr File;                
00153             Position Offset;                
00154             Length OuterLength;             
00155             Int32 KLSize;                   
00156             bool Valid;                     
00157 
00158             KLVInfo()
00159             {
00160                 Valid = false;
00161                 Offset = -1;
00162                 OuterLength = 0;
00163                 KLSize = -1;
00164             }
00165             
00166             KLVInfo &operator=(const KLVInfo &RHS)
00167             {
00168                 Valid = RHS.Valid;
00169                 File = RHS.File;
00170                 Offset = RHS.Offset;
00171                 OuterLength = RHS.OuterLength;
00172                 KLSize = RHS.KLSize;
00173 
00174                 return *this;
00175             }
00176         };
00177 
00178         KLVInfo Source;                     
00179         KLVInfo Dest;                       
00180 
00181         ULPtr TheUL;                        
00182         Length ValueLength;                 
00183 
00184         DataChunk Data;                     
00185         Position DataBase;                  
00186 
00187         KLVReadHandlerPtr ReadHandler;      
00188 //      KLVWriteHandlerPtr WriteHandler;    //!< A read-handler to supply data in response to read requests. If NULL data will be read from SourceFile (if available)
00189 
00190         //## DRAGONS: Ensure any new properties are copied by the KLVObject --> KLVEObject copy constructor ##
00191 
00192         //@@@ Is this another MSVC bug?  KLVEObject can't access protected KLVObject properties from KLVEObject constructor!!
00193         friend class KLVEObject;
00194 
00195     public:
00196         KLVObject(ULPtr ObjectUL = NULL);
00197         virtual void Init(void);
00198         virtual ~KLVObject() {};        
00199 
00201 
00204         virtual void SetSource(MXFFilePtr File, Position Location = -1)
00205         {
00206             Source.Valid = true;
00207             Source.File = File;
00208             if(Location < 0) Source.Offset = File->Tell();
00209             else Source.Offset = Location;
00210 
00211             // If we don't have a destination file assume it is the same as the source file
00212             if(!Dest.Valid)
00213             {
00214                 Dest = Source;
00215             }
00216         }
00217 
00219 
00222         virtual void SetDestination(MXFFilePtr File, Position Location = -1)
00223         {
00224             Dest.Valid = true;
00225             Dest.File = File;
00226 
00227             if(Location < 0) Dest.Offset = File->Tell();
00228             else Dest.Offset = Location;
00229         }
00230 
00232         virtual ULPtr GetUL(void) { return TheUL; }
00233 
00235         virtual void SetUL(ULPtr NewUL) { TheUL = NewUL; }
00236 
00238         virtual Position GetLocation(void) { return Source.Offset; }
00239 
00241         virtual std::string GetSource(void);
00242 
00244         std::string GetSourceLocation(void) 
00245         {
00246             if(!Source.File) return std::string("KLVObject created in memory");
00247             return std::string("0x") + Int64toHexString(GetLocation(),8) + std::string(" in ") + GetSource();
00248         }
00249 
00251         virtual Int32 GetKLSize(void) { return Source.KLSize >= 0 ? Source.KLSize : Dest.KLSize; }
00252 
00254 
00256         virtual void SetKLSize(Int32 NewKLSize) { Dest.KLSize = NewKLSize; }
00257 
00259         virtual GCElementKind GetGCElementKind(void) { return mxflib::GetGCElementKind(TheUL); }
00260 
00262         virtual UInt32 GetGCTrackNumber(void) { return mxflib::GetGCTrackNumber(TheUL); };
00263 
00265 
00267         virtual Position GetDataBase(void) { return DataBase; };
00268 
00270 
00272         virtual void SetDataBase(Position NewBase) { DataBase = NewBase; };
00273 
00275 
00277         virtual Int32 ReadKL(void) { return Base_ReadKL(); }
00278 
00280 
00286         Int32 Base_ReadKL(void);
00287 
00289 
00292         virtual size_t ReadData(size_t Size = static_cast<size_t>(-1)) { return Base_ReadDataFrom(0, Size); }
00293 
00295 
00299         virtual size_t ReadDataFrom(Position Offset, size_t Size = static_cast<size_t>(-1)) { return Base_ReadDataFrom(Offset, Size); }
00300 
00302 
00310         inline size_t Base_ReadDataFrom(Position Offset, size_t Size = static_cast<size_t>(-1)) { return Base_ReadDataFrom(Data, Offset, Size); }
00311 
00313 
00324         size_t Base_ReadDataFrom(DataChunk &Buffer, Position Offset, size_t Size = static_cast<size_t>(-1));
00325 
00327 
00330         virtual Int32 WriteKL(Int32 LenSize = 0) { return Base_WriteKL(LenSize); }
00331 
00333 
00341         Int32 Base_WriteKL(Int32 LenSize = 0, Length NewLength = -1);
00342 
00343 
00345 
00348         virtual size_t WriteData(size_t Size = static_cast<size_t>(-1)) { return WriteDataFromTo(0, 0, Size); }
00349 
00351 
00355         virtual size_t WriteDataFrom(Position Start, size_t Size = static_cast<size_t>(-1)) { return WriteDataFromTo(0, Start, Size); }
00356 
00358 
00362         virtual size_t WriteDataTo(Position Offset, size_t Size = static_cast<size_t>(-1)) { return WriteDataFromTo(Offset, 0, Size); }
00363 
00365 
00370         virtual size_t WriteDataFromTo(Position Offset, Position Start, size_t Size = static_cast<size_t>(-1))
00371         {
00372             // Calculate default number of bytes to write
00373             Length BytesToWrite = Data.Size - Start;
00374 
00375             // Write the requested size (if valid)
00376             if((Size > 0) && (Size < BytesToWrite)) BytesToWrite = Size;
00377 
00378             // Sanity check the size of this chunk
00379             if((sizeof(size_t) < 8) && (BytesToWrite > 0xffffffff))
00380             {
00381                 error("Tried to write > 4GBytes, but this platform can only handle <= 4GByte chunks\n");
00382                 return 0;
00383             }
00384 
00385             return Base_WriteDataTo(&Data.Data[Start], Offset, static_cast<size_t>(BytesToWrite));
00386         }
00387 
00389 
00396         virtual size_t WriteDataTo(const UInt8 *Buffer, Position Offset, size_t Size) { return Base_WriteDataTo(Buffer, Offset, Size); }
00397 
00399 
00408         size_t Base_WriteDataTo(const UInt8 *Buffer, Position Offset, size_t Size);
00409 
00410 
00412 
00414         virtual void SetReadHandler(KLVReadHandlerPtr Handler) { ReadHandler = Handler; }
00415 
00416 //      //! Set a handler to supply data when a write is performed
00417 //      /*! \note If not set it will be written to destination file (if available) or cause an error message
00418 //       */
00419 //      virtual void SetWriteHandler(KLVWriteHandlerPtr Handler) { WriteHandler = Handler; }
00420 
00422         virtual Length GetLength(void) { return ValueLength; }
00423 
00425         virtual void SetLength(Length NewLength) { ValueLength = Dest.OuterLength = Source.OuterLength = NewLength; }
00426 
00428         virtual DataChunk& GetData(void) { return Data; }
00429     };
00430 }
00431 
00432 #endif // MXFLIB__KLVOBJECT_H

Generated on Mon Apr 2 15:20:53 2007 for MXFLib by  doxygen 1.5.1-p1