RCFProto
 All Classes Functions Typedefs
ByteBuffer.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_BYTEBUFFER_HPP
20 #define INCLUDE_RCF_BYTEBUFFER_HPP
21 
22 #include <string>
23 #include <vector>
24 
25 #include <boost/shared_ptr.hpp>
26 
27 #include <RCF/Export.hpp>
28 #include <RCF/MemStream.hpp>
29 #include <RCF/MinMax.hpp>
30 #include <RCF/ReallocBuffer.hpp>
31 
32 namespace RCF {
33 
34  class MemOstream;
35  typedef boost::shared_ptr<MemOstream> MemOstreamPtr;
36 
37  // ByteBuffer class for facilitating zero-copy transmission and reception
38 
39  class RCF_EXPORT ByteBuffer
40  {
41  public:
42 
43  ByteBuffer();
44 
45  explicit
46  ByteBuffer(std::size_t pvlen);
47 
48  explicit
49  ByteBuffer(
50  const std::vector<char> & vc);
51 
52  explicit
53  ByteBuffer(
54  const std::string & s);
55 
56  explicit
57  ByteBuffer(
58  boost::shared_ptr<std::vector<char> > spvc,
59  bool readOnly = false);
60 
61  explicit
62  ByteBuffer(
63  ReallocBufferPtr sprb,
64  bool readOnly = false);
65 
66  explicit
67  ByteBuffer(
68  MemOstreamPtr spos,
69  bool readOnly = false);
70 
71  ByteBuffer(
72  char *pv,
73  std::size_t pvlen,
74  bool readOnly = false);
75 
76  ByteBuffer(
77  char *pv,
78  std::size_t pvlen,
79  std::size_t leftMargin,
80  bool readOnly = false);
81 
82  ByteBuffer(
83  char *pv,
84  std::size_t pvlen,
85  boost::shared_ptr<MemOstream> spos,
86  bool readOnly = false);
87 
88  ByteBuffer(
89  char *pv,
90  std::size_t pvlen,
91  std::size_t leftMargin,
92  boost::shared_ptr<MemOstream> spos,
93  bool readOnly = false);
94 
95  ByteBuffer(
96  char *pv,
97  std::size_t pvlen,
98  boost::shared_ptr<std::vector<char> > spvc,
99  bool readOnly = false);
100 
101  ByteBuffer(
102  char *pv,
103  std::size_t pvlen,
104  std::size_t leftMargin,
105  boost::shared_ptr<std::vector<char> > spvc,
106  bool readOnly = false);
107 
108  ByteBuffer(
109  char *pv,
110  std::size_t pvlen,
111  ReallocBufferPtr sprb,
112  bool readOnly = false);
113 
114  ByteBuffer(
115  char *pv,
116  std::size_t pvlen,
117  std::size_t leftMargin,
118  ReallocBufferPtr sprb,
119  bool readOnly = false);
120 
121  ByteBuffer(
122  const ByteBuffer & byteBuffer,
123  std::size_t offset = 0,
124  std::size_t len = std::size_t(-1));
125 
126  char * getPtr() const;
127  std::size_t getLength() const;
128  std::size_t getLeftMargin() const;
129  bool getReadOnly() const;
130  bool isEmpty() const;
131  std::string string() const;
132 
133  void setLeftMargin(std::size_t len);
134  void expandIntoLeftMargin(std::size_t len);
135  ByteBuffer release();
136  void swap(ByteBuffer & rhs);
137  void clear();
138 
139  operator bool();
140  bool operator !();
141 
142  static const std::size_t npos;
143 
144  private:
145  // sentries
146  boost::shared_ptr< std::vector<char> > mSpvc;
147  boost::shared_ptr< MemOstream > mSpos;
148  boost::shared_ptr< ReallocBuffer > mSprb;
149 
150  char * mPv;
151  std::size_t mPvlen;
152  std::size_t mLeftMargin;
153  bool mReadOnly;
154  };
155 
156  RCF_EXPORT bool operator==(const ByteBuffer &lhs, const ByteBuffer &rhs);
157 
158  RCF_EXPORT std::size_t lengthByteBuffers(
159  const std::vector<ByteBuffer> &byteBuffers);
160 
161  template<typename Functor>
162  inline void forEachByteBuffer(
163  const Functor &functor,
164  const std::vector<ByteBuffer> &byteBuffers,
165  std::size_t offset,
166  std::size_t length = -1)
167  {
168  std::size_t pos0 = 0;
169  std::size_t pos1 = 0;
170  std::size_t remaining = length;
171 
172  for (std::size_t i=0; i<byteBuffers.size(); ++i)
173  {
174  pos1 = pos0 + byteBuffers[i].getLength() ;
175 
176  if (pos1 <= offset)
177  {
178  pos0 = pos1;
179  }
180  else if (pos0 <= offset && offset < pos1)
181  {
182  std::size_t len = RCF_MIN(pos1-offset, remaining);
183 
184  ByteBuffer byteBuffer(
185  byteBuffers[i],
186  offset-pos0,
187  len);
188 
189  functor(byteBuffer);
190  pos0 = pos1;
191  remaining -= len;
192  }
193  else if (remaining > 0)
194  {
195  std::size_t len = RCF_MIN(pos1-pos0, remaining);
196 
197  ByteBuffer byteBuffer(
198  byteBuffers[i],
199  0,
200  len);
201 
202  functor(byteBuffer);
203  pos1 = pos0;
204  remaining -= len;
205  }
206  }
207  }
208 
209  RCF_EXPORT ByteBuffer sliceByteBuffer(
210  const std::vector<ByteBuffer> &slicedBuffers,
211  std::size_t offset,
212  std::size_t length = -1);
213 
214  RCF_EXPORT void sliceByteBuffers(
215  std::vector<ByteBuffer> &slicedBuffers,
216  const std::vector<ByteBuffer> &byteBuffers,
217  std::size_t offset,
218  std::size_t length = std::size_t(-1));
219 
220  RCF_EXPORT void copyByteBuffers(
221  const std::vector<ByteBuffer> &byteBuffers,
222  char *pch);
223 
224  RCF_EXPORT void copyByteBuffers(
225  const std::vector<ByteBuffer> &byteBuffers,
226  ByteBuffer &byteBuffer);
227 
228 } // namespace RCF
229 
230 namespace SF {
231 
232  class Archive;
233 
234  RCF_EXPORT void serialize(SF::Archive &ar, RCF::ByteBuffer &byteBuffer);
235 
236 }
237 
238 #endif // ! INCLUDE_RCF_BYTEBUFFER_HPP