// -*- Mode: C++; indent-tabs-mode: nil; c-basic-offset: 2 -*-

/* 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, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <glibmm/interface.h>
#include <giomm/asyncresult.h>

_DEFS(giomm,gio)
_PINCLUDE(glibmm/private/interface_p.h)
_PINCLUDE(gio/gio.h)

#ifndef DOXYGEN_SHOULD_SKIP_THIS
typedef struct _GAsyncInitableIface GAsyncInitableIface;
#endif /* DOXYGEN_SHOULD_SKIP_THIS */

namespace Gio
{

class Cancellable;

/** AsyncInitable - Asynchronously failable object initialization interface.
 * This is the asynchronous version of Initable; it behaves the same in all
 * ways except that initialization is asynchronous. For more details see the
 * descriptions on Initable.
 *
 * A class may implement both the Initable and AsyncInitable interfaces.
 *
 * Users of objects implementing this are not intended to use the interface
 * method directly; instead it will be used automatically in various ways. For
 * C applications you generally just call g_async_initable_new_async()
 * directly, or indirectly via a foo_thing_new_async() wrapper. This will call
 * g_async_initable_init_async() under the cover, calling back with NULL and a
 * set GError on failure. 
 */
class AsyncInitable : public Glib::Interface
{
  _CLASS_INTERFACE(AsyncInitable, GAsyncInitable, G_ASYNC_INITABLE, GAsyncInitableIface)

protected:
  /** Starts asynchronous initialization of the object implementing the
   * interface. This must be done before any real use of the object after
   * initial construction. If the object also implements Initable you can
   * optionally call Gio::Initable::init() instead.
   *
   * When the initialization is finished, @a slot will be called. You can
   * then call init_finish() to get the result of the initialization.
   *
   * Implementations may also support cancellation. Initialization can be
   * cancelled by triggering the @a cancellable object from another thread. If
   * the operation was cancelled, the error Gio::IO_ERROR_CANCELLED will be
   * returned. If the object doesn't support cancellable initialization, the
   * error Gio::IO_ERROR_NOT_SUPPORTED will be returned.
   *
   * If this function is not called, or returns with an error, then all
   * operations on the object should fail, generally returning the error
   * Gio::IO_ERROR_NOT_INITIALIZED.
   *
   * Implementations of this method must be idempotent: i.e. multiple calls to
   * this function with the same argument should return the same results. Only
   * the first call initializes the object; further calls return the result of
   * the first call. This is so that it's safe to implement the singleton
   * pattern in the GObject constructor function.
   *
   * For classes that also support the Initable interface, the default
   * implementation of this method will run the Gio::Initable::init() method
   * in a thread, so if you want to support asynchronous initialization via
   * threads, just implement the AsyncInitable interface without overriding
   * any interface methods.
   *
   * @param io_priority The I/O priority of the operation.
   * @param cancellable Cancellable object to make cancellation possible.
   * @param slot a SlotAsyncReady to call when the request is satisfied.
   *
   * @newin{2,28}
   */
  void init_async(const SlotAsyncReady& slot,
    const Glib::RefPtr<Cancellable>& cancellable,
    int io_priority = Glib::PRIORITY_DEFAULT);
  _IGNORE(g_async_initable_init_async)

  /// Non-cancellable version of init_async().
  void init_async(const SlotAsyncReady& slot,
    int io_priority = Glib::PRIORITY_DEFAULT);

  _WRAP_METHOD(bool init_finish(const Glib::RefPtr<AsyncResult>& result), g_async_initable_init_finish, errthrow)

  _WRAP_METHOD(Glib::RefPtr<Glib::Object> create_finish(const Glib::RefPtr<AsyncResult>& res), g_async_initable_new_finish, errthrow)

  virtual void init_async_vfunc(const SlotAsyncReady& slot,
    const Glib::RefPtr<Cancellable>& cancellable,
    int io_priority = Glib::PRIORITY_DEFAULT);

  /** @throw Glib::Errror.
   */
    virtual bool init_finish_vfunc(const Glib::RefPtr<AsyncResult>& res);
  
protected:
#m4begin
  _PUSH(SECTION_PCC_CLASS_INIT_VFUNCS)
  klass->init_async = &init_async_vfunc_callback;
  klass->init_finish = &init_finish_vfunc_callback;
  _SECTION(SECTION_PH_VFUNCS)
  static void init_async_vfunc_callback(GAsyncInitable* self,
    int io_priority, GCancellable* cancellable, GAsyncReadyCallback callback,
    gpointer user_data);
  static gboolean init_finish_vfunc_callback(GAsyncInitable* self,
    GAsyncResult* res, GError** error);
  _POP()
#m4end
};

} // namespace Gio
