/// a chunk at a time.
class raw_ostream {
protected:
+ /// \invariant { The buffer is uninitialized (OutBufStart,
+ /// OutBufEnd, and OutBufCur are non-zero), or none of them are zero
+ /// and there are at least 64 total bytes in the buffer. }
+
char *OutBufStart, *OutBufEnd, *OutBufCur;
bool Unbuffered;
void flush() {
if (OutBufCur != OutBufStart)
- flush_impl();
+ flush_nonempty();
}
raw_ostream &operator<<(char C) {
return write(C);
*OutBufCur++ = C;
if (Unbuffered)
- flush_impl();
+ flush_nonempty();
return *this;
}
return write(C);
*OutBufCur++ = C;
if (Unbuffered)
- flush_impl();
+ flush_nonempty();
return *this;
}
return write(C);
*OutBufCur++ = C;
if (Unbuffered)
- flush_impl();
+ flush_nonempty();
return *this;
}
// Subclass Interface
//===--------------------------------------------------------------------===//
-protected:
-
+private:
/// flush_impl - The is the piece of the class that is implemented by
- /// subclasses. This outputs the currently buffered data and resets the
- /// buffer to empty.
+ /// subclasses. This only outputs the currently buffered data.
+ ///
+ /// raw_ostream guarantees to only call this routine when there is
+ /// buffered data, i.e. OutBufStart != OutBufCur.
virtual void flush_impl() = 0;
- /// HandleFlush - A stream's implementation of flush should call this after
- /// emitting the bytes to the data sink.
- void HandleFlush() {
- if (OutBufStart == 0)
- SetBufferSize(4096);
- OutBufCur = OutBufStart;
- }
-private:
// An out of line virtual method to provide a home for the class vtable.
virtual void handle();
+
+ //===--------------------------------------------------------------------===//
+ // Private Interface
+ //===--------------------------------------------------------------------===//
+private:
+ /// flush_nonempty - Flush the current buffer, which is known to be
+ /// non-empty. This outputs the currently buffered data and resets
+ /// the buffer to empty.
+ void flush_nonempty();
};
//===----------------------------------------------------------------------===//
~raw_fd_ostream();
/// flush_impl - The is the piece of the class that is implemented by
- /// subclasses. This outputs the currently buffered data and resets the
- /// buffer to empty.
+ /// subclasses. This only outputs the currently buffered data.
+ ///
+ /// raw_ostream guarantees to only call this routine when there is
+ /// buffered data, i.e. OutBufStart != OutBufCur.
virtual void flush_impl();
/// close - Manually flush the stream and close the file.
~raw_os_ostream();
/// flush_impl - The is the piece of the class that is implemented by
- /// subclasses. This outputs the currently buffered data and resets the
- /// buffer to empty.
+ /// subclasses. This only outputs the currently buffered data.
+ ///
+ /// raw_ostream guarantees to only call this routine when there is
+ /// buffered data, i.e. OutBufStart != OutBufCur.
virtual void flush_impl();
};
}
/// flush_impl - The is the piece of the class that is implemented by
- /// subclasses. This outputs the currently buffered data and resets the
- /// buffer to empty.
+ /// subclasses. This only outputs the currently buffered data.
+ ///
+ /// raw_ostream guarantees to only call this routine when there is
+ /// buffered data, i.e. OutBufStart != OutBufCur.
virtual void flush_impl();
};
~raw_svector_ostream();
/// flush_impl - The is the piece of the class that is implemented by
- /// subclasses. This outputs the currently buffered data and resets the
- /// buffer to empty.
+ /// subclasses. This only outputs the currently buffered data.
+ ///
+ /// raw_ostream guarantees to only call this routine when there is
+ /// buffered data, i.e. OutBufStart != OutBufCur.
virtual void flush_impl();
};
return write(CurPtr, EndPtr-CurPtr);
}
+void raw_ostream::flush_nonempty() {
+ assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty.");
+ flush_impl();
+ OutBufCur = OutBufStart;
+}
+
raw_ostream &raw_ostream::write(unsigned char C) {
- if (OutBufCur >= OutBufEnd)
- flush_impl();
+ if (!OutBufStart)
+ SetBufferSize(4096);
+ else if (OutBufCur >= OutBufEnd)
+ flush_nonempty();
*OutBufCur++ = C;
return *this;
}
raw_ostream &raw_ostream::write(const char *Ptr, unsigned Size) {
- if (OutBufCur+Size > OutBufEnd)
- flush_impl();
+ if (!OutBufStart)
+ SetBufferSize(4096);
+ else if (OutBufCur+Size > OutBufEnd)
+ flush_nonempty();
// Handle short strings specially, memcpy isn't very good at very short
// strings.
Ptr += NumToEmit;
Size -= NumToEmit;
OutBufCur = OutBufStart + NumToEmit;
- flush_impl();
+ flush_nonempty();
}
break;
}
OutBufCur += Size;
if (Unbuffered)
- flush_impl();
+ flush();
return *this;
}
void raw_fd_ostream::flush_impl() {
assert (FD >= 0 && "File already closed.");
- if (OutBufCur-OutBufStart) {
- pos += (OutBufCur - OutBufStart);
- ::write(FD, OutBufStart, OutBufCur-OutBufStart);
- }
- HandleFlush();
+ pos += (OutBufCur - OutBufStart);
+ ::write(FD, OutBufStart, OutBufCur-OutBufStart);
}
void raw_fd_ostream::close() {
/// subclasses. This outputs the currently buffered data and resets the
/// buffer to empty.
void raw_os_ostream::flush_impl() {
- if (OutBufCur-OutBufStart)
- OS.write(OutBufStart, OutBufCur-OutBufStart);
- HandleFlush();
+ OS.write(OutBufStart, OutBufCur-OutBufStart);
}
//===----------------------------------------------------------------------===//
/// subclasses. This outputs the currently buffered data and resets the
/// buffer to empty.
void raw_string_ostream::flush_impl() {
- if (OutBufCur-OutBufStart)
- OS.append(OutBufStart, OutBufCur-OutBufStart);
- HandleFlush();
+ OS.append(OutBufStart, OutBufCur-OutBufStart);
}
//===----------------------------------------------------------------------===//
/// subclasses. This outputs the currently buffered data and resets the
/// buffer to empty.
void raw_svector_ostream::flush_impl() {
- if (OutBufCur-OutBufStart)
- OS.append(OutBufStart, OutBufCur);
- HandleFlush();
+ OS.append(OutBufStart, OutBufCur);
}