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