Back to index

unity  6.0.0
Classes | Public Types | Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Static Private Member Functions | Private Attributes
unity::glib::Source Class Reference

glib::Source is a wrapper class made to handle GSource based events at C++ level. More...

#include <GLibSource.h>

Inheritance diagram for unity::glib::Source:
Inheritance graph
[legend]
Collaboration diagram for unity::glib::Source:
Collaboration graph
[legend]

List of all members.

Classes

struct  CallBackData

Public Types

enum  Priority {
  HIGH = G_PRIORITY_HIGH, DEFAULT = G_PRIORITY_DEFAULT, HIGH_IDLE = G_PRIORITY_HIGH_IDLE, DEFAULT_IDLE = G_PRIORITY_DEFAULT_IDLE,
  LOW = G_PRIORITY_LOW
}
 This is an enum used for convenience, you can actually cast to this any integer: the bigger it is, the lower priority we have. More...
typedef std::shared_ptr< SourcePtr
typedef std::unique_ptr< SourceUniquePtr
typedef sigc::slot< bool > SourceCallback

Public Member Functions

virtual ~Source ()
unsigned int Id () const
bool Run (SourceCallback callback)
 This Run a source using the function as Source's callback.
bool IsRunning () const
void Remove ()
 This removes a source, and stop it from being executed.
void SetPriority (Priority prio)
Priority GetPriority () const

Public Attributes

sigc::signal< void, unsigned int > removed
 The removed signal is emitted when the Source has been removed and so it can happen both when the Remove() method is called and when the callback function returns false.

Protected Member Functions

 Source ()

Protected Attributes

GSource * source_

Static Private Member Functions

static gboolean Callback (gpointer data)
static void DestroyCallback (gpointer data)

Private Attributes

unsigned int source_id_
CallBackDatacallback_data_
SourceCallback callback_

Detailed Description

glib::Source is a wrapper class made to handle GSource based events at C++ level.

The class is basically to be intended abstract and is currently implemented by glib::Timeout() and glib::Idle() that are the higher-level wrappers for g_timeout and g_idle respectively.

As this is meant to be mostly a wrapper, I've mostly kept the same logic of the glib sources, and so, for example, a source can't be ran more than once.

Sources should define a callback function that will be called every time that the source is dispatched. If the callback function returns false, then the source will be removed, otherwise it will continue.

Pointer types have been defined and should be used to handle dynamically allocated sources. They also implicitly allow to remove running Sources by setting their value to nullptr.

Definition at line 53 of file GLibSource.h.


Member Typedef Documentation

typedef std::shared_ptr<Source> unity::glib::Source::Ptr

Definition at line 56 of file GLibSource.h.

typedef sigc::slot<bool> unity::glib::Source::SourceCallback

Definition at line 58 of file GLibSource.h.

typedef std::unique_ptr<Source> unity::glib::Source::UniquePtr

Definition at line 57 of file GLibSource.h.


Member Enumeration Documentation

This is an enum used for convenience, you can actually cast to this any integer: the bigger it is, the lower priority we have.

Enumerator:
HIGH 
DEFAULT 
HIGH_IDLE 
DEFAULT_IDLE 
LOW 

Definition at line 64 of file GLibSource.h.

  {
    HIGH = G_PRIORITY_HIGH,                 // -100
    DEFAULT = G_PRIORITY_DEFAULT,           // 0
    HIGH_IDLE = G_PRIORITY_HIGH_IDLE,       // 100
    DEFAULT_IDLE = G_PRIORITY_DEFAULT_IDLE, // 200
    LOW = G_PRIORITY_LOW                    // 300
  };

Constructor & Destructor Documentation

Definition at line 34 of file GLibSource.cpp.

{
  Remove();

  if (callback_data_)
    callback_data_->self = nullptr;
}

Here is the call graph for this function:

unity::glib::Source::Source ( ) [protected]

Definition at line 28 of file GLibSource.cpp.

  : source_(nullptr)
  , source_id_(0)
  , callback_data_(nullptr)
{}

Member Function Documentation

gboolean unity::glib::Source::Callback ( gpointer  data) [static, private]

Definition at line 100 of file GLibSource.cpp.

{
  if (!data)
    return G_SOURCE_REMOVE;

  auto self = static_cast<CallBackData*>(data)->self;

  if (self && !self->callback_.empty() && self->callback_())
  {
    return G_SOURCE_CONTINUE;
  }
  else
  {
    return G_SOURCE_REMOVE;
  }
}

Here is the caller graph for this function:

void unity::glib::Source::DestroyCallback ( gpointer  data) [static, private]

Definition at line 117 of file GLibSource.cpp.

{
  if (!data)
    return;

  auto cb_data = static_cast<CallBackData*>(data);

  if (cb_data->self)
  {
    auto self = cb_data->self;
    self->callback_data_ = nullptr;

    if (self->Id())
      self->removed.emit(self->Id());
  }

  delete cb_data;
}

Here is the caller graph for this function:

Definition at line 64 of file GLibSource.cpp.

{
  int prio = 0;
  if (source_)
    prio = g_source_get_priority(source_);

  return static_cast<Priority>(prio);
}
unsigned int unity::glib::Source::Id ( ) const

Definition at line 95 of file GLibSource.cpp.

{
  return source_id_;
}

Definition at line 87 of file GLibSource.cpp.

{
  if (!source_)
    return false;

  return (!g_source_is_destroyed(source_) && g_source_get_context(source_));
}

Here is the caller graph for this function:

This removes a source, and stop it from being executed.

After that a source has been removed, it can't be ran again.

Definition at line 42 of file GLibSource.cpp.

{
  if (source_)
  {
    if (!g_source_is_destroyed(source_))
    {
      g_source_destroy(source_);
    }

    g_source_unref(source_);
    source_ = nullptr;
  }
}

Here is the caller graph for this function:

This Run a source using the function as Source's callback.

The method will return false if the source is already running, true otherwise.

Definition at line 73 of file GLibSource.cpp.

{
  if (!source_ || source_id_ || IsRunning())
    return false;

  callback_ = callback;
  callback_data_ = new CallBackData(this);

  g_source_set_callback(source_, Callback, callback_data_, DestroyCallback);
  source_id_ = g_source_attach(source_, nullptr);

  return true;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 56 of file GLibSource.cpp.

{
  if (!source_)
    return;

  g_source_set_priority(source_, prio);
}

Here is the caller graph for this function:


Member Data Documentation

Definition at line 119 of file GLibSource.h.

Definition at line 118 of file GLibSource.h.

sigc::signal<void, unsigned int> unity::glib::Source::removed

The removed signal is emitted when the Source has been removed and so it can happen both when the Remove() method is called and when the callback function returns false.

Definition at line 97 of file GLibSource.h.

GSource* unity::glib::Source::source_ [protected]

Definition at line 102 of file GLibSource.h.

unsigned int unity::glib::Source::source_id_ [private]

Definition at line 117 of file GLibSource.h.


The documentation for this class was generated from the following files: