Commit 08d25ad1 authored by miloyip@gmail.com's avatar miloyip@gmail.com

Added Flush() to concept stream. Add this new member function to all streams.

Writer and PrettyWriter automatically calls Flush() at the end of JSON text.

git-svn-id: https://rapidjson.googlecode.com/svn/trunk@27 c5894555-1306-4e8d-425f-1f6f381ee07c
parent 99de5312
......@@ -27,7 +27,5 @@ int main(int argc, char* argv[]) {
return 1;
}
os.Flush();
return 0;
}
......@@ -24,6 +24,5 @@ int main(int argc, char* argv[]) {
return 1;
}
os.Flush();
return 0;
}
......@@ -25,6 +25,7 @@ public:
// Not implemented
void Put(char c) { RAPIDJSON_ASSERT(false); }
void Flush() { RAPIDJSON_ASSERT(false); }
char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; }
......
......@@ -21,6 +21,7 @@ public:
char Take() { char c = current_; Read(); return c; }
size_t Tell() const { return count_; }
void Put(char c) { fputc(c, fp_); }
void Flush() { fflush(fp_); }
// Not implemented
char* PutBegin() { return 0; }
......
......@@ -64,7 +64,8 @@ public:
T* Bottom() { return (T*)stack_; }
Allocator& GetAllocator() { return *allocator_; }
size_t GetSize() const { /*return stack_top_;*/ return stack_top_ - stack_; }
bool Empty() const { return stack_top_ == stack_; }
size_t GetSize() const { return stack_top_ - stack_; }
size_t GetCapacity() const { return stack_capacity_; }
private:
......
......@@ -71,6 +71,8 @@ public:
WriteIndent();
}
Base::WriteEndObject();
if (Base::level_stack_.Empty()) // end of json text
Base::stream_.Flush();
return *this;
}
......@@ -91,6 +93,8 @@ public:
WriteIndent();
}
Base::WriteEndArray();
if (Base::level_stack_.Empty()) // end of json text
Base::stream_.Flush();
return *this;
}
......
......@@ -397,9 +397,9 @@ struct UTF32 {
/*! \class rapidjson::Stream
\brief Concept for reading and writing characters.
For read-only stream, no need to implement PutBegin(), Put() and PutEnd().
For read-only stream, no need to implement PutBegin(), Put(), Flush() and PutEnd().
For write-only stream, only need to implement Put().
For write-only stream, only need to implement Put() and Flush().
\code
concept Stream {
......@@ -422,6 +422,9 @@ concept Stream {
//! Write a character.
void Put(Ch c);
//! Flush the buffer.
void Flush();
//! End the writing operation.
//! \param begin The begin write pointer returned by PutBegin().
//! \return Number of characters written.
......@@ -455,6 +458,7 @@ struct GenericStringStream {
Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
void Put(Ch c) { RAPIDJSON_ASSERT(false); }
void Flush() { RAPIDJSON_ASSERT(false); }
size_t PutEnd(Ch* begin) { RAPIDJSON_ASSERT(false); return 0; }
const Ch* src_; //!< Current read position.
......@@ -484,6 +488,7 @@ struct GenericInsituStringStream {
// Write
Ch* PutBegin() { return dst_ = src_; }
void Put(Ch c) { RAPIDJSON_ASSERT(dst_ != 0); *dst_++ = c; }
void Flush() {}
size_t PutEnd(Ch* begin) { return dst_ - begin; }
Ch* src_;
......
......@@ -19,6 +19,7 @@ struct GenericStringBuffer {
GenericStringBuffer(Allocator* allocator = 0, size_t capacity = kDefaultCapacity) : stack_(allocator, capacity) {}
void Put(Ch c) { *stack_.template Push<Ch>() = c; }
void Flush() {}
void Clear() { stack_.Clear(); }
......
......@@ -59,6 +59,8 @@ public:
RAPIDJSON_ASSERT(!level_stack_.template Top<Level>()->inArray);
level_stack_.template Pop<Level>(1);
WriteEndObject();
if (level_stack_.Empty()) // end of json text
stream_.Flush();
return *this;
}
......@@ -74,6 +76,8 @@ public:
RAPIDJSON_ASSERT(level_stack_.template Top<Level>()->inArray);
level_stack_.template Pop<Level>(1);
WriteEndArray();
if (level_stack_.Empty()) // end of json text
stream_.Flush();
return *this;
}
//@}
......
#ifndef PERFTEST_H_
#define PERFTEST_H_
#define TEST_RAPIDJSON 1
#define TEST_JSONCPP 1
#define TEST_YAJL 1
#define TEST_RAPIDJSON 0
#define TEST_JSONCPP 0
#define TEST_YAJL 0
#define TEST_PLATFORM 1
#if TEST_RAPIDJSON
//#define RAPIDJSON_SSE2
//#define RAPIDJSON_SSE42
......@@ -44,7 +44,6 @@ public:
json_ = (char*)malloc(length_ + 1);
fread(json_, 1, length_, fp);
json_[length_] = '\0';
length_++; // include the null terminator
fclose(fp);
// whitespace test
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment