xref: /freebsd/contrib/llvm-project/libcxx/src/ostream.cpp (revision cb14a3fe5122c879eae1fb480ed7ce82a699ddb6)
1*cb14a3feSDimitry Andric //===----------------------------------------------------------------------===//
2*cb14a3feSDimitry Andric //
3*cb14a3feSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*cb14a3feSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*cb14a3feSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*cb14a3feSDimitry Andric //
7*cb14a3feSDimitry Andric //===----------------------------------------------------------------------===//
8*cb14a3feSDimitry Andric 
9*cb14a3feSDimitry Andric #include <__availability>
10*cb14a3feSDimitry Andric #include <__config>
11*cb14a3feSDimitry Andric #ifndef _LIBCPP_HAS_NO_FILESYSTEM
12*cb14a3feSDimitry Andric #  include <fstream>
13*cb14a3feSDimitry Andric #endif
14*cb14a3feSDimitry Andric #include <ostream>
15*cb14a3feSDimitry Andric 
16*cb14a3feSDimitry Andric #include "std_stream.h"
17*cb14a3feSDimitry Andric 
18*cb14a3feSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
19*cb14a3feSDimitry Andric 
20*cb14a3feSDimitry Andric _LIBCPP_AVAILABILITY_PRINT _LIBCPP_EXPORTED_FROM_ABI FILE* __get_ostream_file(ostream& __os) {
21*cb14a3feSDimitry Andric   // dynamic_cast requires RTTI, this only affects users whose vendor builds
22*cb14a3feSDimitry Andric   // the dylib with RTTI disabled. It does not affect users who build with RTTI
23*cb14a3feSDimitry Andric   // disabled but use a dylib where the RTTI is enabled.
24*cb14a3feSDimitry Andric   //
25*cb14a3feSDimitry Andric   // Returning a nullptr means the stream is not considered a terminal and the
26*cb14a3feSDimitry Andric   // special terminal handling is not done. The terminal handling is mainly of
27*cb14a3feSDimitry Andric   // importance on Windows.
28*cb14a3feSDimitry Andric #ifndef _LIBCPP_HAS_NO_RTTI
29*cb14a3feSDimitry Andric   auto* __rdbuf = __os.rdbuf();
30*cb14a3feSDimitry Andric #  ifndef _LIBCPP_HAS_NO_FILESYSTEM
31*cb14a3feSDimitry Andric   if (auto* __buffer = dynamic_cast<filebuf*>(__rdbuf))
32*cb14a3feSDimitry Andric     return __buffer->__file_;
33*cb14a3feSDimitry Andric #  endif
34*cb14a3feSDimitry Andric 
35*cb14a3feSDimitry Andric   if (auto* __buffer = dynamic_cast<__stdoutbuf<char>*>(__rdbuf))
36*cb14a3feSDimitry Andric     return __buffer->__file_;
37*cb14a3feSDimitry Andric #endif // _LIBCPP_HAS_NO_RTTI
38*cb14a3feSDimitry Andric 
39*cb14a3feSDimitry Andric   return nullptr;
40*cb14a3feSDimitry Andric }
41*cb14a3feSDimitry Andric 
42*cb14a3feSDimitry Andric _LIBCPP_END_NAMESPACE_STD
43