libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
streamstring.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <functional>
12#include <promeki/core/string.h>
15
17
27 PROMEKI_OBJECT(StreamStringIODevice, IODevice)
28 public:
30 using OnNewLineFunc = std::function<bool(String &)>;
31
37
43 _onNewLine = std::move(func);
44 }
45
50 const String &line() const { return _line; }
51
53 void clearLine() {
54 _line.clear();
55 }
56
58 Error open(OpenMode mode) override {
59 if(isOpen()) return Error(Error::AlreadyOpen);
60 setOpenMode(mode);
61 return Error();
62 }
63
65 Error close() override {
67 return Error();
68 }
69
71 bool isOpen() const override {
72 return openMode() != NotOpen;
73 }
74
76 int64_t read(void *, int64_t) override {
77 return -1;
78 }
79
86 int64_t write(const void *data, int64_t maxSize) override {
87 if(!isOpen() || !isWritable()) return -1;
88 const char *p = static_cast<const char *>(data);
89 for(int64_t i = 0; i < maxSize; i++) {
90 if(p[i] == '\n') {
91 flushLine();
92 } else {
93 _line += p[i];
94 }
95 }
96 return maxSize;
97 }
98
100 void flush() override {
101 flushLine();
102 }
103
105 bool isSequential() const override { return true; }
106
107 private:
108 OnNewLineFunc _onNewLine;
109 String _line;
110
111 void flushLine() {
112 if(!_line.isEmpty() && _onNewLine) {
113 if(_onNewLine(_line)) _line.clear();
114 }
115 }
116};
117
137 public:
139 using OnNewLineFunc = std::function<bool(String &)>;
140
142 StreamString() : _stream(&_device) {
143 _device.open(IODevice::WriteOnly);
144 }
145
150 StreamString(OnNewLineFunc func) : _stream(&_device) {
151 _device.setOnNewLine(std::move(func));
152 _device.open(IODevice::WriteOnly);
153 }
154
156 StreamString(const StreamString &) = delete;
163
168 TextStream &stream() { return _stream; }
169
175 _device.setOnNewLine(std::move(func));
176 return;
177 }
178
183 const String &line() const { return _device.line(); }
184
186 void clear() {
187 _device.clearLine();
188 return;
189 }
190
191 private:
192 StreamStringIODevice _device;
193 TextStream _stream;
194};
195
Lightweight error code wrapper for the promeki library.
Definition error.h:39
@ AlreadyOpen
Resource is already open.
Definition error.h:81
Abstract base class for all I/O devices.
Definition iodevice.h:29
OpenMode openMode() const
Returns the current open mode.
Definition iodevice.h:160
bool isWritable() const
Returns true if the device is writable.
Definition iodevice.h:176
OpenMode
Mode flags controlling how a device is opened.
Definition iodevice.h:33
@ WriteOnly
Open for writing only.
Definition iodevice.h:36
@ NotOpen
Device is not open.
Definition iodevice.h:34
void setOpenMode(OpenMode mode)
Sets the open mode.
Definition iodevice.h:215
Dynamic array container wrapping std::vector.
Definition list.h:40
Base object for promeki.
Definition objectbase.h:129
ObjectBase * parent() const
Returns the parent object, if one. nullptr if none.
Definition objectbase.h:258
Write-only IODevice that intercepts writes, accumulates characters, and invokes a callback on newline...
Definition streamstring.h:26
bool isOpen() const override
Returns true if the device is open.
Definition streamstring.h:71
Error close() override
Closes the device.
Definition streamstring.h:65
int64_t write(const void *data, int64_t maxSize) override
Writes data, splitting on newlines and invoking the callback.
Definition streamstring.h:86
void clearLine()
Clears the accumulated line buffer.
Definition streamstring.h:53
void setOnNewLine(OnNewLineFunc func)
Sets the callback invoked when a line is complete.
Definition streamstring.h:42
const String & line() const
Returns the current (possibly incomplete) line buffer.
Definition streamstring.h:50
Error open(OpenMode mode) override
Opens the device in the specified mode.
Definition streamstring.h:58
int64_t read(void *, int64_t) override
Always returns -1 (write-only device).
Definition streamstring.h:76
std::function< bool(String &)> OnNewLineFunc
Callback type invoked for each completed line.
Definition streamstring.h:30
void flush() override
Flushes the current line buffer via the callback.
Definition streamstring.h:100
bool isSequential() const override
Returns true (this device is sequential).
Definition streamstring.h:105
StreamStringIODevice(ObjectBase *parent=nullptr)
Constructs a StreamStringIODevice.
Definition streamstring.h:36
Collects text output into a String, splitting on newlines.
Definition streamstring.h:136
std::function< bool(String &)> OnNewLineFunc
Callback type invoked for each completed line.
Definition streamstring.h:139
TextStream & stream()
Returns the TextStream associated with this buffer.
Definition streamstring.h:168
StreamString(OnNewLineFunc func)
Constructs a StreamString with a newline callback.
Definition streamstring.h:150
StreamString & operator=(const StreamString &)=delete
Deleted copy assignment operator.
StreamString & operator=(StreamString &&)=delete
Deleted move assignment operator.
StreamString(StreamString &&)=delete
Deleted move constructor.
const String & line() const
Returns the current (possibly incomplete) line buffer.
Definition streamstring.h:183
void clear()
Clears the accumulated line buffer.
Definition streamstring.h:186
void setOnNewLine(OnNewLineFunc func)
Sets or replaces the newline callback.
Definition streamstring.h:174
StreamString(const StreamString &)=delete
Deleted copy constructor.
StreamString()
Constructs a StreamString with no callback.
Definition streamstring.h:142
Encoding-aware string class with copy-on-write semantics.
Definition string.h:35
bool isEmpty() const
Returns true if the string has zero length.
Definition string.h:315
void clear()
Removes all characters from the string.
Definition string.h:449
Formatted text I/O with encoding awareness.
Definition textstream.h:54
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19