genericstream.h
Go to the documentation of this file.
1 //Copyright (c) 2013-2023, The MercuryDPM Developers Team. All rights reserved.
2 //For the list of developers, see <http://www.MercuryDPM.org/Team>.
3 //
4 //Redistribution and use in source and binary forms, with or without
5 //modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name MercuryDPM nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 //DISCLAIMED. IN NO EVENT SHALL THE MERCURYDPM DEVELOPERS TEAM BE LIABLE FOR ANY
19 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 // Generic*Stream code from https://code.google.com/p/rapidjson/issues/detail?id=20
27 #ifndef RAPIDJSON_GENERICSTREAM_H_
28 #define RAPIDJSON_GENERICSTREAM_H_
29 
30 #include "rapidjson.h"
31 #include <iostream>
32 
33 namespace rapidjson {
34 
37  public:
38  typedef char Ch;
39 
41 
44  GenericReadStream(std::istream & is) : is_(&is) {
45  }
46 
47 
48  Ch Peek() const {
49  if(is_->eof()) return '\0';
50  return static_cast<char>(is_->peek());
51  }
52 
53  Ch Take() {
54  if(is_->eof()) return '\0';
55  return static_cast<char>(is_->get());
56  }
57 
58  size_t Tell() const {
59  return (int)is_->tellg();
60  }
61 
62  // Not implemented
63  void Put(Ch) { RAPIDJSON_ASSERT(false); }
64  void Flush() { RAPIDJSON_ASSERT(false); }
65  Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
66  size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
67 
68  std::istream * is_;
69  };
70 
71 
74  public:
75  typedef char Ch;
76 
78 
81  GenericWriteStream(std::ostream& os) : os_(os) {
82  }
83 
84  void Put(char c) {
85  os_.put(c);
86  }
87 
88  void PutN(char c, size_t n) {
89  for (size_t i = 0; i < n; ++i) {
90  Put(c);
91  }
92  }
93 
94  void Flush() {
95  os_.flush();
96  }
97 
98  size_t Tell() const {
99  return (int)os_.tellp();
100  }
101 
102  // Not implemented
103  char Peek() const { RAPIDJSON_ASSERT(false); }
104  char Take() { RAPIDJSON_ASSERT(false); }
105  char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
106  size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; }
107 
108  private:
109  std::ostream& os_;
110  };
111 
112  template<>
113  inline void PutN(GenericWriteStream& stream, char c, size_t n) {
114  stream.PutN(c, n);
115  }
116 
117 } // namespace rapidjson
118 
119 #endif // RAPIDJSON_GENERICSTREAM_H_
const unsigned n
Definition: CG3DPackingUnitTest.cpp:32
Wrapper of std::istream for input.
Definition: genericstream.h:36
Ch Peek() const
Definition: genericstream.h:48
std::istream * is_
Definition: genericstream.h:68
size_t PutEnd(Ch *)
Definition: genericstream.h:66
Ch * PutBegin()
Definition: genericstream.h:65
void Put(Ch)
Definition: genericstream.h:63
char Ch
Character type (byte).
Definition: genericstream.h:38
GenericReadStream(std::istream &is)
Constructor.
Definition: genericstream.h:44
void Flush()
Definition: genericstream.h:64
Ch Take()
Definition: genericstream.h:53
size_t Tell() const
Definition: genericstream.h:58
Wrapper of std::ostream for output.
Definition: genericstream.h:73
void PutN(char c, size_t n)
Definition: genericstream.h:88
size_t Tell() const
Definition: genericstream.h:98
char Take()
Definition: genericstream.h:104
std::ostream & os_
Definition: genericstream.h:109
char Peek() const
Definition: genericstream.h:103
char * PutBegin()
Definition: genericstream.h:105
char Ch
Character type. Only support char.
Definition: genericstream.h:75
size_t PutEnd(char *)
Definition: genericstream.h:106
void Put(char c)
Definition: genericstream.h:84
GenericWriteStream(std::ostream &os)
Constructor.
Definition: genericstream.h:81
void Flush()
Definition: genericstream.h:94
const std::complex< Mdouble > i
Definition: ExtendedMath.h:51
Definition: document.h:38
void PutN(GenericWriteStream &stream, char c, size_t n)
Definition: genericstream.h:113
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:105