Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / protocols / ace / INet / BidirStreamBuffer.cpp
blobfe6850aedb7493ff727cc0aa43215dc7fecbefc4
1 #ifndef ACE_IOS_BIDIR_STREAM_BUFFER_CPP
2 #define ACE_IOS_BIDIR_STREAM_BUFFER_CPP
4 #include "ace/INet/BidirStreamBuffer.h"
5 #include "ace/OS_Memory.h"
6 #include "ace/OS_NS_string.h"
8 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
10 namespace ACE
12 namespace IOS
14 template <class ACE_CHAR_T, class STREAM_HANDLER, class TR>
15 BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::BasicBidirStreamBuffer (
16 STREAM_HANDLER* sh,
17 std::streamsize bufsz,
18 openmode mode)
19 : bufsize_ (bufsz),
20 mode_ (mode),
21 stream_ (sh),
22 interceptor_ (0)
24 this->stream_->add_reference ();
26 char_type* p = 0;
27 ACE_NEW_NORETURN (p, char_type [bufsz]);
28 this->read_buffer_.reset (p);
29 p = 0;
30 ACE_NEW_NORETURN (p, char_type [bufsz]);
31 this->write_buffer_.reset (p);
33 this->reset_buffers ();
36 template <class ACE_CHAR_T, class STREAM_HANDLER, class TR>
37 BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::~BasicBidirStreamBuffer ()
39 this->close_stream ();
42 template <class ACE_CHAR_T, class STREAM_HANDLER, class TR>
43 typename BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::int_type
44 BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::overflow (int_type c)
46 if (!(this->mode_ & ios_type::out)) return char_traits::eof ();
48 if (c != char_traits::eof ())
50 *this->pptr () = char_traits::to_char_type (c);
51 this->pbump (1);
53 if (this->flush_buffer () == std::streamsize (-1)) return char_traits::eof ();
55 return c;
58 template <class ACE_CHAR_T, class STREAM_HANDLER, class TR>
59 typename BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::int_type
60 BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::underflow ()
62 if (!(this->mode_ & ios_type::in)) return char_traits::eof ();
64 if (this->gptr () && (this->gptr () < this->egptr ()))
65 return char_traits::to_int_type (*this->gptr ());
67 int putback = int (this->gptr () - this->eback ());
68 if (putback > 4) putback = 4;
70 ACE_OS::memmove (this->read_buffer_.get () + (4 - putback),
71 this->gptr () - putback,
72 putback * sizeof (char_type));
74 if (this->interceptor_)
75 this->interceptor_->before_read (this->bufsize_ - 4);
77 int n = this->read_from_stream (this->read_buffer_.get () + 4,
78 this->bufsize_ - 4);
80 if (this->interceptor_)
81 this->interceptor_->after_read (this->read_buffer_.get () + 4, n);
83 if (n <= 0)
85 if (this->interceptor_)
86 this->interceptor_->on_eof ();
88 return char_traits::eof ();
91 this->setg (this->read_buffer_.get () + (4 - putback),
92 this->read_buffer_.get () + 4,
93 this->read_buffer_.get () + 4 + n);
95 // return next character
96 return char_traits::to_int_type (*this->gptr ());
99 template <class ACE_CHAR_T, class STREAM_HANDLER, class TR>
101 BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::sync ()
103 if (this->pptr () && this->pptr () > this->pbase ())
105 if (this->flush_buffer () == -1) return -1;
107 return 0;
110 template <class ACE_CHAR_T, class STREAM_HANDLER, class TR>
111 const STREAM_HANDLER&
112 BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::stream () const
114 return *this->stream_;
117 template <class ACE_CHAR_T, class STREAM_HANDLER, class TR>
118 void
119 BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::close_stream ()
121 if (this->stream_ != 0)
123 ACE_Errno_Guard eguard (errno);
124 this->stream_->remove_reference ();
125 this->stream_ = 0;
129 template <class ACE_CHAR_T, class STREAM_HANDLER, class TR>
130 void
131 BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::set_interceptor (interceptor_type& interceptor)
133 this->interceptor_ = &interceptor;
136 template <class ACE_CHAR_T, class STREAM_HANDLER, class TR>
137 void
138 BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::set_mode (openmode mode)
140 this->mode_ = mode;
143 template <class ACE_CHAR_T, class STREAM_HANDLER, class TR>
144 typename BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::openmode
145 BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::get_mode () const
147 return this->mode_;
150 template <class ACE_CHAR_T, class STREAM_HANDLER, class TR>
151 void
152 BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::reset_buffers()
154 this->setg (this->read_buffer_.get () + 4,
155 this->read_buffer_.get () + 4,
156 this->read_buffer_.get () + 4);
157 this->setp (this->write_buffer_.get (),
158 this->write_buffer_.get () + (this->bufsize_ - 1));
161 template <class ACE_CHAR_T, class STREAM_HANDLER, class TR>
163 BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::read_from_stream (char_type* buffer, std::streamsize length)
165 return this->stream_ == 0 ? 0 : this->stream_->read_from_stream (buffer, length, sizeof(char_type));
168 template <class ACE_CHAR_T, class STREAM_HANDLER, class TR>
170 BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::write_to_stream (const char_type* buffer, std::streamsize length)
172 return this->stream_ == 0 ? 0 : this->stream_->write_to_stream (buffer, length, sizeof(char_type));
175 template <class ACE_CHAR_T, class STREAM_HANDLER, class TR>
177 BasicBidirStreamBuffer<ACE_CHAR_T, STREAM_HANDLER, TR>::flush_buffer ()
179 int n = int (this->pptr () - this->pbase ());
181 if (this->interceptor_)
182 this->interceptor_->before_write (this->pbase (), n);
184 int n_out = this->write_to_stream (this->pbase (), n);
186 if (this->interceptor_)
187 this->interceptor_->after_write (n_out);
189 if (n_out == n)
191 this->pbump (-n);
192 return n;
194 return -1;
197 template <class STREAM_HANDLER>
198 BidirStreamBuffer<STREAM_HANDLER>::BidirStreamBuffer (
199 STREAM_HANDLER* sh,
200 std::streamsize bufsz,
201 openmode mode)
202 : BasicBidirStreamBuffer<char, STREAM_HANDLER> (sh, bufsz, mode)
206 template <class STREAM_HANDLER>
207 BidirStreamBuffer<STREAM_HANDLER>::~BidirStreamBuffer ()
214 ACE_END_VERSIONED_NAMESPACE_DECL
216 #endif /* ACE_IOS_BIDIR_STREAM_BUFFER_CPP */