/* 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, see <http://www.gnu.org/licenses/>.
 */

_CONFIGINCLUDE(giommconfig.h)

#include <glibmm/object.h>

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

namespace Gio
{

/** Allows actions to be cancelled.
 * Cancellable is a thread-safe operation cancellation stack used throughout GIO to allow for cancellation of synchronous and asynchronous operations.
 *
 * @newin{2,16}
 */
class GIOMM_API Cancellable : public Glib::Object
{
  _CLASS_GOBJECT(Cancellable, GCancellable, G_CANCELLABLE, Glib::Object, GObject, , , GIOMM_API)

protected:
  _CTOR_DEFAULT

public:
  using SlotCancelledCallback = sigc::slot<void>;

  _WRAP_CREATE()

  _WRAP_METHOD(bool is_cancelled() const, g_cancellable_is_cancelled)

  _IGNORE(g_cancellable_set_error_if_cancelled)

  //May return -1 if fds not supported, or on errors .
  _WRAP_METHOD(int get_fd() const, g_cancellable_get_fd)

  _WRAP_METHOD(bool make_pollfd(GPollFD* pollfd), g_cancellable_make_pollfd)
  _WRAP_METHOD(void release_fd(), g_cancellable_release_fd)

  //This is safe to call from another thread.
  _WRAP_METHOD(void cancel(), g_cancellable_cancel)

  _WRAP_METHOD(static Glib::RefPtr<Cancellable> get_current(), g_cancellable_get_current, refreturn)

  _WRAP_METHOD(void push_current(),
               g_cancellable_push_current)
  _WRAP_METHOD(void pop_current(),
               g_cancellable_pop_current)
  _WRAP_METHOD(void reset(), g_cancellable_reset)

  /** Convenience function to connect to the Cancellable::signal_cancelled()
   * signal. Also handles the race condition that may happen
   * if the cancellable is cancelled right before connecting.
   *
   * @a slot is called at most once, either directly at the
   * time of the connect if @a cancellable is already cancelled,
   * or when @a cancellable is cancelled in some thread.
   *
   * See Cancellable::signal_cancelled() for details on how to use this.
   *
   * @newin{2,22}
   *
   * @param slot The slot to connect.
   * @return The id of the signal handler or 0 if @a cancellable has already
   * been cancelled.
   */
  gulong connect(const SlotCancelledCallback& slot);
  _IGNORE(g_cancellable_connect)

  _WRAP_METHOD(void disconnect(gulong handler_id), g_cancellable_disconnect)

  _WRAP_SIGNAL(void cancelled(), cancelled)
};

} // namespace Gio

