Skip to content

Commit 08195f3

Browse files
authored
[libc++] Inline basic_streambuf functions (llvm#123379)
Most of the `basic_streambuf` functions are really simple, which makes most of the implementation when they are out of line boilerplate.
1 parent 7fb97be commit 08195f3

File tree

1 file changed

+97
-159
lines changed

1 file changed

+97
-159
lines changed

libcxx/include/streambuf

Lines changed: 97 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public:
146146
static_assert(is_same<_CharT, typename traits_type::char_type>::value,
147147
"traits_type::char_type must be the same type as CharT");
148148

149-
virtual ~basic_streambuf();
149+
virtual ~basic_streambuf() {}
150150

151151
// 27.6.2.2.1 locales:
152152
inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 locale pubimbue(const locale& __loc) {
@@ -229,10 +229,36 @@ public:
229229
}
230230

231231
protected:
232-
basic_streambuf();
233-
basic_streambuf(const basic_streambuf& __rhs);
234-
basic_streambuf& operator=(const basic_streambuf& __rhs);
235-
void swap(basic_streambuf& __rhs);
232+
basic_streambuf() {}
233+
basic_streambuf(const basic_streambuf& __sb)
234+
: __loc_(__sb.__loc_),
235+
__binp_(__sb.__binp_),
236+
__ninp_(__sb.__ninp_),
237+
__einp_(__sb.__einp_),
238+
__bout_(__sb.__bout_),
239+
__nout_(__sb.__nout_),
240+
__eout_(__sb.__eout_) {}
241+
242+
basic_streambuf& operator=(const basic_streambuf& __sb) {
243+
__loc_ = __sb.__loc_;
244+
__binp_ = __sb.__binp_;
245+
__ninp_ = __sb.__ninp_;
246+
__einp_ = __sb.__einp_;
247+
__bout_ = __sb.__bout_;
248+
__nout_ = __sb.__nout_;
249+
__eout_ = __sb.__eout_;
250+
return *this;
251+
}
252+
253+
void swap(basic_streambuf& __sb) {
254+
std::swap(__loc_, __sb.__loc_);
255+
std::swap(__binp_, __sb.__binp_);
256+
std::swap(__ninp_, __sb.__ninp_);
257+
std::swap(__einp_, __sb.__einp_);
258+
std::swap(__bout_, __sb.__bout_);
259+
std::swap(__nout_, __sb.__nout_);
260+
std::swap(__eout_, __sb.__eout_);
261+
}
236262

237263
// 27.6.2.3.2 Get area:
238264
_LIBCPP_HIDE_FROM_ABI char_type* eback() const { return __binp_; }
@@ -267,173 +293,85 @@ protected:
267293

268294
// 27.6.2.4 virtual functions:
269295
// 27.6.2.4.1 Locales:
270-
virtual void imbue(const locale& __loc);
296+
virtual void imbue(const locale&) {}
271297

272298
// 27.6.2.4.2 Buffer management and positioning:
273-
virtual basic_streambuf* setbuf(char_type* __s, streamsize __n);
274-
virtual pos_type
275-
seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __which = ios_base::in | ios_base::out);
276-
virtual pos_type seekpos(pos_type __sp, ios_base::openmode __which = ios_base::in | ios_base::out);
277-
virtual int sync();
299+
virtual basic_streambuf* setbuf(char_type*, streamsize) { return this; }
300+
virtual pos_type seekoff(off_type, ios_base::seekdir, ios_base::openmode = ios_base::in | ios_base::out) {
301+
return pos_type(off_type(-1));
302+
}
303+
virtual pos_type seekpos(pos_type, ios_base::openmode = ios_base::in | ios_base::out) {
304+
return pos_type(off_type(-1));
305+
}
306+
virtual int sync() { return 0; }
278307

