Doxygen
Loading...
Searching...
No Matches
TextStream Class Referencefinal

Text streaming class that buffers data. More...

#include <src/textstream.h>

Public Member Functions

 TextStream (size_t capacity=INITIAL_CAPACITY)
 Creates an empty stream object.
 TextStream (std::ostream *s)
 Create a text stream object for writing to a std::ostream.
 TextStream (const std::string &s)
 Create a text stream, initializing the buffer with string s.
 ~TextStream ()
 Writes any data that is buffered to the attached std::ostream.
void setStream (std::ostream *s)
 Sets or changes the std::ostream to write to.
void setFile (FILE *f)
TextStreamoperator<< (char c)
 Adds a character to the stream.
TextStreamoperator<< (unsigned char c)
 Adds an unsigned character to the stream.
TextStreamoperator<< (unsigned char *s)
 Adds an unsigned character string to the stream.
TextStreamoperator<< (const char *s)
 Adds a C-style string to the stream.
TextStreamoperator<< (const QCString &s)
 Adds a QCString to the stream.
TextStreamoperator<< (const std::string &s)
 Adds a std::string to the stream.
TextStreamoperator<< (signed short i)
 Adds a signed short integer to the stream.
TextStreamoperator<< (unsigned short i)
 Adds a unsigned short integer to the stream.
TextStreamoperator<< (signed int i)
 Adds a signed integer to the stream.
TextStreamoperator<< (unsigned int i)
 Adds a unsigned integer to the stream.
template<typename T, typename std::enable_if< std::is_same< T, size_t >::value, T >::type * = nullptr>
TextStreamoperator<< (T i)
 Adds a size_t integer to the stream.
TextStreamoperator<< (float f)
 Adds a float to the stream.
TextStreamoperator<< (double d)
 Adds a double to the stream.
void write (const char *buf, size_t len)
 Adds a array of character to the stream.
void flush ()
 Flushes the buffer.
void clear ()
 Clears any buffered data.
std::string str () const
 Return the contents of the buffer as a std::string object.
void str (const std::string &s)
 Sets the buffer's contents to string s.
void str (const char *s)
 Sets the buffer's contents to string s Any data already in the buffer will be flushed.
bool empty () const
 Returns true iff the buffer is empty.

Private Member Functions

void output_int32 (uint32_t n, bool neg)
 Writes a string representation of an integer to the buffer.
void output_double (double d)

Private Attributes

std::string m_buffer
std::ostream * m_s = nullptr
FILE * m_f = nullptr

Static Private Attributes

static const int INITIAL_CAPACITY = 4096

Detailed Description

Text streaming class that buffers data.

Simpler version of std::ostringstream that has much better performance.

Definition at line 35 of file textstream.h.

Constructor & Destructor Documentation

◆ TextStream() [1/3]

TextStream::TextStream ( size_t capacity = INITIAL_CAPACITY)
inlineexplicit

Creates an empty stream object.

Definition at line 41 of file textstream.h.

42 {
43 m_buffer.reserve(capacity);
44 }
std::string m_buffer
Definition textstream.h:269

References INITIAL_CAPACITY, and m_buffer.

Referenced by operator<<(), operator<<(), operator<<(), operator<<(), operator<<(), operator<<(), operator<<(), operator<<(), operator<<(), operator<<(), operator<<(), operator<<(), and operator<<().

◆ TextStream() [2/3]

TextStream::TextStream ( std::ostream * s)
inlineexplicit

Create a text stream object for writing to a std::ostream.

Note
data is buffered until flush() is called or the object is destroyed.

Definition at line 48 of file textstream.h.

48 : m_s(s)
49 {
51 }
std::ostream * m_s
Definition textstream.h:270
static const int INITIAL_CAPACITY
Definition textstream.h:37

References INITIAL_CAPACITY, m_buffer, and m_s.

◆ TextStream() [3/3]

TextStream::TextStream ( const std::string & s)
inlineexplicit

Create a text stream, initializing the buffer with string s.

Definition at line 54 of file textstream.h.

54 : m_buffer(s)
55 {
56 m_buffer.reserve(s.length()+INITIAL_CAPACITY);
57 }

References INITIAL_CAPACITY, and m_buffer.

◆ ~TextStream()

TextStream::~TextStream ( )
inline

Writes any data that is buffered to the attached std::ostream.

Definition at line 60 of file textstream.h.

60{ flush(); }
void flush()
Flushes the buffer.
Definition textstream.h:196

References flush().

Member Function Documentation

◆ clear()

void TextStream::clear ( )
inline

Clears any buffered data.

Definition at line 210 of file textstream.h.

211 {
212 m_buffer.clear();
213 }

