Commit c3133def authored by Milo Yip's avatar Milo Yip

Renamed IStreamWrapper/OStreamWrapper

parent b9bca8e5
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
#include "stream.h" #include "stream.h"
#include <iosfwd>
#ifdef __clang__ #ifdef __clang__
RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PUSH
...@@ -38,10 +39,10 @@ RAPIDJSON_NAMESPACE_BEGIN ...@@ -38,10 +39,10 @@ RAPIDJSON_NAMESPACE_BEGIN
*/ */
template <typename StreamType> template <typename StreamType>
class IStreamWrapper { class BasicIStreamWrapper {
public: public:
typedef typename StreamType::char_type Ch; typedef typename StreamType::char_type Ch;
IStreamWrapper(StreamType& stream) : stream_(stream), count_(), peekBuffer_() {} BasicIStreamWrapper(StreamType& stream) : stream_(stream), count_(), peekBuffer_() {}
Ch Peek() const { Ch Peek() const {
typename StreamType::int_type c = stream_.peek(); typename StreamType::int_type c = stream_.peek();
...@@ -91,6 +92,9 @@ private: ...@@ -91,6 +92,9 @@ private:
mutable Ch peekBuffer_[4]; mutable Ch peekBuffer_[4];
}; };
typedef BasicIStreamWrapper<std::istream> IStreamWrapper;
typedef BasicIStreamWrapper<std::wistream> WIStreamWrapper;
#ifdef __clang__ #ifdef __clang__
RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_POP
#endif #endif
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
#include "stream.h" #include "stream.h"
#include <iosfwd>
#ifdef __clang__ #ifdef __clang__
RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PUSH
...@@ -38,10 +39,10 @@ RAPIDJSON_NAMESPACE_BEGIN ...@@ -38,10 +39,10 @@ RAPIDJSON_NAMESPACE_BEGIN
*/ */
template <typename StreamType> template <typename StreamType>
class OStreamWrapper { class BasicOStreamWrapper {
public: public:
typedef typename StreamType::char_type Ch; typedef typename StreamType::char_type Ch;
OStreamWrapper(StreamType& stream) : stream_(stream) {} BasicOStreamWrapper(StreamType& stream) : stream_(stream) {}
void Put(Ch c) { void Put(Ch c) {
stream_.put(c); stream_.put(c);
...@@ -62,6 +63,9 @@ private: ...@@ -62,6 +63,9 @@ private:
StreamType& stream_; StreamType& stream_;
}; };
typedef BasicOStreamWrapper<std::ostream> OStreamWrapper;
typedef BasicOStreamWrapper<std::wostream> WOStreamWrapper;
#ifdef __clang__ #ifdef __clang__
RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_POP
#endif #endif
......
...@@ -29,7 +29,7 @@ static void TestStringStream() { ...@@ -29,7 +29,7 @@ static void TestStringStream() {
{ {
StringStreamType iss; StringStreamType iss;
IStreamWrapper<StringStreamType> is(iss); BasicIStreamWrapper<StringStreamType> is(iss);
EXPECT_EQ(0, is.Tell()); EXPECT_EQ(0, is.Tell());
if (sizeof(Ch) == 1) { if (sizeof(Ch) == 1) {
EXPECT_EQ(0, is.Peek4()); EXPECT_EQ(0, is.Peek4());
...@@ -43,7 +43,7 @@ static void TestStringStream() { ...@@ -43,7 +43,7 @@ static void TestStringStream() {
{ {
Ch s[] = { 'A', 'B', 'C', '\0' }; Ch s[] = { 'A', 'B', 'C', '\0' };
StringStreamType iss(s); StringStreamType iss(s);
IStreamWrapper<StringStreamType> is(iss); BasicIStreamWrapper<StringStreamType> is(iss);
EXPECT_EQ(0, is.Tell()); EXPECT_EQ(0, is.Tell());
if (sizeof(Ch) == 1) if (sizeof(Ch) == 1)
EXPECT_EQ(0, is.Peek4()); // less than 4 bytes EXPECT_EQ(0, is.Peek4()); // less than 4 bytes
...@@ -61,7 +61,7 @@ static void TestStringStream() { ...@@ -61,7 +61,7 @@ static void TestStringStream() {
{ {
Ch s[] = { 'A', 'B', 'C', 'D', 'E', '\0' }; Ch s[] = { 'A', 'B', 'C', 'D', 'E', '\0' };
StringStreamType iss(s); StringStreamType iss(s);
IStreamWrapper<StringStreamType> is(iss); BasicIStreamWrapper<StringStreamType> is(iss);
if (sizeof(Ch) == 1) { if (sizeof(Ch) == 1) {
const Ch* c = is.Peek4(); const Ch* c = is.Peek4();
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
...@@ -118,8 +118,8 @@ static bool Open(FileStreamType& fs, const char* filename) { ...@@ -118,8 +118,8 @@ static bool Open(FileStreamType& fs, const char* filename) {
TEST(IStreamWrapper, ifstream) { TEST(IStreamWrapper, ifstream) {
ifstream ifs; ifstream ifs;
ASSERT_TRUE(Open(ifs, "utf8bom.json")); ASSERT_TRUE(Open(ifs, "utf8bom.json"));
IStreamWrapper<ifstream> isw(ifs); IStreamWrapper isw(ifs);
EncodedInputStream<UTF8<>, IStreamWrapper<ifstream> > eis(isw); EncodedInputStream<UTF8<>, IStreamWrapper> eis(isw);
Document d; Document d;
EXPECT_TRUE(!d.ParseStream(eis).HasParseError()); EXPECT_TRUE(!d.ParseStream(eis).HasParseError());
EXPECT_TRUE(d.IsObject()); EXPECT_TRUE(d.IsObject());
...@@ -129,8 +129,8 @@ TEST(IStreamWrapper, ifstream) { ...@@ -129,8 +129,8 @@ TEST(IStreamWrapper, ifstream) {
TEST(IStreamWrapper, fstream) { TEST(IStreamWrapper, fstream) {
fstream fs; fstream fs;
ASSERT_TRUE(Open(fs, "utf8bom.json")); ASSERT_TRUE(Open(fs, "utf8bom.json"));
IStreamWrapper<fstream> isw(fs); IStreamWrapper isw(fs);
EncodedInputStream<UTF8<>, IStreamWrapper<fstream> > eis(isw); EncodedInputStream<UTF8<>, IStreamWrapper> eis(isw);
Document d; Document d;
EXPECT_TRUE(!d.ParseStream(eis).HasParseError()); EXPECT_TRUE(!d.ParseStream(eis).HasParseError());
EXPECT_TRUE(d.IsObject()); EXPECT_TRUE(d.IsObject());
...@@ -147,9 +147,9 @@ TEST(IStreamWrapper, wifstream) { ...@@ -147,9 +147,9 @@ TEST(IStreamWrapper, wifstream) {
ASSERT_TRUE(Open(ifs, "utf16bebom.json")); ASSERT_TRUE(Open(ifs, "utf16bebom.json"));
ifs.imbue(std::locale(ifs.getloc(), ifs.imbue(std::locale(ifs.getloc(),
new std::codecvt_utf16<wchar_t, 0x10ffff, std::consume_header>)); new std::codecvt_utf16<wchar_t, 0x10ffff, std::consume_header>));
IStreamWrapper<wifstream> isw(ifs); WIStreamWrapper isw(ifs);
GenericDocument<UTF16<> > d; GenericDocument<UTF16<> > d;
d.ParseStream<kParseDefaultFlags, UTF16<>, IStreamWrapper<wifstream> >(isw); d.ParseStream<kParseDefaultFlags, UTF16<>, WIStreamWrapper>(isw);
EXPECT_TRUE(!d.HasParseError()); EXPECT_TRUE(!d.HasParseError());
EXPECT_TRUE(d.IsObject()); EXPECT_TRUE(d.IsObject());
EXPECT_EQ(5, d.MemberCount()); EXPECT_EQ(5, d.MemberCount());
...@@ -160,9 +160,9 @@ TEST(IStreamWrapper, wfstream) { ...@@ -160,9 +160,9 @@ TEST(IStreamWrapper, wfstream) {
ASSERT_TRUE(Open(fs, "utf16bebom.json")); ASSERT_TRUE(Open(fs, "utf16bebom.json"));
fs.imbue(std::locale(fs.getloc(), fs.imbue(std::locale(fs.getloc(),
new std::codecvt_utf16<wchar_t, 0x10ffff, std::consume_header>)); new std::codecvt_utf16<wchar_t, 0x10ffff, std::consume_header>));
IStreamWrapper<wfstream> isw(fs); WIStreamWrapper isw(fs);
GenericDocument<UTF16<> > d; GenericDocument<UTF16<> > d;
d.ParseStream<kParseDefaultFlags, UTF16<>, IStreamWrapper<wfstream> >(isw); d.ParseStream<kParseDefaultFlags, UTF16<>, WIStreamWrapper>(isw);
EXPECT_TRUE(!d.HasParseError()); EXPECT_TRUE(!d.HasParseError());
EXPECT_TRUE(d.IsObject()); EXPECT_TRUE(d.IsObject());
EXPECT_EQ(5, d.MemberCount()); EXPECT_EQ(5, d.MemberCount());
......
...@@ -29,7 +29,7 @@ static void TestStringStream() { ...@@ -29,7 +29,7 @@ static void TestStringStream() {
Ch s[] = { 'A', 'B', 'C' }; Ch s[] = { 'A', 'B', 'C' };
StringStreamType oss(s); StringStreamType oss(s);
OStreamWrapper<StringStreamType> os(oss); BasicOStreamWrapper<StringStreamType> os(oss);
for (size_t i = 0; i < 3; i++) for (size_t i = 0; i < 3; i++)
os.Put(s[i]); os.Put(s[i]);
os.Flush(); os.Flush();
...@@ -54,7 +54,7 @@ TEST(OStreamWrapper, wstringstream) { ...@@ -54,7 +54,7 @@ TEST(OStreamWrapper, wstringstream) {
} }
TEST(OStreamWrapper, cout) { TEST(OStreamWrapper, cout) {
OStreamWrapper<ostream> os(cout); OStreamWrapper os(cout);
const char* s = "Hello World!\n"; const char* s = "Hello World!\n";
while (*s) while (*s)
os.Put(*s++); os.Put(*s++);
...@@ -70,7 +70,7 @@ static void TestFileStream() { ...@@ -70,7 +70,7 @@ static void TestFileStream() {
const char* s = "Hello World!\n"; const char* s = "Hello World!\n";
{ {
ofstream ofs(filename, ios::out | ios::binary); ofstream ofs(filename, ios::out | ios::binary);
OStreamWrapper<ofstream> osw(ofs); BasicOStreamWrapper<ofstream> osw(ofs);
for (const char* p = s; *p; p++) for (const char* p = s; *p; p++)
osw.Put(*p); osw.Put(*p);
osw.Flush(); osw.Flush();
......
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