00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
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
00063
00064
00065
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 for(UInt32 i = 0; i < size; ++i)
00187 putValue(value[i]);
00188 }
00189
00190 inline
00191 void BinaryDataHandler::putValues(const UInt8 *value, UInt32 size)
00192 {
00193 put(value, size * sizeof(UInt8));
00194 }
00195
00196 inline
00197 void BinaryDataHandler::putValues(const UInt16 *value, UInt32 size)
00198 {
00199 #if BYTE_ORDER == LITTLE_ENDIAN
00200
00201 if(_networkOrder == true)
00202 {
00203 for(UInt32 i = 0; i < size; ++i)
00204 {
00205 putValue(value[i]);
00206 }
00207 }
00208 else
00209 #endif
00210 {
00211 put(value, size * sizeof(UInt16));
00212 }
00213 }
00214
00215 inline
00216 void BinaryDataHandler::putValues(const UInt32 *value, UInt32 size)
00217 {
00218 #if BYTE_ORDER == LITTLE_ENDIAN
00219 if(_networkOrder == true)
00220 {
00221 for(UInt32 i = 0; i < size; ++i)
00222 {
00223 putValue(value[i]);
00224 }
00225 }
00226 else
00227 #endif
00228 {
00229 put(value, size * sizeof(UInt32));
00230 }
00231 }
00232
00233 inline
00234 void BinaryDataHandler::putValues(const UInt64 *value, UInt32 size)
00235 {
00236 #if BYTE_ORDER == LITTLE_ENDIAN
00237 if(_networkOrder == true)
00238 {
00239 for(UInt32 i = 0; i < size; ++i)
00240 {
00241 putValue(value[i]);
00242 }
00243 }
00244 else
00245 #endif
00246 {
00247 put(value, size * sizeof(UInt64));
00248 }
00249 }
00250
00251 inline
00252 void BinaryDataHandler::putValues(const Int8 *value, UInt32 size)
00253 {
00254 #if BYTE_ORDER == LITTLE_ENDIAN
00255 if(_networkOrder == true)
00256 {
00257 for(UInt32 i = 0; i < size; ++i)
00258 {
00259 putValue(value[i]);
00260 }
00261 }
00262 else
00263 #endif
00264 {
00265 put(value, size * sizeof(Int8));
00266 }
00267 }
00268
00269 inline
00270 void BinaryDataHandler::putValues(const Int16 *value, UInt32 size)
00271 {
00272 #if BYTE_ORDER == LITTLE_ENDIAN
00273 if(_networkOrder == true)
00274 {
00275 for(UInt32 i = 0; i < size; ++i)
00276 {
00277 putValue(value[i]);
00278 }
00279 }
00280 else
00281 #endif
00282 {
00283 put(value, size * sizeof(Int16));
00284 }
00285 }
00286
00287 inline
00288 void BinaryDataHandler::putValues(const Int32 *value, UInt32 size)
00289 {
00290 #if BYTE_ORDER == LITTLE_ENDIAN
00291 if(_networkOrder == true)
00292 {
00293 for(UInt32 i = 0; i < size; ++i)
00294 {
00295 putValue(value[i]);
00296 }
00297 }
00298 else
00299 #endif
00300 {
00301 put(value, size * sizeof(Int32));
00302 }
00303 }
00304
00305 inline
00306 void BinaryDataHandler::putValues(const Int64 *value, UInt32 size)
00307 {
00308 #if BYTE_ORDER == LITTLE_ENDIAN
00309 if(_networkOrder == true)
00310 {
00311 for(UInt32 i = 0; i < size; ++i)
00312 {
00313 putValue(value[i]);
00314 }
00315 }
00316 else
00317 #endif
00318 {
00319 put(value, size * sizeof(Int64));
00320 }
00321 }
00322
00323 inline
00324 void BinaryDataHandler::putValues(const Real16 *value, UInt32 size)
00325 {
00326 #if BYTE_ORDER == LITTLE_ENDIAN
00327 if(_networkOrder == true)
00328 {
00329 for(UInt32 i = 0; i < size; ++i)
00330 {
00331 putValue(value[i]);
00332 }
00333 }
00334 else
00335 #endif
00336 {
00337 put(value, size * sizeof(Real16));
00338 }
00339 }
00340
00341 inline
00342 void BinaryDataHandler::putValues(const Real32 *value, UInt32 size)
00343 {
00344 #if BYTE_ORDER == LITTLE_ENDIAN
00345 if(_networkOrder == true)
00346 {
00347 for(UInt32 i = 0; i < size; ++i)
00348 {
00349 putValue(value[i]);
00350 }
00351 }
00352 else
00353 #endif
00354 {
00355 put(value, size * sizeof(Real32));
00356 }
00357 }
00358
00359 inline
00360 void BinaryDataHandler::putValues(const Real64 *value, UInt32 size)
00361 {
00362 #if BYTE_ORDER == LITTLE_ENDIAN
00363 if(_networkOrder == true)
00364 {
00365 for(UInt32 i = 0; i < size; ++i)
00366 {
00367 putValue(value[i]);
00368 }
00369 }
00370 else
00371 #endif
00372 {
00373 put(value, size * sizeof(Real64));
00374 }
00375 }
00376
00377 inline
00378 void BinaryDataHandler::putValues(const Real128 *value, UInt32 size)
00379 {
00380 #if BYTE_ORDER == LITTLE_ENDIAN
00381 if(_networkOrder == true)
00382 {
00383 for(UInt32 i = 0; i < size; ++i)
00384 {
00385 putValue(value[i]);
00386 }
00387 }
00388 else
00389 #endif
00390 {
00391 put(value, size * sizeof(Real128));
00392 }
00393 }
00394
00395 inline
00396 void BinaryDataHandler::putValues(const std::string *value, UInt32 size)
00397 {
00398 for(UInt32 i = 0; i<size; ++i)
00399 {
00400 putValue(value[i]);
00401 }
00402 }
00403
00404 inline
00405 void BinaryDataHandler::getValue(bool &value)
00406 {
00407
00408 UInt8 temp;
00409 get(&temp, sizeof(UInt8));
00410 value = (temp!=0);
00411 }
00412
00413 inline
00414 void BinaryDataHandler::getValue(UInt8 &value)
00415 {
00416 get(&value, sizeof(UInt8));
00417 }
00418
00419 inline
00420 void BinaryDataHandler::getValue(UInt16 &value)
00421 {
00422 get(&value, sizeof(UInt16));
00423
00424 value = osgntohs(value);
00425 }
00426
00427 inline
00428 void BinaryDataHandler::getValue(UInt32 &value)
00429 {
00430 get(&value, sizeof(UInt32));
00431
00432 value = osgntohl(value);
00433 }
00434
00435 inline
00436 void BinaryDataHandler::getValue(UInt64 &value)
00437 {
00438 get(&value, sizeof(UInt64));
00439
00440 value = osgntohll(value);
00441 }
00442
00443 inline
00444 void BinaryDataHandler::getValue(Int8 &value)
00445 {
00446 get(&value, sizeof(Int8));
00447 }
00448
00449 inline
00450 void BinaryDataHandler::getValue(Int16 &value)
00451 {
00452 get(&value, sizeof(Int16));
00453
00454 value = osgntohs(value);
00455 }
00456
00457 inline
00458 void BinaryDataHandler::getValue(Int32 &value)
00459 {
00460 get(&value, sizeof(Int32));
00461
00462 value = osgntohl(value);
00463 }
00464
00465 inline
00466 void BinaryDataHandler::getValue(Int64 &value)
00467 {
00468 get(&value, sizeof(Int64));
00469
00470 value = osgntohll(value);
00471 }
00472
00473 inline
00474 void BinaryDataHandler::getValue(Real16 &value)
00475 {
00476 UInt16 v;
00477
00478 get(&v, sizeof(Real16));
00479
00480 v = osgntohs(v);
00481 value.setBits(v);
00482 }
00483
00484 inline
00485 void BinaryDataHandler::getValue(Real32 &value)
00486 {
00487 get(&value, sizeof(Real32));
00488
00489 value = osgntohf(value);
00490
00491 #if 0
00492
00493 UInt32 v;
00494
00495 get(&v, sizeof(Real32));
00496
00497 v = osgntohl(v);
00498 value = *(reinterpret_cast<Real32 *>(&v));
00499 #endif
00500 }
00501
00502 inline
00503 void BinaryDataHandler::getValue(Real64 &value)
00504 {
00505 get(&value, sizeof(Real64));
00506
00507 value = osgntohd(value);
00508
00509 #if 0
00510 UInt64 v;
00511
00512 get(&v, sizeof(Real64));
00513
00514 v = osgntohll(v);
00515 value = *(reinterpret_cast<Real64 *>(&v));
00516 #endif
00517 }
00518
00519 inline
00520 void BinaryDataHandler::getValue(Real128 &value)
00521 {
00522 get(&value, sizeof(Real128));
00523
00524 value = osgntohdd(value);
00525
00526 #if 0
00527 UInt64 v[2];
00528
00529 #if BYTE_ORDER == LITTLE_ENDIAN
00530 get(&v[1], sizeof(UInt64));
00531 get(&v[0], sizeof(UInt64));
00532 #else
00533 get(&v[0], sizeof(UInt64));
00534 get(&v[1], sizeof(UInt64));
00535 #endif
00536
00537 v[0] = osgntohll(v[0]);
00538 v[1] = osgntohll(v[1]);
00539 value = *(reinterpret_cast<Real128 *>(&v));
00540 #endif
00541 }
00542
00543 inline
00544 void BinaryDataHandler::getValue(std::string &value)
00545 {
00546 UInt32 len;
00547 Char8 *str = NULL;
00548
00549 getValue(len);
00550
00551 if(len != 0)
00552 {
00553 str = new Char8[len];
00554
00555 get(str, len);
00556
00557 value = str;
00558
00559 delete [] str;
00560 }
00561 else
00562 {
00563 value.erase();
00564 }
00565 }
00566
00567 inline
00568 void BinaryDataHandler::getValues(bool *value, UInt32 size)
00569 {
00570 for(UInt32 i = 0; i < size; ++i)
00571 getValue(value[i]);
00572 }
00573
00574 inline
00575 void BinaryDataHandler::getValues(UInt8 *value, UInt32 size)
00576 {
00577 get(value, size * sizeof(UInt8));
00578 }
00579
00580 inline
00581 void BinaryDataHandler::getValues(UInt16 *value, UInt32 size)
00582 {
00583 get(value, size * sizeof(UInt16));
00584
00585 #if BYTE_ORDER == LITTLE_ENDIAN
00586 if(_networkOrder == true)
00587 {
00588 for(UInt32 i = 0; i < size; ++i)
00589 {
00590 value[i] = osgntohs(value[i]);
00591 }
00592 }
00593 #endif
00594 }
00595
00596 inline
00597 void BinaryDataHandler::getValues(UInt32 *value, UInt32 size)
00598 {
00599 get(value, size * sizeof(UInt32));
00600
00601 #if BYTE_ORDER == LITTLE_ENDIAN
00602 if(_networkOrder == true)
00603 {
00604 for(UInt32 i = 0; i < size; ++i)
00605 {
00606 value[i] = osgntohl(value[i]);
00607 }
00608 }
00609 #endif
00610 }
00611
00612 inline
00613 void BinaryDataHandler::getValues(UInt64 *value, UInt32 size)
00614 {
00615 get(value, size * sizeof(UInt64));
00616
00617 #if BYTE_ORDER == LITTLE_ENDIAN
00618 if(_networkOrder == true)
00619 {
00620 for(UInt32 i = 0; i < size; ++i)
00621 {
00622 value[i] = osgntohll(value[i]);
00623 }
00624 }
00625 #endif
00626 }
00627
00628 inline
00629 void BinaryDataHandler::getValues(Int8 *value, UInt32 size)
00630 {
00631 get(value, size * sizeof(Int8));
00632 }
00633
00634 inline
00635 void BinaryDataHandler::getValues(Int16 *value, UInt32 size)
00636 {
00637 get(value, size * sizeof(Int16));
00638
00639 #if BYTE_ORDER == LITTLE_ENDIAN
00640 if(_networkOrder == true)
00641 {
00642 for(UInt32 i = 0; i < size; ++i)
00643 {
00644 value[i] = osgntohs(value[i]);
00645 }
00646 }
00647 #endif
00648 }
00649
00650 inline
00651 void BinaryDataHandler::getValues(Int32 *value, UInt32 size)
00652 {
00653 get(value, size * sizeof(Int32));
00654
00655 #if BYTE_ORDER == LITTLE_ENDIAN
00656 if(_networkOrder == true)
00657 {
00658 for(UInt32 i = 0; i < size; ++i)
00659 {
00660 value[i] = osgntohl(value[i]);
00661 }
00662 }
00663 #endif
00664 }
00665
00666 inline
00667 void BinaryDataHandler::getValues(Int64 *value, UInt32 size)
00668 {
00669 get(value, size * sizeof(Int64));
00670
00671 #if BYTE_ORDER == LITTLE_ENDIAN
00672 if(_networkOrder == true)
00673 {
00674 for(UInt32 i = 0; i < size; ++i)
00675 {
00676 value[i] = osgntohll(value[i]);
00677 }
00678 }
00679 #endif
00680 }
00681
00682 inline
00683 void BinaryDataHandler::getValues(Real16 *value, UInt32 size)
00684 {
00685 get(value, size * sizeof(Real16));
00686
00687 #if BYTE_ORDER == LITTLE_ENDIAN
00688 if(_networkOrder == true)
00689 {
00690 UInt16 *intValue = reinterpret_cast<UInt16 *>(value);
00691
00692 for(UInt32 i = 0; i < size; ++i)
00693 {
00694 value[i].setBits(osgntohs(intValue[i]));
00695 }
00696 }
00697 #endif
00698 }
00699
00700 inline
00701 void BinaryDataHandler::getValues(Real32 *value, UInt32 size)
00702 {
00703 get(value, size * sizeof(Real32));
00704
00705 #if BYTE_ORDER == LITTLE_ENDIAN
00706 if(_networkOrder == true)
00707 {
00708 UInt32 *intValue = reinterpret_cast<UInt32 *>(value);
00709
00710 for(UInt32 i = 0; i < size; ++i)
00711 {
00712 intValue[i] = osgntohl(intValue[i]);
00713 }
00714 }
00715 #endif
00716 }
00717
00718 inline
00719 void BinaryDataHandler::getValues(Real64 *value, UInt32 size)
00720 {
00721 get(value, size * sizeof(Real64));
00722
00723 #if BYTE_ORDER == LITTLE_ENDIAN
00724 if(_networkOrder == true)
00725 {
00726 UInt64 *longValue = reinterpret_cast<UInt64 *>(value);
00727
00728 for(UInt32 i = 0; i < size; ++i)
00729 {
00730 longValue[i] = osgntohll(longValue[i]);
00731 }
00732 }
00733 #endif
00734 }
00735
00736 inline
00737 void BinaryDataHandler::getValues(Real128 *value, UInt32 size)
00738 {
00739 get(value, size * sizeof(UInt64) * 2);
00740
00741 #if BYTE_ORDER == LITTLE_ENDIAN
00742 if(_networkOrder == true)
00743 {
00744 UInt64 *longValue = reinterpret_cast<UInt64 *>(value);
00745
00746 for(UInt32 i = 0; i < size; i += 2)
00747 {
00748 UInt64 l = longValue[i];
00749 longValue[i] = osgntohll(longValue[i + 1]);
00750 longValue[i + 1] = osgntohll(longValue[l ]);
00751 }
00752 }
00753 #endif
00754 }
00755
00756 inline
00757 void BinaryDataHandler::getValues(std::string *value, UInt32 size)
00758 {
00759 for(UInt32 i = 0; i < size; ++i)
00760 {
00761 getValue(value[i]);
00762 }
00763 }
00764
00765 inline
00766 BinaryDataHandler::MemoryBlock::MemoryBlock(MemoryHandle m,
00767 UInt32 s,
00768 UInt32 ds) :
00769 _mem (m ),
00770 _size (s ),
00771 _dataSize(ds)
00772 {
00773 }
00774
00775 inline
00776 MemoryHandle BinaryDataHandler::MemoryBlock::getMem(void)
00777 {
00778 return _mem;
00779 }
00780
00781 inline
00782 void BinaryDataHandler::MemoryBlock::setMem(MemoryHandle mem)
00783 {
00784 _mem = mem;
00785 }
00786
00787 inline
00788 UInt32 BinaryDataHandler::MemoryBlock::getSize(void)
00789 {
00790 return _size;
00791 }
00792
00793 inline
00794 void BinaryDataHandler::MemoryBlock::setSize(UInt32 size)
00795 {
00796 _size = size;
00797 }
00798
00799 inline
00800 UInt32 BinaryDataHandler::MemoryBlock::getDataSize(void)
00801 {
00802 return _dataSize;
00803 }
00804
00805 inline
00806 void BinaryDataHandler::MemoryBlock::setDataSize(UInt32 dataSize)
00807 {
00808 _dataSize = dataSize;
00809 }
00810
00811 inline
00812 BinaryDataHandler::BuffersT::iterator BinaryDataHandler::readBufBegin(void)
00813 {
00814 return _readBuffers.begin();
00815 }
00816
00817 inline
00818 BinaryDataHandler::BuffersT::iterator BinaryDataHandler::readBufEnd(void)
00819 {
00820 return _readBuffers.end();
00821 }
00822
00823 inline
00824 BinaryDataHandler::BuffersT::iterator BinaryDataHandler::writeBufBegin(void)
00825 {
00826 return _writeBuffers.begin();
00827 }
00828
00829 inline
00830 BinaryDataHandler::BuffersT::iterator BinaryDataHandler::writeBufEnd(void)
00831 {
00832 return _writeBuffers.end();
00833 }
00834
00835 OSG_END_NAMESPACE
00836
00837 #define OSGBINARYDATAHANDLER_INLINE_CVSID "@(#)$Id: $"
00838