References m_buffer.

◆ empty()

bool TextStream::empty ( ) const
inline

Returns true iff the buffer is empty.

Definition at line 240 of file textstream.h.

241 {
242 return m_buffer.empty();
243 }

References m_buffer.

Referenced by HtmlGenerator::endClassDiagram(), insertMapFile(), DotFilePatcher::run(), and writeDotImageMapFromFile().

◆ flush()

void TextStream::flush ( )
inline

Flushes the buffer.

If a std::ostream is attached, the buffer's contents will be written to the stream.

Definition at line 196 of file textstream.h.

197 {
198 if (m_s)
199 {
200 m_s->write(m_buffer.c_str(),m_buffer.length());
201 }
202 else if (m_f)
203 {
204 fwrite(m_buffer.c_str(),1,m_buffer.length(),m_f);
205 }
206 m_buffer.clear();
207 }
FILE * m_f
Definition textstream.h:271

References m_buffer, m_f, and m_s.

Referenced by Qhp::addContentsItem(), FormulaManager::createLatexFile(), RTFGenerator::preProcessFileInplace(), DotFilePatcher::run(), setFile(), setStream(), str(), str(), FlowChart::writeFlowChart(), and ~TextStream().

◆ operator<<() [1/13]

TextStream & TextStream::operator<< ( char c)
inline

Adds a character to the stream.

Definition at line 82 of file textstream.h.

83 {
84 m_buffer+=c;
85 return static_cast<TextStream&>(*this);
86 }
TextStream(size_t capacity=INITIAL_CAPACITY)
Creates an empty stream object.
Definition textstream.h:41

References m_buffer, and TextStream().

◆ operator<<() [2/13]

TextStream & TextStream::operator<< ( const char * s)
inline

Adds a C-style string to the stream.

Definition at line 110 of file textstream.h.

111 {
112 if (s) m_buffer+=s;
113 return static_cast<TextStream&>(*this);
114 }

References m_buffer, and TextStream().

◆ operator<<() [3/13]

TextStream & TextStream::operator<< ( const QCString & s)
inline

Adds a QCString to the stream.

Definition at line 117 of file textstream.h.

118 {
119 m_buffer+=s.str();
120 return static_cast<TextStream&>(*this);
121 }
const std::string & str() const
Definition qcstring.h:552

References m_buffer, QCString::str(), and TextStream().

◆ operator<<() [4/13]

TextStream & TextStream::operator<< ( const std::string & s)
inline

Adds a std::string to the stream.

Definition at line 124 of file textstream.h.

125 {
126 m_buffer+=s;
127 return static_cast<TextStream&>(*this);
128 }

References m_buffer, and TextStream().

◆ operator<<() [5/13]

TextStream & TextStream::operator<< ( double d)
inline

Adds a double to the stream.

Definition at line 178 of file textstream.h.

179 {
180 output_double(d);
181 return static_cast<TextStream&>(*this);
182 }
void output_double(double d)
Definition textstream.h:263

References output_double(), and TextStream().

◆ operator<<() [6/13]

TextStream & TextStream::operator<< ( float f)
inline

Adds a float to the stream.

Definition at line 171 of file textstream.h.

172 {
173 output_double(static_cast<double>(f));
174 return static_cast<TextStream&>(*this);
175 }

References output_double(), and TextStream().

◆ operator<<() [7/13]

TextStream & TextStream::operator<< ( signed int i)
inline

Adds a signed integer to the stream.

Definition at line 145 of file textstream.h.

146 {
147 output_int32(i,i<0);
148 return static_cast<TextStream&>(*this);
149 }
void output_int32(uint32_t n, bool neg)
Writes a string representation of an integer to the buffer.
Definition textstream.h:250

References output_int32(), and TextStream().

◆ operator<<() [8/13]

TextStream & TextStream::operator<< ( signed short i)
inline

Adds a signed short integer to the stream.

Definition at line 131 of file textstream.h.

132 {
133 output_int32(i,i<0);
134 return static_cast<TextStream&>(*this);
135 }

References output_int32(), and TextStream().

◆ operator<<() [9/13]

template<typename T, typename std::enable_if< std::is_same< T, size_t >::value, T >::type * = nullptr>
TextStream & TextStream::operator<< ( T i)
inline

Adds a size_t integer to the stream.

We use SFINAE to avoid a compiler error in case size_t already matches the 'unsigned int' overload.

Definition at line 164 of file textstream.h.

165 {
166 output_int32(static_cast<uint32_t>(i),false);
167 return static_cast<TextStream&>(*this);
168 }

References output_int32(), and TextStream().

◆ operator<<() [10/13]

TextStream & TextStream::operator<< ( unsigned char * s)
inline

