COROIO: NNet::TLineSplitter Class Reference
COROIO
 
Loading...
Searching...
No Matches
NNet::TLineSplitter Class Reference

Splits incoming data into lines using a circular buffer of fixed capacity. More...

#include <sockutils.hpp>

Public Member Functions

 TLineSplitter (int maxLen)
 Constructs a line splitter with a fixed ring buffer capacity.
 
TLine Pop ()
 Retrieves and removes the next complete line from the buffer.
 
void Push (const char *buf, size_t size)
 Appends new data to the circular buffer.
 

Detailed Description

Splits incoming data into lines using a circular buffer of fixed capacity.

This class maintains a ring buffer of maximum length (maxLen), allowing you to push new data in and then pop complete lines without additional copying. When a line wraps around the circular boundary, it is represented in two segments. A corresponding TLine object holds these segments as string views (Part1 and Part2).

Important Notes

  • The maximum capacity of the buffer is given by maxLen. Pushing more data than the available space may cause old data to be overwritten or an implementation-defined behavior.
  • The returned TLine references the internal buffer via string views. Any subsequent push operations (or further pops) may invalidate these views.

Example Usage

// Suppose TLine is defined as:
// struct TLine {
// std::string_view Part1;
// std::string_view Part2; // empty if the line doesn't wrap around
// };
int main() {
TLineSplitter splitter(1024); // up to 1024 bytes of data stored
// Push some data containing two lines
const char* data = "Hello\nWorld\n";
splitter.Push(data, std::strlen(data));
// Pop the first line -> "Hello"
TLine line1 = splitter.Pop();
// line1.Part1: "Hello"
// line1.Part2: "" (empty, not wrapped)
// Pop the second line -> "World"
TLine line2 = splitter.Pop();
// line2.Part1: "World"
// line2.Part2: "" (empty, not wrapped)
return 0;
}
Definition sockutils.hpp:9
TLineSplitter(int maxLen)
Constructs a line splitter with a fixed ring buffer capacity.

Constructor & Destructor Documentation

◆ TLineSplitter()

NNet::TLineSplitter::TLineSplitter ( int maxLen)

Constructs a line splitter with a fixed ring buffer capacity.

Parameters
maxLenThe maximum number of bytes the circular buffer can hold.

Member Function Documentation

◆ Pop()

TLine NNet::TLineSplitter::Pop ( )

Retrieves and removes the next complete line from the buffer.

A line is typically delimited by a newline character (implementation-specific). If the line crosses the circular boundary, TLine will contain two parts:

  • Part1 referencing the tail end of the buffer,
  • Part2 referencing the beginning portion.
Returns
A TLine object with string views that reference the extracted line. If there is no complete line available, behavior may be undefined or implementation-dependent (e.g., returns an empty line or throws).
Warning
The returned TLine's string views are valid only until the next call to Push() or further modifications to the buffer.

◆ Push()

void NNet::TLineSplitter::Push ( const char * buf,
size_t size )

Appends new data to the circular buffer.

The data is copied into the ring buffer without extra allocations. If the buffer does not have enough free space, the runtime_exception is thrown.

Parameters
bufPointer to the raw bytes to insert.
sizeNumber of bytes to insert from buf.

The documentation for this class was generated from the following file: