"Fossies" - the Fresh Open Source Software Archive

Member "glibmm-2.74.0/gio/src/asyncresult.hg" (19 Sep 2022, 5320 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) 2007 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 _CONFIGINCLUDE(giommconfig.h)
   18 
   19 #include <glibmm/interface.h>
   20 #include <glibmm/object.h>
   21 
   22 _DEFS(giomm,gio)
   23 _PINCLUDE(glibmm/private/interface_p.h)
   24 
   25 #ifndef DOXYGEN_SHOULD_SKIP_THIS
   26 typedef struct _GAsyncResultIface GAsyncResultIface;
   27 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
   28 
   29 namespace Gio
   30 {
   31 
   32 class GIOMM_API AsyncResult;
   33 
   34 /** A function that will be called when an asynchronous operation within GIO has been completed.
   35  * @param result The asynchronous function's results.
   36  *
   37  * For instance,
   38  * @code
   39  * void on_async_ready(Glib::RefPtr<Gio::AsyncResult>& result);
   40  * @endcode
   41  *
   42  * @newin{2,16}
   43  * @relates Gio::AsyncResult
   44  */
   45 using SlotAsyncReady = sigc::slot<void(Glib::RefPtr<AsyncResult>&)>;
   46 
   47 /** Provides a base class for implementing asynchronous function results.
   48  *
   49  * Asynchronous operations are broken up into two separate operations which are chained together by a SlotAsyncReady.
   50  * To begin an asynchronous operation, provide a SlotAsyncReady to the asynchronous function. This callback will be triggered
   51  * when the operation has completed, and will be passed an %AsyncResult instance
   52  * (an instance of a class that implements the %AsyncResult interface)
   53  * filled with the details of the operation's success or failure,
   54  * the object the asynchronous function was started for and any error codes returned. The asynchronous callback function is then
   55  * expected to call the corresponding "_finish()" function of the object the async function was called for, with the %AsyncResult instance.
   56  *
   57  * The purpose of the "_finish()" function is to take the generic result of type %AsyncResult and return the specific result that the operation
   58  * in question yields (e.g. a FileEnumerator for an "enumerate children" operation). If the result or error status of the operation is not needed,
   59  * there is no need to call the "_finish()" function and GIO will take care of cleaning up the result and error information after the
   60  * SlotAsyncReady returns. You may also store the %AsyncResult and call "_finish()" later.
   61  * However, the "_finish()" function may be called at most once.
   62  *
   63  * Example of a typical asynchronous operation flow:
   64  * @code
   65  * class Theoretical : public Glib::Object
   66  * {
   67  * public:
   68  *   Glib::RefPtr<Theoretical> create();
   69  *
   70  *   void frobnitz_async(const Gio::SlotAsyncReady& slot);
   71  *   Glib::ustring frobnitz_finish(const Glib::RefPtr<Gio::AsyncResult>& result);
   72  * // ...
   73  * };
   74  *
   75  * // ...
   76  *
   77  * namespace
   78  * {
   79  * Glib::RefPtr<Theoretical> theoretical;
   80  *
   81  * void on_frobnitz_ready(Glib::RefPtr<Gio::AsyncResult>& result)
   82  * {
   83  *   try
   84  *   {
   85  *     Glib::ustring s = theoretical->frobnitz_finish(result);
   86  *     std::cout << s << std::endl;
   87  *   }
   88  *   catch (const Glib::Error& err)
   89  *   {
   90  *     std::cerr << err.what() << std::endl;
   91  *   }
   92  *   // ...
   93  * }
   94  * } // anonymous namespace
   95  *
   96  * int main(int argc, void* argv[])
   97  * {
   98  *   // ...
   99  *   theoretical = Theoretical::create();
  100  *   theoretical->frobnitz_async(sigc::ptr_fun(&on_frobnitz_ready));
  101  *   // ...
  102  * }
  103  * @endcode
  104  *
  105  * The async function could also take an optional Gio::Cancellable object, allowing the calling function to cancel the asynchronous operation.
  106  *
  107  * The callback for an asynchronous operation is called only once, and is always called, even in the case of a cancelled operation.
  108  * On cancellation the "_finish()" function will throw a Gio::Error exception with a <tt>Gio::Error::CANCELLED</tt> error code.
  109  *
  110  * @newin{2,16}
  111  */
  112 class GIOMM_API AsyncResult : public Glib::Interface
  113 {
  114   _CLASS_INTERFACE(AsyncResult, GAsyncResult, G_ASYNC_RESULT, GAsyncResultIface, , , GIOMM_API)
  115 
  116 public:
  117   _IGNORE(g_async_result_get_user_data)
  118   _IGNORE(g_async_result_get_source_object)
  119 
  120   //Note that this returns a reference, unlike most GTK+ get_*() functions,
  121   //so we don't need to use refreturn.
  122   Glib::RefPtr<Glib::ObjectBase> get_source_object_base();
  123   Glib::RefPtr<const Glib::ObjectBase> get_source_object_base() const;
  124 
  125   _IGNORE(g_async_result_legacy_propagate_error)dnl// Shall not be used in new code
  126 
  127   _WRAP_METHOD(bool is_tagged(gpointer source_tag) const, g_async_result_is_tagged)
  128 
  129 protected:
  130   // The compiler cannot find an unwrap() for ObjectBase, because
  131   // ObjectBase::BaseObjectType is not declared.
  132   //#m4 _CONVERSION(`Glib::RefPtr<Glib::ObjectBase>',`GObject*',__CONVERT_REFPTR_TO_P)
  133 #m4 _CONVERSION(`Glib::RefPtr<Glib::ObjectBase>',`GObject*',`unwrap_objectbase_custom($3)')
  134   _WRAP_VFUNC(Glib::RefPtr<Glib::ObjectBase> get_source_object(), "get_source_object")
  135   _WRAP_VFUNC(bool is_tagged(gpointer source_tag), "is_tagged")
  136 };
  137 
  138 } // namespace Gio