diff options
author | Nicolas Catania <niko@google.com> | 2010-02-03 15:43:36 -0800 |
---|---|---|
committer | Nicolas Catania <niko@google.com> | 2010-02-03 15:43:36 -0800 |
commit | e5b7af0c3c5af401b46d34987cbd8a65f45454de (patch) | |
tree | 459d4e0ccd997fabdbf656a1a5c9632e41732261 | |
parent | d1e702c1f745428a7bc53cbbd80b0c283ca52de1 (diff) | |
download | astl-e5b7af0c3c5af401b46d34987cbd8a65f45454de.tar.gz |
Added support for endl ends and flush.
-rw-r--r-- | include/ostream | 20 | ||||
-rw-r--r-- | src/ostream.cpp | 7 | ||||
-rw-r--r-- | tests/test_iostream.cpp | 7 |
3 files changed, 34 insertions, 0 deletions
diff --git a/include/ostream b/include/ostream index ec2817e..35ff8dc 100644 --- a/include/ostream +++ b/include/ostream @@ -73,8 +73,28 @@ class ostream: public basic_ios } ostream& operator<<(const char_type *str); + + /** + * Tries to insert a char. + */ + ostream& put(char_type c); }; +/** + * Flushes the output stream. + */ +inline ostream& flush(ostream& os) { return os.flush(); } + +/** + * Write a newline and flush the stream. + */ +inline ostream& endl(ostream& os) { return flush(os.put('\n')); } + +/** + * Write a null character into the output sequence. + */ +inline ostream& ends(ostream& os) { return os.put(char()); } + } // namespace std #endif diff --git a/src/ostream.cpp b/src/ostream.cpp index d3efafd..fff02ef 100644 --- a/src/ostream.cpp +++ b/src/ostream.cpp @@ -44,6 +44,13 @@ ostream& ostream::operator<<(const char_type *str) { return *this; } +ostream& ostream::put(char_type c) { + if (this->rdbuf()) { + this->rdbuf()->sputn(&c, 1); + } + return *this; +} + ostream& ostream::flush() { if (this->rdbuf()) { // TODO: if pubsync returns -1 should mark this stream as diff --git a/tests/test_iostream.cpp b/tests/test_iostream.cpp index 3477fc6..a3437a2 100644 --- a/tests/test_iostream.cpp +++ b/tests/test_iostream.cpp @@ -71,11 +71,18 @@ bool testCoutCerr() { return true; } +bool testManip() { + std::cout << "line 1" << std::endl + << " a nul char |" << std::ends << std::flush << "| in line 2."; + return true; +} + } // namespace android int main(int argc, char **argv){ FAIL_UNLESS(testStaticInit); FAIL_UNLESS(testOstream); FAIL_UNLESS(testCoutCerr); + FAIL_UNLESS(testManip); return kPassed; } |