|
| 1 | +/************************************************************************************ |
| 2 | + * Copyright (c) 2023, xeus-cpp contributors * |
| 3 | + * Copyright (c) 2023, Johan Mabille, Loic Gouarin, Sylvain Corlay, Wolf Vollprecht * |
| 4 | + * * |
| 5 | + * Distributed under the terms of the BSD 3-Clause License. * |
| 6 | + * * |
| 7 | + * The full license is in the file LICENSE, distributed with this software. * |
| 8 | + ************************************************************************************/ |
| 9 | + |
| 10 | +#ifndef XEUS_CPP_BUFFER_HPP |
| 11 | +#define XEUS_CPP_BUFFER_HPP |
| 12 | + |
| 13 | +#include <functional> |
| 14 | +#include <memory> |
| 15 | +#include <mutex> |
| 16 | +#include <streambuf> |
| 17 | +#include <string> |
| 18 | + |
| 19 | +namespace xcpp |
| 20 | +{ |
| 21 | + /******************** |
| 22 | + * output streambuf * |
| 23 | + ********************/ |
| 24 | + |
| 25 | + class xoutput_buffer : public std::streambuf |
| 26 | + { |
| 27 | + public: |
| 28 | + |
| 29 | + using base_type = std::streambuf; |
| 30 | + using callback_type = std::function<void(const std::string&)>; |
| 31 | + using traits_type = base_type::traits_type; |
| 32 | + |
| 33 | + xoutput_buffer(callback_type callback) |
| 34 | + : m_callback(std::move(callback)) |
| 35 | + { |
| 36 | + } |
| 37 | + |
| 38 | + protected: |
| 39 | + |
| 40 | + traits_type::int_type overflow(traits_type::int_type c) override |
| 41 | + { |
| 42 | + std::lock_guard<std::mutex> lock(m_mutex); |
| 43 | + // Called for each output character. |
| 44 | + if (!traits_type::eq_int_type(c, traits_type::eof())) |
| 45 | + { |
| 46 | + m_output.push_back(traits_type::to_char_type(c)); |
| 47 | + } |
| 48 | + return c; |
| 49 | + } |
| 50 | + |
| 51 | + std::streamsize xsputn(const char* s, std::streamsize count) override |
| 52 | + { |
| 53 | + std::lock_guard<std::mutex> lock(m_mutex); |
| 54 | + // Called for a string of characters. |
| 55 | + m_output.append(s, count); |
| 56 | + return count; |
| 57 | + } |
| 58 | + |
| 59 | + traits_type::int_type sync() override |
| 60 | + { |
| 61 | + std::lock_guard<std::mutex> lock(m_mutex); |
| 62 | + // Called in case of flush. |
| 63 | + if (!m_output.empty()) |
| 64 | + { |
| 65 | + m_callback(m_output); |
| 66 | + m_output.clear(); |
| 67 | + } |
| 68 | + return 0; |
| 69 | + } |
| 70 | + |
| 71 | + callback_type m_callback; |
| 72 | + std::string m_output; |
| 73 | + std::mutex m_mutex; |
| 74 | + }; |
| 75 | + |
| 76 | + /******************* |
| 77 | + * input streambuf * |
| 78 | + *******************/ |
| 79 | + |
| 80 | + class xinput_buffer : public std::streambuf |
| 81 | + { |
| 82 | + public: |
| 83 | + |
| 84 | + using base_type = std::streambuf; |
| 85 | + using callback_type = std::function<void(std::string&)>; |
| 86 | + using traits_type = base_type::traits_type; |
| 87 | + |
| 88 | + xinput_buffer(callback_type callback) |
| 89 | + : m_callback(std::move(callback)) |
| 90 | + , m_value() |
| 91 | + { |
| 92 | + char* data = const_cast<char*>(m_value.data()); |
| 93 | + this->setg(data, data, data); |
| 94 | + } |
| 95 | + |
| 96 | + protected: |
| 97 | + |
| 98 | + traits_type::int_type underflow() override |
| 99 | + { |
| 100 | + m_callback(m_value); |
| 101 | + // Terminate the string to trigger parsing. |
| 102 | + m_value += '\n'; |
| 103 | + char* data = const_cast<char*>(m_value.data()); |
| 104 | + setg(data, data, data + m_value.size()); |
| 105 | + return traits_type::to_int_type(*gptr()); |
| 106 | + } |
| 107 | + |
| 108 | + callback_type m_callback; |
| 109 | + std::string m_value; |
| 110 | + }; |
| 111 | + |
| 112 | + /************************* |
| 113 | + * output null streambuf * |
| 114 | + *************************/ |
| 115 | + |
| 116 | + class xnull : public std::streambuf |
| 117 | + { |
| 118 | + using base_type = std::streambuf; |
| 119 | + using traits_type = base_type::traits_type; |
| 120 | + |
| 121 | + traits_type::int_type overflow(traits_type::int_type c) override |
| 122 | + { |
| 123 | + return c; |
| 124 | + } |
| 125 | + }; |
| 126 | +} |
| 127 | + |
| 128 | +#endif |
0 commit comments