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$
00010  * Date modified: $Date: 2005-12-21 16:43:31 -0600 (Wed, 21 Dec 2005) $
00011  * Version:       $Revision: 18311 $
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-2005 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 VPR_CLASS_API BufferObjectWriter : public ObjectWriter
00063 {
00064 public:
00070    static const unsigned int STRING_LENGTH_SIZE;
00071 
00072    BufferObjectWriter();
00073 
00074    BufferObjectWriter(std::vector<vpr::Uint8>* data,
00075                       const unsigned int curPos = 0);
00076 
00077    void setCurPos(unsigned int val)
00078    {
00079       mCurHeadPos = val;
00080    }
00081 
00082    unsigned int getCurPos()
00083    {
00084       return mCurHeadPos;
00085    }
00086 
00087    std::vector<vpr::Uint8>* getData()
00088    {
00089       return mData;
00090    }
00091 
00095    virtual vpr::ReturnStatus beginTag(const std::string& tagName)
00096    {
00097       boost::ignore_unused_variable_warning(tagName);
00098       return vpr::ReturnStatus::Succeed;
00099    }
00100    
00102    virtual vpr::ReturnStatus endTag()
00103    {
00104       return vpr::ReturnStatus::Succeed;
00105    }
00106 
00108    virtual vpr::ReturnStatus beginAttribute(const std::string& attributeName)
00109    {
00110       boost::ignore_unused_variable_warning(attributeName);
00111       return vpr::ReturnStatus::Succeed;
00112    }
00113 
00115    virtual vpr::ReturnStatus endAttribute()
00116    {
00117       return vpr::ReturnStatus::Succeed;
00118    }
00120 
00125    virtual vpr::ReturnStatus writeUint8(vpr::Uint8 val);
00126    virtual vpr::ReturnStatus writeUint16(vpr::Uint16 val);
00127    virtual vpr::ReturnStatus writeUint32(vpr::Uint32 val);
00128    virtual vpr::ReturnStatus writeUint64(vpr::Uint64 val);
00129    virtual vpr::ReturnStatus writeFloat(float val);
00130    virtual vpr::ReturnStatus writeDouble(double val);
00131    virtual vpr::ReturnStatus writeString(std::string val);
00132    virtual vpr::ReturnStatus writeBool(bool val);
00133 
00134    /* Writes raw data of length \p len. */
00135    inline vpr::ReturnStatus writeRaw(vpr::Uint8* data,
00136                                      const unsigned int len = 1);
00137 
00138 public:
00139    std::vector<vpr::Uint8>*   mData;
00140    unsigned int               mCurHeadPos;
00141 };
00142 
00143 inline vpr::ReturnStatus BufferObjectWriter::writeUint8(vpr::Uint8 val)
00144 {
00145    return writeRaw(&val, 1);
00146 }
00147 
00148 inline vpr::ReturnStatus BufferObjectWriter::writeUint16(vpr::Uint16 val)
00149 {
00150    vpr::Uint16 nw_val = vpr::System::Htons(val);
00151 
00152    return writeRaw((vpr::Uint8*) &nw_val, 2);
00153 }
00154 
00155 inline vpr::ReturnStatus BufferObjectWriter::writeUint32(vpr::Uint32 val)
00156 {
00157    vpr::Uint32 nw_val = vpr::System::Htonl(val);
00158 
00159    return writeRaw((vpr::Uint8*) &nw_val, 4);
00160 }
00161 
00162 inline vpr::ReturnStatus BufferObjectWriter::writeUint64(vpr::Uint64 val)
00163 {
00164    vpr::Uint64 nw_val = vpr::System::Htonll(val);
00165 
00166    return writeRaw((vpr::Uint8*) &nw_val, 8);
00167 }
00168 
00169 inline vpr::ReturnStatus BufferObjectWriter::writeFloat(float val)
00170 {
00171    // We are writing the float as a 4 byte value
00172    BOOST_STATIC_ASSERT(sizeof(float) == 4);
00173    vpr::Uint32 nw_val = vpr::System::Htonl(*((vpr::Uint32*) &val));
00174 
00175    return writeRaw((vpr::Uint8*) &nw_val, 4);
00176 }
00177 
00178 inline vpr::ReturnStatus BufferObjectWriter::writeDouble(double val)
00179 {
00180    // We are writing the double as a 8 byte value
00181    BOOST_STATIC_ASSERT(sizeof(double) == 8);
00182    vpr::Uint64 nw_val = vpr::System::Htonll(*((vpr::Uint64*) &val));
00183 
00184    return writeRaw((vpr::Uint8*) &nw_val, 8);
00185 }
00186 
00187 inline vpr::ReturnStatus BufferObjectWriter::writeString(std::string val)
00188 {
00189    // Note: If you change this, you need to change STRING_LENGTH_SIZE
00190    writeUint32(val.size());
00191 
00192    for ( vpr::Uint32 i = 0; i < val.length(); ++i )
00193    {
00194       writeRaw((vpr::Uint8*) &val[i], 1);
00195    }
00196 
00197    return vpr::ReturnStatus::Succeed;
00198 }
00199 
00200 inline vpr::ReturnStatus BufferObjectWriter::writeBool(bool val)
00201 {
00202    // Darwin uses four bytes (!) for bools.
00203 #ifdef VPR_OS_Darwin
00204    vpr::Uint8 temp = (vpr::Uint8) val;
00205    return writeRaw((vpr::Uint8*) &temp, 1);
00206 #else
00207    return writeRaw((vpr::Uint8*) &val, 1);
00208 #endif
00209 }
00210 
00211 inline vpr::ReturnStatus BufferObjectWriter::writeRaw(vpr::Uint8* data,
00212                                                       const unsigned int len)
00213 {
00214    for ( unsigned int i = 0; i < len; ++i )
00215    {
00216       mData->push_back(data[i]);
00217    }
00218 
00219    mCurHeadPos += len;
00220    return vpr::ReturnStatus::Succeed;
00221 }
00222 
00223 } // namespace vpr
00224 
00225 #endif

Generated on Thu Jan 4 10:52:09 2007 for VR Juggler Portable Runtime by  doxygen 1.5.1