// src/sgpemv2/cpu_policies_gatekeeper.hh - Copyright 2005, 2006, University
//               of Padova, dept. of Pure and Applied
//               Mathematics
//
// This file is part of SGPEMv2.
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// SGPEMv2 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SGPEMv2. If not, see http://www.gnu.org/licenses/.


#ifndef CPU_POLICIES_GATEKEEPER_HH
#define CPU_POLICIES_GATEKEEPER_HH 1

namespace sgpem
{
  class CPUPolicyManager;
  class CPUPolicy;
  class History;
}

#include <sgpemv2/sgpemv2-visibility.hh>

#include <vector>
#include <map>
#include <stdexcept>

#include <sgpemv2/templates/singleton.hh>
#include <sgpemv2/malformed_policy_exception.hh>
#include <sgpemv2/user_interrupt_exception.hh>

namespace sgpem
{
  class CPUPoliciesGatekeeper;

  /** \brief This is the container of all CPU policies found on the system.
   *
   * Keeps all registered policy managers in order to access to available policies
   * Every CPUPolicyManager should register itself to this class so that
   * its policies are made available to the user.
   *
   * This class' constructor should be friend with Singleton (shake hands, make a smile!).
   */
  class SG_DLLEXPORT CPUPoliciesGatekeeper : public Singleton<CPUPoliciesGatekeeper>
  {
    friend class Singleton<CPUPoliciesGatekeeper>;

  public:
    typedef CPUPolicyManager Manager;
    typedef std::vector<Manager*> Managers;
    
    /** \brief Returns registered ::CPUManager */
    Managers   get_registered() const;

    /** \brief Register a new manager
     *
     * A no-op if a manager of the same type already exists
     */
    void       register_manager(CPUPolicyManager* manager);

    /** \brief Unregister a given manager
     *
     * If not present, it is a no-op. When unregistering a manager, care should be taken 
     * to ensure that the currently active policies weren't managed by it. If so, the policies
     * should be deactivated before removal.
     */
    void       unregister_manager(CPUPolicyManager* manager);

    /** \brief Returns the currently active policy
     *
     * If no policy was previously activated for the attached 
     * ::History, throw an appropriate exception.
     */
    CPUPolicy* get_current_policy(History* history);

    /** \brief Associates a policy with history. 
     * If an exception is thrown, the current associated *policy with this history 
     * (if there are any), is \b no more active, \b nor associated 
     */
    void       activate_policy(History* history, CPUPolicy* policy);

  private:

    CPUPoliciesGatekeeper();     //private constructor.
    CPUPoliciesGatekeeper(const CPUPoliciesGatekeeper&);
    CPUPoliciesGatekeeper& operator=(const CPUPoliciesGatekeeper&);

    /** \brief Deactivates active policies managed by the specified manager. */
    void       deactivate_policies(CPUPolicyManager* manager);

    Managers                           _registered;
    std::map<History*, CPUPolicy*>     _active_policies;
  };

}//~ namespace sgpem

#endif //~ CPU_POLICIES_GATEKEEPER_HH

