/* 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 "Stream.h"
#include "StreamMgr.h"

namespace Blinker {

/// constructor
StreamMgr::StreamMgr()
{
}

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

/**
 * @brief reference stream
 * @param[in] name stream name
 * @return stream
 *
 * if the stream does not exists, it is created
 */
Stream & StreamMgr::refStream(const std::string &name)
{
  Entry &entry = m_streams[name];
  entry.m_stream.m_name = name;
  entry.m_refCnt++;
  return entry.m_stream;
}

/**
 * @brief unreference stream
 * @param[in] name stream name
 *
 * if the last reference is removed, the stream is deleted
 */
void StreamMgr::unrefStream(const std::string &name)
{
  StreamMap::iterator itStream = m_streams.find(name);
  if (itStream != m_streams.end()) {
    if (itStream->second.m_refCnt > 0)
      itStream->second.m_refCnt--;
    if (itStream->second.m_refCnt == 0)
      m_streams.erase(itStream);
  }
}

/* ####################
   # StreamMgr::Entry #
   #################### */

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

} // namespace Blinker

