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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
#include <giomm.h>
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
#include <cstdlib>
namespace
{
//Use this line if you want debug output:
//std::ostream& ostr = std::cout;
//This seems nicer and more useful than putting an ifdef around the use of ostr:
std::ostringstream debug;
std::ostream& ostr = debug;
std::string func1_output;
std::string func2_output;
void destroy_func1(void* data)
{
char* cdata = static_cast<char*>(data);
func1_output += "Deleting ";
func1_output += cdata;
delete[] cdata;
}
void destroy_func2(void* data, const Glib::ustring& intro)
{
char* cdata = static_cast<char*>(data);
func2_output += intro + cdata;
delete[] cdata;
}
} // anonymous namespace
int main(int, char**)
{
Glib::init();
Gio::init();
gchar buffer[1000];
std::memset(buffer, 0, sizeof buffer);
try
{
Glib::RefPtr<Gio::MemoryInputStream> stream = Gio::MemoryInputStream::create();
if (!stream)
{
std::cerr << "Could not create a MemoryInputStream." << std::endl;
return EXIT_FAILURE;
}
// Add data that shall not be deleted by stream.
static const char data1[] = "Data not owned by stream.\n";
stream->add_data(data1, sizeof data1 - 1, 0);
// Add data that shall be deleted by destroy_func1().
char* data2 = new char[7];
std::strcpy(data2, "data2\n");
stream->add_data(data2, 6, destroy_func1);
// Add data that shall be deleted by destroy_func2().
char* data3 = new char[7];
std::strcpy(data3, "data3\n");
stream->add_data(data3, 6, sigc::bind(sigc::ptr_fun(destroy_func2), "Now deleting "));
const gsize bytes_read = stream->read(buffer, sizeof buffer - 1);
if (bytes_read)
ostr << "Memory contents read: " << buffer << std::endl;
else
{
std::cerr << "Gio::InputStream::read() read 0 bytes." << std::endl;
return EXIT_FAILURE;
}
}
catch (const Glib::Exception& ex)
{
std::cerr << "Exception caught: " << ex.what() << std::endl;
return EXIT_FAILURE;
}
ostr << func1_output << std::endl;
ostr << func2_output << std::endl;
if (std::strcmp(buffer, "Data not owned by stream.\ndata2\ndata3\n") == 0 &&
func1_output == "Deleting data2\n" &&
func2_output == "Now deleting data3\n")
return EXIT_SUCCESS;
std::cerr << "buffer: \"" << buffer << "\"" << std::endl;
std::cerr << "func1_output: \"" << func1_output << "\"" << std::endl;
std::cerr << "func2_output: \"" << func2_output << "\"" << std::endl;
return EXIT_FAILURE;
}
|