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