Skip to content

Commit 914fd91

Browse files
authored
[libc++][NFC] Simplify basic_ostream by combining operator<<(Arithmetic) (#121011)
The bodies of all the `operator<<` for arithmetic types have very similar or even identical bodies. This introduces two new functions to avoid all the duplication.
1 parent c7fa3cf commit 914fd91

File tree

1 file changed

+62
-222
lines changed

1 file changed

+62
-222
lines changed

libcxx/include/__ostream/basic_ostream.h

Lines changed: 62 additions & 222 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,55 @@ class _LIBCPP_TEMPLATE_VIS basic_ostream : virtual public basic_ios<_CharT, _Tra
8888
return *this;
8989
}
9090

91+
template <class _Tp>
92+
_LIBCPP_HIDE_FROM_ABI basic_ostream& __put_num(_Tp __value) {
93+
# if _LIBCPP_HAS_EXCEPTIONS
94+
try {
95+
# endif // _LIBCPP_HAS_EXCEPTIONS
96+
sentry __s(*this);
97+
if (__s) {
98+
using _Fp = num_put<char_type, ostreambuf_iterator<char_type, traits_type> >;
99+
const _Fp& __facet = std::use_facet<_Fp>(this->getloc());
100+
if (__facet.put(*this, *this, this->fill(), __value).failed())
101+
this->setstate(ios_base::badbit | ios_base::failbit);
102+
}
103+
# if _LIBCPP_HAS_EXCEPTIONS
104+
} catch (...) {
105+
this->__set_badbit_and_consider_rethrow();
106+
}
107+
# endif // _LIBCPP_HAS_EXCEPTIONS
108+
return *this;
109+
}
110+
111+
template <class _Tp>
112+
_LIBCPP_HIDE_FROM_ABI basic_ostream& __put_num_integer_promote(_Tp __value) {
113+
# if _LIBCPP_HAS_EXCEPTIONS
114+
try {
115+
# endif // _LIBCPP_HAS_EXCEPTIONS
116+
sentry __s(*this);
117+
if (__s) {
118+
ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield;
119+
120+
using _Fp = num_put<char_type, ostreambuf_iterator<char_type, traits_type> >;
121+
const _Fp& __facet = std::use_facet<_Fp>(this->getloc());
122+
if (__facet
123+
.put(*this,
124+
*this,
125+
this->fill(),
126+
__flags == ios_base::oct || __flags == ios_base::hex
127+
? static_cast<__copy_unsigned_t<_Tp, long> >(std::__to_unsigned_like(__value))
128+
: static_cast<__copy_unsigned_t<_Tp, long> >(__value))
129+
.failed())
130+
this->setstate(ios_base::badbit | ios_base::failbit);
131+
}
132+
# if _LIBCPP_HAS_EXCEPTIONS
133+
} catch (...) {
134+
this->__set_badbit_and_consider_rethrow();
135+
}
136+
# endif // _LIBCPP_HAS_EXCEPTIONS
137+
return *this;
138+
}
139+
91140
basic_ostream& operator<<(bool __n);
92141
basic_ostream& operator<<(short __n);
93142
basic_ostream& operator<<(unsigned short __n);
@@ -225,276 +274,67 @@ basic_ostream<_CharT, _Traits>::operator<<(basic_streambuf<char_type, traits_typ
225274

226275
template <class _CharT, class _Traits>
227276
basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(bool __n) {
228-
# if _LIBCPP_HAS_EXCEPTIONS
229-
try {
230-
# endif // _LIBCPP_HAS_EXCEPTIONS
231-
sentry __s(*this);
232-
if (__s) {
233-
typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
234-
const _Fp& __f = std::use_facet<_Fp>(this->getloc());
235-
if (__f.put(*this, *this, this->fill(), __n).failed())
236-
this->setstate(ios_base::badbit | ios_base::failbit);
237-
}
238-
# if _LIBCPP_HAS_EXCEPTIONS
239-
} catch (...) {
240-
this->__set_badbit_and_consider_rethrow();
241-
}
242-
# endif // _LIBCPP_HAS_EXCEPTIONS
243-
return *this;
277+
return __put_num(__n);
244278
}
245279

246280
template <class _CharT, class _Traits>
247281
basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(short __n) {
248-
# if _LIBCPP_HAS_EXCEPTIONS
249-
try {
250-
# endif // _LIBCPP_HAS_EXCEPTIONS
251-
sentry __s(*this);
252-
if (__s) {
253-
ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield;
254-
typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
255-
const _Fp& __f = std::use_facet<_Fp>(this->getloc());
256-
if (__f.put(*this,
257-
*this,
258-
this->fill(),
259-
__flags == ios_base::oct || __flags == ios_base::hex
260-
? static_cast<long>(static_cast<unsigned short>(__n))
261-
: static_cast<long>(__n))
262-
.failed())
263-
this->setstate(ios_base::badbit | ios_base::failbit);
264-
}
265-
# if _LIBCPP_HAS_EXCEPTIONS
266-
} catch (...) {
267-
this->__set_badbit_and_consider_rethrow();
268-
}
269-
# endif // _LIBCPP_HAS_EXCEPTIONS
270-
return *this;
282+
return __put_num_integer_promote(__n);
271283
}
272284

273285
template <class _CharT, class _Traits>
274286
basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned short __n) {
275-
# if _LIBCPP_HAS_EXCEPTIONS
276-
try {
277-
# endif // _LIBCPP_HAS_EXCEPTIONS
278-
sentry __s(*this);
279-
if (__s) {
280-
typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
281-
const _Fp& __f = std::use_facet<_Fp>(this->getloc());
282-
if (__f.put(*this, *this, this->fill(), static_cast<unsigned long>(__n)).failed())
283-
this->setstate(ios_base::badbit | ios_base::failbit);
284-
}
285-
# if _LIBCPP_HAS_EXCEPTIONS
286-
} catch (...) {
287-
this->__set_badbit_and_consider_rethrow();
288-
}
289-
# endif // _LIBCPP_HAS_EXCEPTIONS
290-
return *this;
287+
return __put_num_integer_promote(__n);
291288
}
292289

