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