GridComputing
Job Management in Grid Computing
Main Page
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Macros
bytebuffer.h
Go to the documentation of this file.
1
#ifndef BYTEBUFFER_H
2
#define BYTEBUFFER_H
3
4
#include <vector>
5
#include <ostream>
6
// #include <cassert>
7
#include <stdexcept>
8
#include "
utils.h
"
9
10
typedef
uint8
Byte
;
11
13
class
ByteBufferException
:
public
std::exception {};
14
16
21
class
ByteBuffer
22
{
23
public
:
24
ByteBuffer
(
uint32
capacity);
25
ByteBuffer
(
const
ByteBuffer
& other);
26
27
const
Byte
*
Data
()
const
;
28
29
void
Clear
();
30
31
Byte
operator[]
(
uint32
pos)
const
;
32
template
<
typename
T> T
Read
(
uint32
position)
const
;
33
34
uint32
GetReadPos
()
const
;
35
void
SetReadPos
(
uint32
readPos);
36
37
uint32
GetWritePos
()
const
;
38
void
SetWritePos
(
uint32
writePos);
39
40
void
FinishRead
();
41
42
template
<
typename
T>
void
ReadSkip
();
43
void
ReadSkip
(
uint32
size);
44
45
uint32
Size
()
const
;
46
bool
IsEmpty
()
const
;
47
48
void
Resize
(
uint32
newSize);
49
void
Reserve
(
uint32
size);
50
51
void
Print
(std::ostream& stream)
const
;
52
53
void
WriteBool
(
bool
value);
54
void
WriteUInt8
(
uint8
value);
55
void
WriteUInt16
(
uint16
value);
56
void
WriteUInt32
(
uint32
value);
57
void
WriteUInt64
(
uint64
value);
58
void
WriteInt8
(
int8
value);
59
void
WriteInt16
(
int16
value);
60
void
WriteInt32
(
int32
value);
61
void
WriteInt64
(
int64
value);
62
void
WriteFloat
(
float
value);
63
void
WriteDouble
(
double
value);
64
void
WriteString
(
const
std::string& value);
65
void
WriteString
(
const
char
* value) {
WriteString
(std::string(value)); }
66
void
WriteCString
(
const
char
* value);
67
void
WriteCString
(
const
std::string& value) {
WriteCString
(value.c_str()); }
68
void
WriteBuffer
(
const
ByteBuffer
& other);
69
void
WriteBuffer
(
const
char
* src,
uint32
count) {
Append
(src, count); }
70
71
bool
ReadBool
();
72
uint8
ReadUInt8
();
73
uint16
ReadUInt16
();
74
uint32
ReadUInt32
();
75
uint64
ReadUInt64
();
76
int8
ReadInt8
();
77
int16
ReadInt16
();
78
int32
ReadInt32
();
79
int64
ReadInt64
();
80
float
ReadFloat
();
81
double
ReadDouble
();
82
std::string
ReadString
();
83
std::string
ReadCString
();
84
85
ByteBuffer
&
operator <<
(
bool
value) {
WriteBool
(value);
return
*
this
; }
86
ByteBuffer
&
operator <<
(
uint8
value) {
WriteUInt8
(value);
return
*
this
; }
87
ByteBuffer
&
operator <<
(
uint16
value) {
WriteUInt16
(value);
return
*
this
; }
88
ByteBuffer
&
operator <<
(
uint32
value) {
WriteUInt32
(value);
return
*
this
; }
89
ByteBuffer
&
operator <<
(
uint64
value) {
WriteUInt64
(value);
return
*
this
; }
90
ByteBuffer
&
operator <<
(
int8
value) {
WriteInt8
(value);
return
*
this
; }
91
ByteBuffer
&
operator <<
(
int16
value) {
WriteInt16
(value);
return
*
this
; }
92
ByteBuffer
&
operator <<
(
int32
value) {
WriteInt32
(value);
return
*
this
; }
93
ByteBuffer
&
operator <<
(
int64
value) {
WriteInt64
(value);
return
*
this
; }
94
ByteBuffer
&
operator <<
(
float
value) {
WriteFloat
(value);
return
*
this
; }
95
ByteBuffer
&
operator <<
(
double
value) {
WriteDouble
(value);
return
*
this
; }
96
ByteBuffer
&
operator <<
(
const
std::string& value) {
WriteString
(value);
return
*
this
; }
97
ByteBuffer
&
operator <<
(
const
char
* value) {
WriteCString
(value);
return
*
this
; }
98
ByteBuffer
&
operator <<
(
const
ByteBuffer
& value) {
WriteBuffer
(value);
return
*
this
; }
99
100
ByteBuffer
&
operator >>
(
bool
& value) { value =
ReadBool
();
return
*
this
; }
101
ByteBuffer
&
operator >>
(
uint8
& value) { value =
ReadUInt8
();
return
*
this
; }
102
ByteBuffer
&
operator >>
(
uint16
& value) { value =
ReadUInt16
();
return
*
this
; }
103
ByteBuffer
&
operator >>
(
uint32
& value) { value =
ReadUInt32
();
return
*
this
; }
104
ByteBuffer
&
operator >>
(
uint64
& value) { value =
ReadUInt64
();
return
*
this
; }
105
ByteBuffer
&
operator >>
(
int8
& value) { value =
ReadInt8
();
return
*
this
; }
106
ByteBuffer
&
operator >>
(
int16
& value) { value =
ReadInt16
();
return
*
this
; }
107
ByteBuffer
&
operator >>
(
int32
& value) { value =
ReadInt32
();
return
*
this
; }
108
ByteBuffer
&
operator >>
(
int64
& value) { value =
ReadInt64
();
return
*
this
; }
109
ByteBuffer
&
operator >>
(
float
& value) { value =
ReadFloat
();
return
*
this
; }
110
ByteBuffer
&
operator >>
(
double
& value) { value =
ReadDouble
();
return
*
this
; }
111
ByteBuffer
&
operator >>
(std::string& value) { value =
ReadString
();
return
*
this
; }
112
113
operator
const
char
*() {
return
(
const
char
*)
Data
(); }
114
operator
const
std::string() {
return
std::string(
_buffer
.begin(),
_buffer
.end()); }
115
116
protected
:
117
std::vector<Byte>
_buffer
;
118
uint32
_readPos
;
119
uint32
_writePos
;
120
121
// Use stream operators to read and write
122
template
<
typename
T> T
Read
();
123
void
Read
(
Byte
* dest,
uint32
count);
124
template
<
typename
T>
void
Append
(T val);
125
template
<
typename
T>
void
Append
(
const
T* src,
uint32
count);
126
void
Append
(
const
ByteBuffer
& other);
127
void
Append
(
const
Byte
* src,
uint32
count);
128
template
<
typename
T>
void
Put
(
uint32
pos, T val);
129
void
Put
(
uint32
pos,
const
Byte
* src,
uint32
count);
130
131
private
:
132
void
Append7BitEncodedInt
(
uint32
value);
133
uint32
Read7BitEncodedInt
();
134
};
135
136
template
<
typename
T>
137
void
ByteBuffer::ReadSkip
()
138
{
139
ReadSkip
(
sizeof
(T));
140
}
141
142
template
<
typename
T>
143
T
ByteBuffer::Read
(
uint32
position)
const
144
{
145
//assert(position + sizeof(T) <= Size());
146
147
if
(position +
sizeof
(T) >
Size
())
148
throw
ByteBufferException
();
149
150
T value = *((T
const
*)&
_buffer
[position]);
151
return
value;
152
}
153
154
template
<
typename
T>
155
T
ByteBuffer::Read
()
156
{
157
T val = Read<T>(
_readPos
);
158
_readPos
+=
sizeof
(T);
159
return
val;
160
}
161
162
template
<
typename
T>
163
void
ByteBuffer::Append
(T val)
164
{
165
Append
((
Byte
*)&val,
sizeof
(T));
166
}
167
168
template
<
typename
T>
169
void
ByteBuffer::Put
(
uint32
pos, T val)
170
{
171
Put
(pos, (
Byte
*)&val,
sizeof
(T));
172
}
173
174
template
<
typename
T>
175
void
ByteBuffer::Append
(
const
T* src,
uint32
count)
176
{
177
return
Append
((
const
Byte
*)src, count *
sizeof
(T));
178
}
179
180
#endif // BYTEBUFFER_H
src
bytebuffer.h
Generated on Sun Dec 30 2012 14:13:36 for GridComputing by
1.8.2