293290
template <class _CharT, class _Traits>
294291
basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(int __n) {
295-
# if _LIBCPP_HAS_EXCEPTIONS
296-
try {
297-
# endif // _LIBCPP_HAS_EXCEPTIONS
298-
sentry __s(*this);
299-
if (__s) {
300-
ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield;
301-
typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
302-
const _Fp& __f = std::use_facet<_Fp>(this->getloc());
303-
if (__f.put(*this,
304-
*this,
305-
this->fill(),
306-
__flags == ios_base::oct || __flags == ios_base::hex
307-
? static_cast<long>(static_cast<unsigned int>(__n))
308-
: static_cast<long>(__n))
309-
.failed())
310-
this->setstate(ios_base::badbit | ios_base::failbit);
311-
}
312-
# if _LIBCPP_HAS_EXCEPTIONS
313-
} catch (...) {
314-
this->__set_badbit_and_consider_rethrow();
315-
}
316-
# endif // _LIBCPP_HAS_EXCEPTIONS
317-
return *this;
292+
return __put_num_integer_promote(__n);
318293
}
319294

320295
template <class _CharT, class _Traits>
321296
basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned int __n) {
322-
# if _LIBCPP_HAS_EXCEPTIONS
323-
try {
324-
# endif // _LIBCPP_HAS_EXCEPTIONS
325-
sentry __s(*this);
326-
if (__s) {
327-
typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
328-
const _Fp& __f = std::use_facet<_Fp>(this->getloc());
329-
if (__f.put(*this, *this, this->fill(), static_cast<unsigned long>(__n)).failed())
330-
this->setstate(ios_base::badbit | ios_base::failbit);
331-
}
332-
# if _LIBCPP_HAS_EXCEPTIONS
333-
} catch (...) {
334-
this->__set_badbit_and_consider_rethrow();
335-
}
336-
# endif // _LIBCPP_HAS_EXCEPTIONS
337-
return *this;
297+
return __put_num_integer_promote(__n);
338298
}
339299

340300
template <class _CharT, class _Traits>
341301
basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(long __n) {
342-
# if _LIBCPP_HAS_EXCEPTIONS
343-
try {
344-
# endif // _LIBCPP_HAS_EXCEPTIONS
345-
sentry __s(*this);
346-
if (__s) {
347-
typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
348-
const _Fp& __f = std::use_facet<_Fp>(this->getloc());
349-
if (__f.put(*this, *this, this->fill(), __n).failed())
350-
this->setstate(ios_base::badbit | ios_base::failbit);
351-
}
352-
# if _LIBCPP_HAS_EXCEPTIONS
353-
} catch (...) {
354-
this->__set_badbit_and_consider_rethrow();
355-
}
356-
# endif // _LIBCPP_HAS_EXCEPTIONS
357-
return *this;
302+
return __put_num(__n);
358303
}
359304

360305
template <class _CharT, class _Traits>
361306
basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned long __n) {
362-
# if _LIBCPP_HAS_EXCEPTIONS
363-
try {
364-
# endif // _LIBCPP_HAS_EXCEPTIONS
365-
sentry __s(*this);
366-
if (__s) {
367-
typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
368-
const _Fp& __f = std::use_facet<_Fp>(this->getloc());
369-
if (__f.put(*this, *this, this->fill(), __n).failed())
370-
this->setstate(ios_base::badbit | ios_base::failbit);
371-
}
372-
# if _LIBCPP_HAS_EXCEPTIONS
373-
} catch (...) {
374-
this->__set_badbit_and_consider_rethrow();
375-
}
376-
# endif // _LIBCPP_HAS_EXCEPTIONS
377-
return *this;
307+
return __put_num(__n);
378308
}
379309

