"Fossies" - the Fresh Open Source Software Archive

Member "glibmm-2.74.0/gio/src/asyncinitable.ccg" (19 Sep 2022, 6459 Bytes) of package /linux/misc/glibmm-2.74.0.tar.xz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 /* Copyright (C) 2010 The giomm Development Team
    2  *
    3  * This library is free software; you can redistribute it and/or
    4  * modify it under the terms of the GNU Lesser General Public
    5  * License as published by the Free Software Foundation; either
    6  * version 2.1 of the License, or (at your option) any later version.
    7  *
    8  * This library is distributed in the hope that it will be useful,
    9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   11  * Lesser General Public License for more details.
   12  *
   13  * You should have received a copy of the GNU Lesser General Public
   14  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
   15  */
   16 
   17 #include <gio/gio.h>
   18 #include <glibmm/error.h>
   19 #include <glibmm/exceptionhandler.h>
   20 #include <giomm/cancellable.h>
   21 #include <giomm/slot_async.h>
   22 
   23 namespace Gio
   24 {
   25 
   26 void
   27 AsyncInitable::init_async(
   28   const SlotAsyncReady& slot, const Glib::RefPtr<Cancellable>& cancellable, int io_priority)
   29 {
   30   // Create a copy of the slot.
   31   // A pointer to it will be passed through the callback's data parameter
   32   // and deleted in the callback.
   33   auto slot_copy = new SlotAsyncReady(slot);
   34 
   35   g_async_initable_init_async(
   36     gobj(), io_priority, Glib::unwrap(cancellable), &SignalProxy_async_callback, slot_copy);
   37 }
   38 
   39 void
   40 AsyncInitable::init_async(const SlotAsyncReady& slot, int io_priority)
   41 {
   42   // Create a copy of the slot.
   43   // A pointer to it will be passed through the callback's data parameter
   44   // and deleted in the callback.
   45   auto slot_copy = new SlotAsyncReady(slot);
   46 
   47   g_async_initable_init_async(gobj(), io_priority, nullptr, &SignalProxy_async_callback, slot_copy);
   48 }
   49 
   50 void
   51 AsyncInitable_Class::init_async_vfunc_callback(GAsyncInitable* self, int io_priority,
   52   GCancellable* cancellable, GAsyncReadyCallback callback, gpointer user_data)
   53 {
   54   const auto obj_base =
   55     static_cast<Glib::ObjectBase*>(Glib::ObjectBase::_get_current_wrapper((GObject*)self));
   56 
   57   // Non-gtkmmproc-generated custom classes implicitly call the default
   58   // Glib::ObjectBase constructor, which sets is_derived_. But gtkmmproc-
   59   // generated classes can use this optimisation, which avoids the unnecessary
   60   // parameter conversions if there is no possibility of the virtual function
   61   // being overridden:
   62   if (obj_base && obj_base->is_derived_())
   63   {
   64     const auto obj = dynamic_cast<CppObjectType* const>(obj_base);
   65     if (obj) // This can be nullptr during destruction.
   66     {
   67       try // Trap C++ exceptions which would normally be lost because this is a C callback.
   68       {
   69         // Get the slot.
   70         Gio::SlotAsyncReady* the_slot = static_cast<Gio::SlotAsyncReady*>(user_data);
   71 
   72         // Call the virtual member method, which derived classes might override.
   73         obj->init_async_vfunc(*the_slot, Glib::wrap(cancellable, true), io_priority);
   74         return;
   75       }
   76       catch (...)
   77       {
   78         Glib::exception_handlers_invoke();
   79       }
   80     }
   81   }
   82 
   83   const auto base = static_cast<BaseClassType*>(
   84     g_type_interface_peek_parent( // Get the parent interface of the interface (The original
   85                                   // underlying C interface).
   86       g_type_interface_peek(
   87         G_OBJECT_GET_CLASS(self), CppObjectType::get_type()) // Get the interface.
   88       ));
   89 
   90   // Call the original underlying C function:
   91   if (base && base->init_async)
   92     (*base->init_async)(self, io_priority, cancellable, callback, user_data);
   93 }
   94 void
   95 Gio::AsyncInitable::init_async_vfunc(
   96   const SlotAsyncReady& slot, const Glib::RefPtr<Cancellable>& cancellable, int io_priority)
   97 {
   98   const auto base = static_cast<BaseClassType*>(
   99     g_type_interface_peek_parent( // Get the parent interface of the interface (The original
  100                                   // underlying C interface).
  101       g_type_interface_peek(
  102         G_OBJECT_GET_CLASS(gobject_), CppObjectType::get_type()) // Get the interface.
  103       ));
  104 
  105   if (base && base->init_async)
  106   {
  107     (*base->init_async)(gobj(), io_priority, const_cast<GCancellable*>(Glib::unwrap(cancellable)),
  108       &SignalProxy_async_callback, const_cast<SlotAsyncReady*>(&slot));
  109   }
  110 }
  111 gboolean
  112 AsyncInitable_Class::init_finish_vfunc_callback(
  113   GAsyncInitable* self, GAsyncResult* res, GError** error)
  114 {
  115   const auto obj_base =
  116     static_cast<Glib::ObjectBase*>(Glib::ObjectBase::_get_current_wrapper((GObject*)self));
  117 
  118   // Non-gtkmmproc-generated custom classes implicitly call the default
  119   // Glib::ObjectBase constructor, which sets is_derived_. But gtkmmproc-
  120   // generated classes can use this optimisation, which avoids the unnecessary
  121   // parameter conversions if there is no possibility of the virtual function
  122   // being overridden:
  123   if (obj_base && obj_base->is_derived_())
  124   {
  125     const auto obj = dynamic_cast<CppObjectType* const>(obj_base);
  126     if (obj) // This can be nullptr during destruction.
  127     {
  128       try // Trap C++ exceptions which would normally be lost because this is a C callback.
  129       {
  130         // Call the virtual member method, which derived classes might override.
  131         return static_cast<int>(obj->init_finish_vfunc(Glib::wrap(res, true)));
  132       }
  133       catch (...)
  134       {
  135         Glib::exception_handlers_invoke();
  136       }
  137     }
  138   }
  139 
  140   const auto base = static_cast<BaseClassType*>(
  141     g_type_interface_peek_parent( // Get the parent interface of the interface (The original
  142                                   // underlying C interface).
  143       g_type_interface_peek(
  144         G_OBJECT_GET_CLASS(self), CppObjectType::get_type()) // Get the interface.
  145       ));
  146 
  147   // Call the original underlying C function:
  148   if (base && base->init_finish)
  149     return (*base->init_finish)(self, res, error);
  150 
  151   using RType = gboolean;
  152   return RType();
  153 }
  154 bool
  155 Gio::AsyncInitable::init_finish_vfunc(const Glib::RefPtr<AsyncResult>& res)
  156 {
  157   const auto base = static_cast<BaseClassType*>(
  158     g_type_interface_peek_parent( // Get the parent interface of the interface (The original
  159                                   // underlying C interface).
  160       g_type_interface_peek(
  161         G_OBJECT_GET_CLASS(gobject_), CppObjectType::get_type()) // Get the interface.
  162       ));
  163 
  164   if (base && base->init_finish)
  165   {
  166     GError* gerror = nullptr;
  167 
  168     bool const result = (*base->init_finish)(gobj(), Glib::unwrap(res), &gerror);
  169 
  170     if (gerror)
  171       ::Glib::Error::throw_exception(gerror);
  172 
  173     return result;
  174   }
  175 
  176   using RType = bool;
  177   return RType();
  178 }
  179 
  180 } // namespace Gio