Max OS 0.3
Loading...
Searching...
No Matches
inputStream.h
Go to the documentation of this file.
1
11#ifndef MAXOS_COMMON_INPUTSTREAM_H
12#define MAXOS_COMMON_INPUTSTREAM_H
13
14#include <cstdint>
15#include <common/vector.h>
16#include <common/string.h>
17
18namespace MaxOS::common {
19
20 // Forward declaration
21 template<class Type> class GenericInputStream;
22
29 template<class Type> class InputStreamEventHandler {
30 friend class GenericInputStream<Type>;
31
32 protected:
34 public:
37
38 virtual void on_stream_read(Type);
40 };
41
49 template<class Type, class ProcessorType> class InputStreamProcessor : public InputStreamEventHandler<Type>, public GenericInputStream<ProcessorType> {
50 public:
52 explicit InputStreamProcessor(InputStreamEventHandler<ProcessorType>* generic_stream_event_handler);
54
56 };
57
58
65 template<class Type> class InputStreamSocket : public InputStreamProcessor<Type, Type> {
66 public:
70
71 void on_stream_read(Type) override;
72 };
73
80 template<class Type> class InputStreamBuffer : protected InputStreamProcessor<Type, Type*> {
81 protected:
82 Type m_buffer[10240];
83 int m_offset { 0 };
86
87 public:
88 InputStreamBuffer(Type event_fire_element, Type termination_element);
90
91 void on_stream_read(Type) override;
93 void flush();
94 };
95
102 template<class Type> class GenericInputStream {
103
104 protected:
106
107 public:
111
114
115 };
116
124 template<class Type> void operator >>(GenericInputStream<Type>& source, InputStreamEventHandler<Type>& input_stream_event_handler);
125
135 template<class Type, class ProcessorType> GenericInputStream<ProcessorType>& operator >>(GenericInputStream<Type>& source, InputStreamProcessor<Type, ProcessorType>& processor);
136
141 class InputStream : public GenericInputStream<string> {
142 public:
144 };
145
146
148
154 template<class Type> InputStreamEventHandler<Type>::InputStreamEventHandler() = default;
155
162
163 // Disconnect the handler from all streams
164 while(!m_generic_input_streams.empty())
165 (*m_generic_input_streams.begin())->disconnect_input_stream_event_handler(this);
166
167 }
168
174 template<class Type> void InputStreamEventHandler<Type>::on_stream_read(Type) {
175
176 }
177
185
186 // Remove the stream
187 m_generic_input_streams.erase(stream);
188 }
189
190
197 template<class Type, class ProcessorType> InputStreamProcessor<Type, ProcessorType>::InputStreamProcessor()
198 : InputStreamEventHandler<Type>(),
199 GenericInputStream<ProcessorType>() {
200
201 }
202
210 template<class Type, class ProcessorType> InputStreamProcessor<Type, ProcessorType>::InputStreamProcessor(InputStreamEventHandler<ProcessorType>* generic_stream_event_handler)
211 : InputStreamEventHandler<Type>(),
212 GenericInputStream<ProcessorType>(generic_stream_event_handler) {
213
214 }
215
222 template<class Type, class ProcessorType> InputStreamProcessor<Type, ProcessorType>::~InputStreamProcessor() = default;
223
231 template<class Type, class ProcessorType> void InputStreamProcessor<Type, ProcessorType>::on_end_of_stream(GenericInputStream<Type>* stream) {
232
233 // Pass the end of stream event on to the handlers
234 for(auto& event_handler : this->m_input_stream_event_handlers)
235 event_handler->on_end_of_stream(this);
236
237 // Remove the stream
239
240 }
241
247 template<class Type> InputStreamSocket<Type>::InputStreamSocket() = default;
248
256 : InputStreamProcessor<Type, Type>(processor_handler) {
257
258 }
259
265 template<class Type> InputStreamSocket<Type>::~InputStreamSocket() = default;
266
273 template<class Type> void InputStreamSocket<Type>::on_stream_read(Type read_element) {
274
275 // Pass the read event on to the handlers
276 for(auto& event_handler : this->m_input_stream_event_handlers)
277 event_handler->on_stream_read(read_element);
278
279 }
280
288 template<class Type> InputStreamBuffer<Type>::InputStreamBuffer(Type event_fire_element, Type termination_element)
289 : m_event_fire_element(event_fire_element),
290 m_termination_element(termination_element) {
291
292 }
293
299 template<class Type> InputStreamBuffer<Type>::~InputStreamBuffer() = default;
300
307 template<class Type> void InputStreamBuffer<Type>::on_stream_read(Type read_element) {
308
309 // flush the buffer if the event fire element is read
310 if(read_element == m_event_fire_element) {
311 flush();
312 return;
313 }
314
315 // Ensure the buffer is not full
316 if(m_offset >= 10238) {
317 flush();
318 }
319
320 // Add the element
321 m_buffer[m_offset++] = read_element;
322
323 }
324
332
333 // flush the buffer if there is any data in it
334 if(m_offset > 0)
335 flush();
336
337 // Pass the event on to the handlers and remove the stream
339 }
340
346 template<class Type> void InputStreamBuffer<Type>::flush() {
347
348 // Ensure the buffer is not empty
349 if(m_offset == 0)
350 return;
351
352 // Add the termination element to the buffer
353 m_buffer[m_offset] = m_termination_element;
354
355 // Fire the on read event
357
358 // Reset the offset
359 m_offset = 0;
360
361 }
362
368 template<class Type> GenericInputStream<Type>::GenericInputStream() = default;
369
376 template<class Type> GenericInputStream<Type>::GenericInputStream(InputStreamEventHandler<Type>* input_stream_event_handler) {
377
378 // Connect the handler
379 connect_input_stream_event_handler(input_stream_event_handler);
380 }
381
388
389 // Disconnect all handlers
390 while(!m_input_stream_event_handlers.empty())
391 disconnect_input_stream_event_handler(*(m_input_stream_event_handlers.begin()));
392 }
393
400
401 // Don't add the handler if it is already connected
402 if(m_input_stream_event_handlers.find(input_stream_event_handler) != m_input_stream_event_handlers.end())
403 return;
404
405 // Add the handler
406 m_input_stream_event_handlers.push_back(input_stream_event_handler);
407
408 }
409
417
418 // Don't remove the handler if it is not connected
419 if(m_input_stream_event_handlers.find(input_stream_event_handler) == m_input_stream_event_handlers.end())
420 return;
421
422 // Remove the handler
423 m_input_stream_event_handlers.erase(input_stream_event_handler);
424
425 // Fire the end of stream event
426 input_stream_event_handler->on_end_of_stream(this);
427
428 }
429}
430
431#endif //MAXOS_COMMON_INPUTSTREAM_H
Manages the connection of a stream to handlers.
void connect_input_stream_event_handler(InputStreamEventHandler< Type > *)
Adds a inputStreamEventHandler to the list of internetProtocolHandlers.
GenericInputStream()
Creates a new GenericInputStream.
common::Vector< InputStreamEventHandler< Type > * > m_input_stream_event_handlers
List of streams being observed by this handler.
void disconnect_input_stream_event_handler(InputStreamEventHandler< Type > *)
Removes a handler from the list of handlers.
GenericInputStream(InputStreamEventHandler< Type > *)
Creates a new GenericInputStream and connects it to the handler.
~GenericInputStream()
Destroys the GenericInputStream and disconnects all handlers.
Buffers data from a stream and fires an event when a certain element is read.
Definition inputStream.h:80
int m_offset
The current position in the buffer.
Definition inputStream.h:83
void flush()
Flushes the buffer by adding the termination element and firing an on read event (NOTE: The buffer is...
void on_stream_read(Type) override
Called when data is read from a stream. Adds the data to the buffer and checks if the event should be...
Type m_event_fire_element
The element that will cause the buffer to flush and fire an event.
Definition inputStream.h:84
InputStreamBuffer(Type event_fire_element, Type termination_element)
Creates a new InputStreamBuffer.
Type m_termination_element
The element that will cause the buffer to flush and end the stream.
Definition inputStream.h:85
~InputStreamBuffer()
Destroys the InputStreamBuffer.
void on_end_of_stream(GenericInputStream< Type > *) override
Called when a stream has finished. Flushes the buffer if there is any data in it.
Type m_buffer[10240]
The buffer to store data in.
Definition inputStream.h:82
Handles read and end of stream events from a set of streams.
Definition inputStream.h:29
common::Vector< GenericInputStream< Type > * > m_generic_input_streams
List of streams being observed by this handler.
Definition inputStream.h:33
virtual void on_end_of_stream(GenericInputStream< Type > *)
Called when a stream has finished. Removes the stream from the array of streams.
virtual void on_stream_read(Type)
Called when data is read from a stream (overridden by subclasses)
~InputStreamEventHandler()
Destroys the InputStreamProcessor and disconnects it from all streams.
InputStreamEventHandler()
_______________________________________________TEMPLATES_____________________________________________...
Converts one stream data type into another.
Definition inputStream.h:49
void on_end_of_stream(GenericInputStream< Type > *stream) override
Called when a stream has finished. Passes the event on to the handlers and then removes the stream fr...
InputStreamProcessor(InputStreamEventHandler< ProcessorType > *generic_stream_event_handler)
Creates a new InputStreamProcessor.
InputStreamProcessor()
Creates a new InputStreamProcessor.
~InputStreamProcessor()
Destroys the InputStreamProcessor.
Passes read events on to the handlers.
Definition inputStream.h:65
InputStreamSocket()
Creates a new InputStreamSocket.
void on_stream_read(Type) override
Called when data is read from a stream. Passes the event on to the internetProtocolHandlers.
~InputStreamSocket()
Destroys the InputStreamSocket.
A stream that handles strings.
Dynamically stores an array of elements.
Definition vector.h:39
Defines a String class for dynamically sized strings with various operations.
Defines a Vector class for dynamically storing an array of elements.