#include "FWCore/PluginManager/interface/PresenceFactory.h"
#include "FWCore/Utilities/interface/EDMException.h"
#include "FWCore/Utilities/interface/DebugMacros.h"
#include "FWCore/Utilities/interface/thread_safety_macros.h"

#include <iostream>

EDM_REGISTER_PLUGINFACTORY(edm::PresencePluginFactory, "CMS EDM Framework Presence");

namespace edm {

  PresenceFactory::~PresenceFactory() {}

  PresenceFactory::PresenceFactory() {}

  PresenceFactory* PresenceFactory::get() {
    CMS_THREAD_SAFE static PresenceFactory singleInstance_;
    return &singleInstance_;
  }

  std::unique_ptr<Presence> PresenceFactory::makePresence(std::string const& presence_type) const {
    std::unique_ptr<Presence> sp(PresencePluginFactory::get()->create(presence_type));

    if (sp.get() == nullptr) {
      throw edm::Exception(errors::Configuration, "NoPresenceModule")
          << "Presence Factory:\n"
          << "Cannot find presence type: " << presence_type << "\n"
          << "Perhaps the name is misspelled or is not a Plugin?\n"
          << "Try running EdmPluginDump to obtain a list of available Plugins.";
    }

    FDEBUG(1) << "PresenceFactory: created presence " << presence_type << std::endl;

    return sp;
  }
}  // namespace edm
