Msrp/MsrpServer/inc/MSRPBuffer.h
branchMSRP_FrameWork
changeset 25 505ad3f0ce5c
equal deleted inserted replaced
22:f1578314b8da 25:505ad3f0ce5c
       
     1 /*
       
     2 * Copyright (c) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html."
       
     8 * Initial Contributors:
       
     9 * Nokia Corporation - initial contribution.
       
    10 * Contributors:
       
    11 *
       
    12 * Description:
       
    13 * MSRP Implementation
       
    14 *
       
    15 */
       
    16 
       
    17 #ifndef __MMSRPBUFFER_H
       
    18 #define __MMSRPBUFFER_H
       
    19 
       
    20 // INCLUDES
       
    21 #include <e32base.h>
       
    22 #include "MsrpCommon.h"
       
    23 
       
    24 // CLASS DECLARATIONS
       
    25 class RMsrpBuf;
       
    26 class CMSRPBuffer;
       
    27 class CMSRPBufPool;
       
    28 /**
       
    29  * pool of buffers
       
    30  */
       
    31 
       
    32 
       
    33 class CMSRPBufPool : public CBase
       
    34     {
       
    35     friend class CMSRPBuffer;     
       
    36 public: 
       
    37     inline RMsrpBuf ProvideBufferL(TInt aSize = KBufSize);
       
    38     //inline RMsrpBuf ProvideBufferL(TInt aSize);
       
    39         
       
    40     //InitializePool; //dummy
       
    41     //void ReleasePool(); //force free all buffers
       
    42     inline virtual ~CMSRPBufPool();
       
    43         
       
    44 private:
       
    45     inline void TakeBackBufferL(CMSRPBuffer* aBuffer); //append to pool
       
    46 
       
    47 private:    
       
    48     RPointerArray<CMSRPBuffer> iFreeBufPool; //free buf pool
       
    49     RPointerArray<CMSRPBuffer> iUsedBufPool; //free buf pool
       
    50     };
       
    51 
       
    52 
       
    53 /**
       
    54 * The buffer class
       
    55 */
       
    56 class CMSRPBuffer : public CBase
       
    57     {
       
    58     
       
    59 friend class RMsrpBuf;  
       
    60 
       
    61     public:
       
    62         inline static CMSRPBuffer* NewL(TInt aSize, CMSRPBufPool& aPool );        
       
    63         inline virtual ~CMSRPBuffer();
       
    64     
       
    65     private:
       
    66         inline void ConstructL(TInt aSize);
       
    67         inline CMSRPBuffer(CMSRPBufPool& aPool);
       
    68         TPtr8 MsrpDes(TInt aOffset);
       
    69         TPtr8 MsrpDes();
       
    70 
       
    71         inline void IncRef(void);           
       
    72         inline void DecRef(void);
       
    73 
       
    74     private:
       
    75         TUint iRef;
       
    76         HBufC8* iBuf;
       
    77         CMSRPBufPool& iBufPool;
       
    78                 
       
    79     };
       
    80 
       
    81 class RMsrpBuf
       
    82     {
       
    83     public:    
       
    84         
       
    85     inline RMsrpBuf(CMSRPBuffer& aBuf);
       
    86             
       
    87     inline RMsrpBuf(RMsrpBuf& aClone, TPtr8 aPtr);
       
    88     
       
    89     //get a R ref on the C class using the R class //MidTPtr
       
    90     inline RMsrpBuf(const RMsrpBuf& aCopy);
       
    91     
       
    92     //makes the rmsrpbuf point to the remaining buffer after the length  
       
    93     TInt MsrpRightExtract();
       
    94         
       
    95     //if same pool then append to our Rmsrpbuf and return true
       
    96     //no continuity check, not required as of now
       
    97     inline TBool Collate(RMsrpBuf& aSuffix);
       
    98     
       
    99     inline void MsrpRightTPtr(TInt length);
       
   100     
       
   101     inline void MsrpLeftTPtr(TInt length);
       
   102     
       
   103     inline void MsrpMidTPtr(TInt offset);
       
   104             
       
   105     inline void MsrpMidTPtr(TInt offset, TInt length);
       
   106     
       
   107     inline void Zero();
       
   108     
       
   109     inline void Append(TDesC8& aDes);
       
   110         
       
   111     
       
   112     /*implicit TPtr conv, temp only*/
       
   113     inline operator TPtr8() const;
       
   114     
       
   115     inline operator TPtr8&();
       
   116     
       
   117     /**
       
   118      * if TPtrs are passed to and stored in some class obj
       
   119      * then the passer must call close on rbuf after the obj has cleaned up itself or 
       
   120      *  released the reference 
       
   121      */
       
   122     inline TPtr8 Ptr();
       
   123     
       
   124     inline virtual ~RMsrpBuf();
       
   125         
       
   126     private:
       
   127         //assignment
       
   128         RMsrpBuf& operator=(const RMsrpBuf&);
       
   129         
       
   130     private:
       
   131         CMSRPBuffer& iBuf;
       
   132         TPtr8 iPtr;    
       
   133     };
       
   134 
       
   135 
       
   136 
       
   137     inline RMsrpBuf CMSRPBufPool::ProvideBufferL(TInt aSize) //remove head element of pool or allocate and provide
       
   138         {
       
   139         CMSRPBuffer* buf = NULL;
       
   140         if (iFreeBufPool.Count())
       
   141             {
       
   142             buf = iFreeBufPool[0];
       
   143             iFreeBufPool.Remove(0);
       
   144             }
       
   145         else
       
   146             {
       
   147             buf = CMSRPBuffer::NewL(aSize, *this);
       
   148             }
       
   149         CleanupStack::PushL(buf);
       
   150         iUsedBufPool.AppendL(buf); 
       
   151         CleanupStack::Pop();
       
   152         RMsrpBuf ptr(*buf);
       
   153         return ptr;
       
   154         }
       
   155    
       
   156     inline CMSRPBufPool::~CMSRPBufPool()
       
   157         {
       
   158         iFreeBufPool.ResetAndDestroy();
       
   159         iFreeBufPool.Close();
       
   160         iUsedBufPool.ResetAndDestroy();
       
   161         iUsedBufPool.Close();
       
   162         }
       
   163 
       
   164     inline void CMSRPBufPool::TakeBackBufferL(CMSRPBuffer* aBuffer) //append to pool
       
   165         {
       
   166         TInt index = iUsedBufPool.Find(aBuffer);    
       
   167         iUsedBufPool.Remove(index);
       
   168         CleanupStack::PushL(aBuffer);
       
   169         iFreeBufPool.AppendL(aBuffer);
       
   170         CleanupStack::Pop(aBuffer);
       
   171         }
       
   172 
       
   173 
       
   174 /**
       
   175 * The buffer class
       
   176 */
       
   177         inline CMSRPBuffer* CMSRPBuffer::NewL(TInt aSize, CMSRPBufPool& aPool )
       
   178             {
       
   179             CMSRPBuffer* self = new (ELeave) CMSRPBuffer(aPool );
       
   180             CleanupStack::PushL(self);
       
   181             self->ConstructL(aSize);
       
   182             CleanupStack::Pop(self);
       
   183             return self;
       
   184             }
       
   185             
       
   186         inline CMSRPBuffer::CMSRPBuffer(CMSRPBufPool& aPool ): iBufPool(aPool)
       
   187             {
       
   188 
       
   189             }
       
   190         
       
   191         inline void CMSRPBuffer::ConstructL(TInt aSize)
       
   192            {
       
   193            iBuf = HBufC8::NewL(aSize);
       
   194            }
       
   195         
       
   196         inline CMSRPBuffer::~CMSRPBuffer()
       
   197             {
       
   198             delete iBuf;
       
   199             }      
       
   200 
       
   201         inline TPtr8 CMSRPBuffer::MsrpDes(TInt aOffset) //MsrpRef
       
   202             {
       
   203             TPtr8 mPtr(iBuf->Des());
       
   204             mPtr.Set(mPtr.MidTPtr(aOffset));
       
   205             return mPtr;
       
   206             }
       
   207         
       
   208         inline TPtr8 CMSRPBuffer::MsrpDes() //MsrpRef
       
   209             {
       
   210             return iBuf->Des();            
       
   211             }
       
   212 
       
   213         inline void CMSRPBuffer::IncRef(void)
       
   214             {
       
   215             iRef++;
       
   216             }
       
   217         inline void CMSRPBuffer::DecRef(void)
       
   218             {
       
   219             iRef--;
       
   220             if (iRef == 0)
       
   221                 iBufPool.TakeBackBufferL(this);
       
   222                 //delete this;                
       
   223             }
       
   224 
       
   225 
       
   226     inline RMsrpBuf::RMsrpBuf(CMSRPBuffer& aBuf):iBuf(aBuf), iPtr(aBuf.MsrpDes())    
       
   227         {
       
   228         iBuf.IncRef();                 
       
   229         }
       
   230             
       
   231     inline RMsrpBuf::RMsrpBuf(RMsrpBuf& aClone, TPtr8 aPtr):iBuf(aClone.iBuf), iPtr(aPtr) 
       
   232         {
       
   233         //check TPtr belongs to buf
       
   234         //if(iBuf.IsInBuf(aPtr))//DEBUG
       
   235         iBuf.IncRef();
       
   236         //iPtr = aPtr;         
       
   237         }
       
   238     //get a R ref on the C class using the R class //MidTPtr
       
   239     inline RMsrpBuf::RMsrpBuf(const RMsrpBuf& aCopy):iBuf(aCopy.iBuf), iPtr(aCopy.iPtr)
       
   240         {        
       
   241         iBuf.IncRef();
       
   242         }
       
   243     
       
   244     inline TInt RMsrpBuf::MsrpRightExtract()
       
   245         {
       
   246         TInt length = iPtr.Length();
       
   247         iPtr.SetMax();
       
   248         iPtr.Set(iPtr.MidTPtr(length));     
       
   249         iPtr.Zero();
       
   250         return iPtr.MaxLength();
       
   251         }
       
   252     
       
   253     inline TBool RMsrpBuf::Collate(RMsrpBuf& aSuffix)
       
   254         {
       
   255         if (&aSuffix.iBuf != &iBuf)
       
   256             return FALSE;
       
   257                 
       
   258         iPtr.SetLength(iPtr.Length() + aSuffix.iPtr.Length());      
       
   259         return TRUE;
       
   260         }
       
   261     
       
   262     inline void RMsrpBuf::MsrpRightTPtr(TInt length)
       
   263         {
       
   264         iPtr.Set(iPtr.RightTPtr(length));    
       
   265         }
       
   266     
       
   267     inline void RMsrpBuf::MsrpLeftTPtr(TInt length)
       
   268         {
       
   269         iPtr.Set(iPtr.LeftTPtr(length));    
       
   270         }
       
   271     
       
   272     inline void RMsrpBuf::MsrpMidTPtr(TInt offset)
       
   273         {
       
   274         TInt length = iPtr.Length();
       
   275         iPtr.SetMax();
       
   276         iPtr.Set(iPtr.MidTPtr(offset));     
       
   277         iPtr.SetLength(length - offset);  
       
   278         }
       
   279             
       
   280     inline void RMsrpBuf::MsrpMidTPtr(TInt offset, TInt length)
       
   281         {
       
   282         iPtr.Set(iPtr.MidTPtr(offset, length));    
       
   283         }
       
   284     
       
   285     inline void RMsrpBuf::Zero()
       
   286         {
       
   287         iPtr.Zero();    
       
   288         }
       
   289     
       
   290     inline void RMsrpBuf::Append(TDesC8& aDes)
       
   291         {
       
   292         iPtr.Append(aDes);    
       
   293         }
       
   294     
       
   295     /*implicit TPtr conv, temp only*/
       
   296     inline RMsrpBuf::operator TPtr8() const
       
   297         {
       
   298         return iPtr;        
       
   299         }
       
   300     
       
   301     inline RMsrpBuf::operator TPtr8&()
       
   302         {
       
   303         return iPtr;        
       
   304         }
       
   305     
       
   306     /**
       
   307      * if TPtrs are passed to and stored in some class obj
       
   308      * then the passer must call close on rbuf after the obj has cleaned up itself or 
       
   309      *  released the reference 
       
   310      */
       
   311     inline TPtr8 RMsrpBuf::Ptr()
       
   312         {
       
   313         return iPtr;
       
   314         }
       
   315     
       
   316     inline RMsrpBuf::~RMsrpBuf()
       
   317     //inline void Close()
       
   318         {
       
   319         iBuf.DecRef();
       
   320         iPtr.Zero();
       
   321         //iBuf = 0;
       
   322         }
       
   323    
       
   324 
       
   325 #endif // __MMSRPBUFFER_H
       
   326 
       
   327 // End of file