Remote Call Framework 3.0
MemStream.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2018, Delta V Software. All rights reserved.
6 // http://www.deltavsoft.com
7 //
8 // RCF is distributed under dual licenses - closed source or GPL.
9 // Consult your particular license for conditions of use.
10 //
11 // If you have not purchased a commercial license, you are using RCF
12 // under GPL terms.
13 //
14 // Version: 3.0
15 // Contact: support <at> deltavsoft.com
16 //
17 //******************************************************************************
18 
19 #ifndef INCLUDE_RCF_MEMSTREAM_HPP
20 #define INCLUDE_RCF_MEMSTREAM_HPP
21 
22 #include <istream>
23 #include <streambuf>
24 #include <cstdint>
25 
26 #include <RCF/Config.hpp>
27 #include <RCF/ByteBuffer.hpp>
28 #include <RCF/ReallocBuffer.hpp>
29 #include <RCF/Tools.hpp>
30 
31 namespace RCF {
32 
33  class ByteBuffer;
34 
35  // MemIstreamBuf
36 
37  class MemIstreamBuf :
38  public std::streambuf,
39  Noncopyable
40  {
41  public:
42  MemIstreamBuf(char * buffer = NULL, std::size_t bufferLen = 0);
43  ~MemIstreamBuf();
44  void reset(char * buffer, std::size_t bufferLen);
45 
46  private:
47  std::streambuf::int_type underflow();
48 
49  pos_type seekoff(
50  off_type off,
51  std::ios_base::seekdir dir,
52  std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out);
53 
54  char * mBuffer;
55  std::size_t mBufferLen;
56  };
57 
58  // MemIstream - a replacement for std::istrstream.
59 
60  class RCF_EXPORT MemIstream :
61  public std::basic_istream<char>
62  {
63  public:
64  MemIstream(const char * buffer = NULL, std::size_t bufferLen = 0);
65  ~MemIstream();
66  void reset(const char * buffer, std::size_t bufferLen);
67 
68  private:
69 
70  MemIstreamBuf * mpBuf;
71 
72  public:
73 
74  std::istream::pos_type getReadPos();
75  bool fail();
76  void get(char& ch);
77  void read(char *_Str, std::streamsize _Count);
78  std::streamsize readsome(char *_Str, std::streamsize _Count);
79  std::streamsize gcount();
80  void putback(char _Ch);
81  std::istream::pos_type moveReadPos(std::istream::pos_type newPos);
82 
83  };
84 
85  RCF_EXPORT MemIstream & operator>>(MemIstream & is, std::string & s);
86  RCF_EXPORT MemIstream & operator>>(MemIstream & is, char & ch);
87  RCF_EXPORT MemIstream & operator>>(MemIstream & is, signed char & ch);
88  RCF_EXPORT MemIstream & operator>>(MemIstream & is, unsigned char & ch);
89  RCF_EXPORT MemIstream & operator>>(MemIstream & is, bool & b);
90  RCF_EXPORT MemIstream & operator>>(MemIstream & is, short & b);
91  RCF_EXPORT MemIstream & operator>>(MemIstream & is, unsigned short & b);
92  RCF_EXPORT MemIstream & operator>>(MemIstream & is, int & n);
93  RCF_EXPORT MemIstream & operator>>(MemIstream & is, unsigned int & n);
94  RCF_EXPORT MemIstream & operator>>(MemIstream & is, long & n);
95  RCF_EXPORT MemIstream & operator>>(MemIstream & is, unsigned long & n);
96  RCF_EXPORT MemIstream & operator>>(MemIstream & is, float & d);
97  RCF_EXPORT MemIstream & operator>>(MemIstream & is, double & d);
98  RCF_EXPORT MemIstream & operator>>(MemIstream & is, long double & d);
99 
100 #ifdef _MSC_VER
101  RCF_EXPORT MemIstream & operator>>(MemIstream & is, __int64 & n);
102  RCF_EXPORT MemIstream & operator>>(MemIstream & is, unsigned __int64 & n);
103 #else
104  RCF_EXPORT MemIstream & operator>>(MemIstream & is, long long & n);
105  RCF_EXPORT MemIstream & operator>>(MemIstream & is, unsigned long long & n);
106 #endif
107 
108  // MemOstreamBuf
109 
110  class MemOstreamBuf :
111  public std::streambuf,
112  Noncopyable
113  {
114  public:
115  MemOstreamBuf();
116  ~MemOstreamBuf();
117 
118  private:
119  std::streambuf::int_type overflow(std::streambuf::int_type ch);
120 
121  pos_type seekoff(
122  off_type off,
123  std::ios_base::seekdir dir,
124  std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out);
125 
126  friend class MemOstream;
127  ReallocBuffer mWriteBuffer;
128  };
129 
130  // MemOstream - a replacement for std::ostrstream.
131 
132  class RCF_EXPORT MemOstream :
133  public std::basic_ostream<char>
134  {
135  public:
136  MemOstream();
137  ~MemOstream();
138 
139  char * str();
140  std::size_t length();
141  std::string string();
142 
143  std::size_t capacity();
144 
145  void rewind()
146  {
147  rdbuf()->pubseekoff(0, std::ios::beg, std::ios::out);
148  }
149 
150 
151  private:
152 
153  MemOstreamBuf * mpBuf;
154 
155  public:
156 
157  /*
158  // std:ostream interface.
159 
160  std::ostream::pos_type tellp()
161  {
162  return std::ostream::tellp();
163  }
164 
165  void write(const char * _Str, std::streamsize _Count)
166  {
167  std::ostream::write(_Str, _Count);
168  }
169 
170  bool fail()
171  {
172  return std::ostream::fail();
173  }
174 
175  void clear()
176  {
177  std::ostream::clear();
178  }
179  */
180 
181  };
182 
183  typedef std::shared_ptr<MemOstream> MemOstreamPtr;
184 
185  // iostream impl
186 
187  template<typename T>
188  inline MemOstream & operator<<(MemOstream & os, const T * pt)
189  {
190  static_cast<std::ostream&>(os) << pt;
191  return os;
192  }
193 
194  template<typename T>
195  inline MemOstream & operator<<(MemOstream & os, const std::shared_ptr<T> & pt)
196  {
197  static_cast<std::ostream&>(os) << pt.get();
198  return os;
199  }
200 
201  RCF_EXPORT MemOstream & operator<<(MemOstream & os, const std::string & s);
202  RCF_EXPORT MemOstream & operator<<(MemOstream & os, const char * sz);
203  RCF_EXPORT MemOstream & operator<<(MemOstream & os, char ch);
204  RCF_EXPORT MemOstream & operator<<(MemOstream & os, signed char ch);
205  RCF_EXPORT MemOstream & operator<<(MemOstream & os, unsigned char ch);
206  RCF_EXPORT MemOstream & operator<<(MemOstream & os, bool b);
207  RCF_EXPORT MemOstream & operator<<(MemOstream & os, short b);
208  RCF_EXPORT MemOstream & operator<<(MemOstream & os, unsigned short b);
209  RCF_EXPORT MemOstream & operator<<(MemOstream & os, int n);
210  RCF_EXPORT MemOstream & operator<<(MemOstream & os, unsigned int n);
211  RCF_EXPORT MemOstream & operator<<(MemOstream & os, long n);
212  RCF_EXPORT MemOstream & operator<<(MemOstream & os, unsigned long n);
213  RCF_EXPORT MemOstream & operator<<(MemOstream & os, float d);
214  RCF_EXPORT MemOstream & operator<<(MemOstream & os, double d);
215  RCF_EXPORT MemOstream & operator<<(MemOstream & os, long double d);
216 
217 #ifdef _MSC_VER
218  RCF_EXPORT MemOstream & operator<<(MemOstream & os, __int64 n);
219  RCF_EXPORT MemOstream & operator<<(MemOstream & os, unsigned __int64 n);
220 #else
221  RCF_EXPORT MemOstream & operator<<(MemOstream & os, long long n);
222  RCF_EXPORT MemOstream & operator<<(MemOstream & os, unsigned long long n);
223 #endif
224 
225  typedef std::ostream& (*Pfn)(std::ostream&);
226  RCF_EXPORT MemOstream & operator<<(MemOstream & os, Pfn pfn);
227 
228 } // namespace RCF
229 
230 #endif
Definition: AmiIoHandler.hpp:24