RCFProto

- Main Page
- Classes
- Files

- File List

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