Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

OSGBinaryDataHandler.inl

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *                   Copyright (C) 2000-2002 by OpenSG Forum                 *
00006  *                                                                           *
00007  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00008  *                                                                           *
00009 \*---------------------------------------------------------------------------*/
00010 /*---------------------------------------------------------------------------*\
00011  *                                License                                    *
00012  *                                                                           *
00013  * This library is free software; you can redistribute it and/or modify it   *
00014  * under the terms of th4e GNU Library General Public License as published    *
00015  * by the Free Software Foundation, version 2.                               *
00016  *                                                                           *
00017  * This library is distributed in the hope that it will be useful, but       *
00018  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00020  * Library General Public License for more details.                          *
00021  *                                                                           *
00022  * You should have received a copy of the GNU Library General Public         *
00023  * License along with this library; if not, write to the Free Software       *
00024  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00025  *                                                                           *
00026 \*---------------------------------------------------------------------------*/
00027 /*---------------------------------------------------------------------------*\
00028  *                                Changes                                    *
00029  *                                                                           *
00030  *                                                                           *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035 \*---------------------------------------------------------------------------*/
00036 
00037 #include <sys/types.h>
00038 
00039 #include <OSGBaseFunctions.h>
00040 
00041 OSG_BEGIN_NAMESPACE
00042 
00043 inline
00044 BinaryDataHandler::ReadError::ReadError(const Char8 *reason) : 
00045     Exception() 
00046 {
00047     _what += "BinaryDataHandler ReadError: ";
00048     _what += reason;
00049 }
00050 
00051 inline
00052 BinaryDataHandler::WriteError::WriteError(const Char8 *reason) : 
00053     Exception() 
00054 {
00055     _what += "BinaryDataHandler WriteError: ";
00056     _what += reason;
00057 }
00058 
00059 inline 
00060 void BinaryDataHandler::putValue(const bool &value)
00061 {
00062     // on Mac OS X a bool is four bytes long on all other
00063     // platfroms it is one byte long. So we write now always
00064     // one byte out.
00065     // put(&value, sizeof(bool));
00066     UInt8 temp = (UInt8) value;
00067     put(&temp, sizeof(UInt8));
00068 }
00069 
00070 inline 
00071 void BinaryDataHandler::putValue(const UInt8 &value)
00072 {
00073     put(&value, sizeof(UInt8));
00074 }
00075 
00076 inline 
00077 void BinaryDataHandler::putValue(const UInt16 &value)
00078 {
00079     UInt16 z = osghtons(value);
00080 
00081     put(&z, sizeof(UInt16));
00082 }
00083 
00084 inline 
00085 void BinaryDataHandler::putValue(const UInt32 &value)
00086 {
00087     UInt32 z = osghtonl(value);
00088 
00089     put(&z, sizeof(UInt32));
00090 }
00091 
00092 inline 
00093 void BinaryDataHandler::putValue(const UInt64 &value)
00094 {
00095     UInt64 z = osghtonll(value);
00096 
00097     put(&z, sizeof(UInt64));
00098 }
00099 
00100 inline 
00101 void BinaryDataHandler::putValue(const Int8 &value)
00102 {
00103     put(&value, sizeof(Int8));
00104 }
00105 
00106 inline 
00107 void BinaryDataHandler::putValue(const Int16 &value)
00108 {
00109     Int16 z = osghtons(value);
00110 
00111     put(&z, sizeof(Int16));
00112 }
00113 
00114 inline 
00115 void BinaryDataHandler::putValue(const Int32 &value)
00116 {
00117     Int32 z = osghtonl(value);
00118 
00119     put(&z, sizeof(Int32));
00120 }
00121 
00122 inline 
00123 void BinaryDataHandler::putValue(const Int64 &value)
00124 {
00125     Int64 z = osghtonll(value);
00126     put(&z, sizeof(Int64));
00127 
00128 }
00129 
00130 inline 
00131 void BinaryDataHandler::putValue(const Real16 &value)
00132 {
00133     UInt16 v = osghtons(value.bits());
00134 
00135     put(&v, sizeof(Real16));
00136 }
00137 
00138 inline 
00139 void BinaryDataHandler::putValue(const Real32 &value)
00140 {
00141     UInt32 v = osghtonl( *((const UInt32 *)(&value)) );
00142 
00143     put(&v, sizeof(Real32));
00144 }
00145 
00146 inline 
00147 void BinaryDataHandler::putValue(const Real64 &value)
00148 {
00149     UInt64 v = osghtonll( *((const UInt64 *)(&value)) );
00150 
00151     put(&v, sizeof(Real64));
00152 }
00153 
00154 inline 
00155 void BinaryDataHandler::putValue(const Real128 &value)
00156 {
00157     UInt64 v = osghtonll( *( (const UInt64 *)(&value)) );
00158     UInt64 w = osghtonll( *(((const UInt64 *)(&value)) + 1) );
00159 
00160 #if BYTE_ORDER == LITTLE_ENDIAN
00161     put(&w, sizeof(UInt64));
00162     put(&v, sizeof(UInt64));
00163 #else
00164     put(&v, sizeof(UInt64));
00165     put(&w, sizeof(UInt64));
00166 #endif
00167 }
00168 
00169 inline 
00170 void BinaryDataHandler::putValue(const std::string &value)
00171 {
00172     UInt32 len = stringlen(value.c_str()) + 1;
00173 
00174     putValue(len);
00175 
00176     if(len != 0)
00177     {
00178         put(value.c_str(), len);
00179     }
00180 }
00181 
00182 
00183 inline 
00184 void BinaryDataHandler::putValues(const bool *value, UInt32 size)
00185 {
00186     put(value, size * sizeof(bool));
00187 }
00188 
00189 inline 
00190 void BinaryDataHandler::putValues(const UInt8 *value, UInt32 size)
00191 {
00192     put(value, size * sizeof(UInt8));
00193 }
00194 
00195 inline 
00196 void BinaryDataHandler::putValues(const UInt16 *value, UInt32 size)
00197 {
00198 #if BYTE_ORDER == LITTLE_ENDIAN
00199 
00200     if(_networkOrder == true)
00201     {
00202         for(UInt32 i = 0; i < size; ++i)
00203         {
00204             putValue(value[i]);
00205         }
00206     }
00207     else
00208 #endif
00209     {
00210         put(value, size * sizeof(UInt16));
00211     }
00212 }
00213 
00214 inline 
00215 void BinaryDataHandler::putValues(const UInt32 *value, UInt32 size)
00216 {
00217 #if BYTE_ORDER == LITTLE_ENDIAN
00218     if(_networkOrder == true)
00219     {
00220         for(UInt32 i = 0; i < size; ++i)
00221         {
00222             putValue(value[i]);
00223         }
00224     }
00225     else
00226 #endif
00227     {
00228         put(value, size * sizeof(UInt32));
00229     }
00230 }
00231 
00232 inline 
00233 void BinaryDataHandler::putValues(const UInt64 *value, UInt32 size)
00234 {
00235 #if BYTE_ORDER == LITTLE_ENDIAN
00236     if(_networkOrder == true)
00237     {
00238         for(UInt32 i = 0; i < size; ++i)
00239         {
00240             putValue(value[i]);
00241         }
00242     }
00243     else
00244 #endif
00245     {
00246         put(value, size * sizeof(UInt64));
00247     }
00248 }
00249 
00250 inline 
00251 void BinaryDataHandler::putValues(const Int8 *value, UInt32 size)
00252 {
00253 #if BYTE_ORDER == LITTLE_ENDIAN
00254     if(_networkOrder == true)
00255     {
00256         for(UInt32 i = 0; i < size; ++i)
00257         {
00258             putValue(value[i]);
00259         }
00260     }
00261     else
00262 #endif
00263     {
00264         put(value, size * sizeof(Int8));
00265     }
00266 }
00267 
00268 inline 
00269 void BinaryDataHandler::putValues(const Int16 *value, UInt32 size)
00270 {
00271 #if BYTE_ORDER == LITTLE_ENDIAN
00272     if(_networkOrder == true)
00273     {
00274         for(UInt32 i = 0; i < size; ++i)
00275         {
00276             putValue(value[i]);
00277         }
00278     }
00279     else
00280 #endif
00281     {
00282         put(value, size * sizeof(Int16));
00283     }
00284 }
00285 
00286 inline 
00287 void BinaryDataHandler::putValues(const Int32 *value, UInt32 size)
00288 {
00289 #if BYTE_ORDER == LITTLE_ENDIAN
00290     if(_networkOrder == true)
00291     {
00292         for(UInt32 i = 0; i < size; ++i)
00293         {
00294             putValue(value[i]);
00295         }
00296     }
00297     else
00298 #endif
00299     {
00300         put(value, size * sizeof(Int32));
00301     }
00302 }
00303 
00304 inline 
00305 void BinaryDataHandler::putValues(const Int64 *value, UInt32 size)
00306 {
00307 #if BYTE_ORDER == LITTLE_ENDIAN
00308     if(_networkOrder == true)
00309     {
00310         for(UInt32 i = 0; i < size; ++i)
00311         {
00312             putValue(value[i]);
00313         }
00314     }
00315     else
00316 #endif
00317     {
00318         put(value, size * sizeof(Int64));
00319     }
00320 }
00321 
00322 inline 
00323 void BinaryDataHandler::putValues(const Real16 *value, UInt32 size)
00324 {
00325 #if BYTE_ORDER == LITTLE_ENDIAN
00326     if(_networkOrder == true)
00327     {
00328         for(UInt32 i = 0; i < size; ++i)
00329         {
00330             putValue(value[i]);
00331         }
00332     }
00333     else
00334 #endif
00335     {
00336         put(value, size * sizeof(Real16));
00337     }
00338 }
00339 
00340 inline 
00341 void BinaryDataHandler::putValues(const Real32 *value, UInt32 size)
00342 {
00343 #if BYTE_ORDER == LITTLE_ENDIAN
00344     if(_networkOrder == true)
00345     {
00346         for(UInt32 i = 0; i < size; ++i)
00347         {
00348             putValue(value[i]);
00349         }
00350     }
00351     else
00352 #endif
00353     {
00354         put(value, size * sizeof(Real32));
00355     }
00356 }
00357 
00358 inline 
00359 void BinaryDataHandler::putValues(const Real64 *value, UInt32 size)
00360 {
00361 #if BYTE_ORDER == LITTLE_ENDIAN
00362     if(_networkOrder == true)
00363     {
00364         for(UInt32 i = 0; i < size; ++i)
00365         {
00366             putValue(value[i]);
00367         }
00368     }
00369     else
00370 #endif
00371     {
00372         put(value, size * sizeof(Real64));
00373     }
00374 }
00375 
00376 inline 
00377 void BinaryDataHandler::putValues(const Real128 *value, UInt32 size)
00378 {
00379 #if BYTE_ORDER == LITTLE_ENDIAN
00380     if(_networkOrder == true)
00381     {
00382         for(UInt32 i = 0; i < size; ++i)
00383         {
00384             putValue(value[i]);
00385         }
00386     }
00387     else
00388 #endif
00389     {
00390         put(value, size * sizeof(Real128));
00391     }
00392 }
00393 
00394 inline 
00395 void BinaryDataHandler::putValues(const std::string *value, UInt32 size)
00396 {
00397     for(UInt32 i = 0; i<size; ++i)
00398     {
00399         putValue(value[i]);    
00400     }
00401 }
00402 
00403 inline 
00404 void BinaryDataHandler::getValue(bool &value)
00405 {
00406     //get(&value, sizeof(bool));
00407     UInt8 temp;
00408     get(&temp, sizeof(UInt8));
00409     value = (temp!=0);
00410 }
00411 
00412 inline 
00413 void BinaryDataHandler::getValue(UInt8 &value)
00414 {
00415     get(&value, sizeof(UInt8));
00416 }
00417 
00418 inline 
00419 void BinaryDataHandler::getValue(UInt16 &value)
00420 {
00421     get(&value, sizeof(UInt16));
00422 
00423     value = osgntohs(value);
00424 }
00425 
00426 inline 
00427 void BinaryDataHandler::getValue(UInt32 &value)
00428 {
00429     get(&value, sizeof(UInt32));
00430 
00431     value = osgntohl(value);
00432 }
00433 
00434 inline 
00435 void BinaryDataHandler::getValue(UInt64 &value)
00436 {
00437     get(&value, sizeof(UInt64));
00438 
00439     value = osgntohll(value);
00440 }
00441 
00442 inline 
00443 void BinaryDataHandler::getValue(Int8 &value)
00444 {
00445     get(&value, sizeof(Int8));
00446 }
00447 
00448 inline 
00449 void BinaryDataHandler::getValue(Int16 &value)
00450 {
00451     get(&value, sizeof(Int16));
00452 
00453     value = osgntohs(value);
00454 }
00455 
00456 inline 
00457 void BinaryDataHandler::getValue(Int32 &value)
00458 {
00459     get(&value, sizeof(Int32));
00460 
00461     value = osgntohl(value);
00462 }
00463 
00464 inline 
00465 void BinaryDataHandler::getValue(Int64 &value)
00466 {
00467     get(&value, sizeof(Int64));
00468 
00469     value = osgntohll(value);
00470 }
00471 
00472 inline 
00473 void BinaryDataHandler::getValue(Real16 &value)
00474 {
00475     UInt16 v;
00476 
00477     get(&v, sizeof(Real16));
00478 
00479     v     = osgntohs(v);
00480     value.setBits(v);
00481 }
00482 
00483 inline 
00484 void BinaryDataHandler::getValue(Real32 &value)
00485 {
00486     get(&value, sizeof(Real32));
00487 
00488     value = osgntohf(value);
00489 
00490 #if 0
00491     // doesn't work on my 64 bit linux ...
00492     UInt32 v;
00493 
00494     get(&v, sizeof(Real32));
00495 
00496     v     = osgntohl(v);
00497     value = *(reinterpret_cast<Real32 *>(&v));
00498 #endif
00499 }
00500 
00501 inline 
00502 void BinaryDataHandler::getValue(Real64 &value)
00503 {
00504     get(&value, sizeof(Real64));
00505 
00506     value = osgntohd(value);
00507 
00508 #if 0
00509     UInt64 v;
00510 
00511     get(&v, sizeof(Real64));
00512 
00513     v     = osgntohll(v);
00514     value = *(reinterpret_cast<Real64 *>(&v));
00515 #endif
00516 }
00517 
00518 inline 
00519 void BinaryDataHandler::getValue(Real128 &value)
00520 {
00521     get(&value, sizeof(Real128));
00522 
00523     value = osgntohdd(value);
00524 
00525 #if 0
00526     UInt64 v[2];
00527 
00528 #if BYTE_ORDER == LITTLE_ENDIAN
00529     get(&v[1], sizeof(UInt64));
00530     get(&v[0], sizeof(UInt64));
00531 #else
00532     get(&v[0], sizeof(UInt64));
00533     get(&v[1], sizeof(UInt64));
00534 #endif
00535 
00536     v[0]     = osgntohll(v[0]);
00537     v[1]     = osgntohll(v[1]);
00538     value = *(reinterpret_cast<Real128 *>(&v));
00539 #endif
00540 }
00541 
00542 inline 
00543 void BinaryDataHandler::getValue(std::string &value)
00544 {
00545     UInt32  len;
00546     Char8  *str = NULL;
00547 
00548     getValue(len);
00549 
00550     if(len != 0)
00551     {
00552         str = new Char8[len];
00553         
00554         get(str, len);
00555 
00556         value = str;
00557         
00558         delete [] str;
00559     }
00560     else
00561     {
00562         value.erase();
00563     }
00564 }
00565 
00566 inline 
00567 void BinaryDataHandler::getValues(bool *value, UInt32 size)
00568 {
00569     get(value, size * sizeof(bool));
00570 }
00571 
00572 inline 
00573 void BinaryDataHandler::getValues(UInt8 *value, UInt32 size)
00574 {
00575     get(value, size * sizeof(UInt8));
00576 }
00577 
00578 inline 
00579 void BinaryDataHandler::getValues(UInt16 *value, UInt32 size)
00580 {
00581     get(value, size * sizeof(UInt16));
00582 
00583 #if BYTE_ORDER == LITTLE_ENDIAN
00584     if(_networkOrder == true)
00585     {
00586         for(UInt32 i = 0; i < size; ++i)
00587         {
00588             value[i] = osgntohs(value[i]);
00589         }
00590     }
00591 #endif
00592 }
00593 
00594 inline 
00595 void BinaryDataHandler::getValues(UInt32 *value, UInt32 size)
00596 {
00597     get(value, size * sizeof(UInt32));
00598 
00599 #if BYTE_ORDER == LITTLE_ENDIAN
00600     if(_networkOrder == true)
00601     {
00602         for(UInt32 i = 0; i < size; ++i)
00603         {
00604             value[i] = osgntohl(value[i]);
00605         }
00606     }
00607 #endif
00608 }
00609 
00610 inline 
00611 void BinaryDataHandler::getValues(UInt64 *value, UInt32 size)
00612 {
00613     get(value, size * sizeof(UInt64));
00614 
00615 #if BYTE_ORDER == LITTLE_ENDIAN
00616     if(_networkOrder == true)
00617     {
00618         for(UInt32 i = 0; i < size; ++i)
00619         {
00620             value[i] = osgntohll(value[i]);
00621         }
00622     }
00623 #endif
00624 }
00625 
00626 inline 
00627 void BinaryDataHandler::getValues(Int8 *value, UInt32 size)
00628 {
00629     get(value, size * sizeof(Int8));
00630 }
00631 
00632 inline 
00633 void BinaryDataHandler::getValues(Int16 *value, UInt32 size)
00634 {
00635     get(value, size * sizeof(Int16));
00636 
00637 #if BYTE_ORDER == LITTLE_ENDIAN
00638     if(_networkOrder == true)
00639     {
00640         for(UInt32 i = 0; i < size; ++i)
00641         {
00642             value[i] = osgntohs(value[i]);
00643         }
00644     }
00645 #endif
00646 }
00647 
00648 inline 
00649 void BinaryDataHandler::getValues(Int32 *value, UInt32 size)
00650 {
00651     get(value, size * sizeof(Int32));
00652 
00653 #if BYTE_ORDER == LITTLE_ENDIAN
00654     if(_networkOrder == true)
00655     {
00656         for(UInt32 i = 0; i < size; ++i)
00657         {
00658             value[i] = osgntohl(value[i]);
00659         }
00660     }
00661 #endif
00662 }
00663 
00664 inline 
00665 void BinaryDataHandler::getValues(Int64 *value, UInt32 size)
00666 {
00667     get(value, size * sizeof(Int64));
00668 
00669 #if BYTE_ORDER == LITTLE_ENDIAN
00670     if(_networkOrder == true)
00671     {
00672         for(UInt32 i = 0; i < size; ++i)
00673         {
00674             value[i] = osgntohll(value[i]);
00675         }
00676     }
00677 #endif
00678 }
00679 
00680 inline 
00681 void BinaryDataHandler::getValues(Real16 *value, UInt32 size)
00682 {
00683     get(value, size * sizeof(Real16));
00684 
00685 #if BYTE_ORDER == LITTLE_ENDIAN
00686     if(_networkOrder == true)
00687     {
00688         UInt16 *intValue = reinterpret_cast<UInt16 *>(value);
00689 
00690         for(UInt32 i = 0; i < size; ++i)
00691         {
00692             value[i].setBits(osgntohs(intValue[i]));
00693         }
00694     }
00695 #endif
00696 }
00697 
00698 inline 
00699 void BinaryDataHandler::getValues(Real32 *value, UInt32 size)
00700 {
00701     get(value, size * sizeof(Real32));
00702 
00703 #if BYTE_ORDER == LITTLE_ENDIAN
00704     if(_networkOrder == true)
00705     {
00706         UInt32 *intValue = reinterpret_cast<UInt32 *>(value);
00707 
00708         for(UInt32 i = 0; i < size; ++i)
00709         {
00710             intValue[i] = osgntohl(intValue[i]);
00711         }
00712     }
00713 #endif
00714 }
00715 
00716 inline 
00717 void BinaryDataHandler::getValues(Real64 *value, UInt32 size)
00718 {
00719     get(value, size * sizeof(Real64));
00720 
00721 #if BYTE_ORDER == LITTLE_ENDIAN
00722     if(_networkOrder == true)
00723     {
00724         UInt64 *longValue = reinterpret_cast<UInt64 *>(value);
00725 
00726         for(UInt32 i = 0; i < size; ++i)
00727         {
00728             longValue[i] = osgntohll(longValue[i]);
00729         }
00730     }
00731 #endif
00732 }
00733 
00734 inline 
00735 void BinaryDataHandler::getValues(Real128 *value, UInt32 size)
00736 {
00737     get(value, size * sizeof(UInt64) * 2);
00738 
00739 #if BYTE_ORDER == LITTLE_ENDIAN
00740     if(_networkOrder == true)
00741     {
00742         UInt64 *longValue = reinterpret_cast<UInt64 *>(value);
00743 
00744         for(UInt32 i = 0; i < size; i += 2)
00745         {
00746             UInt64 l = longValue[i];
00747             longValue[i]     = osgntohll(longValue[i + 1]);
00748             longValue[i + 1] = osgntohll(longValue[l    ]);
00749         }
00750     }
00751 #endif
00752 }
00753 
00754 inline 
00755 void BinaryDataHandler::getValues(std::string *value, UInt32 size)
00756 {
00757     for(UInt32 i = 0; i < size; ++i)
00758     {
00759         getValue(value[i]);
00760     }
00761 }
00762 
00763 inline
00764 BinaryDataHandler::MemoryBlock::MemoryBlock(MemoryHandle m,
00765                                             UInt32       s,
00766                                             UInt32       ds) : 
00767     _mem     (m ),
00768     _size    (s ),
00769     _dataSize(ds)
00770 {
00771 }
00772 
00773 inline
00774 MemoryHandle BinaryDataHandler::MemoryBlock::getMem(void)
00775 {
00776     return _mem;
00777 }
00778 
00779 inline
00780 void BinaryDataHandler::MemoryBlock::setMem(MemoryHandle mem)
00781 {
00782     _mem = mem;
00783 }
00784 
00785 inline
00786 UInt32 BinaryDataHandler::MemoryBlock::getSize(void)
00787 {
00788     return _size;
00789 }
00790 
00791 inline
00792 void BinaryDataHandler::MemoryBlock::setSize(UInt32 size)
00793 {
00794     _size = size;
00795 }
00796 
00797 inline
00798 UInt32 BinaryDataHandler::MemoryBlock::getDataSize(void)
00799 {
00800     return _dataSize;
00801 }
00802 
00803 inline
00804 void BinaryDataHandler::MemoryBlock::setDataSize(UInt32 dataSize)
00805 {
00806     _dataSize = dataSize;
00807 }
00808 
00809 inline
00810 BinaryDataHandler::BuffersT::iterator BinaryDataHandler::readBufBegin(void)
00811 {
00812     return _readBuffers.begin();
00813 }
00814 
00815 inline
00816 BinaryDataHandler::BuffersT::iterator BinaryDataHandler::readBufEnd(void)
00817 {
00818     return _readBuffers.end();
00819 }
00820 
00821 inline
00822 BinaryDataHandler::BuffersT::iterator BinaryDataHandler::writeBufBegin(void)
00823 {
00824     return _writeBuffers.begin();
00825 }
00826 
00827 inline
00828 BinaryDataHandler::BuffersT::iterator BinaryDataHandler::writeBufEnd(void)
00829 {
00830     return _writeBuffers.end();
00831 }
00832 
00833 OSG_END_NAMESPACE
00834 
00835 #define OSGBINARYDATAHANDLER_INLINE_CVSID "@(#)$Id: $"
00836 

Generated on Thu Aug 25 04:01:18 2005 for OpenSG by  doxygen 1.4.3