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

#ifndef BLINKER_STREAMMGR_H
#define BLINKER_STREAMMGR_H

#include <map>
#include <string>

#include "Stream.h"

namespace Blinker {

/// stream manager
class StreamMgr
{
protected:
  /// stream map entry
  struct Entry {
    Stream       m_stream; ///< the stream
    unsigned int m_refCnt; ///< reference count
    Entry(); ///< constructor
  };

  /// map of streams
  typedef std::map<std::string, Entry> StreamMap;

public:
  /// constructor
  StreamMgr();

  /// destructor
  ~StreamMgr();

private:
  /// copy constructor disabled
  StreamMgr(const StreamMgr &that);

  /// assignment operator disabled
  const StreamMgr & operator=(const StreamMgr &that);

public:
  /**
   * @brief reference stream
   * @param[in] name stream name
   * @return stream
   *
   * if the stream does not exists, it is created
   */
  Stream & refStream(const std::string &name);

  /**
   * @brief unreference stream
   * @param[in] name stream name
   *
   * if the last reference is removed, the stream is deleted
   */
  void unrefStream(const std::string &name);

protected:
  /// map of streams
  StreamMap m_streams;
}; // class StreamMgr

} // namespace Blinker

#endif // #ifndef BLINKER_STREAMMGR_H

