src/hbcore/svgext/hbnvgdecoder/hbdereferencer_p.h
changeset 5 627c4a0fd0e7
equal deleted inserted replaced
3:11d3954df52a 5:627c4a0fd0e7
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (developer.feedback@nokia.com)
       
     6 **
       
     7 ** This file is part of the HbCore module of the UI Extensions for Mobile.
       
     8 **
       
     9 ** GNU Lesser General Public License Usage
       
    10 ** This file may be used under the terms of the GNU Lesser General Public
       
    11 ** License version 2.1 as published by the Free Software Foundation and
       
    12 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
       
    13 ** Please review the following information to ensure the GNU Lesser General
       
    14 ** Public License version 2.1 requirements will be met:
       
    15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    16 **
       
    17 ** In addition, as a special exception, Nokia gives you certain additional
       
    18 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    20 **
       
    21 ** If you have questions regarding the use of this file, please contact
       
    22 ** Nokia at developer.feedback@nokia.com.
       
    23 **
       
    24 ****************************************************************************/
       
    25 
       
    26 #ifndef HB_DEREFERENCER_P_H
       
    27 #define HB_DEREFERENCER_P_H
       
    28 
       
    29 #include <QByteArray>
       
    30 #include <QtGlobal>
       
    31 
       
    32 #include "hbnvg_p.h"
       
    33 #include "hbnvgexception_p.h"
       
    34 
       
    35 class HbDereferencer
       
    36 {
       
    37 #define DEREF_PTR(TOTYPE, Offset, Size) do {\
       
    38                                 checkOutOfBound(Offset + Size); \
       
    39                                 return * (TOTYPE *)&mReadStream[mTotalRead + Offset];\
       
    40                            } while (0)
       
    41 public:
       
    42    
       
    43     HbDereferencer(quint8* buf, qint32 length)
       
    44         : mTotalRead(0),
       
    45         mDataLength(length),
       
    46         mReadStream((unsigned char*)buf)
       
    47     {
       
    48     }
       
    49 
       
    50     HbDereferencer(const QByteArray& buffer)
       
    51             : mTotalRead(0),
       
    52             mDataLength(buffer.length()),
       
    53             mReadStream((unsigned char*)buffer.data())
       
    54     {        
       
    55     }
       
    56 
       
    57     void skip(qint32 length)
       
    58     {
       
    59         checkOutOfBound(length);
       
    60         mTotalRead += length;
       
    61     }
       
    62 
       
    63     qint16 derefInt16(qint16 at = 0)
       
    64     {
       
    65         DEREF_PTR(qint16, at, sizeof(qint16));
       
    66     }
       
    67     operator qint16()
       
    68     {
       
    69         return derefInt16();    
       
    70     }
       
    71 
       
    72     qint32 derefInt32(qint32 at = 0)
       
    73     {
       
    74         DEREF_PTR(qint32, at, sizeof(qint32));
       
    75     }
       
    76     
       
    77     operator qint32()
       
    78     {
       
    79         return derefInt32();    
       
    80     }
       
    81 
       
    82     qint8 derefInt8(qint32 at = 0)
       
    83     {
       
    84         DEREF_PTR(qint8, at, sizeof(qint8));
       
    85     }
       
    86     
       
    87     operator qint8()
       
    88     {
       
    89         return derefInt8();    
       
    90     }
       
    91 
       
    92     quint8* derefInt8Array(qint32 length, qint32 at = 0)
       
    93     {
       
    94         checkOutOfBound(at + length);
       
    95         return (quint8 *)&mReadStream[mTotalRead + at];
       
    96     }
       
    97 
       
    98     float derefReal32(qint32 at = 0)
       
    99     {
       
   100         DEREF_PTR(float, at, sizeof(float));
       
   101     }
       
   102     operator float()
       
   103     {
       
   104         return derefReal32();
       
   105     }
       
   106 
       
   107     void assertBound(qint32 length, qint32 at = 0)
       
   108     {
       
   109         checkOutOfBound(at + length);
       
   110     }
       
   111 
       
   112     quint8* getPtr() const
       
   113     {
       
   114         return mReadStream;
       
   115     }
       
   116 
       
   117     qint32 getLength() const
       
   118     {
       
   119         return mDataLength;
       
   120     }
       
   121 
       
   122     qint32 getReadingPos() const
       
   123     {
       
   124         return mTotalRead;
       
   125     }
       
   126     
       
   127 private:
       
   128     void checkOutOfBound(qint32 length)
       
   129     {
       
   130         if (mTotalRead + length > mDataLength ||
       
   131                 mTotalRead + length < 0){
       
   132             throw HbNvgException(HbNvgEngine::NvgErrEof);
       
   133         }
       
   134     }
       
   135 
       
   136     qint32                mTotalRead;
       
   137     qint32                mDataLength;
       
   138     quint8*            mReadStream;
       
   139 };
       
   140 
       
   141 #endif
       
   142