380310
template <class _CharT, class _Traits>
381311
basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(long long __n) {
382-
# if _LIBCPP_HAS_EXCEPTIONS
383-
try {
384-
# endif // _LIBCPP_HAS_EXCEPTIONS
385-
sentry __s(*this);
386-
if (__s) {
387-
typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
388-
const _Fp& __f = std::use_facet<_Fp>(this->getloc());
389-
if (__f.put(*this, *this, this->fill(), __n).failed())
390-
this->setstate(ios_base::badbit | ios_base::failbit);
391-
}
392-
# if _LIBCPP_HAS_EXCEPTIONS
393-
} catch (...) {
394-
this->__set_badbit_and_consider_rethrow();
395-
}
396-
# endif // _LIBCPP_HAS_EXCEPTIONS
397-
return *this;
312+
return __put_num(__n);
398313
}
399314

400315
template <class _CharT, class _Traits>
401316
basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned long long __n) {
402-
# if _LIBCPP_HAS_EXCEPTIONS
403-
try {
404-
# endif // _LIBCPP_HAS_EXCEPTIONS
405-
sentry __s(*this);
406-
if (__s) {
407-
typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
408-
const _Fp& __f = std::use_facet<_Fp>(this->getloc());
409-
if (__f.put(*this, *this, this->fill(), __n).failed())
410-
this->setstate(ios_base::badbit | ios_base::failbit);
411-
}
412-
# if _LIBCPP_HAS_EXCEPTIONS
413-
} catch (...) {
414-
this->__set_badbit_and_consider_rethrow();
415-
}
416-
# endif // _LIBCPP_HAS_EXCEPTIONS
417-
return *this;
317+
return __put_num(__n);
418318
}
419319

420320
template <class _CharT, class _Traits>
421321
basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(float __n) {
422-
# if _LIBCPP_HAS_EXCEPTIONS
423-
try {
424-
# endif // _LIBCPP_HAS_EXCEPTIONS
425-
sentry __s(*this);
426-
if (__s) {
427-
typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
428-
const _Fp& __f = std::use_facet<_Fp>(this->getloc());
429-
if (__f.put(*this, *this, this->fill(), static_cast<double>(__n)).failed())
430-
this->setstate(ios_base::badbit | ios_base::failbit);
431-
}
432-
# if _LIBCPP_HAS_EXCEPTIONS
433-
} catch (...) {
434-
this->__set_badbit_and_consider_rethrow();
435-
}
436-
# endif // _LIBCPP_HAS_EXCEPTIONS
437-
return *this;
322+
return *this << static_cast<double>(__n);
438323
}
439324

440325
template <class _CharT, class _Traits>
441326
basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(double __n) {
442-
# if _LIBCPP_HAS_EXCEPTIONS
443-
try {
444-
# endif // _LIBCPP_HAS_EXCEPTIONS
445-
sentry __s(*this);
446-
if (__s) {
447-
typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
448-
const _Fp& __f = std::use_facet<_Fp>(this->getloc());
449-
if (__f.put(*this, *this, this->fill(), __n).failed())
450-
this->setstate(ios_base::badbit | ios_base::failbit);
451-
}
452-
# if _LIBCPP_HAS_EXCEPTIONS
453-
} catch (...) {
454-
this->__set_badbit_and_consider_rethrow();
455-
}
456-
# endif // _LIBCPP_HAS_EXCEPTIONS
457-
return *this;
327+
return __put_num(__n);
458328
}
459329

460330
template <class _CharT, class _Traits>
461331
basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(long double __n) {
462-
# if _LIBCPP_HAS_EXCEPTIONS
463-
try {
464-
# endif // _LIBCPP_HAS_EXCEPTIONS
465-
sentry __s(*this);
466-
if (__s) {
467-
typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
468-
const _Fp& __f = std::use_facet<_Fp>(this->getloc());
469-
if (__f.put(*this, *this, this->fill(), __n).failed())
470-
this->setstate(ios_base::badbit | ios_base::failbit);
471-
}
472-
# if _LIBCPP_HAS_EXCEPTIONS
473-
} catch (...) {
474-
this->__set_badbit_and_consider_rethrow();
475-
}
476-
# endif // _LIBCPP_HAS_EXCEPTIONS
477-
return *this;
332+
return __put_num(__n);
478333
}
479334

480335
template <class _CharT, class _Traits>
481336
basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(const void* __n) {
482-
# if _LIBCPP_HAS_EXCEPTIONS
483-
try {
484-
# endif // _LIBCPP_HAS_EXCEPTIONS
485-
sentry __s(*this);
486-
if (__s) {
487-
typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
488-
const _Fp& __f = std::use_facet<_Fp>(this->getloc());
489-
if (__f.put(*this, *this, this->fill(), __n).failed())
490-
this->setstate(ios_base::badbit | ios_base::failbit);
491-
}
492-
# if _LIBCPP_HAS_EXCEPTIONS
493-
} catch (...) {
494-
this->__set_badbit_and_consider_rethrow();
495-
}
496-
# endif // _LIBCPP_HAS_EXCEPTIONS
497-
return *this;
337+
return __put_num(__n);
498338
}
499339

500340
template <class _CharT, class _Traits>

0 commit comments

Comments
 (0)