Boost.Nowide
console_buffer.hpp
1// Copyright (c) 2012 Artyom Beilis (Tonkikh)
2// Copyright (c) 2020 - 2021 Alexander Grund
3//
4// Distributed under the Boost Software License, Version 1.0.
5// https://www.boost.org/LICENSE_1_0.txt
6
7#ifndef BOOST_NOWIDE_DETAIL_CONSOLE_BUFFER_HPP_INCLUDED
8#define BOOST_NOWIDE_DETAIL_CONSOLE_BUFFER_HPP_INCLUDED
9
11#include <boost/nowide/utf/utf.hpp>
12#include <streambuf>
13#include <vector>
14
15#include <boost/config/abi_prefix.hpp> // must be the last #include
16
17// Internal header, not meant to be used outside of the implementation
18namespace boost {
19namespace nowide {
20 namespace detail {
21
23 class BOOST_NOWIDE_DECL console_output_buffer_base : public std::streambuf
24 {
25 protected:
26 int sync() override;
27 int overflow(int c) override;
28
29 private:
30 using decoder = utf::utf_traits<char>;
31 using encoder = utf::utf_traits<wchar_t>;
32
33 int write(const char* p, int n);
34 virtual bool
35 do_write(const wchar_t* buffer, std::size_t num_chars_to_write, std::size_t& num_chars_written) = 0;
36
37 static constexpr int buffer_size = 1024;
38 static constexpr int wbuffer_size = buffer_size * encoder::max_width;
39 char buffer_[buffer_size];
40 wchar_t wbuffer_[wbuffer_size];
41 };
42
43#ifdef BOOST_MSVC
44#pragma warning(push)
45#pragma warning(disable : 4251)
46#endif
47
48 class BOOST_NOWIDE_DECL console_input_buffer_base : public std::streambuf
49 {
50 protected:
51 int sync() override;
52 int pbackfail(int c) override;
53 int underflow() override;
54
55 private:
56 using decoder = utf::utf_traits<wchar_t>;
57 using encoder = utf::utf_traits<char>;
58
59 size_t read();
60 virtual bool do_read(wchar_t* buffer, std::size_t num_chars_to_read, std::size_t& num_chars_read) = 0;
61
62 static constexpr size_t wbuffer_size = 1024;
63 static constexpr size_t buffer_size = wbuffer_size * encoder::max_width;
64 char buffer_[buffer_size];
65 wchar_t wbuffer_[wbuffer_size];
66 size_t wsize_ = 0;
67 std::vector<char> pback_buffer_;
68 bool was_newline_ = true;
69 };
70
71#ifdef BOOST_MSVC
72#pragma warning(pop)
73#endif
74
76
77 } // namespace detail
78} // namespace nowide
79} // namespace boost
80
81#include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
82
83#endif
UTF Traits class - functions to convert UTF sequences to and from Unicode code points.
Definition utf.hpp:57