/* Blinker
   Copyright 2011-2019 Stefan Schuermans <stefan@blinkenarea.org>
   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
   a blinkenarea.org project */

#include <map>
#include <string>

#include "Sync.h"
#include "SyncMgr.h"

namespace Blinker {

/// constructor
SyncMgr::SyncMgr()
{
}

/// destructor
SyncMgr::~SyncMgr()
{
}

/**
 * @brief reference sync stream
 * @param[in] name sync stream name
 * @return sync stream
 *
 * if the sync stream does not exists, it is created
 */
Sync & SyncMgr::refSync(const std::string &name)
{
  Entry &entry = m_syncs[name];
  entry.m_sync.m_name = name;
  entry.m_refCnt++;
  return entry.m_sync;
}

/**
 * @brief unreference sync stream
 * @param[in] name sync stream name
 *
 * if the last reference is removed, the sync stream is deleted
 */
void SyncMgr::unrefSync(const std::string &name)
{
  SyncMap::iterator itSync = m_syncs.find(name);
  if (itSync != m_syncs.end()) {
    if (itSync->second.m_refCnt > 0)
      itSync->second.m_refCnt--;
    if (itSync->second.m_refCnt == 0)
      m_syncs.erase(itSync);
  }
}

/* ##################
   # SyncMgr::Entry #
   ################## */

/// constructor
SyncMgr::Entry::Entry():
  m_refCnt(0)
{
}

} // namespace Blinker

