// src/backend/history.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 HISTORY_HH
#define HISTORY_HH 1

#include "config.h"

#include <sgpemv2/environment.hh>

#include <glibmm/ustring.h>

#include <stdexcept>
#include <utility>
#include <vector>


namespace sgpem
{
  /** \brief Manages the history of the simulation

  Manages the history of the simulation from the instant 0 to the current time,
  i.e. permits to know the state of each schedulable object inside this time interval.
  In particoular it's possible to know which entity was running at a precise moment.

  In a future iteration it will be possible to revert the entire simulation to a state
  present in the history ("undo operation")
  */
  class History;

  // Forward declarations:
  class HistoryObserver;
  class Resource;
  class Process;
  class Thread;
  class Request;
  class SubRequest;

  class SG_DLLEXPORT History
  {
  public:
    typedef unsigned int                     size_t;
    typedef unsigned int                     time_t;
    typedef unsigned int                     position;
    typedef int                              prio_t;

    typedef Environment::resource_key_t      resource_key_t;
    typedef const std::pair<resource_key_t, Resource*>  ResourcePair;

    History();
    virtual ~History() = 0;

    virtual       size_t       get_size() const = 0;
    virtual const Environment& get_last_environment() const = 0;
    virtual const Environment& get_environment_at(position index) const throw(std::out_of_range) = 0;

    virtual void  remove(resource_key_t resource_key)  = 0;
    virtual void  remove(Process&    process)          = 0;
    virtual void  remove(Thread&     thread)           = 0;
    virtual void  remove(Request&    request)          = 0;
    virtual void  remove(SubRequest& subrequest)       = 0;

    // \brief Clears the whole history
    virtual void clear() = 0;

    virtual ResourcePair add_resource(const Glib::ustring& name,
                                      bool   preemptable  = false,
                                      size_t places       = 1,
                                      size_t availability = 0) = 0;

    virtual void        edit_resource(Resource& resource,
                                      const Glib::ustring& name,
                                      bool   preemptable  = false,
                                      size_t places       = 1,
                                      size_t availability = 0) = 0;

    virtual Process&     add_process(const Glib::ustring& name,
                                     time_t arrival_time,
                                     prio_t base_priority = 0) = 0;

    virtual void        edit_process(Process& process,
                                     const Glib::ustring& name,
                                     time_t arrival_time,
                                     prio_t base_priority = 0) = 0;


    virtual Thread&      add_thread(const Glib::ustring& name,
                                    Process& parent,
                                    time_t cpu_time,
                                    time_t arrival_time = 0,
                                    prio_t base_priority = 0) = 0;

    virtual void         edit_thread(Thread& thread,
                                     const Glib::ustring& name,
                                     time_t cpu_time,
                                     time_t arrival_time = 0,
                                     prio_t base_priority = 0) = 0;

    virtual Request&     add_request(Thread& owner,
                                     time_t instant) = 0;

    virtual void         edit_request(Request& request,
                                      time_t instant) = 0;


    virtual SubRequest&  add_subrequest(Request& request,
                                        resource_key_t resource_key,
                                        time_t  duration) = 0;

    virtual void         edit_subrequest(SubRequest& subrequest,
                                        resource_key_t resource_key,
                                        time_t  duration) = 0;

		virtual position get_front() const;
		virtual bool     is_sealed() const = 0;

    virtual void  attach(HistoryObserver& observer);
    virtual void  detach(const HistoryObserver& observer);

    /** \brief Enable/disable notifications to registered observers
     *
     * This is quite useful to disable momentarily notification while you
     * do a bunch of insertions and/or deletions in one go, in order to 
     * speed up things.
     *
     * \return The old value
     */
    virtual bool  set_notify_enabled(bool enabled = true);
    bool          is_notify_enabled() const;

    virtual void reset() = 0;

  protected:
    typedef std::vector<HistoryObserver*> RegisteredObservers;
    RegisteredObservers _observers;

    virtual void notify_change();

		position _front;

  private:


    bool _notify;

  }
  ; //~ class History

}//~ namespace sgpem

#endif //~ HISTORY_HH