279308
// 27.6.2.4.3 Get area:
280-
virtual streamsize showmanyc();
281-
virtual streamsize xsgetn(char_type* __s, streamsize __n);
282-
virtual int_type underflow();
283-
virtual int_type uflow();
309+
virtual streamsize showmanyc() { return 0; }
310+
311+
virtual streamsize xsgetn(char_type* __s, streamsize __n) {
312+
const int_type __eof = traits_type::eof();
313+
int_type __c;
314+
streamsize __i = 0;
315+
while (__i < __n) {
316+
if (__ninp_ < __einp_) {
317+
const streamsize __len = std::min(static_cast<streamsize>(INT_MAX), std::min(__einp_ - __ninp_, __n - __i));
318+
traits_type::copy(__s, __ninp_, __len);
319+
__s += __len;
320+
__i += __len;
321+
this->gbump(__len);
322+
} else if ((__c = uflow()) != __eof) {
323+
*__s = traits_type::to_char_type(__c);
324+
++__s;
325+
++__i;
326+
} else
327+
break;
328+
}
329+
return __i;
330+
}
331+
332+
virtual int_type underflow() { return traits_type::eof(); }
333+
virtual int_type uflow() {
334+
if (underflow() == traits_type::eof())
335+
return traits_type::eof();
336+
return traits_type::to_int_type(*__ninp_++);
337+
}
284338

285339
// 27.6.2.4.4 Putback:
286-
virtual int_type pbackfail(int_type __c = traits_type::eof());
340+
virtual int_type pbackfail(int_type = traits_type::eof()) { return traits_type::eof(); }
287341

288342
// 27.6.2.4.5 Put area:
289-
virtual streamsize xsputn(const char_type* __s, streamsize __n);
290-
virtual int_type overflow(int_type __c = traits_type::eof());
343+
virtual streamsize xsputn(const char_type* __s, streamsize __n) {
344+
streamsize __i = 0;
345+
int_type __eof = traits_type::eof();
346+
while (__i < __n) {
347+
if (__nout_ >= __eout_) {
348+
if (overflow(traits_type::to_int_type(*__s)) == __eof)
349+
break;
350+
++__s;
351+
++__i;
352+
} else {
353+
streamsize __chunk_size = std::min(__eout_ - __nout_, __n - __i);
354+
traits_type::copy(__nout_, __s, __chunk_size);
355+
__nout_ += __chunk_size;
356+
__s += __chunk_size;
357+
__i += __chunk_size;
358+
}
359+
}
360+
return __i;
361+
}
362+
363+
virtual int_type overflow(int_type = traits_type::eof()) { return traits_type::eof(); }
291364

292365
private:
293366
locale __loc_;
294-
char_type* __binp_;
295-
char_type* __ninp_;
296-
char_type* __einp_;
297-
char_type* __bout_;
298-
char_type* __nout_;
299-
char_type* __eout_;
367+
char_type* __binp_ = nullptr;
368+
char_type* __ninp_ = nullptr;
369+
char_type* __einp_ = nullptr;
370+
char_type* __bout_ = nullptr;
371+
char_type* __nout_ = nullptr;
372+
char_type* __eout_ = nullptr;
300373
};
301374

