AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
iostream.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++23 standard
3 Copyright (C) 2020 Vincent Lachenal
4
5 This file is part of ANCH Framework.
6
7 ANCH Framework is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 ANCH Framework is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with ANCH Framework. If not, see <https://www.gnu.org/licenses/>.
19*/
20#pragma once
21
22#include "anch-cutils-export.hpp"
23
24#include <cstddef>
25#include <functional>
26#include <istream>
27#include <memory>
28#include <ostream>
29#include <streambuf>
30
31namespace anch::cutils {
32
40 enum class Direction {
41
43 IN = 0b01,
44
46 OUT = 0b10,
47
49 INOUT = 0b11,
50
51 };
52
70 struct ANCH_CUTILS_EXPORT cbuffer {
71
73 char* data = nullptr;
74
76 std::size_t size = 0;
77
79 std::function<std::size_t(char*,std::size_t)> read = nullptr;
80
82 std::function<std::size_t(const char*,std::size_t)> write = nullptr;
83
84 };
85
96 class ANCH_CUTILS_EXPORT CStreambuf: public std::streambuf {
97
98 // Attributes +
99 private:
101 std::function<std::size_t(char*,std::size_t)> _read;
102
104 std::function<std::size_t(const char*,std::size_t)> _write;
105
107 std::size_t _size;
108
110 std::unique_ptr<char[]> _owned;
111
113 char* _in;
114
116 char* _out;
117 // Attributes -
118
119 // Constructors +
120 public:
130
134 CStreambuf() noexcept;
135
139 CStreambuf(const CStreambuf&) = delete;
140 // Constructors -
141
142 // Destructor +
143 public:
148 virtual ~CStreambuf();
149 // Destructor -
150
151 // Operators +
152 public:
156 CStreambuf& operator=(const CStreambuf&) = delete;
157 // Operators -
158
159 // Methods +
160 public:
171
172 protected:
178 virtual std::streambuf::int_type underflow() override;
179
187 virtual std::streambuf::int_type overflow(std::streambuf::int_type value) override;
188
194 virtual int sync() override;
195
196 private:
202 bool flushOutput();
203 // Methods -
204
205 };
206
214 class ANCH_CUTILS_EXPORT CIStream: public std::istream {
215
216 // Attributes +
217 private:
219 anch::cutils::CStreambuf _streambuf;
220 // Attributes -
221
222 // Constructors +
223 public:
232
233 protected:
238 // Constructors -
239
240 // Destructor +
241 public:
245 virtual ~CIStream();
246 // Destructor -
247
248 // Methods +
249 protected:
258 // Methods -
259
260 };
261
269 class ANCH_CUTILS_EXPORT COStream: public std::ostream {
270
271 // Attributes +
272 private:
274 anch::cutils::CStreambuf _streambuf;
275 // Attributes -
276
277 // Constructors +
278 public:
287
288 protected:
293 // Constructors -
294
295 // Destructor +
296 public:
301 virtual ~COStream();
302 // Destructor -
303
304 // Methods +
305 protected:
314 // Methods -
315
316 };
317
325 class ANCH_CUTILS_EXPORT CIOStream: public std::iostream {
326
327 // Attributes +
328 private:
330 anch::cutils::CStreambuf _streambuf;
331 // Attributes -
332
333 // Constructors +
334 public:
343
344 protected:
349 // Constructors -
350
351 // Destructor +
352 public:
357 virtual ~CIOStream();
358 // Destructor -
359
360 // Methods +
361 protected:
370 // Methods -
371
372 };
373
374}
void setBuffer(const anch::cutils::cbuffer &cbuffer)
CIOStream(const anch::cutils::cbuffer &cbuffer)
CIStream(const anch::cutils::cbuffer &cbuffer)
void setBuffer(const anch::cutils::cbuffer &cbuffer)
void setBuffer(const anch::cutils::cbuffer &cbuffer)
COStream(const anch::cutils::cbuffer &cbuffer)
Definition iostream.hpp:96
CStreambuf(const anch::cutils::cbuffer &cbuffer, anch::cutils::Direction dir)
virtual std::streambuf::int_type overflow(std::streambuf::int_type value) override
virtual int sync() override
void setBuffer(const anch::cutils::cbuffer &cbuffer, anch::cutils::Direction dir)
virtual std::streambuf::int_type underflow() override
AnCH C utilities library namespace.
Direction
Definition iostream.hpp:40
@ INOUT
Definition iostream.hpp:49
@ IN
Definition iostream.hpp:43
@ OUT
Definition iostream.hpp:46
AnCH framework base namespace.
Definition app.hpp:30
C buffer.
Definition iostream.hpp:70
std::function< std::size_t(char *, std::size_t)> read
Definition iostream.hpp:79
std::function< std::size_t(const char *, std::size_t)> write
Definition iostream.hpp:82
char * data
Definition iostream.hpp:73
std::size_t size
Definition iostream.hpp:76