/* 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_SYNCMGR_H
#define BLINKER_SYNCMGR_H

#include <map>
#include <string>

#include "Sync.h"

namespace Blinker {

/// sync stream manager
class SyncMgr
{
protected:
  /// sync stream map entry
  struct Entry {
    Sync         m_sync; ///< the sync stream
    unsigned int m_refCnt; ///< reference count
    Entry(); ///< constructor
  };

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

public:
  /// constructor
  SyncMgr();

  /// destructor
  ~SyncMgr();

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

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

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

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

protected:
  /// map of sync streams
  SyncMap m_syncs;
}; // class SyncMgr

} // namespace Blinker

#endif // #ifndef BLINKER_SYNCMGR_H