302-
template <class _CharT, class _Traits>
303-
basic_streambuf<_CharT, _Traits>::~basic_streambuf() {}
304-
305-
template <class _CharT, class _Traits>
306-
basic_streambuf<_CharT, _Traits>::basic_streambuf()
307-
: __binp_(nullptr), __ninp_(nullptr), __einp_(nullptr), __bout_(nullptr), __nout_(nullptr), __eout_(nullptr) {}
308-
309-
template <class _CharT, class _Traits>
310-
basic_streambuf<_CharT, _Traits>::basic_streambuf(const basic_streambuf& __sb)
311-
: __loc_(__sb.__loc_),
312-
__binp_(__sb.__binp_),
313-
__ninp_(__sb.__ninp_),
314-
__einp_(__sb.__einp_),
315-
__bout_(__sb.__bout_),
316-
__nout_(__sb.__nout_),
317-
__eout_(__sb.__eout_) {}
318-
319-
template <class _CharT, class _Traits>
320-
basic_streambuf<_CharT, _Traits>& basic_streambuf<_CharT, _Traits>::operator=(const basic_streambuf& __sb) {
321-
__loc_ = __sb.__loc_;
322-
__binp_ = __sb.__binp_;
323-
__ninp_ = __sb.__ninp_;
324-
__einp_ = __sb.__einp_;
325-
__bout_ = __sb.__bout_;
326-
__nout_ = __sb.__nout_;
327-
__eout_ = __sb.__eout_;
328-
return *this;
329-
}
330-
331-
template <class _CharT, class _Traits>
332-
void basic_streambuf<_CharT, _Traits>::swap(basic_streambuf& __sb) {
333-
std::swap(__loc_, __sb.__loc_);
334-
std::swap(__binp_, __sb.__binp_);
335-
std::swap(__ninp_, __sb.__ninp_);
336-
std::swap(__einp_, __sb.__einp_);
337-
std::swap(__bout_, __sb.__bout_);
338-
std::swap(__nout_, __sb.__nout_);
339-
std::swap(__eout_, __sb.__eout_);
340-
}
341-
342-
template <class _CharT, class _Traits>
343-
void basic_streambuf<_CharT, _Traits>::imbue(const locale&) {}
344-
345-
template <class _CharT, class _Traits>
346-
basic_streambuf<_CharT, _Traits>* basic_streambuf<_CharT, _Traits>::setbuf(char_type*, streamsize) {
347-
return this;
348-
}
349-
350-
template <class _CharT, class _Traits>
351-
typename basic_streambuf<_CharT, _Traits>::pos_type
352-
basic_streambuf<_CharT, _Traits>::seekoff(off_type, ios_base::seekdir, ios_base::openmode) {
353-
return pos_type(off_type(-1));
354-
}
355-
356-
template <class _CharT, class _Traits>
357-
typename basic_streambuf<_CharT, _Traits>::pos_type
358-
basic_streambuf<_CharT, _Traits>::seekpos(pos_type, ios_base::openmode) {
359-
return pos_type(off_type(-1));
360-
}
361-
362-
template <class _CharT, class _Traits>
363-
int basic_streambuf<_CharT, _Traits>::sync() {
364-
return 0;
365-
}
366-
367-
template <class _CharT, class _Traits>
368-
streamsize basic_streambuf<_CharT, _Traits>::showmanyc() {
369-
return 0;
370-
}
371-
372-
template <class _CharT, class _Traits>
373-
streamsize basic_streambuf<_CharT, _Traits>::xsgetn(char_type* __s, streamsize __n) {
374-
const int_type __eof = traits_type::eof();
375-
int_type __c;
376-
streamsize __i = 0;
377-
while (__i < __n) {
378-
if (__ninp_ < __einp_) {
379-
const streamsize __len = std::min(static_cast<streamsize>(INT_MAX), std::min(__einp_ - __ninp_, __n - __i));
380-
traits_type::copy(__s, __ninp_, __len);
381-
__s += __len;
382-
__i += __len;
383-
this->gbump(__len);
384-
} else if ((__c = uflow()) != __eof) {
385-
*__s = traits_type::to_char_type(__c);
386-
++__s;
387-
++__i;
388-
} else
389-
break;
390-
}
391-
return __i;
392-
}
393-
394-
template <class _CharT, class _Traits>
395-
typename basic_streambuf<_CharT, _Traits>::int_type basic_streambuf<_CharT, _Traits>::underflow() {
396-
return traits_type::eof();
397-
}
398-
399-
template <class _CharT, class _Traits>
400-
typename basic_streambuf<_CharT, _Traits>::int_type basic_streambuf<_CharT, _Traits>::uflow() {
401-
if (underflow() == traits_type::eof())
402-
return traits_type::eof();
403-
return traits_type::to_int_type(*__ninp_++);
404-
}
405-
406-
template <class _CharT, class _Traits>
407-
typename basic_streambuf<_CharT, _Traits>::int_type basic_streambuf<_CharT, _Traits>::pbackfail(int_type) {
408-
return traits_type::eof();
409-
}
410-
411-
template <class _CharT, class _Traits>
412-
streamsize basic_streambuf<_CharT, _Traits>::xsputn(const char_type* __s, streamsize __n) {
413-
streamsize __i = 0;
414-
int_type __eof = traits_type::eof();
415-
while (__i < __n) {
416-
if (__nout_ >= __eout_) {
417-
if (overflow(traits_type::to_int_type(*__s)) == __eof)
418-
break;
419-
++__s;
420-
++__i;
421-
} else {
422-
streamsize __chunk_size = std::min(__eout_ - __nout_, __n - __i);
423-
traits_type::copy(__nout_, __s, __chunk_size);
424-
__nout_ += __chunk_size;
425-
__s += __chunk_size;
426-
__i += __chunk_size;
427-
}
428-
}
429-
return __i;
430-
}
431-
432-
template <class _CharT, class _Traits>
433-
typename basic_streambuf<_CharT, _Traits>::int_type basic_streambuf<_CharT, _Traits>::overflow(int_type) {
434-
return traits_type::eof();
435-
}
436-
437375
extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_streambuf<char>;
438376

439377
# if _LIBCPP_HAS_WIDE_CHARACTERS

0 commit comments

Comments
 (0)