Cleanup ACE_HAS_PTHREAD_SIGMASK_PROTOTYPE, all platforms support it so far as I can...
[ACE_TAO.git] / ACE / ace / CDR_Stream.cpp
blobb3f3dcf3db2ec8af9b5130d82bf266de0f55af02
1 #include "ace/CDR_Stream.h"
2 #include "ace/SString.h"
3 #include "ace/Truncate.h"
4 #include <memory>
6 #if !defined (__ACE_INLINE__)
7 # include "ace/CDR_Stream.inl"
8 #endif /* ! __ACE_INLINE__ */
10 // ****************************************************************
12 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
14 size_t ACE_OutputCDR::wchar_maxbytes_ = sizeof (ACE_CDR::WChar);
16 ACE_OutputCDR::ACE_OutputCDR (size_t size,
17 int byte_order,
18 ACE_Allocator *buffer_allocator,
19 ACE_Allocator *data_block_allocator,
20 ACE_Allocator *message_block_allocator,
21 size_t memcpy_tradeoff,
22 ACE_CDR::Octet major_version,
23 ACE_CDR::Octet minor_version)
24 : start_ ((size ? size : (size_t) ACE_CDR::DEFAULT_BUFSIZE) + ACE_CDR::MAX_ALIGNMENT,
25 ACE_Message_Block::MB_DATA,
28 buffer_allocator,
31 ACE_Time_Value::zero,
32 ACE_Time_Value::max_time,
33 data_block_allocator,
34 message_block_allocator),
35 #if !defined (ACE_LACKS_CDR_ALIGNMENT)
36 current_alignment_ (0),
37 #endif /* ACE_LACKS_CDR_ALIGNMENT */
38 current_is_writable_ (true),
39 do_byte_swap_ (byte_order != ACE_CDR_BYTE_ORDER),
40 good_bit_ (true),
41 memcpy_tradeoff_ (memcpy_tradeoff),
42 major_version_ (major_version),
43 minor_version_ (minor_version),
44 char_translator_ (0),
45 wchar_translator_ (0)
48 ACE_CDR::mb_align (&this->start_);
49 this->current_ = &this->start_;
51 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
52 ACE_NEW (this->monitor_,
53 ACE::Monitor_Control::Size_Monitor);
54 this->monitor_->receive (this->total_length ());
55 #endif /* ACE_HAS_MONITOR_POINTS==1 */
58 ACE_OutputCDR::ACE_OutputCDR (char *data,
59 size_t size,
60 int byte_order,
61 ACE_Allocator *buffer_allocator,
62 ACE_Allocator *data_block_allocator,
63 ACE_Allocator *message_block_allocator,
64 size_t memcpy_tradeoff,
65 ACE_CDR::Octet major_version,
66 ACE_CDR::Octet minor_version)
67 : start_ (size,
68 ACE_Message_Block::MB_DATA,
70 data,
71 buffer_allocator,
74 ACE_Time_Value::zero,
75 ACE_Time_Value::max_time,
76 data_block_allocator,
77 message_block_allocator),
78 #if !defined (ACE_LACKS_CDR_ALIGNMENT)
79 current_alignment_ (0),
80 #endif /* ACE_LACKS_CDR_ALIGNMENT */
81 current_is_writable_ (true),
82 do_byte_swap_ (byte_order != ACE_CDR_BYTE_ORDER),
83 good_bit_ (true),
84 memcpy_tradeoff_ (memcpy_tradeoff),
85 major_version_ (major_version),
86 minor_version_ (minor_version),
87 char_translator_ (0),
88 wchar_translator_ (0)
90 // We cannot trust the buffer to be properly aligned
91 ACE_CDR::mb_align (&this->start_);
92 this->current_ = &this->start_;
94 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
95 ACE_NEW (this->monitor_,
96 ACE::Monitor_Control::Size_Monitor);
97 this->monitor_->receive (this->total_length ());
98 #endif /* ACE_HAS_MONITOR_POINTS==1 */
101 ACE_OutputCDR::ACE_OutputCDR (ACE_Data_Block *data_block,
102 int byte_order,
103 ACE_Allocator *message_block_allocator,
104 size_t memcpy_tradeoff,
105 ACE_CDR::Octet major_version,
106 ACE_CDR::Octet minor_version)
107 : start_ (data_block,
108 ACE_Message_Block::DONT_DELETE,
109 message_block_allocator),
110 #if !defined (ACE_LACKS_CDR_ALIGNMENT)
111 current_alignment_ (0),
112 #endif /* ACE_LACKS_CDR_ALIGNMENT */
113 current_is_writable_ (true),
114 do_byte_swap_ (byte_order != ACE_CDR_BYTE_ORDER),
115 good_bit_ (true),
116 memcpy_tradeoff_ (memcpy_tradeoff),
117 major_version_ (major_version),
118 minor_version_ (minor_version),
119 char_translator_ (0),
120 wchar_translator_ (0)
122 // We cannot trust the buffer to be properly aligned
123 ACE_CDR::mb_align (&this->start_);
124 this->current_ = &this->start_;
126 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
127 ACE_NEW (this->monitor_,
128 ACE::Monitor_Control::Size_Monitor);
129 this->monitor_->receive (this->total_length ());
130 #endif /* ACE_HAS_MONITOR_POINTS==1 */
133 ACE_OutputCDR::ACE_OutputCDR (ACE_Message_Block *data,
134 int byte_order,
135 size_t memcpy_tradeoff,
136 ACE_CDR::Octet major_version,
137 ACE_CDR::Octet minor_version)
138 : start_ (data->data_block ()->duplicate ()),
139 #if !defined (ACE_LACKS_CDR_ALIGNMENT)
140 current_alignment_ (0),
141 #endif /* ACE_LACKS_CDR_ALIGNMENT */
142 current_is_writable_ (true),
143 do_byte_swap_ (byte_order != ACE_CDR_BYTE_ORDER),
144 good_bit_ (true),
145 memcpy_tradeoff_ (memcpy_tradeoff),
146 major_version_ (major_version),
147 minor_version_ (minor_version),
148 char_translator_ (0),
149 wchar_translator_ (0)
151 // We cannot trust the buffer to be properly aligned
152 ACE_CDR::mb_align (&this->start_);
153 this->current_ = &this->start_;
155 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
156 ACE_NEW (this->monitor_,
157 ACE::Monitor_Control::Size_Monitor);
158 this->monitor_->receive (this->total_length ());
159 #endif /* ACE_HAS_MONITOR_POINTS==1 */
162 /*static*/ void
163 ACE_OutputCDR::wchar_maxbytes (size_t maxbytes)
165 ACE_OutputCDR::wchar_maxbytes_ = maxbytes;
168 /*static*/ size_t
169 ACE_OutputCDR::wchar_maxbytes ()
171 return ACE_OutputCDR::wchar_maxbytes_;
175 ACE_OutputCDR::grow_and_adjust (size_t size,
176 size_t align,
177 char*& buf)
179 if (!this->current_is_writable_
180 || this->current_->cont () == 0
181 || this->current_->cont ()->size () < size + ACE_CDR::MAX_ALIGNMENT)
183 // Calculate the new buffer's length; if growing for encode, we
184 // don't grow in "small" chunks because of the cost.
185 size_t cursize = this->current_->size ();
186 if (this->current_->cont () != 0)
187 cursize = this->current_->cont ()->size ();
188 size_t minsize = size;
190 #if !defined (ACE_LACKS_CDR_ALIGNMENT)
191 minsize += ACE_CDR::MAX_ALIGNMENT;
192 #endif /* ACE_LACKS_CDR_ALIGNMENT */
194 // Make sure that there is enough room for <minsize> bytes, but
195 // also make it bigger than whatever our current size is.
196 if (minsize < cursize)
197 minsize = cursize;
199 size_t const newsize = ACE_CDR::next_size (minsize);
201 this->good_bit_ = false;
202 ACE_Message_Block* tmp = 0;
203 ACE_NEW_RETURN (tmp,
204 ACE_Message_Block (newsize,
205 ACE_Message_Block::MB_DATA,
208 this->current_->data_block ()->allocator_strategy (),
211 ACE_Time_Value::zero,
212 ACE_Time_Value::max_time,
213 this->current_->data_block ()->data_block_allocator ()),
214 -1);
216 // Message block initialization may fail while the construction
217 // succeds. Since as a matter of policy, ACE may throw no
218 // exceptions, we have to do a separate check like this.
219 if (tmp != 0 && tmp->size () < newsize)
221 delete tmp;
222 errno = ENOMEM;
223 return -1;
226 this->good_bit_ = true;
228 #if !defined (ACE_LACKS_CDR_ALIGNMENT)
229 // The new block must start with the same alignment as the
230 // previous block finished.
231 ptrdiff_t const tmpalign =
232 reinterpret_cast<ptrdiff_t> (tmp->rd_ptr ()) % ACE_CDR::MAX_ALIGNMENT;
233 ptrdiff_t const curalign =
234 static_cast<ptrdiff_t> (this->current_alignment_) % ACE_CDR::MAX_ALIGNMENT;
235 ptrdiff_t offset = curalign - tmpalign;
236 if (offset < 0)
237 offset += ACE_CDR::MAX_ALIGNMENT;
238 tmp->rd_ptr (static_cast<size_t> (offset));
239 tmp->wr_ptr (tmp->rd_ptr ());
240 #endif /* ACE_LACKS_CDR_ALIGNMENT */
242 // grow the chain and set the current block.
243 tmp->cont (this->current_->cont ());
244 this->current_->cont (tmp);
246 this->current_ = this->current_->cont ();
247 this->current_is_writable_ = true;
249 return this->adjust (size, align, buf);
252 ACE_CDR::Boolean
253 ACE_OutputCDR::write_wchar (ACE_CDR::WChar x)
255 if (this->wchar_translator_ != 0)
256 return (this->good_bit_ = this->wchar_translator_->write_wchar (*this, x));
257 if (ACE_OutputCDR::wchar_maxbytes_ == 0)
259 errno = EACCES;
260 return (this->good_bit_ = false);
262 if (static_cast<ACE_CDR::Short> (major_version_) == 1
263 && static_cast<ACE_CDR::Short> (minor_version_) == 2)
265 ACE_CDR::Octet len =
266 static_cast<ACE_CDR::Octet> (ACE_OutputCDR::wchar_maxbytes_);
267 if (this->write_1 (&len))
269 if (ACE_OutputCDR::wchar_maxbytes_ == sizeof(ACE_CDR::WChar))
270 return
271 this->write_octet_array (
272 reinterpret_cast<const ACE_CDR::Octet*> (&x),
273 static_cast<ACE_CDR::ULong> (len));
274 else
275 if (ACE_OutputCDR::wchar_maxbytes_ == 2)
277 ACE_CDR::Short sx = static_cast<ACE_CDR::Short> (x);
278 return
279 this->write_octet_array (
280 reinterpret_cast<const ACE_CDR::Octet*> (&sx),
281 static_cast<ACE_CDR::ULong> (len));
283 else
285 ACE_CDR::Octet ox = static_cast<ACE_CDR::Octet> (x);
286 return
287 this->write_octet_array (
288 reinterpret_cast<const ACE_CDR::Octet*> (&ox),
289 static_cast<ACE_CDR::ULong> (len));
293 else if (static_cast<ACE_CDR::Short> (minor_version_) == 0)
294 { // wchar is not allowed with GIOP 1.0.
295 errno = EINVAL;
296 return (this->good_bit_ = false);
298 if (ACE_OutputCDR::wchar_maxbytes_ == sizeof (ACE_CDR::WChar))
300 void const * const temp = &x;
301 return
302 this->write_4 (reinterpret_cast<const ACE_CDR::ULong *> (temp));
304 else if (ACE_OutputCDR::wchar_maxbytes_ == 2)
306 ACE_CDR::Short sx = static_cast<ACE_CDR::Short> (x);
307 return this->write_2 (reinterpret_cast<const ACE_CDR::UShort *> (&sx));
309 ACE_CDR::Octet ox = static_cast<ACE_CDR::Octet> (x);
310 return this->write_1 (reinterpret_cast<const ACE_CDR::Octet *> (&ox));
313 ACE_CDR::Boolean
314 ACE_OutputCDR::write_string (ACE_CDR::ULong len,
315 const ACE_CDR::Char *x)
317 // @@ This is a slight violation of "Optimize for the common case",
318 // i.e. normally the translator will be 0, but OTOH the code is
319 // smaller and should be better for the cache ;-) ;-)
320 if (this->char_translator_ != 0)
321 return this->char_translator_->write_string (*this, len, x);
323 if (len != 0)
325 if (this->write_ulong (len + 1))
326 return this->write_char_array (x, len + 1);
328 else
330 // Be nice to programmers: treat nulls as empty strings not
331 // errors. (OMG-IDL supports languages that don't use the C/C++
332 // notion of null v. empty strings; nulls aren't part of the OMG-IDL
333 // string model.)
334 if (this->write_ulong (1))
335 return this->write_char (0);
338 return (this->good_bit_ = false);
341 ACE_CDR::Boolean
342 ACE_OutputCDR::write_string (const ACE_CString &x)
344 // @@ Leave this method in here, not the `.i' file so that we don't
345 // have to unnecessarily pull in the `ace/SString.h' header.
346 return this->write_string (static_cast<ACE_CDR::ULong> (x.length ()),
347 x.c_str());
350 ACE_CDR::Boolean
351 ACE_OutputCDR::write_wstring (ACE_CDR::ULong len,
352 const ACE_CDR::WChar *x)
354 // @@ This is a slight violation of "Optimize for the common case",
355 // i.e. normally the translator will be 0, but OTOH the code is
356 // smaller and should be better for the cache ;-) ;-)
357 // What do we do for GIOP 1.2???
358 if (this->wchar_translator_ != 0)
359 return this->wchar_translator_->write_wstring (*this, len, x);
360 if (ACE_OutputCDR::wchar_maxbytes_ == 0)
362 errno = EACCES;
363 return (this->good_bit_ = false);
366 if (static_cast<ACE_CDR::Short> (this->major_version_) == 1
367 && static_cast<ACE_CDR::Short> (this->minor_version_) == 2)
369 if (x != 0)
371 //In GIOP 1.2 the length field contains the number of bytes
372 //the wstring occupies rather than number of wchars
373 //Taking sizeof might not be a good way! This is a temporary fix.
374 ACE_CDR::Boolean good_ulong =
375 this->write_ulong (
376 ACE_Utils::truncate_cast<ACE_CDR::ULong> (
377 ACE_OutputCDR::wchar_maxbytes_ * len));
379 if (good_ulong)
381 return this->write_wchar_array (x, len);
384 else
386 //In GIOP 1.2 zero length wstrings are legal
387 return this->write_ulong (0);
391 else
392 if (x != 0)
394 if (this->write_ulong (len + 1))
395 return this->write_wchar_array (x, len + 1);
397 else if (this->write_ulong (1))
398 return this->write_wchar (0);
399 return (this->good_bit_ = false);
402 ACE_CDR::Boolean
403 ACE_OutputCDR::write_octet_array_mb (const ACE_Message_Block* mb)
405 // If the buffer is small and it fits in the current message
406 // block it is be cheaper just to copy the buffer.
407 for (const ACE_Message_Block* i = mb;
408 i != 0;
409 i = i->cont ())
411 size_t const length = i->length ();
413 // If the mb does not own its data we are forced to make a copy.
414 if (ACE_BIT_ENABLED (i->flags (),
415 ACE_Message_Block::DONT_DELETE))
417 if (! this->write_array (i->rd_ptr (),
418 ACE_CDR::OCTET_SIZE,
419 ACE_CDR::OCTET_ALIGN,
420 static_cast<ACE_CDR::ULong> (length)))
421 return (this->good_bit_ = false);
422 continue;
425 if (length < this->memcpy_tradeoff_
426 && this->current_->wr_ptr () + length < this->current_->end ())
428 if (! this->write_array (i->rd_ptr (),
429 ACE_CDR::OCTET_SIZE,
430 ACE_CDR::OCTET_ALIGN,
431 static_cast<ACE_CDR::ULong> (length)))
432 return (this->good_bit_ = false);
433 continue;
436 ACE_Message_Block* cont = 0;
437 this->good_bit_ = false;
438 ACE_NEW_RETURN (cont,
439 ACE_Message_Block (i->data_block ()->duplicate ()),
440 false);
441 this->good_bit_ = true;
443 if (this->current_->cont () != 0)
444 ACE_Message_Block::release (this->current_->cont ());
445 cont->rd_ptr (i->rd_ptr ());
446 cont->wr_ptr (i->wr_ptr ());
448 this->current_->cont (cont);
449 this->current_ = cont;
450 this->current_is_writable_ = false;
451 #if !defined (ACE_LACKS_CDR_ALIGNMENT)
452 this->current_alignment_ =
453 (this->current_alignment_ + cont->length ()) % ACE_CDR::MAX_ALIGNMENT;
454 #endif /* ACE_LACKS_CDR_ALIGNMENT */
457 return true;
460 ACE_CDR::Boolean
461 ACE_OutputCDR::write_1 (const ACE_CDR::Octet *x)
463 char *buf = 0;
464 if (this->adjust (1, buf) == 0)
466 *reinterpret_cast<ACE_CDR::Octet*> (buf) = *x;
467 return true;
470 return false;
473 ACE_CDR::Boolean
474 ACE_OutputCDR::write_2 (const ACE_CDR::UShort *x)
476 char *buf = 0;
477 if (this->adjust (ACE_CDR::SHORT_SIZE, buf) == 0)
479 #if !defined (ACE_ENABLE_SWAP_ON_WRITE)
480 *reinterpret_cast<ACE_CDR::UShort*> (buf) = *x;
481 return true;
482 #else
483 if (!this->do_byte_swap_)
485 *reinterpret_cast<ACE_CDR::UShort *> (buf) = *x;
486 return true;
488 else
490 ACE_CDR::swap_2 (reinterpret_cast<const char*> (x), buf);
491 return true;
493 #endif /* ACE_ENABLE_SWAP_ON_WRITE */
496 return false;
499 ACE_CDR::Boolean
500 ACE_OutputCDR::write_4 (const ACE_CDR::ULong *x)
502 char *buf = 0;
503 if (this->adjust (ACE_CDR::LONG_SIZE, buf) == 0)
505 #if !defined (ACE_ENABLE_SWAP_ON_WRITE)
506 *reinterpret_cast<ACE_CDR::ULong*> (buf) = *x;
507 return true;
508 #else
509 if (!this->do_byte_swap_)
511 *reinterpret_cast<ACE_CDR::ULong *> (buf) = *x;
512 return true;
514 else
516 ACE_CDR::swap_4 (reinterpret_cast<const char*> (x), buf);
517 return true;
519 #endif /* ACE_ENABLE_SWAP_ON_WRITE */
522 return false;
525 ACE_CDR::Boolean
526 ACE_OutputCDR::write_8 (const ACE_CDR::ULongLong *x)
528 char *buf = 0;
530 if (this->adjust (ACE_CDR::LONGLONG_SIZE, buf) == 0)
532 #if !defined (ACE_ENABLE_SWAP_ON_WRITE)
533 *reinterpret_cast<ACE_CDR::ULongLong *> (buf) = *x;
534 return true;
535 #else
536 if (!this->do_byte_swap_)
538 *reinterpret_cast<ACE_CDR::ULongLong *> (buf) = *x;
539 return true;
541 else
543 ACE_CDR::swap_8 (reinterpret_cast<const char*> (x), buf);
544 return true;
546 #endif /* ACE_ENABLE_SWAP_ON_WRITE */
549 return false;
552 ACE_CDR::Boolean
553 ACE_OutputCDR::write_16 (const ACE_CDR::LongDouble *x)
555 char* buf = 0;
556 if (this->adjust (ACE_CDR::LONGDOUBLE_SIZE,
557 ACE_CDR::LONGDOUBLE_ALIGN,
558 buf) == 0)
560 #if !defined (ACE_ENABLE_SWAP_ON_WRITE)
561 *reinterpret_cast<ACE_CDR::LongDouble*> (buf) = *x;
562 return 1;
563 #else
564 if (!this->do_byte_swap_)
566 *reinterpret_cast<ACE_CDR::LongDouble *> (buf) = *x;
567 return true;
569 else
571 ACE_CDR::swap_16 (reinterpret_cast<const char*> (x), buf);
572 return true;
574 #endif /* ACE_ENABLE_SWAP_ON_WRITE */
577 return false;
580 ACE_CDR::Boolean
581 ACE_OutputCDR::write_wchar_array_i (const ACE_CDR::WChar *x,
582 ACE_CDR::ULong length)
584 if (length == 0)
585 return true;
586 char* buf = 0;
587 size_t const align = (ACE_OutputCDR::wchar_maxbytes_ == 2) ?
588 ACE_CDR::SHORT_ALIGN :
589 ACE_CDR::OCTET_ALIGN;
591 if (this->adjust (ACE_OutputCDR::wchar_maxbytes_ * length, align, buf) == 0)
593 if (ACE_OutputCDR::wchar_maxbytes_ == 2)
595 ACE_CDR::UShort *sb = reinterpret_cast<ACE_CDR::UShort *> (buf);
596 for (size_t i = 0; i < length; ++i)
597 #if !defined (ACE_ENABLE_SWAP_ON_WRITE)
598 sb[i] = static_cast<ACE_CDR::UShort> (x[i]);
599 #else
600 if (!this->do_byte_swap_)
601 sb[i] = static_cast<ACE_CDR::UShort> (x[i]);
602 else
604 ACE_CDR::UShort sx = static_cast<ACE_CDR::UShort> (x[i]);
605 ACE_CDR::swap_2 (reinterpret_cast<char *> (&sx), &buf[i * 2]);
607 #endif /* ACE_ENABLE_SWAP_ON_WRITE */
609 else
611 for (size_t i = 0; i < length; ++i)
612 buf[i] = static_cast<char> (x[i]);
614 return this->good_bit_;
616 return false;
620 ACE_CDR::Boolean
621 ACE_OutputCDR::write_array (const void *x,
622 size_t size,
623 size_t align,
624 ACE_CDR::ULong length)
626 if (length == 0)
627 return true;
628 char *buf = 0;
629 if (this->adjust (size * length, align, buf) == 0)
631 #if !defined (ACE_ENABLE_SWAP_ON_WRITE)
632 ACE_OS::memcpy (buf, x, size*length);
633 return true;
634 #else
635 if (!this->do_byte_swap_ || size == 1)
637 ACE_OS::memcpy (buf, x, size*length);
638 return true;
640 else
642 const char *source = reinterpret_cast<const char *> (x);
643 switch (size)
645 case 2:
646 ACE_CDR::swap_2_array (source, buf, length);
647 return true;
648 case 4:
649 ACE_CDR::swap_4_array (source, buf, length);
650 return true;
651 case 8:
652 ACE_CDR::swap_8_array (source, buf, length);
653 return true;
654 case 16:
655 ACE_CDR::swap_16_array (source, buf, length);
656 return true;
657 default:
658 // TODO: print something?
659 this->good_bit_ = false;
660 return false;
663 #endif /* ACE_ENABLE_SWAP_ON_WRITE */
665 this->good_bit_ = false;
666 return false;
670 ACE_CDR::Boolean
671 ACE_OutputCDR::write_boolean_array (const ACE_CDR::Boolean* x,
672 ACE_CDR::ULong length)
674 // It is hard to optimize this, the spec requires that on the wire
675 // booleans be represented as a byte with value 0 or 1, but in
676 // memory it is possible (though very unlikely) that a boolean has
677 // a non-zero value (different from 1).
678 // We resort to a simple loop.
679 ACE_CDR::Boolean const * const end = x + length;
681 for (ACE_CDR::Boolean const * i = x;
682 i != end && this->good_bit ();
683 ++i)
684 (void) this->write_boolean (*i);
686 return this->good_bit ();
689 char *
690 ACE_OutputCDR::write_long_placeholder ()
692 char *buf = 0;
693 if (this->adjust (ACE_CDR::LONG_SIZE, buf) == 0)
694 *reinterpret_cast<ACE_CDR::ULong*> (buf) = 0;
695 else
696 buf = 0;
697 return buf;
700 char *
701 ACE_OutputCDR::write_short_placeholder ()
703 char *buf = 0;
704 if (this->adjust (ACE_CDR::SHORT_SIZE, buf) == 0)
705 *reinterpret_cast<ACE_CDR::UShort*> (buf) = 0;
706 else
707 buf = 0;
708 return buf;
711 char *
712 ACE_OutputCDR::write_boolean_placeholder ()
714 char *buf = 0;
715 if (this->adjust (ACE_CDR::OCTET_SIZE, buf) == 0)
716 *reinterpret_cast<ACE_CDR::Boolean*> (buf) = 0;
717 else
718 buf = 0;
719 return buf;
722 char *
723 ACE_OutputCDR::write_char_placeholder ()
725 char *buf = 0;
726 if (this->adjust (ACE_CDR::OCTET_SIZE, buf) == 0)
727 *reinterpret_cast<ACE_CDR::Char*> (buf) = 0;
728 else
729 buf = 0;
730 return buf;
733 char *
734 ACE_OutputCDR::write_octet_placeholder ()
736 char *buf = 0;
737 if (this->adjust (ACE_CDR::OCTET_SIZE, buf) == 0)
738 *reinterpret_cast<ACE_CDR::Octet*> (buf) = 0;
739 else
740 buf = 0;
741 return buf;
744 char *
745 ACE_OutputCDR::write_longlong_placeholder ()
747 char *buf = 0;
748 if (this->adjust (ACE_CDR::LONGLONG_SIZE, buf) == 0)
749 *reinterpret_cast<ACE_CDR::ULongLong*> (buf) = 0;
750 else
751 buf = 0;
752 return buf;
755 char *
756 ACE_OutputCDR::write_float_placeholder ()
758 char *buf = 0;
759 if (this->adjust (ACE_CDR::LONG_SIZE, buf) == 0)
760 *reinterpret_cast<ACE_CDR::ULong*> (buf) = 0;
761 else
762 buf = 0;
763 return buf;
766 char *
767 ACE_OutputCDR::write_double_placeholder ()
769 char *buf = 0;
770 if (this->adjust (ACE_CDR::LONGLONG_SIZE, buf) == 0)
771 *reinterpret_cast<ACE_CDR::ULongLong*> (buf) = 0;
772 else
773 buf = 0;
774 return buf;
777 ACE_CDR::Boolean
778 ACE_OutputCDR::replace (ACE_CDR::Long x, char* loc)
780 if (this->find (loc) == 0)
781 return false;
783 #if !defined (ACE_ENABLE_SWAP_ON_WRITE)
784 *reinterpret_cast<ACE_CDR::Long*> (loc) = x;
785 #else
786 if (!this->do_byte_swap_)
788 *reinterpret_cast<ACE_CDR::Long *> (loc) = x;
790 else
792 ACE_CDR::swap_4 (reinterpret_cast<const char*> (&x), loc);
794 #endif /* ACE_ENABLE_SWAP_ON_WRITE */
796 return true;
799 ACE_CDR::Boolean
800 ACE_OutputCDR::replace (ACE_CDR::ULong x, char* loc)
802 if (this->find (loc) == 0)
803 return false;
805 #if !defined (ACE_ENABLE_SWAP_ON_WRITE)
806 *reinterpret_cast<ACE_CDR::ULong*> (loc) = x;
807 #else
808 if (!this->do_byte_swap_)
810 *reinterpret_cast<ACE_CDR::ULong *> (loc) = x;
812 else
814 ACE_CDR::swap_4 (reinterpret_cast<const char*> (&x), loc);
816 #endif /* ACE_ENABLE_SWAP_ON_WRITE */
818 return true;
821 ACE_CDR::Boolean
822 ACE_OutputCDR::replace (ACE_CDR::Short x, char* loc)
824 if (this->find (loc) == 0)
825 return false;
827 #if !defined (ACE_ENABLE_SWAP_ON_WRITE)
828 *reinterpret_cast<ACE_CDR::Short*> (loc) = x;
829 #else
830 if (!this->do_byte_swap_)
832 *reinterpret_cast<ACE_CDR::Short *> (loc) = x;
834 else
836 ACE_CDR::swap_2 (reinterpret_cast<const char*> (&x), loc);
838 #endif /* ACE_ENABLE_SWAP_ON_WRITE */
840 return true;
843 ACE_CDR::Boolean
844 ACE_OutputCDR::replace (ACE_CDR::UShort x, char* loc)
846 if (this->find (loc) == 0)
847 return false;
849 #if !defined (ACE_ENABLE_SWAP_ON_WRITE)
850 *reinterpret_cast<ACE_CDR::UShort*> (loc) = x;
851 #else
852 if (!this->do_byte_swap_)
854 *reinterpret_cast<ACE_CDR::UShort *> (loc) = x;
856 else
858 ACE_CDR::swap_2 (reinterpret_cast<const char*> (&x), loc);
860 #endif /* ACE_ENABLE_SWAP_ON_WRITE */
862 return true;
865 ACE_CDR::Boolean
866 ACE_OutputCDR::replace (ACE_CDR::Boolean x, char* loc)
868 if (this->find (loc) == 0)
869 return false;
871 *reinterpret_cast<ACE_CDR::Boolean*> (loc) = x;
873 return true;
876 ACE_CDR::Boolean
877 ACE_OutputCDR::replace (ACE_CDR::Char x, char* loc)
879 if (this->find (loc) == 0)
880 return false;
882 *reinterpret_cast<ACE_CDR::Char*> (loc) = x;
884 return true;
887 ACE_CDR::Boolean
888 ACE_OutputCDR::replace (ACE_CDR::Octet x, char* loc)
890 if (this->find (loc) == 0)
891 return false;
893 *reinterpret_cast<ACE_CDR::Octet*> (loc) = x;
895 return true;
898 ACE_CDR::Boolean
899 ACE_OutputCDR::replace (ACE_CDR::LongLong x, char* loc)
901 if (this->find (loc) == 0)
902 return false;
904 #if !defined (ACE_ENABLE_SWAP_ON_WRITE)
905 *reinterpret_cast<ACE_CDR::LongLong*> (loc) = x;
906 #else
907 if (!this->do_byte_swap_)
909 *reinterpret_cast<ACE_CDR::LongLong*> (loc) = x;
911 else
913 ACE_CDR::swap_8 (reinterpret_cast<const char*> (&x), loc);
915 #endif /* ACE_ENABLE_SWAP_ON_WRITE */
917 return true;
920 ACE_CDR::Boolean
921 ACE_OutputCDR::replace (ACE_CDR::ULongLong x, char* loc)
923 if (this->find (loc) == 0)
924 return false;
926 #if !defined (ACE_ENABLE_SWAP_ON_WRITE)
927 *reinterpret_cast<ACE_CDR::ULongLong*> (loc) = x;
928 #else
929 if (!this->do_byte_swap_)
931 *reinterpret_cast<ACE_CDR::ULongLong*> (loc) = x;
933 else
935 ACE_CDR::swap_8 (reinterpret_cast<const char*> (&x), loc);
937 #endif /* ACE_ENABLE_SWAP_ON_WRITE */
939 return true;
942 ACE_CDR::Boolean
943 ACE_OutputCDR::replace (ACE_CDR::Float x, char* loc)
945 if (this->find (loc) == 0)
946 return false;
948 #if !defined (ACE_ENABLE_SWAP_ON_WRITE)
949 *reinterpret_cast<ACE_CDR::Float*> (loc) = x;
950 #else
951 if (!this->do_byte_swap_)
953 *reinterpret_cast<ACE_CDR::Float*> (loc) = x;
955 else
957 ACE_CDR::swap_4 (reinterpret_cast<const char*> (&x), loc);
959 #endif /* ACE_ENABLE_SWAP_ON_WRITE */
961 return true;
964 ACE_CDR::Boolean
965 ACE_OutputCDR::replace (ACE_CDR::Double x, char* loc)
967 if (this->find (loc) == 0)
968 return false;
970 #if !defined (ACE_ENABLE_SWAP_ON_WRITE)
971 *reinterpret_cast<ACE_CDR::Double*> (loc) = x;
972 #else
973 if (!this->do_byte_swap_)
975 *reinterpret_cast<ACE_CDR::Double*> (loc) = x;
977 else
979 ACE_CDR::swap_8 (reinterpret_cast<const char*> (&x), loc);
981 #endif /* ACE_ENABLE_SWAP_ON_WRITE */
983 return true;
987 ACE_OutputCDR::consolidate ()
989 // Optimize by only doing something if we need to
990 if (this->current_ != &this->start_)
992 // Set the number of bytes in the top-level block, reallocating
993 // if necessary. The rd_ptr and wr_ptr remain at the original offsets
994 // into the buffer, even if it is reallocated.
995 // Return an error if the allocation failed.
996 size_t const newsize =
997 ACE_CDR::first_size (this->total_length ()
998 + ACE_CDR::MAX_ALIGNMENT);
999 if (this->start_.size (newsize) < 0)
1001 return -1;
1004 // Consolidate the chain into the first block. NOTE that
1005 // ACE_CDR::consolidate can not be used since we don't want to
1006 // overwrite what is already in the first block. We just append it since
1007 // the read and write pointers weren't affected by the resizing above.
1008 // We also don't have to worry about alignment since the start block is
1009 // already aligned.
1010 // NOTE also we know there is a continuation since we checked for it
1011 // above. There is therefore no reason to check for a 0 continuation
1012 // field here.
1013 ACE_Message_Block *cont = this->start_.cont ();
1014 for (const ACE_Message_Block* i = cont; i != 0; i = i->cont ())
1016 this->start_.copy (i->rd_ptr (), i->length ());
1019 // Release the old blocks that were consolidated and reset the
1020 // current_ and current_is_writable_ to reflect the single used block.
1021 ACE_Message_Block::release (cont);
1022 this->start_.cont (0);
1023 this->current_ = &this->start_;
1024 this->current_is_writable_ = true;
1027 return 0;
1031 ACE_Message_Block*
1032 ACE_OutputCDR::find (char* loc)
1034 ACE_Message_Block* mb = 0;
1035 for (mb = &this->start_; mb != 0; mb = mb->cont ())
1037 if (loc <= mb->wr_ptr () && loc >= mb->rd_ptr ())
1039 break;
1043 return mb;
1046 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
1048 void
1049 ACE_OutputCDR::register_monitor (const char *id)
1051 this->monitor_->name (id);
1052 this->monitor_->add_to_registry ();
1055 void
1056 ACE_OutputCDR::unregister_monitor ()
1058 this->monitor_->remove_from_registry ();
1061 #endif /* ACE_HAS_MONITOR_POINTS==1 */
1063 // ****************************************************************
1065 ACE_InputCDR::ACE_InputCDR (const char *buf,
1066 size_t bufsiz,
1067 int byte_order,
1068 ACE_CDR::Octet major_version,
1069 ACE_CDR::Octet minor_version)
1070 : start_ (buf, bufsiz),
1071 do_byte_swap_ (byte_order != ACE_CDR_BYTE_ORDER),
1072 good_bit_ (true),
1073 major_version_ (major_version),
1074 minor_version_ (minor_version),
1075 char_translator_ (0),
1076 wchar_translator_ (0)
1078 this->start_.wr_ptr (bufsiz);
1080 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
1081 ACE_NEW (this->monitor_,
1082 ACE::Monitor_Control::Size_Monitor);
1083 this->monitor_->receive (bufsiz);
1084 #endif /* ACE_HAS_MONITOR_POINTS==1 */
1087 ACE_InputCDR::ACE_InputCDR (size_t bufsiz,
1088 int byte_order,
1089 ACE_CDR::Octet major_version,
1090 ACE_CDR::Octet minor_version)
1091 : start_ (bufsiz),
1092 do_byte_swap_ (byte_order != ACE_CDR_BYTE_ORDER),
1093 good_bit_ (true),
1094 major_version_ (major_version),
1095 minor_version_ (minor_version),
1096 char_translator_ (0),
1097 wchar_translator_ (0)
1099 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
1100 ACE_NEW (this->monitor_,
1101 ACE::Monitor_Control::Size_Monitor);
1102 this->monitor_->receive (bufsiz);
1103 #endif /* ACE_HAS_MONITOR_POINTS==1 */
1106 ACE_InputCDR::ACE_InputCDR (const ACE_Message_Block *data,
1107 int byte_order,
1108 ACE_CDR::Octet major_version,
1109 ACE_CDR::Octet minor_version,
1110 ACE_Lock* lock)
1111 : start_ (0, ACE_Message_Block::MB_DATA, 0, 0, 0, lock),
1112 good_bit_ (true),
1113 major_version_ (major_version),
1114 minor_version_ (minor_version),
1115 char_translator_ (0),
1116 wchar_translator_ (0)
1118 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
1119 ACE_NEW (this->monitor_,
1120 ACE::Monitor_Control::Size_Monitor);
1121 this->monitor_->receive (this->start_.total_size ());
1122 #endif /* ACE_HAS_MONITOR_POINTS==1 */
1124 this->reset (data, byte_order);
1127 ACE_InputCDR::ACE_InputCDR (ACE_Data_Block *data,
1128 ACE_Message_Block::Message_Flags flag,
1129 int byte_order,
1130 ACE_CDR::Octet major_version,
1131 ACE_CDR::Octet minor_version)
1132 : start_ (data, flag),
1133 do_byte_swap_ (byte_order != ACE_CDR_BYTE_ORDER),
1134 good_bit_ (true),
1135 major_version_ (major_version),
1136 minor_version_ (minor_version),
1137 char_translator_ (0),
1138 wchar_translator_ (0)
1140 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
1141 ACE_NEW (this->monitor_,
1142 ACE::Monitor_Control::Size_Monitor);
1143 this->monitor_->receive (data->size ());
1144 #endif /* ACE_HAS_MONITOR_POINTS==1 */
1147 ACE_InputCDR::ACE_InputCDR (ACE_Data_Block *data,
1148 ACE_Message_Block::Message_Flags flag,
1149 size_t rd_pos,
1150 size_t wr_pos,
1151 int byte_order,
1152 ACE_CDR::Octet major_version,
1153 ACE_CDR::Octet minor_version)
1154 : start_ (data, flag),
1155 do_byte_swap_ (byte_order != ACE_CDR_BYTE_ORDER),
1156 good_bit_ (true),
1157 major_version_ (major_version),
1158 minor_version_ (minor_version),
1159 char_translator_ (0),
1160 wchar_translator_ (0)
1162 // Set the read pointer
1163 this->start_.rd_ptr (rd_pos);
1165 // Set the write pointer after doing a sanity check.
1166 char* wrpos = this->start_.base () + wr_pos;
1168 if (this->start_.end () >= wrpos)
1170 this->start_.wr_ptr (wr_pos);
1173 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
1174 ACE_NEW (this->monitor_,
1175 ACE::Monitor_Control::Size_Monitor);
1176 this->monitor_->receive (data->size ());
1177 #endif /* ACE_HAS_MONITOR_POINTS==1 */
1180 ACE_InputCDR::ACE_InputCDR (const ACE_InputCDR& rhs,
1181 size_t size,
1182 ACE_CDR::Long offset)
1183 : start_ (rhs.start_,
1184 ACE_CDR::MAX_ALIGNMENT),
1185 do_byte_swap_ (rhs.do_byte_swap_),
1186 good_bit_ (true),
1187 major_version_ (rhs.major_version_),
1188 minor_version_ (rhs.minor_version_),
1189 char_translator_ (rhs.char_translator_),
1190 wchar_translator_ (rhs.wchar_translator_)
1192 #if !defined (ACE_LACKS_CDR_ALIGNMENT)
1193 // Align the base pointer assuming that the incoming stream is also
1194 // aligned the way we are aligned
1195 char *incoming_start = ACE_ptr_align_binary (rhs.start_.base (),
1196 ACE_CDR::MAX_ALIGNMENT);
1197 #else
1198 char *incoming_start = rhs.start_.base ();
1199 #endif /* ACE_LACKS_CDR_ALIGNMENT */
1201 const size_t newpos =
1202 (rhs.start_.rd_ptr() - incoming_start) + offset;
1204 if (newpos <= this->start_.space ()
1205 && newpos + size <= this->start_.space ())
1207 this->start_.rd_ptr (newpos);
1208 this->start_.wr_ptr (newpos + size);
1210 else
1212 this->good_bit_ = false;
1215 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
1216 ACE_NEW (this->monitor_,
1217 ACE::Monitor_Control::Size_Monitor);
1218 this->monitor_->receive (this->start_.total_size ());
1219 #endif /* ACE_HAS_MONITOR_POINTS==1 */
1222 ACE_InputCDR::ACE_InputCDR (const ACE_InputCDR& rhs,
1223 size_t size)
1224 : start_ (rhs.start_,
1225 ACE_CDR::MAX_ALIGNMENT),
1226 do_byte_swap_ (rhs.do_byte_swap_),
1227 good_bit_ (true),
1228 major_version_ (rhs.major_version_),
1229 minor_version_ (rhs.minor_version_),
1230 char_translator_ (rhs.char_translator_),
1231 wchar_translator_ (rhs.wchar_translator_)
1233 #if !defined (ACE_LACKS_CDR_ALIGNMENT)
1234 // Align the base pointer assuming that the incoming stream is also
1235 // aligned the way we are aligned
1236 char *incoming_start = ACE_ptr_align_binary (rhs.start_.base (),
1237 ACE_CDR::MAX_ALIGNMENT);
1238 #else
1239 char *incoming_start = rhs.start_.base ();
1240 #endif /* ACE_LACKS_CDR_ALIGNMENT */
1242 const size_t newpos =
1243 rhs.start_.rd_ptr() - incoming_start;
1245 if (newpos <= this->start_.space ()
1246 && newpos + size <= this->start_.space ())
1248 // Notice that ACE_Message_Block::duplicate may leave the
1249 // wr_ptr() with a higher value than what we actually want.
1250 this->start_.rd_ptr (newpos);
1251 this->start_.wr_ptr (newpos + size);
1253 ACE_CDR::Octet byte_order = 0;
1254 (void) this->read_octet (byte_order);
1255 this->do_byte_swap_ = (byte_order != ACE_CDR_BYTE_ORDER);
1257 else
1259 this->good_bit_ = false;
1262 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
1263 ACE_NEW (this->monitor_,
1264 ACE::Monitor_Control::Size_Monitor);
1265 this->monitor_->receive (this->start_.total_size ());
1266 #endif /* ACE_HAS_MONITOR_POINTS==1 */
1269 ACE_InputCDR::ACE_InputCDR (const ACE_InputCDR& rhs)
1270 : start_ (rhs.start_,
1271 ACE_CDR::MAX_ALIGNMENT),
1272 do_byte_swap_ (rhs.do_byte_swap_),
1273 good_bit_ (true),
1274 major_version_ (rhs.major_version_),
1275 minor_version_ (rhs.minor_version_),
1276 char_translator_ (rhs.char_translator_),
1277 wchar_translator_ (rhs.wchar_translator_)
1279 #if !defined (ACE_LACKS_CDR_ALIGNMENT)
1280 char *buf = ACE_ptr_align_binary (rhs.start_.base (),
1281 ACE_CDR::MAX_ALIGNMENT);
1282 #else
1283 char *buf = rhs.start_.base ();
1284 #endif /* ACE_LACKS_CDR_ALIGNMENT */
1286 size_t rd_offset = rhs.start_.rd_ptr () - buf;
1287 size_t wr_offset = rhs.start_.wr_ptr () - buf;
1288 this->start_.rd_ptr (rd_offset);
1289 this->start_.wr_ptr (wr_offset);
1291 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
1292 ACE_NEW (this->monitor_,
1293 ACE::Monitor_Control::Size_Monitor);
1294 this->monitor_->receive (this->start_.total_size ());
1295 #endif /* ACE_HAS_MONITOR_POINTS==1 */
1298 ACE_InputCDR::ACE_InputCDR (ACE_InputCDR::Transfer_Contents x)
1299 : start_ (x.rhs_.start_.data_block ()),
1300 do_byte_swap_ (x.rhs_.do_byte_swap_),
1301 good_bit_ (true),
1302 major_version_ (x.rhs_.major_version_),
1303 minor_version_ (x.rhs_.minor_version_),
1304 char_translator_ (x.rhs_.char_translator_),
1305 wchar_translator_ (x.rhs_.wchar_translator_)
1307 this->start_.rd_ptr (x.rhs_.start_.rd_ptr ());
1308 this->start_.wr_ptr (x.rhs_.start_.wr_ptr ());
1310 ACE_Data_Block* db = this->start_.data_block ()->clone_nocopy ();
1311 (void) x.rhs_.start_.replace_data_block (db);
1313 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
1314 ACE_NEW (this->monitor_,
1315 ACE::Monitor_Control::Size_Monitor);
1316 this->monitor_->receive (this->start_.total_size ());
1317 #endif /* ACE_HAS_MONITOR_POINTS==1 */
1320 ACE_InputCDR&
1321 ACE_InputCDR::operator= (const ACE_InputCDR& rhs)
1323 if (this != &rhs)
1325 this->start_.data_block (rhs.start_.data_block ()->duplicate ());
1326 this->start_.rd_ptr (rhs.start_.rd_ptr ());
1327 this->start_.wr_ptr (rhs.start_.wr_ptr ());
1328 this->do_byte_swap_ = rhs.do_byte_swap_;
1329 this->good_bit_ = true;
1330 this->char_translator_ = rhs.char_translator_;
1331 this->major_version_ = rhs.major_version_;
1332 this->minor_version_ = rhs.minor_version_;
1335 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
1336 this->monitor_->receive (this->start_.total_size ());
1337 #endif /* ACE_HAS_MONITOR_POINTS==1 */
1339 return *this;
1342 ACE_InputCDR::ACE_InputCDR (const ACE_OutputCDR& rhs,
1343 ACE_Allocator* buffer_allocator,
1344 ACE_Allocator* data_block_allocator,
1345 ACE_Allocator* message_block_allocator)
1346 : start_ (rhs.total_length () + ACE_CDR::MAX_ALIGNMENT,
1347 ACE_Message_Block::MB_DATA,
1350 buffer_allocator,
1353 ACE_Time_Value::zero,
1354 ACE_Time_Value::max_time,
1355 data_block_allocator,
1356 message_block_allocator),
1357 do_byte_swap_ (rhs.do_byte_swap_),
1358 good_bit_ (true),
1359 major_version_ (rhs.major_version_),
1360 minor_version_ (rhs.minor_version_),
1361 char_translator_ (rhs.char_translator_),
1362 wchar_translator_ (rhs.wchar_translator_)
1364 ACE_CDR::mb_align (&this->start_);
1365 for (const ACE_Message_Block *i = rhs.begin ();
1366 i != rhs.end ();
1367 i = i->cont ())
1369 this->start_.copy (i->rd_ptr (), i->length ());
1372 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
1373 ACE_NEW (this->monitor_,
1374 ACE::Monitor_Control::Size_Monitor);
1375 this->monitor_->receive (this->start_.total_size ());
1376 #endif /* ACE_HAS_MONITOR_POINTS==1 */
1379 ACE_CDR::Boolean
1380 ACE_InputCDR::skip_wchar ()
1382 if (static_cast<ACE_CDR::Short> (major_version_) == 1
1383 && static_cast<ACE_CDR::Short> (minor_version_) == 2)
1385 ACE_CDR::Octet len;
1386 if (this->read_1 (&len))
1387 return this->skip_bytes (static_cast<size_t> (len));
1389 else
1391 ACE_CDR::WChar x;
1392 void * const temp = &x;
1393 if (ACE_OutputCDR::wchar_maxbytes_ == 2)
1394 return this->read_2 (reinterpret_cast<ACE_CDR::UShort *> (temp));
1395 else
1396 return this->read_4 (reinterpret_cast<ACE_CDR::ULong *> (temp));
1399 return (this->good_bit_ = false);
1402 ACE_CDR::Boolean
1403 ACE_InputCDR::read_wchar (ACE_CDR::WChar& x)
1405 if (this->wchar_translator_ != 0)
1407 this->good_bit_ = this->wchar_translator_->read_wchar (*this,x);
1408 return this->good_bit_;
1410 if (ACE_OutputCDR::wchar_maxbytes_ == 0)
1412 errno = EACCES;
1413 return (this->good_bit_ = false);
1416 if (ACE_OutputCDR::wchar_maxbytes_ == sizeof (ACE_CDR::WChar))
1418 if (static_cast<ACE_CDR::Short> (major_version_) == 1
1419 && static_cast<ACE_CDR::Short> (minor_version_) == 2)
1421 ACE_CDR::Octet len;
1423 if (this->read_1 (&len))
1424 return this->read_array
1425 (reinterpret_cast<ACE_CDR::Octet*> (&x),
1426 static_cast<ACE_CDR::ULong> (len),
1427 ACE_CDR::OCTET_ALIGN,
1430 else
1431 return (this->good_bit_ = false);
1434 void * const temp = &x;
1435 if (sizeof (ACE_CDR::WChar) == 2)
1436 return this->read_2 (reinterpret_cast<ACE_CDR::UShort *> (temp));
1437 else
1438 return this->read_4 (reinterpret_cast<ACE_CDR::ULong *> (temp));
1441 if (static_cast<ACE_CDR::Short> (major_version_) == 1
1442 && static_cast<ACE_CDR::Short> (minor_version_) == 2)
1444 ACE_CDR::Octet len;
1446 if (this->read_1 (&len))
1448 if (len == 2)
1450 ACE_CDR::Short sx;
1451 if (this->read_array
1452 (reinterpret_cast<ACE_CDR::Octet*> (&sx),
1453 static_cast<ACE_CDR::ULong> (len),
1454 ACE_CDR::OCTET_ALIGN,
1457 x = static_cast<ACE_CDR::WChar> (sx);
1458 return true;
1461 else
1463 ACE_CDR::Octet ox;
1464 if (this->read_array
1465 (reinterpret_cast<ACE_CDR::Octet*> (&ox),
1466 static_cast<ACE_CDR::ULong> (len),
1467 ACE_CDR::OCTET_ALIGN,
1470 x = static_cast<ACE_CDR::WChar> (ox);
1471 return true;
1476 else
1478 if (ACE_OutputCDR::wchar_maxbytes_ == 2)
1480 ACE_CDR::UShort sx;
1481 if (this->read_2 (reinterpret_cast<ACE_CDR::UShort *> (&sx)))
1483 x = static_cast<ACE_CDR::WChar> (sx);
1484 return true;
1487 else
1489 ACE_CDR::Octet ox;
1490 if (this->read_1 (&ox))
1492 x = static_cast<ACE_CDR::WChar> (ox);
1493 return true;
1498 return (this->good_bit_ = false);
1501 ACE_CDR::Boolean
1502 ACE_InputCDR::read_string (ACE_CDR::Char *&x)
1504 // @@ This is a slight violation of "Optimize for the common case",
1505 // i.e. normally the translator will be 0, but OTOH the code is
1506 // smaller and should be better for the cache ;-) ;-)
1507 if (this->char_translator_ != 0)
1509 this->good_bit_ = this->char_translator_->read_string (*this, x);
1510 return this->good_bit_;
1513 ACE_CDR::ULong len = 0;
1515 if (!this->read_ulong (len))
1516 return false;
1518 // A check for the length being too great is done later in the
1519 // call to read_char_array but we want to have it done before
1520 // the memory is allocated.
1521 if (len > 0 && len <= this->length())
1523 #if defined (ACE_HAS_ALLOC_HOOKS)
1524 ACE_ALLOCATOR_RETURN (x,
1525 static_cast<ACE_CDR::Char*> (ACE_Allocator::instance()->malloc(sizeof (ACE_CDR::Char) * (len))),
1527 #else
1528 ACE_NEW_RETURN (x,
1529 ACE_CDR::Char[len],
1531 #endif /* ACE_HAS_ALLOC_HOOKS */
1533 std::unique_ptr<ACE_CDR::Char[]> safe_data (x);
1535 if (this->read_char_array (x, len))
1537 (void) safe_data.release ();
1538 return true;
1541 else if (len == 0)
1543 // Convert any null strings to empty strings since empty
1544 // strings can cause crashes. (See bug 58.)
1545 #if defined (ACE_HAS_ALLOC_HOOKS)
1546 ACE_ALLOCATOR_RETURN (x,
1547 static_cast<ACE_CDR::Char*> (ACE_Allocator::instance()->malloc(sizeof (ACE_CDR::Char) * (1))),
1549 #else
1550 ACE_NEW_RETURN (x,
1551 ACE_CDR::Char[1],
1553 #endif /* ACE_HAS_ALLOC_HOOKS */
1555 ACE_OS::strcpy (const_cast<char *&> (x), "");
1556 return true;
1559 x = 0;
1560 return (this->good_bit_ = false);
1563 ACE_CDR::Boolean
1564 ACE_InputCDR::read_string (ACE_CString &x)
1566 ACE_CDR::Char * data = nullptr;
1567 if (this->read_string (data))
1569 std::unique_ptr<ACE_CDR::Char[]> safe_data (data);
1570 x = data;
1571 return true;
1574 x = "";
1575 return (this->good_bit_ = false);
1578 ACE_CDR::Boolean
1579 ACE_InputCDR::read_wstring (ACE_CDR::WChar*& x)
1581 // @@ This is a slight violation of "Optimize for the common case",
1582 // i.e. normally the translator will be 0, but OTOH the code is
1583 // smaller and should be better for the cache ;-) ;-)
1584 if (this->wchar_translator_ != 0)
1586 this->good_bit_ = this->wchar_translator_->read_wstring (*this, x);
1587 return this->good_bit_;
1589 if (ACE_OutputCDR::wchar_maxbytes_ == 0)
1591 errno = EACCES;
1592 return (this->good_bit_ = false);
1595 ACE_CDR::ULong len = 0;
1597 if (!this->read_ulong (len))
1599 return false;
1602 // A check for the length being too great is done later in the
1603 // call to read_char_array but we want to have it done before
1604 // the memory is allocated.
1605 if (len > 0 && len <= this->length ())
1607 std::unique_ptr<ACE_CDR::WChar[]> safe_data;
1609 if (static_cast<ACE_CDR::Short> (this->major_version_) == 1
1610 && static_cast<ACE_CDR::Short> (this->minor_version_) == 2)
1612 len /=
1613 ACE_Utils::truncate_cast<ACE_CDR::ULong> (
1614 ACE_OutputCDR::wchar_maxbytes_);
1616 //allocating one extra for the null character needed by applications
1617 #if defined (ACE_HAS_ALLOC_HOOKS)
1618 ACE_ALLOCATOR_RETURN (x,
1619 static_cast<ACE_CDR::WChar*> (ACE_Allocator::instance()->malloc(sizeof (ACE_CDR::WChar) * (len + 1))),
1621 #else
1622 ACE_NEW_RETURN (x,
1623 ACE_CDR::WChar [len + 1],
1624 false);
1625 #endif /* ACE_HAS_ALLOC_HOOKS */
1627 safe_data.reset (x);
1629 if (this->read_wchar_array (x, len))
1631 //Null character used by applications to find the end of
1632 //the wstring
1633 //Is this okay with the GIOP 1.2 spec??
1634 x[len] = '\x00';
1636 (void) safe_data.release ();
1638 return true;
1641 else
1643 #if defined (ACE_HAS_ALLOC_HOOKS)
1644 ACE_ALLOCATOR_RETURN (x,
1645 static_cast<ACE_CDR::WChar*> (ACE_Allocator::instance()->malloc(sizeof (ACE_CDR::WChar) * (len))),
1647 #else
1648 ACE_NEW_RETURN (x,
1649 ACE_CDR::WChar [len],
1650 false);
1651 #endif /* ACE_HAS_ALLOC_HOOKS */
1653 safe_data.reset (x);
1655 if (this->read_wchar_array (x, len))
1657 (void) safe_data.release ();
1659 return true;
1663 else if (len == 0)
1665 // Convert any null strings to empty strings since empty
1666 // strings can cause crashes. (See bug 58.)
1667 #if defined (ACE_HAS_ALLOC_HOOKS)
1668 ACE_ALLOCATOR_RETURN (x,
1669 static_cast<ACE_CDR::WChar*> (ACE_Allocator::instance()->malloc(sizeof (ACE_CDR::WChar) * (1))),
1671 #else
1672 ACE_NEW_RETURN (x,
1673 ACE_CDR::WChar [1],
1674 false);
1675 #endif /* ACE_HAS_ALLOC_HOOKS */
1677 x[0] = '\x00';
1678 return true;
1681 this->good_bit_ = false;
1682 x = 0;
1683 return false;
1686 // As of C++11 std::string guarantees contiguous memory storage.
1687 // That provides the opportunity to optimize CDR streaming.
1688 ACE_CDR::Boolean
1689 ACE_InputCDR::read_string (std::string& x)
1691 // @@ This is a slight violation of "Optimize for the common case",
1692 // i.e. normally the translator will be 0, but OTOH the code is
1693 // smaller and should be better for the cache ;-) ;-)
1694 if (this->char_translator_ != 0)
1696 this->good_bit_ = this->char_translator_->read_string (*this, x);
1697 return this->good_bit_;
1700 ACE_CDR::ULong len = 0;
1702 if (!this->read_ulong (len))
1703 return false;
1705 // A check for the length being too great is done later in the
1706 // call to read_char_array but we want to have it done before
1707 // the memory is allocated.
1708 if (len > 0 && len <= this->length())
1712 x.resize (len-1); // no need to include the terminating '\0' here
1714 catch (const std::bad_alloc&)
1716 return false;
1719 if (len == 0 || this->read_char_array (&x[0], len-1))
1721 return this->skip_char (); // skip the terminating '\0'
1725 this->good_bit_ = false;
1726 x.clear ();
1727 return false;
1730 #if !defined(ACE_LACKS_STD_WSTRING)
1731 ACE_CDR::Boolean
1732 ACE_InputCDR::read_wstring (std::wstring& x)
1734 // @@ This is a slight violation of "Optimize for the common case",
1735 // i.e. normally the translator will be 0, but OTOH the code is
1736 // smaller and should be better for the cache ;-) ;-)
1737 if (this->wchar_translator_ != 0)
1739 this->good_bit_ = this->wchar_translator_->read_wstring (*this, x);
1740 return this->good_bit_;
1742 if (ACE_OutputCDR::wchar_maxbytes_ == 0)
1744 errno = EACCES;
1745 return (this->good_bit_ = false);
1748 ACE_CDR::ULong len = 0;
1750 if (!this->read_ulong (len))
1752 return false;
1755 // A check for the length being too great is done later in the
1756 // call to read_char_array but we want to have it done before
1757 // the memory is allocated.
1758 if (len > 0 && len <= this->length ())
1760 if (static_cast<ACE_CDR::Short> (this->major_version_) == 1
1761 && static_cast<ACE_CDR::Short> (this->minor_version_) == 2)
1763 len /=
1764 ACE_Utils::truncate_cast<ACE_CDR::ULong> (
1765 ACE_OutputCDR::wchar_maxbytes_);
1769 x.resize (len);
1771 catch (const std::bad_alloc&)
1773 return false;
1776 if (this->read_wchar_array (&x[0], len))
1778 return true;
1781 else
1785 x.resize (len-1); // no need to include the terminating '\0' here
1787 catch (const std::bad_alloc&)
1789 return false;
1792 if (len == 1 || this->read_wchar_array (&x[0], len-1))
1794 return this->skip_wchar (); // skip the terminating '\0'
1798 else if (len == 0)
1800 x.clear ();
1801 return true;
1804 this->good_bit_ = false;
1805 x.clear ();
1806 return false;
1808 #endif
1810 ACE_CDR::Boolean
1811 ACE_InputCDR::read_array (void* x,
1812 size_t size,
1813 size_t align,
1814 ACE_CDR::ULong length)
1816 if (length == 0)
1817 return true;
1818 char* buf = 0;
1820 if (this->adjust (size * length, align, buf) == 0)
1822 #if defined (ACE_DISABLE_SWAP_ON_READ)
1823 ACE_OS::memcpy (x, buf, size*length);
1824 #else
1825 if (!this->do_byte_swap_ || size == 1)
1826 ACE_OS::memcpy (x, buf, size*length);
1827 else
1829 char *target = reinterpret_cast<char*> (x);
1830 switch (size)
1832 case 2:
1833 ACE_CDR::swap_2_array (buf, target, length);
1834 break;
1835 case 4:
1836 ACE_CDR::swap_4_array (buf, target, length);
1837 break;
1838 case 8:
1839 ACE_CDR::swap_8_array (buf, target, length);
1840 break;
1841 case 16:
1842 ACE_CDR::swap_16_array (buf, target, length);
1843 break;
1844 default:
1845 // TODO: print something?
1846 this->good_bit_ = false;
1847 return false;
1850 #endif /* ACE_DISABLE_SWAP_ON_READ */
1851 return this->good_bit_;
1853 return false;
1856 ACE_CDR::Boolean
1857 ACE_InputCDR::read_wchar_array_i (ACE_CDR::WChar* x,
1858 ACE_CDR::ULong length)
1860 if (length == 0)
1861 return true;
1862 char* buf = 0;
1863 size_t const align = (ACE_OutputCDR::wchar_maxbytes_ == 2) ?
1864 ACE_CDR::SHORT_ALIGN :
1865 ACE_CDR::OCTET_ALIGN;
1867 if (this->adjust (ACE_OutputCDR::wchar_maxbytes_ * length, align, buf) == 0)
1869 if (ACE_OutputCDR::wchar_maxbytes_ == 2)
1871 ACE_CDR::UShort *sb = reinterpret_cast<ACE_CDR::UShort *> (buf);
1872 for (size_t i = 0; i < length; ++i)
1873 #if defined (ACE_DISABLE_SWAP_ON_READ)
1874 x[i] = static_cast<ACE_CDR::WChar> (sb[i]);
1875 #else
1876 if (!this->do_byte_swap_)
1877 x[i] = static_cast<ACE_CDR::WChar> (sb[i]);
1878 else
1880 ACE_CDR::UShort sx;
1881 ACE_CDR::swap_2 (&buf[i * 2], reinterpret_cast<char *> (&sx));
1882 x[i] = static_cast<ACE_CDR::WChar> (sx);
1884 #endif /* ACE_DISABLE_SWAP_ON_READ */
1886 else
1888 for (size_t i = 0; i < length; ++i)
1889 x[i] = static_cast<ACE_CDR::Octet> (buf[i]);
1891 return this->good_bit_;
1893 return false;
1897 ACE_CDR::Boolean
1898 ACE_InputCDR::read_boolean_array (ACE_CDR::Boolean *x,
1899 ACE_CDR::ULong length)
1901 // Make sure the length of the array isn't greater than the length of
1902 // the stream.
1903 if (length > this->length ())
1905 this->good_bit_ = false;
1906 return false;
1909 // It is hard to optimize this, the spec requires that on the wire
1910 // booleans be represented as a byte with value 0 or 1, but in
1911 // memory it is possible (though very unlikely) that a boolean has
1912 // a non-zero value (different from 1).
1913 // We resort to a simple loop.
1914 for (ACE_CDR::ULong i = 0; i != length && this->good_bit_; ++i)
1915 (void) this->read_boolean (x[i]);
1917 return this->good_bit_;
1920 ACE_CDR::Boolean
1921 ACE_InputCDR::read_1 (ACE_CDR::Octet *x)
1923 if (this->rd_ptr () < this->wr_ptr ())
1925 *x = *reinterpret_cast<ACE_CDR::Octet*> (this->rd_ptr ());
1926 this->start_.rd_ptr (1);
1927 return true;
1930 this->good_bit_ = false;
1931 return false;
1934 ACE_CDR::Boolean
1935 ACE_InputCDR::read_2 (ACE_CDR::UShort *x)
1937 char *buf = 0;
1938 if (this->adjust (ACE_CDR::SHORT_SIZE, buf) == 0)
1940 #if !defined (ACE_DISABLE_SWAP_ON_READ)
1941 if (!this->do_byte_swap_)
1942 *x = *reinterpret_cast<ACE_CDR::UShort*> (buf);
1943 else
1944 ACE_CDR::swap_2 (buf, reinterpret_cast<char*> (x));
1945 #else
1946 *x = *reinterpret_cast<ACE_CDR::UShort*> (buf);
1947 #endif /* ACE_DISABLE_SWAP_ON_READ */
1948 return true;
1950 this->good_bit_ = false;
1951 return false;
1954 ACE_CDR::Boolean
1955 ACE_InputCDR::read_4 (ACE_CDR::ULong *x)
1957 char *buf = 0;
1958 if (this->adjust (ACE_CDR::LONG_SIZE, buf) == 0)
1960 #if !defined (ACE_DISABLE_SWAP_ON_READ)
1961 if (!this->do_byte_swap_)
1962 *x = *reinterpret_cast<ACE_CDR::ULong*> (buf);
1963 else
1964 ACE_CDR::swap_4 (buf, reinterpret_cast<char*> (x));
1965 #else
1966 *x = *reinterpret_cast<ACE_CDR::ULong*> (buf);
1967 #endif /* ACE_DISABLE_SWAP_ON_READ */
1968 return true;
1970 this->good_bit_ = false;
1971 return false;
1974 ACE_CDR::Boolean
1975 ACE_InputCDR::read_8 (ACE_CDR::ULongLong *x)
1977 char *buf = 0;
1979 if (this->adjust (ACE_CDR::LONGLONG_SIZE, buf) == 0)
1981 #if !defined (ACE_DISABLE_SWAP_ON_READ)
1982 if (!this->do_byte_swap_)
1983 *x = *reinterpret_cast<ACE_CDR::ULongLong *> (buf);
1984 else
1985 ACE_CDR::swap_8 (buf, reinterpret_cast<char *> (x));
1986 #else
1987 *x = *reinterpret_cast<ACE_CDR::ULongLong *> (buf);
1988 #endif /* ACE_DISABLE_SWAP_ON_READ */
1989 return true;
1992 this->good_bit_ = false;
1993 return false;
1996 ACE_CDR::Boolean
1997 ACE_InputCDR::read_16 (ACE_CDR::LongDouble *x)
1999 char *buf = 0;
2000 if (this->adjust (ACE_CDR::LONGDOUBLE_SIZE,
2001 ACE_CDR::LONGDOUBLE_ALIGN,
2002 buf) == 0)
2004 #if !defined (ACE_DISABLE_SWAP_ON_READ)
2005 if (!this->do_byte_swap_)
2006 *x = *reinterpret_cast<ACE_CDR::LongDouble *> (buf);
2007 else
2008 ACE_CDR::swap_16 (buf, reinterpret_cast<char*> (x));
2009 #else
2010 *x = *reinterpret_cast<ACE_CDR::LongDouble*> (buf);
2011 #endif /* ACE_DISABLE_SWAP_ON_READ */
2012 return true;
2015 this->good_bit_ = false;
2016 return false;
2019 ACE_CDR::Boolean
2020 ACE_InputCDR::skip_string ()
2022 ACE_CDR::ULong len = 0;
2023 if (this->read_ulong (len))
2025 if (static_cast<ACE_CDR::ULong> (~0u) == len)
2027 // Indirection, next Long in stream is signed offset to actual
2028 // string location (backwards in same stream from here).
2029 ACE_CDR::Long offset = 0;
2030 if (this->read_long (offset))
2032 return true;
2035 else if (this->rd_ptr () + len <= this->wr_ptr ())
2037 this->rd_ptr (len);
2038 return true;
2040 this->good_bit_ = false;
2042 return false;
2045 ACE_CDR::Boolean
2046 ACE_InputCDR::skip_wstring ()
2048 ACE_CDR::ULong len = 0;
2049 ACE_CDR::Boolean continue_skipping = read_ulong (len);
2051 if (continue_skipping && len != 0)
2053 if (static_cast<ACE_CDR::Short> (this->major_version_) == 1
2054 && static_cast<ACE_CDR::Short> (this->minor_version_) == 2)
2055 continue_skipping = this->skip_bytes ((size_t)len);
2056 else
2057 while (continue_skipping && len--)
2058 continue_skipping = this->skip_wchar ();
2060 return continue_skipping;
2063 ACE_CDR::Boolean
2064 ACE_InputCDR::skip_bytes (size_t len)
2066 if (this->rd_ptr () + len <= this->wr_ptr ())
2068 this->rd_ptr (len);
2069 return true;
2071 this->good_bit_ = false;
2072 return false;
2076 ACE_InputCDR::grow (size_t newsize)
2078 if (ACE_CDR::grow (&this->start_, newsize) == -1)
2079 return -1;
2081 ACE_CDR::mb_align (&this->start_);
2082 this->start_.wr_ptr (newsize);
2084 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
2085 if (newsize > this->start_.total_size ())
2087 this->monitor_->receive (newsize);
2089 #endif /* ACE_HAS_MONITOR_POINTS==1 */
2091 return 0;
2094 void
2095 ACE_InputCDR::reset (const ACE_Message_Block* data,
2096 int byte_order)
2098 this->reset_byte_order (byte_order);
2099 ACE_CDR::consolidate (&this->start_, data);
2101 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
2102 this->monitor_->receive (this->start_.total_size ());
2103 #endif /* ACE_HAS_MONITOR_POINTS==1 */
2106 void
2107 ACE_InputCDR::steal_from (ACE_InputCDR &cdr)
2109 this->do_byte_swap_ = cdr.do_byte_swap_;
2110 this->start_.data_block (cdr.start_.data_block ()->duplicate ());
2112 // If the message block had a DONT_DELETE flags, just clear it off..
2113 this->start_.clr_self_flags (ACE_Message_Block::DONT_DELETE);
2114 this->start_.rd_ptr (cdr.start_.rd_ptr ());
2116 this->start_.wr_ptr (cdr.start_.wr_ptr ());
2117 this->major_version_ = cdr.major_version_;
2118 this->minor_version_ = cdr.minor_version_;
2119 cdr.reset_contents ();
2121 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
2122 this->monitor_->receive (this->start_.total_size ());
2123 #endif /* ACE_HAS_MONITOR_POINTS==1 */
2126 void
2127 ACE_InputCDR::exchange_data_blocks (ACE_InputCDR &cdr)
2129 // Exchange byte orders
2130 int const byte_order = cdr.do_byte_swap_;
2131 cdr.do_byte_swap_ = this->do_byte_swap_;
2132 this->do_byte_swap_ = byte_order;
2134 // Get the destination read and write pointers
2135 size_t const drd_pos =
2136 cdr.start_.rd_ptr () - cdr.start_.base ();
2137 size_t const dwr_pos =
2138 cdr.start_.wr_ptr () - cdr.start_.base ();
2140 // Get the source read & write pointers
2141 size_t const srd_pos =
2142 this->start_.rd_ptr () - this->start_.base ();
2143 size_t const swr_pos =
2144 this->start_.wr_ptr () - this->start_.base ();
2146 // Exchange data_blocks. Dont release any of the data blocks.
2147 ACE_Data_Block *dnb =
2148 this->start_.replace_data_block (cdr.start_.data_block ());
2149 cdr.start_.replace_data_block (dnb);
2151 // Exchange the flags information..
2152 ACE_Message_Block::Message_Flags df = cdr.start_.self_flags ();
2153 ACE_Message_Block::Message_Flags sf = this->start_.self_flags ();
2155 cdr.start_.clr_self_flags (df);
2156 this->start_.clr_self_flags (sf);
2158 cdr.start_.set_self_flags (sf);
2159 this->start_.set_self_flags (df);
2161 // Reset the <cdr> pointers to zero before it is set again.
2162 cdr.start_.reset ();
2163 this->start_.reset ();
2165 // Set the read and write pointers.
2166 if (cdr.start_.size () >= srd_pos)
2168 cdr.start_.rd_ptr (srd_pos);
2171 if (cdr.start_.size () >= swr_pos)
2173 cdr.start_.wr_ptr (swr_pos);
2176 if (this->start_.size () >= drd_pos)
2178 this->start_.rd_ptr (drd_pos);
2181 if (this->start_.size () >= dwr_pos)
2183 this->start_.wr_ptr (dwr_pos);
2186 ACE_CDR::Octet const dmajor = cdr.major_version_;
2187 ACE_CDR::Octet const dminor = cdr.minor_version_;
2189 // Exchange the GIOP version info
2190 cdr.major_version_ = this->major_version_;
2191 cdr.minor_version_ = this->minor_version_;
2193 this->major_version_ = dmajor;
2194 this->minor_version_ = dminor;
2196 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
2197 this->monitor_->receive (this->start_.total_size ());
2198 #endif /* ACE_HAS_MONITOR_POINTS==1 */
2201 ACE_Data_Block *
2202 ACE_InputCDR::clone_from (ACE_InputCDR &cdr)
2204 this->do_byte_swap_ = cdr.do_byte_swap_;
2206 // Get the read & write pointer positions in the incoming CDR
2207 // streams
2208 char *rd_ptr = cdr.start_.rd_ptr ();
2209 char *wr_ptr = cdr.start_.wr_ptr ();
2211 // Now reset the incoming CDR stream
2212 cdr.start_.reset ();
2214 // As we have reset the stream, try to align the underlying message
2215 // block in the incoming stream
2216 ACE_CDR::mb_align (&cdr.start_);
2218 // Get the read & write pointer positions again
2219 char *nrd_ptr = cdr.start_.rd_ptr ();
2220 char *nwr_ptr = cdr.start_.wr_ptr ();
2222 // Actual length of the stream is..
2223 // @todo: This will look idiotic, but we dont seem to have much of a
2224 // choice. How do we calculate the length of the incoming stream?
2225 // Calling the method before calling reset () would give us the
2226 // wrong length of the stream that needs copying. So we do the
2227 // calulation like this
2228 // (1) We get the <rd_ptr> and <wr_ptr> positions of the incoming
2229 // stream.
2230 // (2) Then we reset the <incoming> stream and then align it.
2231 // (3) We get the <rd_ptr> and <wr_ptr> positions again. (Points #1
2232 // thru #3 has been done already)
2233 // (4) The difference in the <rd_ptr> and <wr_ptr> positions gives
2234 // us the following, the actual bytes traversed by the <rd_ptr> and
2235 // <wr_ptr>.
2236 // (5) The bytes traversed by the <wr_ptr> is the actual length of
2237 // the stream.
2239 // Actual bytes traversed
2240 size_t rd_bytes = rd_ptr - nrd_ptr;
2241 size_t wr_bytes = wr_ptr - nwr_ptr;
2243 ACE_CDR::mb_align (&this->start_);
2245 ACE_Data_Block *db = this->start_.data_block ();
2247 // If the size of the data that needs to be copied are higher than
2248 // what is available, then do a reallocation.
2249 if (wr_bytes > (this->start_.size () - ACE_CDR::MAX_ALIGNMENT))
2251 // @@NOTE: We need to probably add another method to the message
2252 // block interface to simplify this
2253 db = cdr.start_.data_block ()->clone_nocopy ();
2255 if (db == 0 || db->size ((wr_bytes) +
2256 ACE_CDR::MAX_ALIGNMENT) == -1)
2257 return 0;
2259 // Replace our data block by using the incoming CDR stream.
2260 db = this->start_.replace_data_block (db);
2262 // Align the start_ message block.
2263 ACE_CDR::mb_align (&this->start_);
2265 // Clear the DONT_DELETE flag if it has been set
2266 this->start_.clr_self_flags (ACE_Message_Block::DONT_DELETE);
2269 // Now do the copy
2270 (void) ACE_OS::memcpy (this->start_.wr_ptr (),
2271 cdr.start_.rd_ptr (),
2272 wr_bytes);
2274 // Set the read pointer position to the same point as that was in
2275 // <incoming> cdr.
2276 this->start_.rd_ptr (rd_bytes);
2277 this->start_.wr_ptr (wr_bytes);
2279 // We have changed the read & write pointers for the incoming
2280 // stream. Set them back to the positions that they were before..
2281 cdr.start_.rd_ptr (rd_bytes);
2282 cdr.start_.wr_ptr (wr_bytes);
2284 this->major_version_ = cdr.major_version_;
2285 this->minor_version_ = cdr.minor_version_;
2287 // Copy the char/wchar translators
2288 this->char_translator_ = cdr.char_translator_;
2289 this->wchar_translator_ = cdr.wchar_translator_;
2291 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
2292 this->monitor_->receive (this->start_.total_size ());
2293 #endif /* ACE_HAS_MONITOR_POINTS==1 */
2295 return db;
2298 ACE_Message_Block*
2299 ACE_InputCDR::steal_contents ()
2301 ACE_Message_Block* block = this->start_.clone ();
2302 this->start_.data_block (block->data_block ()->clone ());
2304 // If at all our message had a DONT_DELETE flag set, just clear it
2305 // off.
2306 this->start_.clr_self_flags (ACE_Message_Block::DONT_DELETE);
2308 ACE_CDR::mb_align (&this->start_);
2310 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
2311 this->monitor_->receive (this->start_.total_size ());
2312 #endif /* ACE_HAS_MONITOR_POINTS==1 */
2314 return block;
2317 void
2318 ACE_InputCDR::reset_contents ()
2320 this->start_.data_block (this->start_.data_block ()->clone_nocopy ());
2322 // Reset the flags...
2323 this->start_.clr_self_flags (ACE_Message_Block::DONT_DELETE);
2325 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
2326 this->monitor_->receive (this->start_.total_size ());
2327 #endif /* ACE_HAS_MONITOR_POINTS==1 */
2330 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
2332 void
2333 ACE_InputCDR::register_monitor (const char *id)
2335 this->monitor_->name (id);
2336 this->monitor_->add_to_registry ();
2339 void
2340 ACE_InputCDR::unregister_monitor ()
2342 this->monitor_->remove_from_registry ();
2345 #endif /* ACE_HAS_MONITOR_POINTS==1 */
2347 // --------------------------------------------------------------
2349 ACE_CDR::Boolean
2350 ACE_Char_Codeset_Translator::read_string (ACE_InputCDR &cdr,
2351 std::string &x)
2353 ACE_CDR::Char *buf = 0;
2354 ACE_CDR::Boolean const marshal_flag = this->read_string (cdr, buf);
2355 x.assign (buf);
2356 ACE::strdelete (buf);
2357 return marshal_flag;
2360 // --------------------------------------------------------------
2362 #if !defined(ACE_LACKS_STD_WSTRING)
2363 ACE_CDR::Boolean
2364 ACE_WChar_Codeset_Translator::read_wstring (ACE_InputCDR &cdr,
2365 std::wstring &x)
2367 ACE_CDR::WChar *buf = 0;
2368 ACE_CDR::Boolean const marshal_flag = this->read_wstring (cdr, buf);
2369 x.assign (buf);
2370 ACE::strdelete (buf);
2371 return marshal_flag;
2373 #endif
2375 // --------------------------------------------------------------
2377 ACE_CDR::Boolean
2378 operator<< (ACE_OutputCDR &os, const ACE_CString &x)
2380 os.write_string (x);
2381 return os.good_bit ();
2384 ACE_CDR::Boolean
2385 operator>> (ACE_InputCDR &is, ACE_CString &x)
2387 is.read_string (x);
2388 return is.good_bit ();
2391 #if defined (GEN_OSTREAM_OPS)
2393 std::ostream&
2394 operator<< (std::ostream &os, ACE_OutputCDR::from_boolean x)
2396 return (x.val_ ? os << "true" : os << "false");
2399 std::ostream&
2400 operator<< (std::ostream &os, ACE_OutputCDR::from_char x)
2402 return os << '\'' << x.val_ << '\'';
2405 std::ostream&
2406 operator<< (std::ostream &os, ACE_OutputCDR::from_wchar x)
2408 os.setf (ios_base::showbase);
2409 os.setf (ios_base::hex, ios_base::basefield);
2410 os << x.val_;
2411 os.unsetf (ios_base::showbase);
2412 os.setf (ios_base::dec, ios_base::basefield);
2413 return os;
2416 std::ostream&
2417 operator<< (std::ostream &os, ACE_OutputCDR::from_octet x)
2419 // Same format (hex) and no risk of overflow.
2420 ACE_CDR::WChar w = static_cast<ACE_CDR::WChar> (x.val_);
2421 ACE_OutputCDR::from_wchar tmp (w);
2422 return os << tmp;
2425 #endif /* GEN_OSTREAM_OPS */
2427 ACE_END_VERSIONED_NAMESPACE_DECL