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

BufferObjectWriter.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: BufferObjectWriter.h,v $
00010  * Date modified: $Date: 2004/02/12 19:30:27 $
00011  * Version:       $Revision: 1.10 $
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_WRITER_H
00043 #define _VPR_BUFFER_OBJECT_WRITER_H
00044 
00045 #include <vpr/vprConfig.h>
00046 #include <vector>
00047 #include <boost/static_assert.hpp>
00048 #include <boost/concept_check.hpp>
00049 
00050 #include <vpr/System.h>
00051 #include <vpr/IO/ObjectWriter.h>
00052 
00053 namespace vpr
00054 {
00055 
00062 class BufferObjectWriter : public ObjectWriter
00063 {
00064 public:
00065    BufferObjectWriter()
00066    {
00067       mData = new std::vector<vpr::Uint8>;
00068       mCurHeadPos = 0;
00069       mIsBinary = true;
00070    }
00071 
00072    BufferObjectWriter(std::vector<vpr::Uint8>* data, unsigned curPos=0)
00073    {
00074       mData = data;
00075       mCurHeadPos = curPos;
00076       mIsBinary = true;
00077    }
00078 
00079    void setCurPos(unsigned val)
00080    { mCurHeadPos = val; }
00081    unsigned getCurPos()
00082    { return mCurHeadPos; }
00083 
00084    std::vector<vpr::Uint8>* getData()
00085    { return mData; }
00086 
00091    virtual vpr::ReturnStatus beginTag(std::string tagName)
00092    {
00093       boost::ignore_unused_variable_warning(tagName);
00094       return vpr::ReturnStatus::Succeed;
00095    }
00096    
00098    virtual vpr::ReturnStatus endTag()
00099    {
00100       return vpr::ReturnStatus::Succeed;
00101    }
00102 
00104    virtual vpr::ReturnStatus beginAttribute(std::string attributeName)
00105    {
00106       boost::ignore_unused_variable_warning(attributeName);
00107       return vpr::ReturnStatus::Succeed;
00108    }
00109 
00111    virtual vpr::ReturnStatus endAttribute()
00112    {
00113       return vpr::ReturnStatus::Succeed;
00114    }
00116 
00117    virtual vpr::ReturnStatus writeUint8(vpr::Uint8 val);
00118    virtual vpr::ReturnStatus writeUint16(vpr::Uint16 val);
00119    virtual vpr::ReturnStatus writeUint32(vpr::Uint32 val);
00120    virtual vpr::ReturnStatus writeUint64(vpr::Uint64 val);
00121    virtual vpr::ReturnStatus writeFloat(float val);
00122    virtual vpr::ReturnStatus writeDouble(double val);
00123    virtual vpr::ReturnStatus writeString(std::string val);
00124    virtual vpr::ReturnStatus writeBool(bool val);
00125 
00126    /* Write raw data of length len */
00127    inline vpr::ReturnStatus writeRaw(vpr::Uint8* data, unsigned len=1);
00128 
00129 public:
00130    std::vector<vpr::Uint8>*   mData;
00131    unsigned                   mCurHeadPos;
00132 };
00133 
00134 /* Write out the single byte.
00135 * @post: data = old(data)+val, mCurHeadPos advaced 1
00136 */
00137 inline vpr::ReturnStatus BufferObjectWriter::writeUint8(vpr::Uint8 val)
00138 {
00139    return writeRaw(&val, 1);
00140 }
00141 
00142 inline vpr::ReturnStatus BufferObjectWriter::writeUint16(vpr::Uint16 val)
00143 {
00144    vpr::Uint16 nw_val = vpr::System::Htons(val);
00145 
00146    return writeRaw((vpr::Uint8*)&nw_val, 2);
00147 }
00148 
00149 inline vpr::ReturnStatus BufferObjectWriter::writeUint32(vpr::Uint32 val)
00150 {
00151    vpr::Uint32 nw_val = vpr::System::Htonl(val);
00152 
00153    return writeRaw((vpr::Uint8*)&nw_val, 4);
00154 }
00155 
00156 inline vpr::ReturnStatus BufferObjectWriter::writeUint64(vpr::Uint64 val)
00157 {
00158    vpr::Uint64 nw_val = vpr::System::Htonll(val);
00159 
00160    return writeRaw((vpr::Uint8*)&nw_val, 8);
00161 }
00162 
00163 inline vpr::ReturnStatus BufferObjectWriter::writeFloat(float val)
00164 {
00165    // We are writing the float as a 4 byte value
00166    BOOST_STATIC_ASSERT(sizeof(float) == 4);
00167    vpr::Uint32 nw_val = vpr::System::Htonl(*((vpr::Uint32*)&val));
00168 
00169    return writeRaw((vpr::Uint8*)&nw_val, 4);
00170 }
00171 
00172 inline vpr::ReturnStatus BufferObjectWriter::writeDouble(double val)
00173 {
00174    // We are writing the double as a 8 byte value
00175    BOOST_STATIC_ASSERT(sizeof(double) == 8);
00176    vpr::Uint64 nw_val = vpr::System::Htonll(*((vpr::Uint64*)&val));
00177 
00178    return writeRaw((vpr::Uint8*)&nw_val, 8);
00179 }
00180 
00181 
00182 inline vpr::ReturnStatus BufferObjectWriter::writeString(std::string val)
00183 {
00184    writeUint16(val.size());
00185    for(unsigned i=0; i<val.length();++i)
00186    {
00187       writeRaw((vpr::Uint8*)&(val[i]),1);
00188    }
00189 
00190    return vpr::ReturnStatus::Succeed;
00191 }
00192 
00193 inline vpr::ReturnStatus BufferObjectWriter::writeBool(bool val)
00194 {
00195    // Darwin uses four bytes (!) for bools.
00196 #ifdef VPR_OS_Darwin
00197    vpr::Uint8 temp = (vpr::Uint8) val;
00198    return writeRaw((vpr::Uint8*)&temp, 1);
00199 #else
00200    return writeRaw((vpr::Uint8*)&val, 1);
00201 #endif
00202 }
00203 
00204 inline vpr::ReturnStatus BufferObjectWriter::writeRaw(vpr::Uint8* data, unsigned len)
00205 {
00206    for(unsigned i=0;i<len;++i)
00207       mData->push_back(data[i]);
00208    mCurHeadPos += len;
00209    return vpr::ReturnStatus::Succeed;
00210 }
00211 
00212 } // namespace vpr
00213 
00214 #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