// src/sgpemv2/cpu_policies_gatekeeper.hhbackend/cpu_policy.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 2 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, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

#ifndef CPU_POLICIES_GATEKEEPER_HH
#define CPU_POLICIES_GATEKEEPER_HH 1

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

#include "config.h"

#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 FIXME document me


   */

  class SG_DLLEXPORT CPUPoliciesGatekeeper : public Singleton<CPUPoliciesGatekeeper>
  {
    friend class Singleton<CPUPoliciesGatekeeper>;

  public:
    typedef CPUPolicyManager Manager;
    typedef std::vector<Manager*> Managers;
    
    Managers   get_registered() const;

    void       register_manager(CPUPolicyManager* manager);

    void       unregister_manager(CPUPolicyManager* manager);

    CPUPolicy* get_current_policy(History* history) throw(std::runtime_error);

    /**
     Associates 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) throw(UserInterruptException, MalformedPolicyException);

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

    // 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

