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
|
// -*- Mode: C++; indent-tabs-mode: nil; c-basic-offset: 2 -*-
/* Copyright (C) 2007 The gtkmm Development Team
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <gio/gio.h>
namespace
{
class SlotWithData
{
public:
SlotWithData(const Gio::MemoryInputStream::SlotDestroyData& slot, void* data)
:
m_slot(new Gio::MemoryInputStream::SlotDestroyData(slot)), m_data(data)
{ }
~SlotWithData() { delete m_slot; }
void operator()() { (*m_slot)(m_data); }
private:
Gio::MemoryInputStream::SlotDestroyData* m_slot;
void* m_data;
};
void destroy_data_callback(void* user_data)
{
auto slot_with_data = static_cast<SlotWithData*>(user_data);
g_return_if_fail(slot_with_data != nullptr);
try
{
(*slot_with_data)(); // Invoke callback
}
catch (...)
{
Glib::exception_handlers_invoke();
}
delete slot_with_data;
}
} // anonymous namespace
namespace Gio
{
_DEPRECATE_IFDEF_START
void MemoryInputStream::add_data(const std::string& data)
{
char *data_copy = g_strdup (data.c_str ());
g_memory_input_stream_add_data(gobj(), data_copy, -1, g_free);
}
void MemoryInputStream::add_data(const void* data, gssize len)
{
char *data_copy = nullptr;
// copy the data so that the caller doesn't need to keep the data alive
if (len < 0)
data_copy = g_strdup (static_cast<const gchar*>(data));
else
data_copy = static_cast<gchar*>(g_memdup (data, len));
g_memory_input_stream_add_data(gobj(), data_copy, len, g_free);
}
_DEPRECATE_IFDEF_END
void MemoryInputStream::add_data(const void* data, gssize len, const SlotDestroyData& destroy_slot)
{
auto slot_with_data = new SlotWithData(destroy_slot, const_cast<void*>(data));
auto bytes = g_bytes_new_with_free_func(data, len, &destroy_data_callback, slot_with_data);
g_memory_input_stream_add_bytes(gobj(), bytes);
g_bytes_unref(bytes); // g_memory_input_stream_add_bytes() takes a reference
}
} // namespace Gio
|