-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuffer_example.cpp
More file actions
59 lines (47 loc) · 1.66 KB
/
buffer_example.cpp
File metadata and controls
59 lines (47 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* Copyright (c) 2012 Aldebaran Robotics. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the COPYING file.
*/
#include <iostream>
#include <qi/buffer.hpp>
#include <qi/types.hpp>
int main(int argc, char **argv)
{
// Create a buffer and add a string in it.
qi::Buffer buffer;
unsigned char tmpChar[] = "ahahahahahahLOLKikou";
buffer.write(tmpChar, sizeof(tmpChar));
// Read the string in the buffer.
const unsigned char *content = (const unsigned char*)buffer.read();
std::cout << content << std::endl;
// Create a buffer and add it as a sub-buffer.
size_t offset;
{
unsigned char tmpChar2[] = "Oh! Oh! Oh! C'est le père Noël!";
qi::Buffer subBuffer;
subBuffer.write(tmpChar2, sizeof(tmpChar2));
offset = buffer.addSubBuffer(subBuffer);
// Here we destroy "subBuffer".
}
// But he's still in buffer.
std::cout << (const unsigned char*)buffer.subBuffer(offset).read() << std::endl;
// Add int and double in the buffer.
int tmpInt = 42;
buffer.write(&tmpInt, sizeof(int));
double tmpDouble = 42;
buffer.write(&tmpDouble, sizeof(double));
// Read the int.
if (buffer.read(&tmpInt, offset + sizeof(qi::uint32_t), sizeof(int)) != sizeof(int)) {
std::cerr << "Can't read an int in the buffer!" << std::endl;
return 1;
}
std::cout << "We read " << tmpInt << std::endl;
// Read the double.
if (buffer.read(&tmpDouble, offset + sizeof(qi::uint32_t) + sizeof(int), sizeof(double)) != sizeof(double)) {
std::cerr << "Can't read a double in the buffer!" << std::endl;
return 1;
}
std::cout << "We read " << tmpDouble << std::endl;
return 0;
}