Adds an unsigned character string to the stream.

Definition at line 95 of file textstream.h.

96 {
97 if (s)
98 {
99 unsigned char *p = s;
100 while(*p)
101 {
102 m_buffer+=*p;
103 p++;
104 }
105 }
106 return static_cast<TextStream&>(*this);
107 }

References m_buffer, and TextStream().

◆ operator<<() [11/13]

TextStream & TextStream::operator<< ( unsigned char c)
inline

Adds an unsigned character to the stream.

Definition at line 88 of file textstream.h.

89 {
90 m_buffer+=c;
91 return static_cast<TextStream&>(*this);
92 }

References m_buffer, and TextStream().

◆ operator<<() [12/13]

TextStream & TextStream::operator<< ( unsigned int i)
inline

Adds a unsigned integer to the stream.

Definition at line 152 of file textstream.h.

153 {
154 output_int32(i,false);
155 return static_cast<TextStream&>(*this);
156 }

References output_int32(), and TextStream().

◆ operator<<() [13/13]

TextStream & TextStream::operator<< ( unsigned short i)
inline

Adds a unsigned short integer to the stream.

Definition at line 138 of file textstream.h.

139 {
140 output_int32(i,false);
141 return static_cast<TextStream&>(*this);
142 }

References output_int32(), and TextStream().

◆ output_double()

void TextStream::output_double ( double d)
inlineprivate

Definition at line 263 of file textstream.h.

264 {
265 char buf[64];
266 snprintf(buf,64,"%f",d);
267 m_buffer+=buf;
268 }

References m_buffer.

Referenced by operator<<(), and operator<<().

◆ output_int32()

void TextStream::output_int32 ( uint32_t n,
bool neg )
inlineprivate

Writes a string representation of an integer to the buffer.

Parameters
nthe absolute value of the integer
negindicates if the integer is negative

Definition at line 250 of file textstream.h.

251 {
252 char buf[20];
253 char *p = &buf[19];
254 *p = '\0';
255 if ( neg )
256 {
257 n = static_cast<uint32_t>(-static_cast<int32_t>(n));
258 }
259 do { *--p = (static_cast<char>(n%10)) + '0'; n /= 10; } while ( n );
260 if ( neg ) *--p = '-';
261 m_buffer+=p;
262 }

References m_buffer.

Referenced by operator<<(), operator<<(), operator<<(), operator<<(), and operator<<().

◆ setFile()

void TextStream::setFile ( FILE * f)
inline

Definition at line 74 of file textstream.h.

75 {
76 flush();
77 m_s = nullptr;
78 m_f = f;
79 }

References flush(), m_f, and m_s.

◆ setStream()

void TextStream::setStream ( std::ostream * s)
inline

Sets or changes the std::ostream to write to.

Note
Any data already buffered will be flushed.

Definition at line 67 of file textstream.h.

68 {
69 flush();
70 m_s = s;
71 m_f = nullptr;
72 }

References flush(), m_f, m_s, and setStream().

Referenced by Qhp::addContentsItem(), DotFilePatcher::run(), and setStream().

◆ str() [1/3]

◆ str() [2/3]

void TextStream::str ( const char * s)
inline

Sets the buffer's contents to string s Any data already in the buffer will be flushed.

Definition at line 233 of file textstream.h.

234 {
235 flush();
236 if (s) m_buffer=s;
237 }

References flush(), and m_buffer.

◆ str() [3/3]

void TextStream::str ( const std::string & s)
inline

Sets the buffer's contents to string s.

Any data already in the buffer will be flushed.

Definition at line 224 of file textstream.h.

225 {
226 flush();
227 m_buffer=s;
228 }

References flush(), and m_buffer.

◆ write()

void TextStream::write ( const char * buf,
size_t len )
inline

Adds a array of character to the stream.

Parameters
bufthe character buffer
lenthe number of characters in the buffer to write

Definition at line 188 of file textstream.h.

189 {
190 m_buffer.append(buf,len);
191 }

References m_buffer.

Referenced by generateXML(), and writeUTF8Char().

Member Data Documentation

◆ INITIAL_CAPACITY

const int TextStream::INITIAL_CAPACITY = 4096
staticprivate

Definition at line 37 of file textstream.h.

Referenced by TextStream(), TextStream(), and TextStream().

◆ m_buffer

◆ m_f

FILE* TextStream::m_f = nullptr
private

Definition at line 271 of file textstream.h.

Referenced by flush(), setFile(), and setStream().

◆ m_s

std::ostream* TextStream::m_s = nullptr
private

Definition at line 270 of file textstream.h.

Referenced by flush(), setFile(), setStream(), and TextStream().


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