genericstream.h
Go to the documentation of this file.
1 // Generic*Stream code from https://code.google.com/p/rapidjson/issues/detail?id=20
2 #ifndef RAPIDJSON_GENERICSTREAM_H_
3 #define RAPIDJSON_GENERICSTREAM_H_
4 
5 #include "rapidjson.h"
6 #include <iostream>
7 
8 namespace rapidjson {
9 
12  public:
13  typedef char Ch;
14 
16 
19  GenericReadStream(std::istream & is) : is_(&is) {
20  }
21 
22 
23  Ch Peek() const {
24  if(is_->eof()) return '\0';
25  return static_cast<char>(is_->peek());
26  }
27 
28  Ch Take() {
29  if(is_->eof()) return '\0';
30  return static_cast<char>(is_->get());
31  }
32 
33  size_t Tell() const {
34  return (int)is_->tellg();
35  }
36 
37  // Not implemented
38  void Put(Ch) { RAPIDJSON_ASSERT(false); }
39  void Flush() { RAPIDJSON_ASSERT(false); }
40  Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
41  size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
42 
43  std::istream * is_;
44  };
45 
46 
49  public:
50  typedef char Ch;
51 
53 
56  GenericWriteStream(std::ostream& os) : os_(os) {
57  }
58 
59  void Put(char c) {
60  os_.put(c);
61  }
62 
63  void PutN(char c, size_t n) {
64  for (size_t i = 0; i < n; ++i) {
65  Put(c);
66  }
67  }
68 
69  void Flush() {
70  os_.flush();
71  }
72 
73  size_t Tell() const {
74  return (int)os_.tellp();
75  }
76 
77  // Not implemented
78  char Peek() const { RAPIDJSON_ASSERT(false); }
79  char Take() { RAPIDJSON_ASSERT(false); }
80  char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
81  size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; }
82 
83  private:
84  std::ostream& os_;
85  };
86 
87  template<>
88  inline void PutN(GenericWriteStream& stream, char c, size_t n) {
89  stream.PutN(c, n);
90  }
91 
92 } // namespace rapidjson
93 
94 #endif // RAPIDJSON_GENERICSTREAM_H_
int i
Definition: BiCGSTAB_step_by_step.cpp:9
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
Wrapper of std::istream for input.
Definition: genericstream.h:11
Ch Peek() const
Definition: genericstream.h:23
std::istream * is_
Definition: genericstream.h:43
size_t PutEnd(Ch *)
Definition: genericstream.h:41
Ch * PutBegin()
Definition: genericstream.h:40
void Put(Ch)
Definition: genericstream.h:38
char Ch
Character type (byte).
Definition: genericstream.h:13
GenericReadStream(std::istream &is)
Constructor.
Definition: genericstream.h:19
void Flush()
Definition: genericstream.h:39
Ch Take()
Definition: genericstream.h:28
size_t Tell() const
Definition: genericstream.h:33
Wrapper of std::ostream for output.
Definition: genericstream.h:48
void PutN(char c, size_t n)
Definition: genericstream.h:63
size_t Tell() const
Definition: genericstream.h:73
char Take()
Definition: genericstream.h:79
std::ostream & os_
Definition: genericstream.h:84
char Peek() const
Definition: genericstream.h:78
char * PutBegin()
Definition: genericstream.h:80
char Ch
Character type. Only support char.
Definition: genericstream.h:50
size_t PutEnd(char *)
Definition: genericstream.h:81
void Put(char c)
Definition: genericstream.h:59
GenericWriteStream(std::ostream &os)
Constructor.
Definition: genericstream.h:56
void Flush()
Definition: genericstream.h:69
int c
Definition: calibrate.py:100
Definition: document.h:13
void PutN(GenericWriteStream &stream, char c, size_t n)
Definition: genericstream.h:88
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:80