Back to index

unity  6.0.0
Public Member Functions | Protected Types | Protected Attributes | Private Member Functions
unity::glib::SourceManager Class Reference

glib::SourceManager is a container for the glib::Source pointers. More...

#include <GLibSource.h>

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

List of all members.

Public Member Functions

 SourceManager ()
 ~SourceManager ()
bool Add (Source *source, std::string const &nick="")
 Adds a new Source to the manager.
bool Add (Source::Ptr const &source, std::string const &nick="")
bool Remove (std::string const &nick)
bool Remove (unsigned int id)
Source::Ptr GetSource (std::string const &nick) const
Source::Ptr GetSource (unsigned int id) const

Protected Types

typedef std::map< std::string,
Source::Ptr
SourcesMap

Protected Attributes

SourcesMap sources_

Private Member Functions

void OnSourceRemoved (unsigned int id)
void RemoveItem (SourcesMap::iterator it)

Detailed Description

glib::SourceManager is a container for the glib::Source pointers.

It can be used to store multiple Sources pointers, possibly defining a "nick" name to reference them.

This is meant to be an utility class that ensures that all the Sources are removed and destructed when the container itself is destructed.

Definition at line 191 of file GLibSource.h.


Member Typedef Documentation

typedef std::map<std::string, Source::Ptr> unity::glib::SourceManager::SourcesMap [protected]

Definition at line 215 of file GLibSource.h.


Constructor & Destructor Documentation

Definition at line 191 of file GLibSource.cpp.

{}

Definition at line 194 of file GLibSource.cpp.

{
  for (auto it = sources_.begin(); it != sources_.end();)
  {
    RemoveItem(it++);
  }
}

Here is the call graph for this function:


Member Function Documentation

bool unity::glib::SourceManager::Add ( Source source,
std::string const &  nick = "" 
)

Adds a new Source to the manager.

Only new valid sources can be added to the manager.

The developer may define a nickname for a Source, when adding a new Source with an already known nickname, the old Source will be removed, and the new one will replace it.

Definition at line 202 of file GLibSource.cpp.

{
  Source::Ptr s(source);
  return Add(s, nick);
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool unity::glib::SourceManager::Add ( Source::Ptr const &  source,
std::string const &  nick = "" 
)

Definition at line 208 of file GLibSource.cpp.

{
  if (!source)
    return false;

  /* If the manager controls another source equal to the one we're passing,
   * we don't add it again */
  for (auto it : sources_)
  {
    if (it.second == source)
    {
      return false;
    }
  }

  std::string source_nick(nick);

  if (source_nick.empty())
  {
    /* If we don't have a nick, we use the source pointer string as nick. */
    source_nick = boost::lexical_cast<std::string>(source.get());
  }

  auto old_source_it = sources_.find(source_nick);
  if (old_source_it != sources_.end())
  {
    /* If a source with the same nick has been found, we can safely replace it,
     * since at this point we're sure that they refers to two different sources.
     * This should not happen when we're trying to re-add the same source from
     * its callback. */
    RemoveItem(old_source_it);
  }

  sources_[source_nick] = source;
  source->removed.connect(sigc::mem_fun(this, &SourceManager::OnSourceRemoved));

  return true;
}

Here is the call graph for this function:

Source::Ptr unity::glib::SourceManager::GetSource ( std::string const &  nick) const

Definition at line 312 of file GLibSource.cpp.

{
  auto it = sources_.find(nick);

  if (it != sources_.end())
  {
    return it->second;
  }

  return Source::Ptr();
}

Here is the caller graph for this function:

Definition at line 299 of file GLibSource.cpp.

{
  for (auto it : sources_)
  {
    if (it.second->Id() == id)
    {
      return it.second;
    }
  }

  return Source::Ptr();
}
void unity::glib::SourceManager::OnSourceRemoved ( unsigned int  id) [private]

Definition at line 247 of file GLibSource.cpp.

{
  for (auto it = sources_.begin(); it != sources_.end(); ++it)
  {
    auto source = it->second;

    if (source->Id() == id)
    {
      sources_.erase(it);
      break;
    }
  }
}

Here is the caller graph for this function:

bool unity::glib::SourceManager::Remove ( std::string const &  nick)

Definition at line 261 of file GLibSource.cpp.

{
  auto it = sources_.find(nick);

  if (it != sources_.end())
  {
    RemoveItem(it);
    return true;
  }

  return false;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool unity::glib::SourceManager::Remove ( unsigned int  id)

Definition at line 274 of file GLibSource.cpp.

{
  for (auto it = sources_.begin(); it != sources_.end(); ++it)
  {
    auto source = it->second;

    if (source->Id() == id)
    {
      RemoveItem(it);
      return true;
    }
  }

  return false;
}

Here is the call graph for this function:

void unity::glib::SourceManager::RemoveItem ( SourcesMap::iterator  it) [private]

Definition at line 290 of file GLibSource.cpp.

{
  auto source = it->second;

  source->removed.clear();
  source->Remove();
  sources_.erase(it);
}

Here is the caller graph for this function:


Member Data Documentation

Definition at line 216 of file GLibSource.h.


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