summaryrefslogtreecommitdiff
path: root/gio/src/asyncinitable.ccg
blob: 314c5b3fb685f073d37fc4ba3edb331b0529a341 (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* Copyright (C) 2010 The giomm 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, see <http://www.gnu.org/licenses/>.
 */

#include <gio/gio.h>
#include <glibmm/error.h>
#include <glibmm/exceptionhandler.h>
#include <giomm/cancellable.h>
#include <giomm/slot_async.h>

namespace Gio
{

void
AsyncInitable::init_async(
  const SlotAsyncReady& slot, const Glib::RefPtr<Cancellable>& cancellable, int io_priority)
{
  // Create a copy of the slot.
  // A pointer to it will be passed through the callback's data parameter
  // and deleted in the callback.
  auto slot_copy = new SlotAsyncReady(slot);

  g_async_initable_init_async(
    gobj(), io_priority, Glib::unwrap(cancellable), &SignalProxy_async_callback, slot_copy);
}

void
AsyncInitable::init_async(const SlotAsyncReady& slot, int io_priority)
{
  // Create a copy of the slot.
  // A pointer to it will be passed through the callback's data parameter
  // and deleted in the callback.
  auto slot_copy = new SlotAsyncReady(slot);

  g_async_initable_init_async(gobj(), io_priority, nullptr, &SignalProxy_async_callback, slot_copy);
}

void
AsyncInitable_Class::init_async_vfunc_callback(GAsyncInitable* self, int io_priority,
  GCancellable* cancellable, GAsyncReadyCallback callback, gpointer user_data)
{
  const auto obj_base =
    static_cast<Glib::ObjectBase*>(Glib::ObjectBase::_get_current_wrapper((GObject*)self));

  // Non-gtkmmproc-generated custom classes implicitly call the default
  // Glib::ObjectBase constructor, which sets is_derived_. But gtkmmproc-
  // generated classes can use this optimisation, which avoids the unnecessary
  // parameter conversions if there is no possibility of the virtual function
  // being overridden:
  if (obj_base && obj_base->is_derived_())
  {
    const auto obj = dynamic_cast<CppObjectType* const>(obj_base);
    if (obj) // This can be nullptr during destruction.
    {
      try // Trap C++ exceptions which would normally be lost because this is a C callback.
      {
        // Get the slot.
        Gio::SlotAsyncReady* the_slot = static_cast<Gio::SlotAsyncReady*>(user_data);

        // Call the virtual member method, which derived classes might override.
        obj->init_async_vfunc(*the_slot, Glib::wrap(cancellable, true), io_priority);
        return;
      }
      catch (...)
      {
        Glib::exception_handlers_invoke();
      }
    }
  }

  const auto base = static_cast<BaseClassType*>(
    g_type_interface_peek_parent( // Get the parent interface of the interface (The original
                                  // underlying C interface).
      g_type_interface_peek(
        G_OBJECT_GET_CLASS(self), CppObjectType::get_type()) // Get the interface.
      ));

  // Call the original underlying C function:
  if (base && base->init_async)
    (*base->init_async)(self, io_priority, cancellable, callback, user_data);
}
void
Gio::AsyncInitable::init_async_vfunc(
  const SlotAsyncReady& slot, const Glib::RefPtr<Cancellable>& cancellable, int io_priority)
{
  const auto base = static_cast<BaseClassType*>(
    g_type_interface_peek_parent( // Get the parent interface of the interface (The original
                                  // underlying C interface).
      g_type_interface_peek(
        G_OBJECT_GET_CLASS(gobject_), CppObjectType::get_type()) // Get the interface.
      ));

  if (base && base->init_async)
  {
    (*base->init_async)(gobj(), io_priority, const_cast<GCancellable*>(Glib::unwrap(cancellable)),
      &SignalProxy_async_callback, const_cast<SlotAsyncReady*>(&slot));
  }
}
gboolean
AsyncInitable_Class::init_finish_vfunc_callback(
  GAsyncInitable* self, GAsyncResult* res, GError** error)
{
  const auto obj_base =
    static_cast<Glib::ObjectBase*>(Glib::ObjectBase::_get_current_wrapper((GObject*)self));

  // Non-gtkmmproc-generated custom classes implicitly call the default
  // Glib::ObjectBase constructor, which sets is_derived_. But gtkmmproc-
  // generated classes can use this optimisation, which avoids the unnecessary
  // parameter conversions if there is no possibility of the virtual function
  // being overridden:
  if (obj_base && obj_base->is_derived_())
  {
    const auto obj = dynamic_cast<CppObjectType* const>(obj_base);
    if (obj) // This can be nullptr during destruction.
    {
      try // Trap C++ exceptions which would normally be lost because this is a C callback.
      {
        // Call the virtual member method, which derived classes might override.
        return static_cast<int>(obj->init_finish_vfunc(Glib::wrap(res, true)));
      }
      catch (...)
      {
        Glib::exception_handlers_invoke();
      }
    }
  }

  const auto base = static_cast<BaseClassType*>(
    g_type_interface_peek_parent( // Get the parent interface of the interface (The original
                                  // underlying C interface).
      g_type_interface_peek(
        G_OBJECT_GET_CLASS(self), CppObjectType::get_type()) // Get the interface.
      ));

  // Call the original underlying C function:
  if (base && base->init_finish)
    return (*base->init_finish)(self, res, error);

  using RType = gboolean;
  return RType();
}
bool
Gio::AsyncInitable::init_finish_vfunc(const Glib::RefPtr<AsyncResult>& res)
{
  const auto base = static_cast<BaseClassType*>(
    g_type_interface_peek_parent( // Get the parent interface of the interface (The original
                                  // underlying C interface).
      g_type_interface_peek(
        G_OBJECT_GET_CLASS(gobject_), CppObjectType::get_type()) // Get the interface.
      ));

  if (base && base->init_finish)
  {
    GError* gerror = nullptr;

    bool const result = (*base->init_finish)(gobj(), Glib::unwrap(res), &gerror);

    if (gerror)
      ::Glib::Error::throw_exception(gerror);

    return result;
  }

  using RType = bool;
  return RType();
}

} // namespace Gio