Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Examples  

BufferObjectReader.h

Go to the documentation of this file.
00001 /****************** <VPR heading BEGIN do not edit this line> *****************
00002  *
00003  * VR Juggler Portable Runtime
00004  *
00005  * Original Authors:
00006  *   Allen Bierbaum, Patrick Hartling, Kevin Meinert, Carolina Cruz-Neira
00007  *
00008  * -----------------------------------------------------------------
00009  * File:          $RCSfile: BufferObjectReader.h,v $
00010  * Date modified: $Date: 2004/02/12 19:30:27 $
00011  * Version:       $Revision: 1.12 $
00012  * -----------------------------------------------------------------
00013  *
00014  ****************** <VPR heading END do not edit this line> ******************/
00015 
00016 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
00017  *
00018  * VR Juggler is (C) Copyright 1998-2003 by Iowa State University
00019  *
00020  * Original Authors:
00021  *   Allen Bierbaum, Christopher Just,
00022  *   Patrick Hartling, Kevin Meinert,
00023  *   Carolina Cruz-Neira, Albert Baker
00024  *
00025  * This library is free software; you can redistribute it and/or
00026  * modify it under the terms of the GNU Library General Public
00027  * License as published by the Free Software Foundation; either
00028  * version 2 of the License, or (at your option) any later version.
00029  *
00030  * This library is distributed in the hope that it will be useful,
00031  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00032  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00033  * Library General Public License for more details.
00034  *
00035  * You should have received a copy of the GNU Library General Public
00036  * License along with this library; if not, write to the
00037  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00038  * Boston, MA 02111-1307, USA.
00039  *
00040  *************** <auto-copyright.pl END do not edit this line> ***************/
00041 
00042 #ifndef _VPR_BUFFER_OBJECT_READER_H
00043 #define _VPR_BUFFER_OBJECT_READER_H
00044 
00045 #include <vpr/vprConfig.h>
00046 
00047 #include <vector>
00048 #include <boost/static_assert.hpp>
00049 #include <boost/concept_check.hpp>
00050 
00051 #include <vpr/IO/ObjectReader.h>
00052 #include <vpr/Util/Assert.h>
00053 #include <vpr/System.h>
00054 
00055 
00056 namespace vpr
00057 {
00058 
00063 class BufferObjectReader : public ObjectReader
00064 {
00065 public:
00066    BufferObjectReader(std::vector<vpr::Uint8>* data, unsigned curPos=0)
00067    {
00068       mIsBinary = true;
00069       mData = data;
00070       mCurHeadPos = curPos;
00071    }
00072 
00073    void setCurPos(unsigned val)
00074    { mCurHeadPos = val; }
00075    unsigned getCurPos()
00076    { return mCurHeadPos; }
00077 
00079    virtual void resetReading()
00080    { setCurPos(0); }
00081 
00082    virtual void pushState()
00083    { mHeadPosStateStack.push_back(mCurHeadPos); }
00084    virtual void popState()
00085    {
00086       unsigned new_head_pos = mHeadPosStateStack.back();
00087       mHeadPosStateStack.pop_back();
00088       setCurPos(new_head_pos);
00089    }
00090 
00091 
00096    virtual vpr::ReturnStatus beginTag(std::string tagName)
00097    {
00098       boost::ignore_unused_variable_warning(tagName);
00099       return vpr::ReturnStatus::Succeed;
00100    }
00101 
00103    virtual vpr::ReturnStatus endTag()
00104    {
00105       return vpr::ReturnStatus::Succeed;
00106    }
00107 
00109    virtual vpr::ReturnStatus beginAttribute(std::string attributeName)
00110    {
00111       boost::ignore_unused_variable_warning(attributeName);
00112       return vpr::ReturnStatus::Succeed;
00113    }
00114 
00116    virtual vpr::ReturnStatus endAttribute()
00117    {
00118       return vpr::ReturnStatus::Succeed;
00119    }
00121 
00122 
00123    inline virtual vpr::Uint8 readUint8();
00124    inline virtual vpr::Uint16 readUint16();
00125    inline virtual vpr::Uint32 readUint32();
00126    inline virtual vpr::Uint64 readUint64();
00127    inline virtual float readFloat();
00128    inline virtual double readDouble();
00129    inline virtual std::string readString();
00130    inline virtual bool readBool();
00131 
00132    /* Helper methods */
00133    virtual void readUint8(vpr::Uint8& val)
00134    { val = this->readUint8(); }
00135    virtual void readUint16(vpr::Uint16& val)
00136    { val = this->readUint16(); }
00137    virtual void readUint32(vpr::Uint32& val)
00138    { val = this->readUint32(); }
00139    virtual void readUint64(vpr::Uint64& val)
00140    { val = this->readUint64(); }
00141    virtual void readFloat(float& val)
00142    { val = this->readFloat(); }
00143    virtual void readDouble(double& val)
00144    { val = this->readDouble(); }
00145    virtual void readString(std::string& str)
00146    { str = this->readString(); }
00147    virtual void readBool(bool& val)
00148    { val = this->readBool(); }
00149 
00150    /* Read raw data of length len
00151    * POST: Pointer to data returned
00152    * NOTE: data points to data owned elsewhere.
00153    * DO NOT MODIFY THE DATA and DO NOT RELY ON THE DATA STAYING THERE LONG.
00154    */
00155    inline vpr::Uint8* readRaw(unsigned len=1);
00156 
00157 public:
00158    std::vector<vpr::Uint8>*   mData;
00159    unsigned                   mCurHeadPos;
00160    std::vector<unsigned>      mHeadPosStateStack;  
00161 };
00162 
00163 /* Read out the single byte.
00164 * @post: data = old(data)+val, mCurHeadPos advaced 1
00165 */
00166 vpr::Uint8 BufferObjectReader::readUint8()
00167 {
00168    vpr::Uint8 temp_data;
00169    memcpy(&temp_data, readRaw(1), 1);
00170    return temp_data;
00171 }
00172 
00173 vpr::Uint16 BufferObjectReader::readUint16()
00174 {
00175    vpr::Uint16 nw_val;
00176    memcpy(&nw_val, readRaw(2), 2);
00177 
00178    return vpr::System::Ntohs(nw_val);
00179 }
00180 
00181 vpr::Uint32 BufferObjectReader::readUint32()
00182 {
00183    vpr::Uint32 nw_val;
00184    memcpy(&nw_val, readRaw(4), 4);
00185 
00186    return vpr::System::Ntohl(nw_val);
00187 }
00188 
00189 vpr::Uint64 BufferObjectReader::readUint64()
00190 {
00191    vpr::Uint64 nw_val;
00192    memcpy(&nw_val, readRaw(8), 8);
00193    vpr::Uint64 h_val = vpr::System::Ntohll(nw_val);
00194 
00195    return h_val;
00196 }
00197 
00198 float BufferObjectReader::readFloat()
00199 {
00200    // We are reading the float as a 4 byte value
00201    BOOST_STATIC_ASSERT(sizeof(float) == 4);
00202 
00203    vpr::Uint32 nw_val;
00204    memcpy(&nw_val, readRaw(4), 4);
00205    vpr::Uint32 h_val = vpr::System::Ntohl(nw_val);
00206 
00207    return *((float*)&h_val);
00208 }
00209 
00210 double BufferObjectReader::readDouble()
00211 {
00212    // We are reading the double as a 8 byte value
00213    BOOST_STATIC_ASSERT(sizeof(double) == 8);
00214 
00215    vpr::Uint64 nw_val;
00216    memcpy(&nw_val, readRaw(8), 8);
00217    vpr::Uint64 h_val = vpr::System::Ntohll(nw_val);
00218    double d_val = *((double*)&h_val);
00219 
00220    return d_val;
00221 }
00222 
00223 
00224 std::string BufferObjectReader::readString()
00225 {
00226    vpr::Uint16 str_len = readUint16();
00227    std::string ret_val;
00228    char tempChar;
00229    for(unsigned i=0; i<str_len;++i)
00230    {
00231       tempChar = (char)(*readRaw(1));
00232       ret_val += tempChar;
00233    }
00234    return ret_val;
00235 }
00236 
00237 bool BufferObjectReader::readBool()
00238 {
00239    return (bool)*(readRaw(1));
00240 }
00241 
00242 
00243 vpr::Uint8* BufferObjectReader::readRaw(unsigned len)
00244 {
00245    mCurHeadPos += len;
00246    vprASSERT((mCurHeadPos-len) < mData->size());
00247 
00248    return &((*mData)[mCurHeadPos-len]);
00249 }
00250 
00251 } // namespace vpr
00252 
00253 #endif

Generated on Sun May 2 14:43:01 2004 for VR Juggler Portable Runtime by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002