Serializer.inl

Go to the documentation of this file.
00001 /*
00002  *
00003  *
00004  * Distributed under the OpenDDS License.
00005  * See: http://www.opendds.org/license.html
00006  */
00007 
00008 #include <ace/Message_Block.h>
00009 #include <ace/CDR_Stream.h>
00010 #include "Serializer.h"
00011 
00012 #ifndef OPENDDS_SAFETY_PROFILE
00013 #include <string>
00014 #endif
00015 
00016 OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL
00017 
00018 namespace OpenDDS {
00019 namespace DCPS {
00020 
00021 // NOTE: I use the ternary operators in here for conditionals to help
00022 //       the compiler inline the code -- and it does end up fairly
00023 //       tight...
00024 ACE_INLINE size_t
00025 Serializer::doread(char* dest, size_t size, bool swap, size_t offset)
00026 {
00027   //
00028   // Ensure we work only with buffer data.
00029   //
00030   if (this->current_ == 0) {
00031     this->good_bit_ = false;
00032     return size;
00033   }
00034 
00035   //
00036   // Determine how much data will remain to be read after the current
00037   // buffer has been entirely read.
00038   //
00039   const size_t len = this->current_->length();
00040   const size_t remainder = (size - offset > len) ? size - offset - len : 0;
00041 
00042   //
00043   // Derive how much data we need to read from the current buffer.
00044   //
00045   const size_t initial = size - offset - remainder;
00046 
00047   //
00048   // Copy or swap the source data from the current buffer into the
00049   // destination.
00050   //
00051   swap
00052     ? this->swapcpy(dest + remainder, this->current_->rd_ptr(), initial)
00053     : this->smemcpy(dest + offset, this->current_->rd_ptr(), initial);
00054   this->current_->rd_ptr(initial);
00055 
00056   //   smemcpy
00057   //
00058   //   dest            b1   b2   b3        offset   remainder   initial
00059   //   xxxxxxxx        01   23   4567xx
00060   //                   ^                   0        6           2
00061   //   01xxxxxx        01   23   4567xx
00062   //                        ^              2        4           2
00063   //   0123xxxx        01   23   4567xx
00064   //                             ^         4        0           4
00065   //   01234567        01   23   4567xx
00066   //                                 ^
00067 
00068   //   swapcpy
00069   //
00070   //   dest            b1   b2   b3        offset   remainder   initial
00071   //   xxxxxxxx        01   23   4567xx
00072   //                   ^                   0        6           2
00073   //   xxxxxx10        01   23   4567xx
00074   //                        ^              2        4           2
00075   //   xxxx3210        01   23   4567xx
00076   //                             ^         4        0           4
00077   //   76543210        01   23   4567xx
00078   //                                 ^
00079 
00080   //
00081   // Move to the next chained block if this one is spent.
00082   //
00083   if (this->current_->length() == 0) {
00084 
00085     if (this->alignment_ == ALIGN_NONE) {
00086       this->current_ = this->current_->cont();
00087     } else {
00088       this->align_cont_r();
00089     }
00090   }
00091   //
00092   // Return the current location in the read.
00093   //
00094   return offset + initial;
00095 }
00096 
00097 ACE_INLINE void
00098 Serializer::buffer_read(char* dest, size_t size, bool swap)
00099 {
00100   size_t offset = 0;
00101 
00102   while (size > offset) {
00103     offset = this->doread(dest, size, swap, offset);
00104   }
00105 }
00106 
00107 // NOTE: I use the ternary operators in here for conditionals to help
00108 //       the compiler inline the code -- and it does end up fairly
00109 //       tight...
00110 ACE_INLINE size_t
00111 Serializer::dowrite(const char* src, size_t size, bool swap, size_t offset)
00112 {
00113   //
00114   // Ensure we work only with buffer data.
00115   //
00116   if (this->current_ == 0) {
00117     this->good_bit_ = false;
00118     return size;
00119   }
00120 
00121   //
00122   // Determine how much data will remain to be written after the current
00123   // buffer has been entirely filled.
00124   //
00125   const size_t spc = this->current_->space();
00126   const size_t remainder = (size - offset > spc) ? size - offset - spc : 0;
00127 
00128   //
00129   // Derive how much data we need to write to the current buffer.
00130   //
00131   const size_t initial = size - offset - remainder;
00132 
00133   //
00134   // Copy or swap the source data into the current buffer.
00135   //
00136   swap
00137     ? this->swapcpy(this->current_->wr_ptr(), src + remainder, initial)
00138     : this->smemcpy(this->current_->wr_ptr(), src + offset, initial);
00139   this->current_->wr_ptr(initial);
00140 
00141   //   smemcpy
00142   //
00143   //   src             b1   b2   b3        offset   remainder   initial
00144   //   01234567        xx   xx   xxxxxx
00145   //                   ^                   0        6           2
00146   //                   01   xx   xxxxxx
00147   //                        ^              2        4           2
00148   //                   01   23   xxxxxx
00149   //                             ^         4        0           4
00150   //                   01   23   4567xx
00151   //                                 ^
00152 
00153   //   swapcpy
00154   //
00155   //   src             b1   b2   b3        offset   remainder   initial
00156   //   01234567        xx   xx   xxxxxx
00157   //                   ^                   0        6           2
00158   //                   76   xx   xxxxxx
00159   //                        ^              2        4           2
00160   //                   76   54   xxxxxx
00161   //                             ^         4        0           4
00162   //                   76   54   3210xx
00163   //                                 ^
00164 
00165   //
00166   // Move to the next chained block if this one is spent.
00167   //
00168   if (this->current_->space() == 0) {
00169 
00170     if (this->alignment_ == ALIGN_NONE) {
00171       this->current_ = this->current_->cont();
00172     } else {
00173       this->align_cont_w();
00174     }
00175   }
00176 
00177   //
00178   // Return the current location in the write.
00179   //
00180   return offset + initial;
00181 }
00182 
00183 ACE_INLINE void
00184 Serializer::buffer_write(const char* src, size_t size, bool swap)
00185 {
00186   size_t offset = 0;
00187 
00188   while (size > offset) {
00189     offset = this->dowrite(src, size, swap, offset);
00190   }
00191 }
00192 
00193 ACE_INLINE void
00194 Serializer::swap_bytes(bool do_swap)
00195 {
00196   this->swap_bytes_ = do_swap;
00197 }
00198 
00199 ACE_INLINE bool
00200 Serializer::swap_bytes() const
00201 {
00202   return this->swap_bytes_;
00203 }
00204 
00205 ACE_INLINE Serializer::Alignment
00206 Serializer::alignment() const
00207 {
00208   return this->alignment_;
00209 }
00210 
00211 ACE_INLINE bool
00212 Serializer::good_bit() const
00213 {
00214   return this->good_bit_;
00215 }
00216 
00217 ACE_INLINE size_t
00218 Serializer::length() const
00219 {
00220   return this->good_bit_ && this->current_ ? this->current_->total_length() : 0;
00221 }
00222 
00223 ACE_INLINE bool
00224 Serializer::skip(ACE_CDR::UShort n, int size)
00225 {
00226   if (size > 1 && this->alignment_ != ALIGN_NONE) {
00227     if (!this->current_) {
00228       this->good_bit_ = false;
00229       return false;
00230     }
00231     this->align_r(size_t(size) > MAX_ALIGN ? MAX_ALIGN : size_t(size));
00232   }
00233   for (size_t len = static_cast<size_t>(n * size); len;) {
00234     if (!this->current_) {
00235       this->good_bit_ = false;
00236       return false;
00237     }
00238     const size_t cur_len = this->current_->length();
00239     if (cur_len <= len) {
00240       len -= cur_len;
00241       this->current_->rd_ptr(this->current_->wr_ptr());
00242       this->align_cont_r();
00243     } else {
00244       this->current_->rd_ptr(len);
00245       break;
00246     }
00247   }
00248   return this->good_bit();
00249 }
00250 
00251 ACE_INLINE void
00252 Serializer::read_array(char* x, size_t size, ACE_CDR::ULong length)
00253 {
00254   this->read_array(x, size, length, this->swap_bytes_);
00255 }
00256 
00257 ACE_INLINE void
00258 Serializer::read_array(char* x, size_t size,
00259                        ACE_CDR::ULong length, bool swap)
00260 {
00261   if (!swap || size == 1) {
00262     //
00263     // No swap, copy direct.  This silently corrupts the data if there is
00264     // padding in the buffer.
00265     //
00266     this->buffer_read(x, size * length, false);
00267 
00268   } else {
00269     //
00270     // Swapping _must_ be done at 'size' boundaries, so we need to spin
00271     // through the array element by element.  This silently corrupts the
00272     // data if there is padding in the buffer.
00273     //
00274     while (length-- > 0) {
00275       this->buffer_read(x, size, true);
00276       x += size;
00277     }
00278   }
00279 }
00280 
00281 ACE_INLINE void
00282 Serializer::write_array(const char* x, size_t size, ACE_CDR::ULong length)
00283 {
00284   this->write_array(x, size, length, this->swap_bytes_);
00285 }
00286 
00287 ACE_INLINE void
00288 Serializer::write_array(const char* x, size_t size,
00289                         ACE_CDR::ULong length, bool swap)
00290 {
00291   if (!swap || size == 1) {
00292     //
00293     // No swap, copy direct.
00294     //
00295     this->buffer_write(x, size * length, false);
00296 
00297   } else {
00298     //
00299     // Swapping _must_ be done at 'size' boundaries, so we need to spin
00300     // through the array element by element.
00301     // NOTE: This assumes that there is _no_ padding between the array
00302     //       elements.  If this is not the case, do not use this
00303     //       method.
00304     //
00305     while (length-- > 0) {
00306       this->buffer_write(x, size, true);
00307       x += size;
00308     }
00309   }
00310 }
00311 
00312 ACE_INLINE bool
00313 Serializer::read_boolean_array(ACE_CDR::Boolean* x, ACE_CDR::ULong length)
00314 {
00315   this->read_array(reinterpret_cast<char*>(x), sizeof(ACE_CDR::Boolean), length);
00316   return this->good_bit();
00317 }
00318 
00319 ACE_INLINE bool
00320 Serializer::read_char_array(ACE_CDR::Char* x, ACE_CDR::ULong length)
00321 {
00322   this->read_array(reinterpret_cast<char*>(x), sizeof(ACE_CDR::Char), length);
00323   return this->good_bit();
00324 }
00325 
00326 ACE_INLINE bool
00327 Serializer::read_wchar_array(ACE_CDR::WChar* x, ACE_CDR::ULong length)
00328 {
00329   for (ACE_CDR::ULong i = 0; i < length; ++i) {
00330     if (!((*this) >> ACE_InputCDR::to_wchar(x[i]))) {
00331       break;
00332     }
00333   }
00334   return this->good_bit();
00335 }
00336 
00337 ACE_INLINE bool
00338 Serializer::read_octet_array(ACE_CDR::Octet* x, ACE_CDR::ULong length)
00339 {
00340   this->read_array(reinterpret_cast<char*>(x), sizeof(ACE_CDR::Octet), length);
00341   return this->good_bit();
00342 }
00343 
00344 ACE_INLINE bool
00345 Serializer::read_short_array(ACE_CDR::Short* x, ACE_CDR::ULong length)
00346 {
00347   this->alignment() == Serializer::ALIGN_NONE ? 0 : this->align_r(sizeof(ACE_CDR::Short));
00348   this->read_array(reinterpret_cast<char*>(x), sizeof(ACE_CDR::Short), length);
00349   return this->good_bit();
00350 }
00351 
00352 ACE_INLINE bool
00353 Serializer::read_ushort_array(ACE_CDR::UShort* x, ACE_CDR::ULong length)
00354 {
00355   this->alignment() == Serializer::ALIGN_NONE ? 0 : this->align_r(sizeof(ACE_CDR::UShort));
00356   this->read_array(reinterpret_cast<char*>(x), sizeof(ACE_CDR::UShort), length);
00357   return this->good_bit();
00358 }
00359 
00360 ACE_INLINE bool
00361 Serializer::read_long_array(ACE_CDR::Long* x, ACE_CDR::ULong length)
00362 {
00363   this->alignment() == Serializer::ALIGN_NONE ? 0 : this->align_r(sizeof(ACE_CDR::Long));
00364   this->read_array(reinterpret_cast<char*>(x), sizeof(ACE_CDR::Long), length);
00365   return this->good_bit();
00366 }
00367 
00368 ACE_INLINE bool
00369 Serializer::read_ulong_array(ACE_CDR::ULong* x, ACE_CDR::ULong length)
00370 {
00371   this->alignment() == Serializer::ALIGN_NONE ? 0 : this->align_r(sizeof(ACE_CDR::ULong));
00372   this->read_array(reinterpret_cast<char*>(x), sizeof(ACE_CDR::ULong), length);
00373   return this->good_bit();
00374 }
00375 
00376 ACE_INLINE bool
00377 Serializer::read_longlong_array(ACE_CDR::LongLong* x, ACE_CDR::ULong length)
00378 {
00379   this->alignment() == Serializer::ALIGN_NONE ? 0 : this->align_r(sizeof(ACE_CDR::LongLong));
00380   this->read_array(reinterpret_cast<char*>(x), sizeof(ACE_CDR::LongLong), length);
00381   return this->good_bit();
00382 }
00383 
00384 ACE_INLINE bool
00385 Serializer::read_ulonglong_array(ACE_CDR::ULongLong* x, ACE_CDR::ULong length)
00386 {
00387   this->alignment() == Serializer::ALIGN_NONE ? 0 : this->align_r(sizeof(ACE_CDR::ULongLong));
00388   this->read_array(reinterpret_cast<char*>(x), sizeof(ACE_CDR::ULongLong), length);
00389   return this->good_bit();
00390 }
00391 
00392 ACE_INLINE bool
00393 Serializer::read_float_array(ACE_CDR::Float* x, ACE_CDR::ULong length)
00394 {
00395   this->alignment() == Serializer::ALIGN_NONE ? 0 : this->align_r(sizeof(ACE_CDR::Float));
00396   this->read_array(reinterpret_cast<char*>(x), sizeof(ACE_CDR::Float), length);
00397   return this->good_bit();
00398 }
00399 
00400 ACE_INLINE bool
00401 Serializer::read_double_array(ACE_CDR::Double* x, ACE_CDR::ULong length)
00402 {
00403   this->alignment() == Serializer::ALIGN_NONE ? 0 : this->align_r(sizeof(ACE_CDR::Double));
00404   this->read_array(reinterpret_cast<char*>(x), sizeof(ACE_CDR::Double), length);
00405   return this->good_bit();
00406 }
00407 
00408 ACE_INLINE bool
00409 Serializer::read_longdouble_array(ACE_CDR::LongDouble* x, ACE_CDR::ULong length)
00410 {
00411   this->alignment() == Serializer::ALIGN_NONE ? 0 : this->align_r(8);
00412   this->read_array(reinterpret_cast<char*>(x), sizeof(ACE_CDR::LongDouble), length);
00413   return this->good_bit();
00414 }
00415 
00416 ACE_INLINE bool
00417 Serializer::write_boolean_array(const ACE_CDR::Boolean* x,
00418                                 ACE_CDR::ULong length)
00419 {
00420   this->write_array(reinterpret_cast<const char*>(x), sizeof(ACE_CDR::Boolean), length);
00421   return this->good_bit();
00422 }
00423 
00424 ACE_INLINE bool
00425 Serializer::write_char_array(const ACE_CDR::Char* x, ACE_CDR::ULong length)
00426 {
00427   this->write_array(reinterpret_cast<const char*>(x), sizeof(ACE_CDR::Char), length);
00428   return this->good_bit();
00429 }
00430 
00431 ACE_INLINE bool
00432 Serializer::write_wchar_array(const ACE_CDR::WChar* x, ACE_CDR::ULong length)
00433 {
00434   for (ACE_CDR::ULong i = 0; i < length; ++i) {
00435     if (!((*this) << ACE_OutputCDR::from_wchar(x[i]))) {
00436       break;
00437     }
00438   }
00439   return this->good_bit();
00440 }
00441 
00442 ACE_INLINE bool
00443 Serializer::write_octet_array(const ACE_CDR::Octet* x, ACE_CDR::ULong length)
00444 {
00445   this->write_array(reinterpret_cast<const char*>(x), sizeof(ACE_CDR::Octet), length);
00446   return this->good_bit();
00447 }
00448 
00449 ACE_INLINE bool
00450 Serializer::write_short_array(const ACE_CDR::Short* x, ACE_CDR::ULong length)
00451 {
00452   this->alignment() == Serializer::ALIGN_NONE ? 0 : this->align_w(sizeof(ACE_CDR::Short));
00453   this->write_array(reinterpret_cast<const char*>(x), sizeof(ACE_CDR::Short), length);
00454   return this->good_bit();
00455 }
00456 
00457 ACE_INLINE bool
00458 Serializer::write_ushort_array(const ACE_CDR::UShort* x, ACE_CDR::ULong length)
00459 {
00460   this->alignment() == Serializer::ALIGN_NONE ? 0 : this->align_w(sizeof(ACE_CDR::UShort));
00461   this->write_array(reinterpret_cast<const char*>(x), sizeof(ACE_CDR::UShort), length);
00462   return this->good_bit();
00463 }
00464 
00465 ACE_INLINE bool
00466 Serializer::write_long_array(const ACE_CDR::Long* x, ACE_CDR::ULong length)
00467 {
00468   this->alignment() == Serializer::ALIGN_NONE ? 0 : this->align_w(sizeof(ACE_CDR::Long));
00469   this->write_array(reinterpret_cast<const char*>(x), sizeof(ACE_CDR::Long), length);
00470   return this->good_bit();
00471 }
00472 
00473 ACE_INLINE bool
00474 Serializer::write_ulong_array(const ACE_CDR::ULong* x, ACE_CDR::ULong length)
00475 {
00476   this->alignment() == Serializer::ALIGN_NONE ? 0 : this->align_w(sizeof(ACE_CDR::ULong));
00477   this->write_array(reinterpret_cast<const char*>(x), sizeof(ACE_CDR::ULong), length);
00478   return this->good_bit();
00479 }
00480 
00481 ACE_INLINE bool
00482 Serializer::write_longlong_array(const ACE_CDR::LongLong* x,
00483                                  ACE_CDR::ULong length)
00484 {
00485   this->alignment() == Serializer::ALIGN_NONE ? 0 : this->align_w(sizeof(ACE_CDR::LongLong));
00486   this->write_array(reinterpret_cast<const char*>(x), sizeof(ACE_CDR::LongLong), length);
00487   return this->good_bit();
00488 }
00489 
00490 ACE_INLINE bool
00491 Serializer::write_ulonglong_array(const ACE_CDR::ULongLong* x,
00492                                   ACE_CDR::ULong length)
00493 {
00494   this->alignment() == Serializer::ALIGN_NONE ? 0 : this->align_w(sizeof(ACE_CDR::ULongLong));
00495   this->write_array(reinterpret_cast<const char*>(x), sizeof(ACE_CDR::ULongLong), length);
00496   return this->good_bit();
00497 }
00498 
00499 ACE_INLINE bool
00500 Serializer::write_float_array(const ACE_CDR::Float* x, ACE_CDR::ULong length)
00501 {
00502   this->alignment() == Serializer::ALIGN_NONE ? 0 : this->align_w(sizeof(ACE_CDR::Float));
00503   this->write_array(reinterpret_cast<const char*>(x), sizeof(ACE_CDR::Float), length);
00504   return this->good_bit();
00505 }
00506 
00507 ACE_INLINE bool
00508 Serializer::write_double_array(const ACE_CDR::Double* x, ACE_CDR::ULong length)
00509 {
00510   this->alignment() == Serializer::ALIGN_NONE ? 0 : this->align_w(sizeof(ACE_CDR::Double));
00511   this->write_array(reinterpret_cast<const char*>(x), sizeof(ACE_CDR::Double), length);
00512   return this->good_bit();
00513 }
00514 
00515 ACE_INLINE bool
00516 Serializer::write_longdouble_array(const ACE_CDR::LongDouble* x,
00517                                    ACE_CDR::ULong length)
00518 {
00519   this->alignment() == Serializer::ALIGN_NONE ? 0 : this->align_w(8);
00520   this->write_array(reinterpret_cast<const char*>(x), sizeof(ACE_CDR::LongDouble), length);
00521   return this->good_bit();
00522 }
00523 
00524 ACE_INLINE int
00525 Serializer::align_r(size_t al)
00526 {
00527   const size_t len =
00528     (al - ptrdiff_t(this->current_->rd_ptr()) + this->align_rshift_) % al;
00529   this->skip(static_cast<ACE_CDR::UShort>(len));
00530   return 0;
00531 }
00532 
00533 ACE_INLINE int
00534 Serializer::align_w(size_t al)
00535 {
00536   size_t len =
00537     (al - ptrdiff_t(this->current_->wr_ptr()) + this->align_wshift_) % al;
00538   while (len) {
00539     if (!this->current_) {
00540       this->good_bit_ = false;
00541       break;
00542     }
00543     const size_t cur_spc = this->current_->space();
00544     if (cur_spc <= len) {
00545       len -= cur_spc;
00546       if (this->alignment_ == ALIGN_INITIALIZE) {
00547         this->smemcpy(this->current_->wr_ptr(), ALIGN_PAD, cur_spc);
00548       }
00549       this->current_->wr_ptr(cur_spc);
00550       this->align_cont_w();
00551     } else {
00552       if (this->alignment_ == ALIGN_INITIALIZE) {
00553         this->smemcpy(this->current_->wr_ptr(), ALIGN_PAD, len);
00554       }
00555       this->current_->wr_ptr(len);
00556       break;
00557     }
00558   }
00559   return 0;
00560 }
00561 
00562 
00563 ACE_INLINE void
00564 Serializer::align_cont_r()
00565 {
00566   const size_t thisblock =
00567     (ptrdiff_t(this->current_->rd_ptr()) - this->align_rshift_) % MAX_ALIGN;
00568 
00569   this->current_ = this->current_->cont();
00570 
00571   if (this->current_) {
00572     this->align_rshift_ =
00573       (ptrdiff_t(this->current_->rd_ptr()) - thisblock) % MAX_ALIGN;
00574   }
00575 }
00576 
00577 ACE_INLINE void
00578 Serializer::align_cont_w()
00579 {
00580   const size_t thisblock =
00581     (ptrdiff_t(this->current_->wr_ptr()) - this->align_wshift_) % MAX_ALIGN;
00582 
00583   this->current_ = this->current_->cont();
00584 
00585   if (this->current_) {
00586     this->align_wshift_ =
00587       (ptrdiff_t(this->current_->wr_ptr()) - thisblock) % MAX_ALIGN;
00588   }
00589 }
00590 
00591 //
00592 // The following insertion operators are done in the style of the
00593 // ACE_CDR insertion operators instead of a stream abstraction.  This
00594 // is done to allow their use in the same way as existing ACE_CDR
00595 // inserters, rather than as a true stream abstraction (which would
00596 // return the argument stream).
00597 //
00598 
00599 ACE_INLINE bool
00600 operator<<(Serializer& s, ACE_CDR::Char x)
00601 {
00602   s.buffer_write(reinterpret_cast<char*>(&x), sizeof(ACE_CDR::Char), s.swap_bytes());
00603   return s.good_bit();
00604 }
00605 
00606 ACE_INLINE bool
00607 operator<<(Serializer& s, ACE_CDR::Short x)
00608 {
00609   s.alignment() == Serializer::ALIGN_NONE ? 0 : s.align_w(sizeof(ACE_CDR::Short));
00610   s.buffer_write(reinterpret_cast<char*>(&x), sizeof(ACE_CDR::Short), s.swap_bytes());
00611   return s.good_bit();
00612 }
00613 
00614 ACE_INLINE bool
00615 operator<<(Serializer& s, ACE_CDR::UShort x)
00616 {
00617   s.alignment() == Serializer::ALIGN_NONE ? 0 : s.align_w(sizeof(ACE_CDR::UShort));
00618   s.buffer_write(reinterpret_cast<char*>(&x), sizeof(ACE_CDR::UShort), s.swap_bytes());
00619   return s.good_bit();
00620 }
00621 
00622 ACE_INLINE bool
00623 operator<<(Serializer& s, ACE_CDR::Long x)
00624 {
00625   s.alignment() == Serializer::ALIGN_NONE ? 0 : s.align_w(sizeof(ACE_CDR::Long));
00626   s.buffer_write(reinterpret_cast<char*>(&x), sizeof(ACE_CDR::Long), s.swap_bytes());
00627   return s.good_bit();
00628 }
00629 
00630 ACE_INLINE bool
00631 operator<<(Serializer& s, ACE_CDR::ULong x)
00632 {
00633   s.alignment() == Serializer::ALIGN_NONE ? 0 : s.align_w(sizeof(ACE_CDR::ULong));
00634   s.buffer_write(reinterpret_cast<char*>(&x), sizeof(ACE_CDR::ULong), s.swap_bytes());
00635   return s.good_bit();
00636 }
00637 
00638 ACE_INLINE bool
00639 operator<<(Serializer& s, ACE_CDR::LongLong x)
00640 {
00641   s.alignment() == Serializer::ALIGN_NONE ? 0 : s.align_w(sizeof(ACE_CDR::LongLong));
00642   s.buffer_write(reinterpret_cast<char*>(&x), sizeof(ACE_CDR::LongLong), s.swap_bytes());
00643   return s.good_bit();
00644 }
00645 
00646 ACE_INLINE bool
00647 operator<<(Serializer& s, ACE_CDR::ULongLong x)
00648 {
00649   s.alignment() == Serializer::ALIGN_NONE ? 0 : s.align_w(sizeof(ACE_CDR::ULongLong));
00650   s.buffer_write(reinterpret_cast<char*>(&x), sizeof(ACE_CDR::ULongLong), s.swap_bytes());
00651   return s.good_bit();
00652 }
00653 
00654 ACE_INLINE bool
00655 operator<<(Serializer& s, ACE_CDR::LongDouble x)
00656 {
00657   s.alignment() == Serializer::ALIGN_NONE ? 0 : s.align_w(8);
00658   s.buffer_write(reinterpret_cast<char*>(&x), sizeof(ACE_CDR::LongDouble), s.swap_bytes());
00659   return s.good_bit();
00660 }
00661 
00662 ACE_INLINE bool
00663 operator<<(Serializer& s, ACE_CDR::Float x)
00664 {
00665   s.alignment() == Serializer::ALIGN_NONE ? 0 : s.align_w(sizeof(ACE_CDR::Float));
00666   s.buffer_write(reinterpret_cast<char*>(&x), sizeof(ACE_CDR::Float), s.swap_bytes());
00667   return s.good_bit();
00668 }
00669 
00670 ACE_INLINE bool
00671 operator<<(Serializer& s, ACE_CDR::Double x)
00672 {
00673   s.alignment() == Serializer::ALIGN_NONE ? 0 : s.align_w(sizeof(ACE_CDR::Double));
00674   s.buffer_write(reinterpret_cast<char*>(&x), sizeof(ACE_CDR::Double), s.swap_bytes());
00675   return s.good_bit();
00676 }
00677 
00678 ACE_INLINE bool
00679 operator<<(Serializer& s, const ACE_CDR::Char* x)
00680 {
00681   if (x != 0) {
00682     // Include the null termination in the serialized data.
00683     const ACE_CDR::ULong stringlen =
00684       1 + static_cast<ACE_CDR::ULong>(ACE_OS::strlen(x));
00685     s << stringlen;
00686     s.buffer_write(reinterpret_cast<const char*>(x), stringlen, false);
00687 
00688   } else {
00689     s << ACE_CDR::ULong(0);
00690   }
00691 
00692   return s.good_bit();
00693 }
00694 
00695 ACE_INLINE bool
00696 operator<<(Serializer& s, const ACE_CDR::WChar* x)
00697 {
00698   //NOTE: Serializing wchar/wstring uses UTF-16BE
00699   if (x != 0) {
00700     // Do not include the null terminatator in the serialized data.
00701     const ACE_CDR::ULong length = static_cast<ACE_CDR::ULong>(ACE_OS::strlen(x));
00702     s << ACE_CDR::ULong(length * Serializer::WCHAR_SIZE);
00703 
00704 #if ACE_SIZEOF_WCHAR == 2
00705     s.write_array(reinterpret_cast<const char*>(x), Serializer::WCHAR_SIZE,
00706                   length, Serializer::SWAP_BE);
00707 #else
00708     for (size_t i = 0; i < length && s.good_bit(); ++i) {
00709       const ACE_UINT16 as_utf16 = static_cast<ACE_UINT16>(x[i]);
00710       if (as_utf16 != x[i]) { // not currently handling surrogates
00711         s.good_bit_ = false;
00712         break;
00713       }
00714       s.buffer_write(reinterpret_cast<const char*>(&as_utf16), Serializer::WCHAR_SIZE, Serializer::SWAP_BE);
00715     }
00716 #endif
00717   } else {
00718     s << ACE_CDR::ULong(0);
00719   }
00720 
00721   return s.good_bit();
00722 }
00723 
00724 ACE_INLINE bool
00725 operator<<(Serializer& s, ACE_OutputCDR::from_boolean x)
00726 {
00727   s.buffer_write(reinterpret_cast<char*>(&x.val_), sizeof(ACE_CDR::Boolean), s.swap_bytes());
00728   return s.good_bit();
00729 }
00730 
00731 ACE_INLINE bool
00732 operator<<(Serializer& s, ACE_OutputCDR::from_char x)
00733 {
00734   s.buffer_write(reinterpret_cast<char*>(&x.val_), sizeof(ACE_CDR::Char), false);
00735   return s.good_bit();
00736 }
00737 
00738 ACE_INLINE bool
00739 operator<<(Serializer& s, ACE_OutputCDR::from_wchar x)
00740 {
00741   // CDR wchar format: 1 octet for # of bytes in wchar, followed by those bytes
00742   static const ACE_CDR::Octet wchar_bytes = Serializer::WCHAR_SIZE;
00743   s.buffer_write(reinterpret_cast<const char*>(&wchar_bytes), 1, false);
00744 #if ACE_SIZEOF_WCHAR == 2
00745   s.buffer_write(reinterpret_cast<char*>(&x.val_), Serializer::WCHAR_SIZE, Serializer::SWAP_BE);
00746 #else
00747   const ACE_UINT16 as_utf16 = static_cast<ACE_UINT16>(x.val_);
00748   if (as_utf16 != x.val_) { // not currently handling surrogates
00749     ACE_DEBUG((LM_DEBUG, ACE_TEXT("(%P|%t) operator<<(Serializer&, ACE_OutputCDR::from_wchar): failure to convert UTF-32 to UTF-16.\n")));
00750     s.good_bit_ = false;
00751   } else {
00752     s.buffer_write(reinterpret_cast<const char*>(&as_utf16), Serializer::WCHAR_SIZE, Serializer::SWAP_BE);
00753   }
00754 #endif
00755   return s.good_bit();
00756 }
00757 
00758 #ifndef OPENDDS_SAFETY_PROFILE
00759 ACE_INLINE bool
00760 operator<<(Serializer& s, const std::string& x)
00761 {
00762   return s << x.c_str();
00763 }
00764 
00765 #ifdef DDS_HAS_WCHAR
00766 ACE_INLINE bool
00767 operator<<(Serializer& s, const std::wstring& x)
00768 {
00769   return s << x.c_str();
00770 }
00771 #endif /* DDS_HAS_WCHAR */
00772 #endif /* !OPENDDS_SAFETY_PROFILE */
00773 
00774 ACE_INLINE bool
00775 operator<<(Serializer& s, ACE_OutputCDR::from_octet x)
00776 {
00777   s.buffer_write(reinterpret_cast<char*>(&x.val_), sizeof(ACE_CDR::Octet), false);
00778   return s.good_bit();
00779 }
00780 
00781 ACE_INLINE bool
00782 operator<<(Serializer& s, ACE_OutputCDR::from_string x)
00783 {
00784   // Include the null termination in the serialized data.
00785   ACE_CDR::ULong stringlen = 0;
00786 
00787   if (x.val_ != 0) {
00788     stringlen = 1 + static_cast<ACE_CDR::ULong>(ACE_OS::strlen(x.val_));
00789     s << stringlen;
00790     s.buffer_write(reinterpret_cast<char*>(x.val_), stringlen, false);
00791 
00792   } else {
00793     s << ACE_CDR::ULong(0);
00794   }
00795 
00796   return s.good_bit() && ((x.bound_ == 0) || (stringlen - 1 <= x.bound_));
00797 }
00798 
00799 ACE_INLINE bool
00800 operator<<(Serializer& s, ACE_OutputCDR::from_wstring x)
00801 {
00802   s << x.val_;
00803   const ACE_CDR::ULong stringlen =
00804     x.bound_ ? static_cast<ACE_CDR::ULong>(ACE_OS::strlen(x.val_)) : 0;
00805   return s.good_bit() && ((x.bound_ == 0) || (stringlen <= x.bound_));
00806 }
00807 
00808 //
00809 // The following extraction operators are done in the style of the
00810 // ACE_CDR extraction operators instead of a stream abstraction.  This
00811 // is done to allow their use in the same way as existing ACE_CDR
00812 // extractors, rather than as a true stream abstraction (which would
00813 // return the argument stream).
00814 //
00815 
00816 ACE_INLINE bool
00817 operator>>(Serializer& s, ACE_CDR::Char& x)
00818 {
00819   s.buffer_read(reinterpret_cast<char*>(&x), sizeof(ACE_CDR::Char), s.swap_bytes());
00820   return s.good_bit();
00821 }
00822 
00823 ACE_INLINE bool
00824 operator>>(Serializer& s, ACE_CDR::Short& x)
00825 {
00826   s.alignment() == Serializer::ALIGN_NONE ? 0 : s.align_r(sizeof(ACE_CDR::Short));
00827   s.buffer_read(reinterpret_cast<char*>(&x), sizeof(ACE_CDR::Short), s.swap_bytes());
00828   return s.good_bit();
00829 }
00830 
00831 ACE_INLINE bool
00832 operator>>(Serializer& s, ACE_CDR::UShort& x)
00833 {
00834   s.alignment() == Serializer::ALIGN_NONE ? 0 : s.align_r(sizeof(ACE_CDR::UShort));
00835   s.buffer_read(reinterpret_cast<char*>(&x), sizeof(ACE_CDR::UShort), s.swap_bytes());
00836   return s.good_bit();
00837 }
00838 
00839 ACE_INLINE bool
00840 operator>>(Serializer& s, ACE_CDR::Long& x)
00841 {
00842   s.alignment() == Serializer::ALIGN_NONE ? 0 : s.align_r(sizeof(ACE_CDR::Long));
00843   s.buffer_read(reinterpret_cast<char*>(&x), sizeof(ACE_CDR::Long), s.swap_bytes());
00844   return s.good_bit();
00845 }
00846 
00847 ACE_INLINE bool
00848 operator>>(Serializer& s, ACE_CDR::ULong& x)
00849 {
00850   s.alignment() == Serializer::ALIGN_NONE ? 0 : s.align_r(sizeof(ACE_CDR::ULong));
00851   s.buffer_read(reinterpret_cast<char*>(&x), sizeof(ACE_CDR::ULong), s.swap_bytes());
00852   return s.good_bit();
00853 }
00854 
00855 ACE_INLINE bool
00856 operator>>(Serializer& s, ACE_CDR::LongLong& x)
00857 {
00858   s.alignment() == Serializer::ALIGN_NONE ? 0 : s.align_r(sizeof(ACE_CDR::LongLong));
00859   s.buffer_read(reinterpret_cast<char*>(&x), sizeof(ACE_CDR::LongLong), s.swap_bytes());
00860   return s.good_bit();
00861 }
00862 
00863 ACE_INLINE bool
00864 operator>>(Serializer& s, ACE_CDR::ULongLong& x)
00865 {
00866   s.alignment() == Serializer::ALIGN_NONE ? 0 : s.align_r(sizeof(ACE_CDR::ULongLong));
00867   s.buffer_read(reinterpret_cast<char*>(&x), sizeof(ACE_CDR::ULongLong), s.swap_bytes());
00868   return s.good_bit();
00869 }
00870 
00871 ACE_INLINE bool
00872 operator>>(Serializer& s, ACE_CDR::LongDouble& x)
00873 {
00874   s.alignment() == Serializer::ALIGN_NONE ? 0 : s.align_r(8);
00875   s.buffer_read(reinterpret_cast<char*>(&x), sizeof(ACE_CDR::LongDouble), s.swap_bytes());
00876   return s.good_bit();
00877 }
00878 
00879 ACE_INLINE bool
00880 operator>>(Serializer& s, ACE_CDR::Float& x)
00881 {
00882   s.alignment() == Serializer::ALIGN_NONE ? 0 : s.align_r(sizeof(ACE_CDR::Float));
00883   s.buffer_read(reinterpret_cast<char*>(&x), sizeof(ACE_CDR::Float), s.swap_bytes());
00884   return s.good_bit();
00885 }
00886 
00887 ACE_INLINE bool
00888 operator>>(Serializer& s, ACE_CDR::Double& x)
00889 {
00890   s.alignment() == Serializer::ALIGN_NONE ? 0 : s.align_r(sizeof(ACE_CDR::Double));
00891   s.buffer_read(reinterpret_cast<char*>(&x), sizeof(ACE_CDR::Double), s.swap_bytes());
00892   return s.good_bit();
00893 }
00894 
00895 ACE_INLINE bool
00896 operator>>(Serializer& s, ACE_CDR::Char*& x)
00897 {
00898   s.read_string(x);
00899   return s.good_bit();
00900 }
00901 
00902 ACE_INLINE bool
00903 operator>>(Serializer& s, ACE_CDR::WChar*& x)
00904 {
00905   s.read_string(x);
00906   return s.good_bit();
00907 }
00908 
00909 ACE_INLINE bool
00910 operator>>(Serializer& s, ACE_InputCDR::to_boolean x)
00911 {
00912   s.buffer_read(reinterpret_cast<char*>(&x.ref_), sizeof(ACE_CDR::Boolean), s.swap_bytes());
00913   return s.good_bit();
00914 }
00915 
00916 ACE_INLINE bool
00917 operator>>(Serializer& s, ACE_InputCDR::to_char x)
00918 {
00919   s.buffer_read(reinterpret_cast<char*>(&x.ref_), sizeof(ACE_CDR::Char), s.swap_bytes());
00920   return s.good_bit();
00921 }
00922 
00923 ACE_INLINE bool
00924 operator>>(Serializer& s, ACE_InputCDR::to_wchar x)
00925 {
00926   ACE_CDR::Octet len;
00927   s.buffer_read(reinterpret_cast<char*>(&len), 1, false);
00928   if (len != Serializer::WCHAR_SIZE) {
00929     s.good_bit_ = false;
00930   } else {
00931 #if ACE_SIZEOF_WCHAR == 2
00932     s.buffer_read(reinterpret_cast<char*>(&x.ref_), Serializer::WCHAR_SIZE, Serializer::SWAP_BE);
00933 #else
00934     ACE_UINT16 as_utf16;
00935     s.buffer_read(reinterpret_cast<char*>(&as_utf16), Serializer::WCHAR_SIZE, Serializer::SWAP_BE);
00936     x.ref_ = as_utf16;
00937 #endif
00938   }
00939   return s.good_bit();
00940 }
00941 
00942 ACE_INLINE bool
00943 operator>>(Serializer& s, ACE_InputCDR::to_octet x)
00944 {
00945   s.buffer_read(reinterpret_cast<char*>(&x.ref_), sizeof(ACE_CDR::Octet), false);
00946   return s.good_bit();
00947 }
00948 
00949 ACE_INLINE bool
00950 operator>>(Serializer& s, ACE_InputCDR::to_string x)
00951 {
00952   const size_t length = s.read_string(const_cast<char*&>(x.val_));
00953   return s.good_bit()
00954          && ((x.bound_ == 0) || (length <= x.bound_));
00955 }
00956 
00957 ACE_INLINE bool
00958 operator>>(Serializer& s, ACE_InputCDR::to_wstring x)
00959 {
00960   const size_t length = s.read_string(const_cast<ACE_CDR::WChar*&>(x.val_));
00961   return s.good_bit()
00962          && ((x.bound_ == 0) || (length <= x.bound_));
00963 }
00964 
00965 #ifndef OPENDDS_SAFETY_PROFILE
00966 ACE_INLINE bool
00967 operator>>(Serializer& s, std::string& x)
00968 {
00969   char* buf = 0;
00970   const size_t length = s.read_string(buf);
00971   x.assign(buf, length);
00972   CORBA::string_free(buf);
00973   return s.good_bit();
00974 }
00975 
00976 #ifdef DDS_HAS_WCHAR
00977 ACE_INLINE bool
00978 operator>>(Serializer& s, std::wstring& x)
00979 {
00980   ACE_CDR::WChar* buf = 0;
00981   const size_t length = s.read_string(buf);
00982   x.assign(buf, length);
00983   CORBA::wstring_free(buf);
00984   return s.good_bit();
00985 }
00986 #endif /* DDS_HAS_WCHAR */
00987 #endif /* !OPENDDS_SAFETY_PROFILE */
00988 
00989 //----------------------------------------------------------------------------
00990 // predefined type gen_max_marshaled_size methods
00991 ACE_INLINE size_t gen_max_marshaled_size(const ACE_CDR::Short& /* x */)
00992 {
00993   return sizeof(ACE_CDR::Short);
00994 }
00995 
00996 ACE_INLINE size_t gen_max_marshaled_size(const ACE_CDR::UShort& /* x */)
00997 {
00998   return sizeof(ACE_CDR::UShort);
00999 }
01000 
01001 ACE_INLINE size_t gen_max_marshaled_size(const ACE_CDR::Long& /* x */)
01002 {
01003   return sizeof(ACE_CDR::Long);
01004 }
01005 
01006 ACE_INLINE size_t gen_max_marshaled_size(const ACE_CDR::ULong& /* x */)
01007 {
01008   return sizeof(ACE_CDR::ULong);
01009 }
01010 
01011 ACE_INLINE size_t gen_max_marshaled_size(const ACE_CDR::LongLong& /* x */)
01012 {
01013   return sizeof(ACE_CDR::LongLong);
01014 }
01015 
01016 ACE_INLINE size_t gen_max_marshaled_size(const ACE_CDR::ULongLong& /* x */)
01017 {
01018   return sizeof(ACE_CDR::ULongLong);
01019 }
01020 
01021 ACE_INLINE size_t gen_max_marshaled_size(const ACE_CDR::LongDouble& /* x */)
01022 {
01023   return sizeof(ACE_CDR::LongDouble);
01024 }
01025 
01026 ACE_INLINE size_t gen_max_marshaled_size(const ACE_CDR::Float& /* x */)
01027 {
01028   return sizeof(ACE_CDR::Float);
01029 }
01030 
01031 ACE_INLINE size_t gen_max_marshaled_size(const ACE_CDR::Double& /* x */)
01032 {
01033   return sizeof(ACE_CDR::Double);
01034 }
01035 
01036 // predefined type gen_max_marshaled_size method disambiguators.
01037 ACE_INLINE size_t gen_max_marshaled_size(const ACE_OutputCDR::from_boolean /* x */)
01038 {
01039   return sizeof(ACE_CDR::Char);
01040 }
01041 
01042 ACE_INLINE size_t gen_max_marshaled_size(const ACE_OutputCDR::from_char /* x */)
01043 {
01044   return sizeof(ACE_CDR::Char);
01045 }
01046 
01047 ACE_INLINE size_t gen_max_marshaled_size(const ACE_OutputCDR::from_wchar /* x */)
01048 {
01049   return Serializer::WCHAR_SIZE + 1; //CDR encoding adds 1 octet for length
01050 }
01051 
01052 ACE_INLINE size_t gen_max_marshaled_size(const ACE_OutputCDR::from_octet /* x */)
01053 {
01054   return sizeof(ACE_CDR::Char);
01055 }
01056 
01057 // predefined type gen_max_marshaled_size method explicit disambiguators.
01058 ACE_INLINE size_t max_marshaled_size_boolean()
01059 {
01060   return sizeof(ACE_CDR::Char);
01061 }
01062 
01063 ACE_INLINE size_t max_marshaled_size_char()
01064 {
01065   return sizeof(ACE_CDR::Char);
01066 }
01067 
01068 ACE_INLINE size_t max_marshaled_size_wchar()
01069 {
01070   return Serializer::WCHAR_SIZE + 1; //CDR encoding adds 1 octet for length
01071 }
01072 
01073 ACE_INLINE size_t max_marshaled_size_octet()
01074 {
01075   return sizeof(ACE_CDR::Char);
01076 }
01077 
01078 ACE_INLINE size_t max_marshaled_size_ulong()
01079 {
01080   return sizeof(ACE_CDR::ULong);
01081 }
01082 
01083 ACE_INLINE void find_size_ulong(size_t& size, size_t& padding)
01084 {
01085   const size_t sz = sizeof(ACE_CDR::ULong);
01086   if ((size + padding) % sz) {
01087     padding += sz - ((size + padding) % sz);
01088   }
01089   size += sz;
01090 }
01091 
01092 } // namespace DCPS
01093 } // namespace OpenDDS
01094 
01095 OPENDDS_END_VERSIONED_NAMESPACE_DECL
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 10 Aug 2018 for OpenDDS by  doxygen 1.6.1