cpp-terminal
1.0.0
Small C++ library for writing multiplatform terminal applications
Loading...
Searching...
No Matches
iostream_initializer.cpp
Go to the documentation of this file.
1
/*
2
* cpp-terminal
3
* C++ library for writing multi-platform terminal applications.
4
*
5
* SPDX-FileCopyrightText: 2019-2024 cpp-terminal
6
*
7
* SPDX-License-Identifier: MIT
8
*/
9
10
#include "
cpp-terminal/iostream_initializer.hpp
"
11
12
#include "
cpp-terminal/iostream.hpp
"
13
#include "
cpp-terminal/private/exception.hpp
"
14
#include "
cpp-terminal/terminal.hpp
"
15
#include "
cpp-terminal/terminal_initializer.hpp
"
16
#include "
cpp-terminal/tty.hpp
"
17
18
#include <cstddef>
19
#include <iostream>
20
std::size_t
Term::IOStreamInitializer::m_counter
{0};
21
22
Term::IOStreamInitializer::IOStreamInitializer
() noexcept
23
try
24
{
25
if
(0 ==
m_counter
)
26
{
27
static
const
std::ios_base::Init iostreams_init;
// Init std::cout etc...
28
static
const
Term::TerminalInitializer
terminal_init;
// Make sure terminal is set up.
29
new
(&
Term::cout
)
TOstream
(
Term::Buffer::Type::FullBuffered
, BUFSIZ);
30
new
(&
Term::clog
)
TOstream
(
Term::Buffer::Type::LineBuffered
, BUFSIZ);
31
new
(&
Term::cerr
)
TOstream
(
Term::Buffer::Type::Unbuffered
, 0);
32
new
(&
Term::cin
)
TIstream
(
Term::Buffer::Type::FullBuffered
, BUFSIZ);
33
if
(
is_stdin_a_tty
()) { std::cin.
rdbuf
(
Term::cin
.rdbuf()); }
34
}
35
++
m_counter
;
36
}
37
catch
(...)
38
{
39
ExceptionHandler(
Private::ExceptionDestination::StdErr
);
40
}
41
42
Term::IOStreamInitializer::~IOStreamInitializer
() noexcept
43
try
44
{
45
--m_counter;
46
if
(0 == m_counter)
47
{
48
(&
Term::cout
)->~
TOstream
();
49
(&
Term::cerr
)->~
TOstream
();
50
(&
Term::clog
)->~
TOstream
();
51
(&
Term::cin
)->~
TIstream
();
52
}
53
}
54
catch
(...)
55
{
56
ExceptionHandler(
Private::ExceptionDestination::StdErr
);
57
}
Term::Buffer::Type::LineBuffered
@ LineBuffered
Term::Buffer::Type::Unbuffered
@ Unbuffered
Term::Buffer::Type::FullBuffered
@ FullBuffered
Term::IOStreamInitializer::IOStreamInitializer
IOStreamInitializer() noexcept
Definition
iostream_initializer.cpp:22
Term::IOStreamInitializer::m_counter
static std::size_t m_counter
Definition
iostream_initializer.hpp:20
Term::IOStreamInitializer::~IOStreamInitializer
~IOStreamInitializer() noexcept
Definition
iostream_initializer.cpp:42
Term::TIstream
Definition
stream.hpp:21
Term::TIstream::rdbuf
std::streambuf * rdbuf() const
Definition
stream.cpp:16
Term::TOstream
Definition
stream.hpp:42
Term::TerminalInitializer
Definition
terminal_initializer.hpp:18
iostream.hpp
iostream_initializer.hpp
Term::Private::ExceptionDestination::StdErr
@ StdErr
Term::clog
TOstream & clog
Definition
iostream.cpp:23
Term::cout
TOstream & cout
Definition
iostream.cpp:22
Term::cerr
TOstream & cerr
Definition
iostream.cpp:24
Term::cin
TIstream & cin
Definition
iostream.cpp:25
Term::is_stdin_a_tty
bool is_stdin_a_tty()
Check if stdin is a tty.
Definition
tty.cpp:32
exception.hpp
terminal.hpp
terminal_initializer.hpp
tty.hpp
cpp-terminal
iostream_initializer.cpp
Generated by
1.11.0